summaryrefslogtreecommitdiffstats
path: root/sdo-java/trunk-cts/sdo2.1/src/main/java/test/sdo21/tests/api/TypeHelper/TypeHelperTest.java
blob: 31384c73728bae372fefcc296e91e84be18a0f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 *  $Rev: 552500 $  $Date: 2007-07-02 15:18:46 +0100 (Mon, 02 Jul 2007) $
 */
package test.sdo21.tests.api.TypeHelper;

import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.DataObject;
import commonj.sdo.Type;

import test.sdo21.tests.TestData.StandardFactory;
import test.sdo21.tests.TestData.TestDataFactory;
import test.sdo21.tests.TestData.StandardXSDFactory;
import test.sdo21.tests.util.CTSUtil;
import test.sdo21.framework.CTSTestCase;

import java.util.List;
import java.util.ArrayList;

import static org.junit.Assert.*;

import org.junit.*;

public class TypeHelperTest extends CTSTestCase {

    TestDataFactory factory;

    public TypeHelperTest() {
        // Tests on the Property interface should be independent of the metadata creation mechanism
        // so just pick one Standard Factory
        factory = new StandardXSDFactory();
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        factory.defineMetaData(getScope());
    }


    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Test that newly defined type has correct default values for open, sequenced, abstract, dataType
     */
    @Test
    public void testNewTypeDefaults() {
        HelperContext scope = getScope();
        TypeHelper typeHelper = scope.getTypeHelper();
        DataObject defineDO2 = scope.getDataFactory().create("commonj.sdo", "Type");
        defineDO2.set("uri", StandardFactory.TEST_NAMESPACE);
        defineDO2.set("name", "testNewTypeOpenIsFalse");
        Type t = typeHelper.define(defineDO2);
        assertFalse("Newly created type should have open=false by default", t.isOpen());
        assertFalse("Newly created type should have abstract=false by default", t.isAbstract());
        assertFalse("Newly created type should have sequenced=false by default", t.isSequenced());
        assertFalse("Newly created type should have dataType=false by default", t.isDataType());
    }

    /**
     * Verify the performance of TypeHelper.getType(URI, Name)
     *
     * @throws Exception
     */
    @Test
    public void getTypeByURI() throws Exception {
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        Type testType = testDO.getType();

        TypeHelper typeHelper = getScope().getTypeHelper();
        Type returnedType = typeHelper.getType(testType.getURI(), testType.getName());

        // TODO I thinkk we can assert that the types are the same instance here?
        assertTrue("TypeHelper.getType(URI, Name) did not return the expected Type.", CTSUtil.areEqualTypes(returnedType, testType));
    }

    /**
     * Verify the performance of TypeHelper.getType(Class)
     *
     * @throws Exception
     */
    @Test
    @Ignore("this test currently never reaches the interesting assertion")
    public void getTypeByClass() throws Exception {
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        Type testType = testDO.getType();

        TypeHelper typeHelper = getScope().getTypeHelper();
        Type returnedType = typeHelper.getType(testType.getURI(), testType.getName());

        if (testType.getInstanceClass() != null) {
            returnedType = typeHelper.getType(testType.getInstanceClass());
            assertTrue("TypeHelper.getType(Class) did not return the expected Type.",
                    CTSUtil.areEqualTypes(returnedType, testType));
        }
    }

    /**
     * Verify the proper performance of TypeHelper.getType(Class) when a non-SDO
     * class is passed to TypeHelper.getType(Class)
     */
    @Test
    public void getTypeWithNonSDOClass() {
        TypeHelper typeHelper = getTestHelper().createHelperContext().getTypeHelper();

        assertNull("TypeHelper.getType(Class) should return null when no Type was defined for the interface Class.",
                typeHelper.getType(TypeHelperTest.class));
    }

    /**
     * Verify the proper performance of TypeHelper.getClass(URI, Name) when the
     * namespace for URI does not include Name
     */
    @Test
    public void getTypeByURIWithInvalidName() throws Exception {
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        TypeHelper typeHelper = getScope().getTypeHelper();

        assertNull("TypeHelper.getType(URI, Name) should return null when no Type has the indicated Name in the URI namespace.",
                typeHelper.getType(testDO.getType().getURI(), "UndefinedName"));
    }

    /**
     * Verify the proper performance of TypeHelper.getClass(URI, Name) when the
     * Name exists but not in the namespace of the URI
     *
     * @throws Exception
     */
    @Test
    public void getTypeByURIWithInvalidURI() throws Exception {
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        TypeHelper typeHelper = getScope().getTypeHelper();

        assertNull("TypeHelper.getType(URI, Name) should return null when no Type has the indicated Name in the URI namespace.",
                typeHelper.getType("UndefinedURI", testDO.getType().getName()));
    }

