summaryrefslogtreecommitdiffstats
path: root/sdo-java/trunk-cts/sdo2.1/src/main/java/test/sdo21/tests/xsd/XSDSimpleTypeTest.java
blob: dfeb5cccc12e27f1f41b66694b1bc75e80feed01 (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
/*
 *  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.
 *
 */
package test.sdo21.tests.xsd;

import java.util.List;
import java.io.File;

import commonj.sdo.Type;
import commonj.sdo.Property;
import commonj.sdo.DataObject;
import commonj.sdo.DataGraph;

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.*;


public class XSDSimpleTypeTest extends XSDBaseTestCase {

    /**
     * <simpleType> with a restriction from type xs:string
     *
     * @throws Exception
     */
    @Test
    public void testRestriction() throws Exception {
        List types = define("/simpleType/restriction.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type = getType(types, "simpleTypeRestriction");
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(1, type.getBaseTypes().size());
        Type baseType = (Type) type.getBaseTypes().get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("String", baseType.getName());
    }

    /**
     * <element> using anonymous <simpleType>. We expect the SDO Type to have the same name as the element
     * as per page 80 of the Java spec.
     */
    @Test
    public void testAnonOpenContentProperty() throws Exception {
        List types = define("/simpleType/anonymous.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        // get open content property defined by global element in XSD
        String uri = "http://www.example.com/simpleType/anonymous";
        Property property = typeHelper.getOpenContentProperty(uri, "simpleTypeAnon");

        assertNotNull(property);

        // The spec maps the name of anonymous types to the name
        // of their enclosing element. This is a broken method however,
        // as it can conflict with same named types and other same
        // named elements with anonymous types. So currently we are
        // intentially non-compliant. using unique names for
        // anonymous types

        Type type = property.getType();
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(1, type.getBaseTypes().size());
        Type baseType = (Type) type.getBaseTypes().get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("String", baseType.getName());
    }

    /**
     * <simpleType> with final="list"
     *
     * @throws Exception
     */
    @Test
    public void testFinalList() throws Exception {
        List types = define("/simpleType/finalList.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type = getType(types, "simpleTypeFinalList");
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(1, type.getBaseTypes().size());

        Type baseType = (Type) type.getBaseTypes().get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("String", baseType.getName());
    }

    /**
     * <simpleType> with final="union"
     *
     * @throws Exception
     */
    @Test
    public void testFinalUnion() throws Exception {
        List types = define("/simpleType/finalUnion.xsd");

        Type type = getType(types, "simpleTypeFinalUnion");
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(1, type.getBaseTypes().size());

        Type baseType = (Type) type.getBaseTypes().get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("String", baseType.getName());
    }

    /**
     * <simpleType> with final="restriction"
     *
     * @throws Exception
     */
    @Test
    public void testFinalRestriction() throws Exception {
        List types = define("/simpleType/finalRestriction.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type = getType(types, "simpleTypeFinalRestriction");
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(1, type.getBaseTypes().size());

        Type baseType = (Type) type.getBaseTypes().get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("String", baseType.getName());
    }

    /**
     * @throws Exception
     */
    @Test
    @Ignore
    public void testDerivedType() throws Exception {
        List types = define("/simpleType/derived.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type0 = getType(types, "derivedType");
        Type type1 = getType(types, "baseType");

        assertEquals("baseType", type1.getName());
        assertEquals("derivedType", type0.getName());

        assertTrue(type0.isDataType());
        assertTrue(type1.isDataType());
        assertFalse(type0.isOpen());
        assertFalse(type0.isSequenced());
        assertEquals(0, type0.getProperties().size());
        assertEquals(1, type0.getBaseTypes().size());

        List baseTypes = type0.getBaseTypes();
        assertEquals(1, baseTypes.size());

        Type baseType = (Type) baseTypes.get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("Integer", baseType.getName());
    }

    /**
     * @throws Exception
     */
    @Test
    @Ignore
    public void testDerivedType2() throws Exception {
        List types = define("/simpleType/derived2.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type0 = getType(types, "baseType");
        Type type1 = getType(types, "derivedType");

        assertEquals("baseType", type0.getName());
        assertEquals("derivedType", type1.getName());

        assertTrue(type0.isDataType());
        assertTrue(type1.isDataType());
        assertFalse(type0.isOpen());
        assertFalse(type0.isSequenced());
        assertEquals(0, type0.getProperties().size());
        assertEquals(1, type0.getBaseTypes().size());

        List baseTypes = type0.getBaseTypes();
        assertEquals(1, baseTypes.size());

        Type baseType = (Type) baseTypes.get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("Integer", baseType.getName());

        List derivedTypes = type1.getBaseTypes();
        assertEquals(1, derivedTypes.size());
        Type derivedType = (Type) derivedTypes.get(0);

        assertEquals("Integer", derivedType.getName());
    }

    @Test
    public void testDerivedUnionType() throws Exception {
        List types = define("/simpleType/derivedUnion.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type0 = getType(types, "fontbystringname");
        Type type1 = getType(types, "fontbynumber");

        assertEquals("fontbynumber", type1.getName());
        assertEquals("fontbystringname", type0.getName());

        assertTrue(type0.isDataType());
        assertTrue(type1.isDataType());

        assertFalse(type0.isOpen());
        assertFalse(type0.isSequenced());
        assertEquals(0, type0.getProperties().size());
        assertEquals(1, type0.getBaseTypes().size());

        //Get the first union type and check
        List basetypes1 = type0.getBaseTypes();
        assertEquals(1, basetypes1.size());
        Type basetype1 = (Type) basetypes1.get(0);

        assertEquals(basetype1.getURI(), "commonj.sdo");
        assertEquals(basetype1.getName(), "String");

        //Get the second union type and check
        List basetypes2 = type1.getBaseTypes();
        assertEquals(1, basetypes2.size());
        Type basetype2 = (Type) basetypes2.get(0);

        assertEquals(basetype2.getName(), "Integer");
    }

    /**
     * <simpleType> containing <annotation> (no effect on SDO type but want to make
     * sure we can parse it ok)
     *
     * @throws Exception
     */
    @Test
    public void testAnnotation() throws Exception {
        List types = define("/simpleType/annotation.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type = getType(types, "simpleTypeAnnotated");
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(1, type.getBaseTypes().size());

        Type baseType = (Type) type.getBaseTypes().get(0);
        assertEquals("commonj.sdo", baseType.getURI());
        assertEquals("String", baseType.getName());
    }

    /**
     * <simpleType> containing <list>
     *
     * @throws Exception
     */
    @Test
    public void testTC200SimpleType() throws Exception {
        List types = define("/simpleType/list.xsd");

        Type type = getType(types, "simpleTypeList");
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(0, type.getBaseTypes().size());
    }

    @Test
    public void testEnumeration() throws Exception {
        List types = define("/simpleType/enumeration.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type thing = getType(types, "thing");
        Property colorofthing = thing.getProperty("colorofthing");
        Property sizeofthing = thing.getProperty("sizeofthing");

        assertEquals("thing", thing.getName());
        assertEquals("colorofthing", colorofthing.getName());
        assertEquals("sizeofthing", sizeofthing.getName());

        DataObject dobj = dataFactory.create(thing.getURI(), thing.getName());
        dobj.setInt("sizeofthing", 2);
        dobj.setString("colorofthing", "red");

        assertEquals("red", dobj.getString("colorofthing"));
        assertEquals(2, dobj.getInt("sizeofthing"));
    }

    /**
     * <simpleType> containing <union>
     *
     * @throws Exception
     */
    @Test
    public void testUnion() throws Exception {
        List types = define("/simpleType/union.xsd");

        // at least one type should have been returned
        assertTrue(types.size() > 0);

        Type type = getType(types, "simpleTypeUnion");
        assertEquals(false, type.isAbstract());
        assertEquals(true, type.isDataType());
        assertEquals(false, type.isOpen());
        assertEquals(false, type.isSequenced());
        assertEquals(0, type.getProperties().size());
        assertEquals(0, type.getBaseTypes().size());
    }

}