    /**
     * Verify the performance of TypeHelper.define(DataObject), use
     * DataFactory.create(URI, name) to verify
     *
     * @throws Exception
     */
    @Test
    public void DefineByDataObjectCreateByURI() throws Exception {
        HelperContext scope = getScope();
        DataObject testDO = factory.createTestData(scope, StandardFactory.API_TYPE);

        TypeHelper typeHelper = scope.getTypeHelper();
        Type stringType = typeHelper.getType("commonj.sdo", "String");

        DataObject defineTypeDO = scope.getDataFactory().create("commonj.sdo", "Type");
        defineTypeDO.set("uri", StandardFactory.TEST_NAMESPACE);
        defineTypeDO.set("name", "DefinedType1");
        DataObject IDProperty = defineTypeDO.createDataObject("property");
        IDProperty.set("name", "ID");
        IDProperty.set("type", stringType);
        DataObject DOProperty = defineTypeDO.createDataObject("property");
        DOProperty.set("name", "contained");
        DOProperty.set("type", testDO.getType());
        typeHelper.define(defineTypeDO);

        // Verify the Type definition by creating a DataObject of the newly
        // defined Type via DataFactory.create(URI, name).

        DataObject result =
                scope.getDataFactory().create(StandardFactory.TEST_NAMESPACE,
                        defineTypeDO.getString("name"));
        assertNotNull("CTSSuite.getTestHelper().getDataFactory() returned null", result);
        assertEquals("CTSSuite.getTestHelper().getDataFactory()e did not create a Type that could be instantiated.",
                result.getType().getName(),
                "DefinedType1");
        assertNotNull("CTSSuite.getTestHelper().getDataFactory() did not create a Type that could be instantiated, getProperty(ID) was null",
                result.getInstanceProperty("ID"));
    }

    /**
     * Verify the performance of TypeHelper.define(DataObject), use
     * DataFactory.create(Type) to verify
     *
     * @throws Exception
     */
    @Test
    public void DefineByDataObjectCreateByType() throws Exception {
        HelperContext scope = getScope();
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        TypeHelper typeHelper = scope.getTypeHelper();
        Type stringType = typeHelper.getType("commonj.sdo", "String");

        DataObject defineTypeDO = scope.getDataFactory().create("commonj.sdo", "Type");
        defineTypeDO.set("uri", StandardFactory.TEST_NAMESPACE);
        defineTypeDO.set("name", "DefinedType2");
        DataObject IDProperty = defineTypeDO.createDataObject("property");
        IDProperty.set("name", "ID");
        IDProperty.set("type", stringType);
        DataObject DOProperty = defineTypeDO.createDataObject("property");
        DOProperty.set("name", "contained");
        DOProperty.set("type", testDO.getType());
        Type definedType = typeHelper.define(defineTypeDO);

        // Verify the Type definition by creating a DataObject of the newly
        // defined Type via DataFactory.create(Type)

        DataObject result = scope.getDataFactory().create(definedType);
        assertNotNull("TypeHelper.define(DataObject) returned null", result);
        assertEquals("TypeHelper.define(DataObject) did not create a Type that could be instantiated.", result
                .getType().getName(), "DefinedType2");
        assertNotNull("TypeHelper.define(DataObject) did not create a Type that could be instantiated, getProperty(ID) was null",
                result.getInstanceProperty("ID"));
    }

    /**
     * Verify the performance of TypeHelper.define(List)
     *
     * @throws Exception
     */
    @Test
    public void DefineByList() throws Exception {
        HelperContext scope = getScope();
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        TypeHelper typeHelper = scope.getTypeHelper();
        Type stringType = typeHelper.getType("commonj.sdo", "String");

        DataObject define3 = scope.getDataFactory().create("commonj.sdo", "Type");
        define3.set("uri", StandardFactory.TEST_NAMESPACE);
        define3.set("name", "DefinedType3");
        DataObject firstNameProperty = define3.createDataObject("property");
        firstNameProperty.set("name", "firstName");
        firstNameProperty.set("type", stringType);
        DataObject DOProperty = define3.createDataObject("property");
        DOProperty.set("name", "contained");
        DOProperty.set("type", testDO.getType());

        DataObject define4 = scope.getDataFactory().create("commonj.sdo", "Type");
        define4.set("uri", StandardFactory.TEST_NAMESPACE);
        define4.set("name", "DefinedType4");
        DataObject lastNameProperty = define4.createDataObject("property");
        lastNameProperty.set("name", "lastName");
        lastNameProperty.set("type", stringType);
        DataObject DOProperty2 = define4.createDataObject("property");
        DOProperty2.set("name", "contained");
        DOProperty2.set("type", testDO.getType());

        List DOList = new ArrayList();
        DOList.add(define3);
        DOList.add(define4);

        List types = typeHelper.define(DOList);

        assertEquals("TypeHelper.define(List) should define the same number of Types as is present in List.", 2, types
                .size());
        Type typeInList = (Type) types.get(0);
        Type define4Type;
        if (typeInList.getName().equals("DefinedType3")) {
            typeInList = (Type) types.get(1);
            assertEquals("TypeHelper.define(List) returned a List that did not include Types with the expected names.",
                    "DefinedType4",
                    typeInList.getName());
            define4Type = typeInList;
        } else {
            define4Type = typeInList;
            assertEquals("TypeHelper.define(List) returned a List that did not include Types with the expected names.",
                    "DefinedType4",
                    typeInList.getName());
            typeInList = (Type) types.get(1);
            assertEquals("TypeHelper.define(List) returned a List that did not include Types with the expected names.",
                    "DefinedType3",
                    typeInList.getName());
        }

        // Attempt to create one of the Types using DataFactory.create(URI,
        // name)

        DataObject result =
                scope.getDataFactory().create(StandardFactory.TEST_NAMESPACE,
                        define3.getString("name"));
        assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(URI, name).  result is null.",
                result);
        assertEquals("TypeHelper.define(List) did not create a Type that could be instantiated  with DataFactory.create(URI, name).  getType() incorrect",
                result.getType().getName(),
                "DefinedType3");
        assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(URI, name).  firstName property returned null",
                result.getInstanceProperty("firstName"));

        // Attempt to create the other type using DataFactory.create(Type)

        result = scope.getDataFactory().create(define4Type);

        assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(Type).  result is null.",
                result);
        assertEquals("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(Type).  getType() incorrect",
                result.getType().getName(),
                "DefinedType4");
        assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(Type).  lastName property returned null",
                result.getInstanceProperty("lastName"));
    }

    /**
     * Verify the error handling of TypeHelper.define(List) when List contains a
     * member that is not a DataObject
     *
     * @throws Exception
     */
    @Test
    public void DefineByListInvalidListMember() throws Exception {
        HelperContext scope = getScope();
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        TypeHelper typeHelper = scope.getTypeHelper();
        Type intType = typeHelper.getType("commonj.sdo", "Int");

        DataObject define5 = scope.getDataFactory().create("commonj.sdo", "Type");
        define5.set("uri", StandardFactory.TEST_NAMESPACE);
        define5.set("name", "DefinedType5");
        DataObject ID1Property = define5.createDataObject("property");
        ID1Property.set("name", "ID1");
        ID1Property.set("type", intType);
        DataObject DOProperty = define5.createDataObject("property");
        DOProperty.set("name", "contained");
        DOProperty.set("type", testDO.getType());

        DataObject define6 = scope.getDataFactory().create("commonj.sdo", "Type");
        define6.set("uri", StandardFactory.TEST_NAMESPACE);
        define6.set("name", "DefinedType6");
        DataObject ID2Property = define6.createDataObject("property");
        ID2Property.set("name", "ID2");
        ID2Property.set("type", intType);

        List DOList = new ArrayList();
        DOList.add(define5);
        DOList.add("A");
        DOList.add(define6);

        try {
            typeHelper.define(DOList);
            fail("TypeHelper.define(List) should throw an Exception when List " + "contains a member that is not a DataObject.");
        } catch (Exception e) {
            // Do nothing
        }
    }

    /**
     * Verify the expected behavior of TypeHelper.define(DataObject) when a Type
     * of the same name has already been defined differently.
     *
     * @throws Exception
     */
    @Test
    @Ignore
    public void singleRedefinitionDifferent() throws Exception {
        // TODO complete this test case
        HelperContext scope = getScope();
        DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE);

        TypeHelper typeHelper = scope.getTypeHelper();
        Type intType = typeHelper.getType("commonj.sdo", "Int");

        // Determine what should happen when a Type is redefined (same URI
        // & Name, but different properties).
        // When this is known, uncomment the following and supply the
        // appropriate test.
        // While it's not clear to what should happen (from the spec), what is
        // happening is that there is no
        // notification of the duplication, and the originally defined Type is
        // returned by the define() in the latter instance.

        DataObject defineDO = scope.getDataFactory().create("commonj.sdo", "Type");
        defineDO.set("uri", StandardFactory.TEST_NAMESPACE);
        defineDO.set("name", "DefineTypeAgain");
        DataObject numProperty = defineDO.createDataObject("property");
        numProperty.set("name", "num");
        numProperty.set("type", intType);

        typeHelper.define(defineDO);

        DataObject defineDO2 = scope.getDataFactory().create("commonj.sdo", "Type");
        defineDO2.set("uri", StandardFactory.TEST_NAMESPACE);
        defineDO2.set("name", "DefineTypeAgain");
        DataObject DOProperty = defineDO2.createDataObject("property");
        DOProperty.set("name", "contained");
        DOProperty.set("type", testDO.getType());

        typeHelper.define(defineDO2);
    }

    /**
     * Verify the expected behavior of TypeHelper.define(DataObject) when a Type
     * of the same name has already been defined, but defined identically.
     */
    @Test
    @Ignore
    public void singleRedefinitionSame() {
        // TODO implement test
    }

    /**
     * Verify the expected behavior of TypeHelper.define(List) when an element
     * of the List has already been defined differently.
     */
    @Test
    @Ignore
    public void listRedefinitionDifferent() {
        // TODO implement test
    }

    /**
     * Verify the expected behavior of TypeHelper.define(List) when an element
     * of the List has already been defined identically.
     */
    @Test
    @Ignore
    public void listRedefinitionSame() {
        //TODO implement test
    }


}