KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > junit > ParserTest


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.junit;
18
19 import java.io.StringReader JavaDoc;
20 import java.math.BigInteger JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Calendar JavaDoc;
23
24 import org.apache.ws.jaxme.generator.Generator;
25 import org.apache.ws.jaxme.generator.impl.GeneratorImpl;
26 import org.apache.ws.jaxme.generator.sg.AttributeSG;
27 import org.apache.ws.jaxme.generator.sg.ComplexContentSG;
28 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
29 import org.apache.ws.jaxme.generator.sg.Facet;
30 import org.apache.ws.jaxme.generator.sg.GroupSG;
31 import org.apache.ws.jaxme.generator.sg.ListTypeSG;
32 import org.apache.ws.jaxme.generator.sg.ObjectSG;
33 import org.apache.ws.jaxme.generator.sg.ParticleSG;
34 import org.apache.ws.jaxme.generator.sg.SGFactory;
35 import org.apache.ws.jaxme.generator.sg.SchemaSG;
36 import org.apache.ws.jaxme.generator.sg.SimpleContentSG;
37 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
38 import org.apache.ws.jaxme.generator.sg.TypeSG;
39 import org.apache.ws.jaxme.generator.sg.UnionTypeSG;
40 import org.apache.ws.jaxme.generator.sg.impl.JAXBSGFactory;
41 import org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader;
42 import org.apache.ws.jaxme.generator.sg.impl.SGFactoryImpl;
43 import org.apache.ws.jaxme.js.JavaQNameImpl;
44 import org.apache.ws.jaxme.xs.XSAttributable;
45 import org.apache.ws.jaxme.xs.XSAttribute;
46 import org.apache.ws.jaxme.xs.XSComplexType;
47 import org.apache.ws.jaxme.xs.XSElement;
48 import org.apache.ws.jaxme.xs.XSParser;
49 import org.apache.ws.jaxme.xs.XSSchema;
50 import org.apache.ws.jaxme.xs.jaxb.JAXBAttribute;
51 import org.apache.ws.jaxme.xs.jaxb.JAXBJavaType;
52 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser;
53 import org.apache.ws.jaxme.xs.types.XSID;
54 import org.apache.ws.jaxme.xs.xml.XsQName;
55 import org.xml.sax.InputSource JavaDoc;
56 import org.xml.sax.SAXException JavaDoc;
57
58
59 /** <p>Implements some basic tests for the Schema generator.</p>
60  *
61  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
62  * @version $Id: ParserTest.java,v 1.10 2005/07/13 19:58:16 jochen Exp $
63  */

64 public class ParserTest extends BaseTestCase {
65   public ParserTest(String JavaDoc arg) { super(arg); }
66
67   protected SchemaSG parse(String JavaDoc pSchema, String JavaDoc pSystemId) throws Exception JavaDoc {
68     JAXBSchemaReader r = getSchemaReader();
69     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(pSchema));
70     isource.setSystemId(pSystemId);
71     return r.parse(isource);
72   }
73
74   protected JAXBSchemaReader getSchemaReader() {
75       Generator generator = new GeneratorImpl();
76       JAXBSchemaReader r = new JAXBSchemaReader();
77       generator.setSchemaReader(r);
78       r.setGenerator(generator);
79       return r;
80   }
81
82   public void testSimpleTypes() throws Exception JavaDoc {
83     final String JavaDoc schema =
84       "<?xml version='1.0'?>\n" +
85       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
86       " <xs:simpleType name='a'>\n" +
87       " <xs:restriction base='xs:string'>\n" +
88       " <xs:minLength value='3'/>\n" +
89       " </xs:restriction>\n" +
90       " </xs:simpleType>\n" +
91       "\n" +
92       " <xs:simpleType name='b'>\n" +
93       " <xs:list itemType='xs:int'/>\n" +
94       " </xs:simpleType>\n" +
95       "\n" +
96       " <xs:simpleType name='c'>\n" +
97       " <xs:union memberTypes='a b'/>\n" +
98       " </xs:simpleType>\n" +
99       "</xs:schema>\n";
100
101     SchemaSG schemaSG = parse(schema, "testSimpleTypes.xsd");
102
103     TypeSG[] st = schemaSG.getTypes();
104     assertEquals(3, st.length);
105
106     TypeSG st1 = st[0];
107     assertTrue(!st1.isComplex());
108     SimpleTypeSG sst1 = st1.getSimpleTypeSG();
109     assertTrue(sst1.isAtomic());
110     assertTrue(!sst1.isList());
111     assertTrue(!sst1.isUnion());
112     assertEquals(sst1.getRuntimeType(), JavaQNameImpl.getInstance(String JavaDoc.class));
113
114     TypeSG st2 = st[1];
115     assertTrue(!st2.isComplex());
116     SimpleTypeSG sst2 = st2.getSimpleTypeSG();
117     assertTrue(!sst2.isAtomic());
118     assertTrue(sst2.isList());
119     assertTrue(!sst2.isUnion());
120     ListTypeSG lt = sst2.getListType();
121     assertNotNull(lt.getItemType());
122     TypeSG sst = lt.getItemType();
123     assertTrue(!sst.isComplex());
124     SimpleTypeSG it = sst.getSimpleTypeSG();
125     assertEquals(JavaQNameImpl.INT, it.getRuntimeType());
126
127     TypeSG st3 = st[2];
128     assertEquals(false, st3.isComplex());
129     SimpleTypeSG sst3 = st3.getSimpleTypeSG();
130     assertEquals(false, sst3.isAtomic());
131     assertEquals(false, sst3.isList());
132     assertEquals(true, sst3.isUnion());
133     UnionTypeSG ut = sst3.getUnionType();
134     TypeSG[] uTypes = ut.getMemberTypes();
135     assertEquals(2, uTypes.length);
136   }
137
138   public void testAttributes() throws Exception JavaDoc {
139     final String JavaDoc schema =
140       "<?xml version='1.0'?>\n" +
141       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
142       " <xs:annotation><xs:appinfo>\n" +
143       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
144       " <jaxb:package name='parsertest.testattributes'/>\n" +
145       " </jaxb:schemaBindings>\n" +
146       " </xs:appinfo></xs:annotation>\n" +
147       " <xs:attribute name='a' type='xs:string'/>\n" +
148       "\n" +
149       " <xs:attribute name='b'>\n" +
150       " <xs:simpleType>\n" +
151       " <xs:restriction base='xs:int'/>\n" +
152       " </xs:simpleType>\n" +
153       " </xs:attribute>\n" +
154       "\n" +
155       " <xs:complexType name='attributesOwner'>\n" +
156       " <xs:attribute ref='a'/>\n" +
157       " <xs:attribute ref='b'/>\n" +
158       " <xs:attribute name='c' type='xs:int'/>\n" +
159       " </xs:complexType>\n" +
160       "</xs:schema>\n";
161
162     SchemaSG jschema = parse(schema, "testAttributes.xsd");
163
164     TypeSG[] types = jschema.getTypes();
165     assertEquals(1, types.length);
166     assertTrue(types[0].isComplex());
167
168     AttributeSG[] attr = types[0].getComplexTypeSG().getAttributes();
169     assertEquals(3, attr.length);
170
171     AttributeSG attr1 = attr[0];
172     assertTrue(!attr1.getTypeSG().isComplex());
173     SimpleTypeSG sst1 = attr1.getTypeSG().getSimpleTypeSG();
174     assertTrue(sst1.isAtomic());
175     assertEquals(JavaQNameImpl.getInstance(String JavaDoc.class), sst1.getRuntimeType());
176
177     AttributeSG attr2 = attr[1];
178     assertTrue(!attr2.getTypeSG().isComplex());
179     SimpleTypeSG sst2 = attr2.getTypeSG().getSimpleTypeSG();
180     assertTrue(sst2.isAtomic());
181     assertEquals(JavaQNameImpl.INT, sst2.getRuntimeType());
182
183     AttributeSG attr3 = attr[2];
184     assertTrue(!attr3.getTypeSG().isComplex());
185     SimpleTypeSG sst3 = attr3.getTypeSG().getSimpleTypeSG();
186     assertTrue(sst3.isAtomic());
187     assertEquals(JavaQNameImpl.INT, sst3.getRuntimeType());
188   }
189
190   public void testAttributeGroups() throws Exception JavaDoc {
191     final String JavaDoc schema =
192       "<?xml version='1.0'?>\n" +
193       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
194       " <xs:annotation><xs:appinfo>\n" +
195       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
196       " <jaxb:package name='parsertest.testattributegroups'/>\n" +
197       " </jaxb:schemaBindings>\n" +
198       " </xs:appinfo></xs:annotation>\n" +
199       " <xs:attribute name='a' type='xs:string'/>\n" +
200       " <xs:attribute name='b' type='xs:int'/>\n" +
201       "\n" +
202       " <xs:attributeGroup name='c'>\n" +
203       " <xs:attribute name='d' type='xs:dateTime'/>\n" +
204       " <xs:attribute name='e' type='xs:float'/>\n" +
205       " <xs:attribute ref='a'/>\n" +
206       " </xs:attributeGroup>\n" +
207       "\n" +
208       " <xs:attributeGroup name='f'>\n" +
209       " <xs:attribute name='g' type='xs:double'/>\n" +
210       " <xs:attributeGroup ref='c'/>\n" +
211       " <xs:attribute ref='b'/>\n" +
212       " </xs:attributeGroup>\n" +
213       "\n" +
214       " <xs:element name='ea'>\n" +
215       " <xs:complexType>\n" +
216       " <xs:attributeGroup ref='c'/>\n" +
217       " </xs:complexType>\n" +
218       " </xs:element>\n" +
219       "\n" +
220       " <xs:element name='eb'>\n" +
221       " <xs:complexType>\n" +
222       " <xs:attributeGroup ref='f'/>\n" +
223       " </xs:complexType>\n" +
224       " </xs:element>\n" +
225       "</xs:schema>\n";
226
227     SchemaSG jschema = parse(schema, "testAttributeGroups.xsd");
228
229     ObjectSG[] elements = jschema.getElements();
230     assertNotNull(elements);
231     assertEquals(2, elements.length);
232
233     ObjectSG ea = elements[0];
234     assertTrue(ea.getTypeSG().isComplex());
235     ComplexTypeSG eact = ea.getTypeSG().getComplexTypeSG();
236     AttributeSG[] eactAttr = eact.getAttributes();
237     assertEquals(3, eactAttr.length);
238     assertEquals(new XsQName((String JavaDoc) null, "d"), eactAttr[0].getName());
239     assertEquals(eactAttr[0].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(Calendar JavaDoc.class));
240     assertEquals(new XsQName((String JavaDoc) null, "e"), eactAttr[1].getName());
241     assertEquals(eactAttr[1].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(float.class));
242     assertEquals(new XsQName((String JavaDoc) null, "a"), eactAttr[2].getName());
243     assertEquals(eactAttr[2].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(String JavaDoc.class));
244
245     ObjectSG eb = elements[1];
246     assertTrue(eb.getTypeSG().isComplex());
247     ComplexTypeSG ebct = eb.getTypeSG().getComplexTypeSG();
248     AttributeSG[] ebctAttr = ebct.getAttributes();
249     assertEquals(5, ebctAttr.length);
250     assertEquals(new XsQName((String JavaDoc) null, "g"), ebctAttr[0].getName());
251     assertEquals(ebctAttr[0].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(double.class));
252     assertEquals(new XsQName((String JavaDoc) null, "d"), ebctAttr[1].getName());
253     assertEquals(ebctAttr[1].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(Calendar JavaDoc.class));
254     assertEquals(ebctAttr[2].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(float.class));
255     assertEquals(ebctAttr[3].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(String JavaDoc.class));
256     assertEquals(ebctAttr[4].getTypeSG().getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(int.class));
257   }
258
259   public void testElements() throws Exception JavaDoc {
260     final String JavaDoc schema =
261       "<?xml version='1.0'?>\n" +
262       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
263       " <xs:annotation><xs:appinfo>\n" +
264       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
265       " <jaxb:package name='parsertest.testelements'/>\n" +
266       " </jaxb:schemaBindings>\n" +
267       " </xs:appinfo></xs:annotation>\n" +
268       " <xs:element name='a' type='xs:string'/>\n" +
269       "\n" +
270       " <xs:element name='b'>\n" +
271       " <xs:simpleType>\n" +
272       " <xs:restriction base='xs:float'/>\n" +
273       " </xs:simpleType>\n" +
274       " </xs:element>\n" +
275       "\n" +
276       " <xs:element name='c'>\n" +
277       " <xs:complexType>\n" +
278       " <xs:annotation>\n" +
279       " <xs:documentation>\n" +
280       " The type of 'c'.\n" +
281       " </xs:documentation>\n" +
282       " </xs:annotation>\n" +
283       " <xs:sequence>\n" +
284       " <xs:element ref='a'/>\n" +
285       " <xs:element name='d' type='xs:double'>\n" +
286       " <xs:annotation>\n" +
287       " <xs:documentation>\n" +
288       " The element 'd'.\n" +
289       " </xs:documentation>\n" +
290       " </xs:annotation>\n" +
291       " </xs:element>\n" +
292       " </xs:sequence>\n" +
293       " <xs:attribute name='e' type='xs:int'>\n" +
294       " <xs:annotation>\n" +
295       " <xs:documentation>\n" +
296       " The attribute 'e'.\n" +
297       " </xs:documentation>\n" +
298       " </xs:annotation>\n" +
299       " </xs:attribute>\n" +
300       " </xs:complexType>\n" +
301       " </xs:element>\n" +
302       "\n" +
303       " <xs:element name='f'>\n" +
304       " <xs:complexType>\n" +
305       " <xs:simpleContent>\n" +
306       " <xs:extension base='xs:int'>\n" +
307       " <xs:attribute name='g' type='xs:boolean'/>\n" +
308       " </xs:extension>\n" +
309       " </xs:simpleContent>\n" +
310       " </xs:complexType>\n" +
311       " </xs:element>\n" +
312       "</xs:schema>\n";
313
314     System.err.println("testElements: ->");
315     SchemaSG jschema = parse(schema, "testElements.xsd");
316
317     ObjectSG[] elements = jschema.getElements();
318     assertEquals(4, elements.length);
319
320     ObjectSG e1 = elements[0];
321     assertEquals(new XsQName((String JavaDoc) null, "a"), e1.getName());
322     TypeSG t1 = e1.getTypeSG();
323     assertTrue(!t1.isComplex());
324     assertTrue(t1.getSimpleTypeSG().isAtomic());
325     assertEquals(t1.getSimpleTypeSG().getRuntimeType(), JavaQNameImpl.getInstance(String JavaDoc.class));
326
327     ObjectSG e2 = elements[1];
328     assertEquals(new XsQName((String JavaDoc) null, "b"), e2.getName());
329     TypeSG t2 = e2.getTypeSG();
330     assertTrue(!t2.isComplex());
331     SimpleTypeSG st2 = t2.getSimpleTypeSG();
332     assertTrue(st2.isAtomic());
333     assertEquals(JavaQNameImpl.getInstance(float.class), st2.getRuntimeType());
334
335     ObjectSG e3 = elements[2];
336     assertEquals(new XsQName((String JavaDoc) null, "c"), e3.getName());
337     TypeSG t3 = e3.getTypeSG();
338     assertTrue(t3.isComplex());
339     ComplexTypeSG ct3 = t3.getComplexTypeSG();
340     assertTrue(!ct3.hasSimpleContent());
341     ComplexContentSG cct3 = ct3.getComplexContentSG();
342     ParticleSG[] childs = cct3.getRootParticle().getGroupSG().getParticles();
343     assertEquals(2, childs.length);
344     assertTrue(childs[0].isElement());
345     ObjectSG child1 = childs[0].getObjectSG();
346     assertEquals(new XsQName((String JavaDoc) null, "a"), child1.getName());
347     assertTrue(childs[1].isElement());
348     ObjectSG child2 = childs[1].getObjectSG();
349     assertEquals(new XsQName((String JavaDoc) null, "d"), child2.getName());
350     assertTrue(!child2.getTypeSG().isComplex());
351     SimpleTypeSG st4 = child2.getTypeSG().getSimpleTypeSG();
352     assertEquals(JavaQNameImpl.getInstance(double.class), st4.getRuntimeType());
353     AttributeSG[] attributes = ct3.getAttributes();
354     assertEquals(1, attributes.length);
355     assertEquals(new XsQName((String JavaDoc) null, "e"), attributes[0].getName());
356     assertTrue(!attributes[0].getTypeSG().isComplex());
357
358     ObjectSG e4 = elements[3];
359     assertTrue(e4.getTypeSG().isComplex());
360     ComplexTypeSG ct4 = e4.getTypeSG().getComplexTypeSG();
361     assertTrue(ct4.hasSimpleContent());
362     SimpleContentSG sct4 = ct4.getSimpleContentSG();
363     assertTrue(!sct4.getContentTypeSG().isComplex());
364     assertEquals(JavaQNameImpl.getInstance(int.class), sct4.getContentTypeSG().getRuntimeType());
365     assertEquals(1, ct4.getAttributes().length);
366     assertEquals(new XsQName((String JavaDoc) null, "g"), ct4.getAttributes()[0].getName());
367     assertEquals(JavaQNameImpl.getInstance(boolean.class), ct4.getAttributes()[0].getTypeSG().getRuntimeType());
368   }
369
370   public void testFacets() throws Exception JavaDoc {
371     final String JavaDoc schema =
372       "<?xml version='1.0'?>\n" +
373       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
374       " <xs:annotation><xs:appinfo>\n" +
375       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
376       " <jaxb:package name='parsertest.testfacets'/>\n" +
377       " </jaxb:schemaBindings>\n" +
378       " </xs:appinfo></xs:annotation>\n" +
379       " <xs:simpleType name='USState'>\n" +
380       " <xs:restriction base='xs:string'>\n" +
381       " <xs:enumeration value='AK'/>\n" +
382       " <xs:enumeration value='AL'/>\n" +
383       " <xs:enumeration value='AR'/>\n" +
384       " <!-- and so on ... -->\n" +
385       " </xs:restriction>\n" +
386       " </xs:simpleType>\n" +
387       " <xs:element name='a' type='USState'/>\n" +
388       "</xs:schema>\n";
389     
390     SchemaSG jschema = parse(schema, "testFacets.xsd");
391
392     ObjectSG[] elements = jschema.getElements();
393     assertEquals(1, elements.length);
394     ObjectSG se1 = elements[0];
395     assertEquals(new XsQName((String JavaDoc) null, "a"), se1.getName());
396     assertTrue(!se1.getTypeSG().isComplex());
397     SimpleTypeSG sst1 = se1.getTypeSG().getSimpleTypeSG();
398     assertEquals(JavaQNameImpl.getInstance(String JavaDoc.class), sst1.getRuntimeType());
399     Facet facet = sst1.getFacet(Facet.ENUMERATION);
400     assertNotNull(facet);
401     String JavaDoc[] values = facet.getValues();
402     assertNotNull(values);
403     assertEquals(3, values.length);
404     assertEquals("AK", values[0]);
405     assertEquals("AL", values[1]);
406     assertEquals("AR", values[2]);
407   }
408
409   public void testGlobalBindingsDefaults() throws Exception JavaDoc {
410     // Parse a schema without globalBindings; it should have the default
411
// settings.
412
final String JavaDoc schema =
413       "<?xml version='1.0'?>\n" +
414       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'/>\n";
415     
416     SchemaSG jschema = parse(schema, "testGlobalBindingsDefaults.xsd");
417
418     String JavaDoc collectionType = jschema.getCollectionType();
419     assertEquals(ArrayList JavaDoc.class.getName(), collectionType);
420     JAXBJavaType[] javaTypes = jschema.getJAXBJavaTypes();
421     assertNotNull(javaTypes);
422     assertEquals(0, javaTypes.length);
423     XsQName[] typesafeEnumBase = jschema.getTypesafeEnumBase();
424     assertNotNull(typesafeEnumBase);
425     assertEquals(1, typesafeEnumBase.length);
426     assertEquals(new XsQName(XSParser.XML_SCHEMA_URI, "NCName"), typesafeEnumBase[0]);
427     boolean isBindingStyleModelGroup = jschema.isBindingStyleModelGroup();
428     assertTrue(!isBindingStyleModelGroup);
429     boolean choiceContentProperty = jschema.isChoiceContentProperty();
430     assertTrue(!choiceContentProperty);
431     boolean enableFailFastCheck = jschema.isFailFastCheckEnabled();
432     assertTrue(!enableFailFastCheck);
433     boolean isJavaNamingConventionsEnabled = jschema.isJavaNamingConventionsEnabled();
434     assertTrue(isJavaNamingConventionsEnabled);
435     boolean isFixedAttributeConstantProperty = jschema.isFixedAttributeConstantProperty();
436     assertTrue(!isFixedAttributeConstantProperty);
437     boolean generatingIsSetMethod = jschema.isGeneratingIsSetMethod();
438     assertTrue(!generatingIsSetMethod);
439     boolean underscoreWordSeparator = jschema.isUnderscoreWordSeparator();
440     assertTrue(underscoreWordSeparator);
441   }
442
443
444   public void testGlobalBindings() throws Exception JavaDoc {
445     final String JavaDoc schema =
446       "<?xml version='1.0'?>\n" +
447       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
448       " <xs:annotation>\n" +
449       " <xs:appinfo>\n" +
450       " <jaxb:globalBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'\n" +
451       " collectionType='indexed'\n" +
452       " typesafeEnumBase='xs:String xs:NCName'\n" +
453       " bindingStyle='modelGroupBinding'\n" +
454       " choiceContentProperty='true'\n" +
455       " enableFailFastCheck='1'\n" +
456       " enableJavaNamingConventions='false'\n" +
457       " fixedAttributeAsConstantProperty='true'\n" +
458       " generateIsSetMethod='1'\n" +
459       " underscoreBinding='asCharInWord'/>\n" +
460       " </xs:appinfo\n>" +
461       " </xs:annotation>\n" +
462       "</xs:schema>";
463     
464     SchemaSG jschema = parse(schema, "testGlobalBindingsDefaults.xsd");
465
466     String JavaDoc collectionType = jschema.getCollectionType();
467     assertEquals("indexed", collectionType);
468     XsQName[] typesafeEnumBase = jschema.getTypesafeEnumBase();
469     assertNotNull(typesafeEnumBase);
470     assertEquals(2, typesafeEnumBase.length);
471     assertEquals(new XsQName(XSParser.XML_SCHEMA_URI, "String"), typesafeEnumBase[0]);
472     assertEquals(new XsQName(XSParser.XML_SCHEMA_URI, "NCName"), typesafeEnumBase[1]);
473
474     boolean isBindingStyleModelGroup = jschema.isBindingStyleModelGroup();
475     assertTrue(isBindingStyleModelGroup);
476
477     boolean isChoiceContentProperty = jschema.isChoiceContentProperty();
478     assertTrue(isChoiceContentProperty);
479
480     boolean enableFailFastCheck = jschema.isFailFastCheckEnabled();
481     assertTrue(enableFailFastCheck);
482
483     boolean isJavaNamingConventionsEnabled = jschema.isJavaNamingConventionsEnabled();
484     assertTrue(!isJavaNamingConventionsEnabled);
485
486     boolean fixedAttributeConstantProperty = jschema.isFixedAttributeConstantProperty();
487     assertTrue(fixedAttributeConstantProperty);
488
489     boolean generatingIsSetMethod = jschema.isGeneratingIsSetMethod();
490     assertTrue(generatingIsSetMethod);
491
492     boolean isUnderscoreWordSeparator = jschema.isUnderscoreWordSeparator();
493     assertTrue(!isUnderscoreWordSeparator);
494
495     JAXBJavaType[] javaTypes = jschema.getJAXBJavaTypes();
496     assertNotNull(javaTypes);
497     assertEquals(0, javaTypes.length);
498   }
499
500 /*
501   public void testSchemaBindingsDefaults() throws Exception {
502     // Parse a schema without schemaBindings; it should have the default
503     // settings.
504     final String mName = "testSchemaBindingsDefaults";
505     log.entering(mName);
506     final String schema =
507       "<?xml version='1.0'?>\n" +
508       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'/>\n";
509     
510     JAXBSchemaReader r = new JAXBSchemaReader();
511     InputSource isource = new InputSource(new StringReader(schema));
512     isource.setSystemId("testSchemaBindingsDefaults.xsd");
513     JAXBSchema jschema = (JAXBSchema) r.parse(isource);
514     SchemaBindings schemaBindings = jschema.getSchemaBindings();
515     List nameXmlTransforms = schemaBindings.getNameXmlTransform();
516     assertNotNull(nameXmlTransforms);
517     assertEquals(0, nameXmlTransforms.size());
518     assertNull(schemaBindings.getPackage());
519   }
520
521   public void testSchemaBindings() throws Exception {
522     final String mName = "testSchemaBindings";
523     final String myPackageName = "org.apache.ws.jaxme.somepackage";
524
525     log.entering(mName);
526     final String schema =
527       "<?xml version='1.0'?>\n" +
528       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
529       " <xs:annotation>\n" +
530       " <xs:appinfo>\n" +
531       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBSchemaReader.JAXB_SCHEMA_URI + "'>\n" +
532       " <jaxb:package name='" + myPackageName + "'>\n" +
533       " <jaxb:javadoc>Test documentation</jaxb:javadoc>\n" +
534       " </jaxb:package>\n" +
535       " <jaxb:nameXmlTransform>\n" +
536       " <jaxb:typeName prefix='b' suffix='a'/>\n" +
537       " </jaxb:nameXmlTransform>\n" +
538       " </jaxb:schemaBindings>\n" +
539       " </xs:appinfo>\n" +
540       " </xs:annotation>\n" +
541       " <xs:complexType name='a'>\n" +
542       " <xs:sequence>\n" +
543       " <xs:element name='x' type='xs:int'/>\n" +
544       " </xs:sequence>\n" +
545       " <xs:attribute name='y' type='xs:string'/>\n" +
546       " </xs:complexType>\n" +
547       " <xs:element name='b' type='a'/>\n" +
548       "</xs:schema>";
549     
550     JAXBSchemaReader r = new JAXBSchemaReader();
551     InputSource isource = new InputSource(new StringReader(schema));
552     isource.setSystemId("testSchemaBindings.xsd");
553     JAXBSchema jschema = (JAXBSchema) r.parse(isource);
554     SchemaBindings schemaBindings = jschema.getSchemaBindings();
555
556     SchemaBindings.Package schemaPackage = schemaBindings.getPackage();
557     assertNotNull(schemaPackage);
558     assertEquals(myPackageName, schemaPackage.getName());
559     Javadoc javadoc = schemaPackage.getJavadoc();
560     assertNotNull(javadoc);
561     assertEquals("Test documentation", javadoc.getText());
562
563     List nameXmlTransforms = schemaBindings.getNameXmlTransform();
564     assertNotNull(nameXmlTransforms);
565     assertEquals(1, nameXmlTransforms.size());
566     SchemaBindings.NameXmlTransform nameXmlTransform =
567       (SchemaBindings.NameXmlTransform) nameXmlTransform.get(0);
568     assertNotNull(nameXmlTransform);
569     SchemaBindings.NameTransformation transformation =
570       (SchemaBindings.NameTransformation) nameXmlTransform.getTypeName();
571     assertEquals("a", transformation.getSuffix());
572     assertEquals("b", transformation.getPrefix());
573
574     JAXBSchemaWriter writer = new JAXBSchemaWriter();
575     SGFactory factory = writer.getSGFactory();
576
577     SchemaType[] types = jschema.getSchemaTypes();
578     assertNotNull(types);
579     assertEquals(1, types.length);
580     SchemaType st = types[0];
581     TypeSG typeSG = factory.getTypeSG(st);
582     JavaQName qName = typeSG.getClassName(st, SchemaClass.CLASS_TYPE_XML_INTERFACE);
583     assertEquals(myPackageName, qName.getPackageName());
584   }
585 */

586
587   public void testPurchaseOrder() throws Exception JavaDoc {
588     /*
589      A modified version of the first example from XML Schema Part 0:
590      http:www.w3.org/TR/xmlschema-0/#po.xml
591      Modifications:
592      1) Removed forward references by re-ordering the document
593      2) Changed some types to remove types not currently supported
594      xsd:NMTOKEN -> xsd:string
595      xsd:positiveInteger -> xsd:integer
596     */

597
598     final String JavaDoc schema =
599       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> \n" +
600       " \n" +
601       " <xsd:annotation> \n" +
602       " <xsd:documentation xml:lang='en'> \n" +
603       " Purchase order schema for Example.com. \n" +
604       " Copyright 2000 Example.com. All rights reserved. \n" +
605       " </xsd:documentation> \n" +
606       " <xsd:appinfo>\n" +
607       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
608       " <jaxb:package name='parsertest.testpurchaseorder'/>\n" +
609       " </jaxb:schemaBindings>\n" +
610       " </xsd:appinfo>\n" +
611       " </xsd:annotation> \n" +
612       " \n" +
613       " <xsd:element name='comment' type='xsd:string'/> \n" +
614       " \n" +
615       " <xsd:simpleType name='SKU'> \n" +
616       " <xsd:restriction base='xsd:string'> \n" +
617       " <xsd:pattern value='\\d{3}-[A-Z]{2}'/> \n" +
618       " </xsd:restriction> \n" +
619       " </xsd:simpleType> \n" +
620       " \n" +
621       " <xsd:complexType name='USAddress'> \n" +
622       " <xsd:sequence> \n" +
623       " <xsd:element name='name' type='xsd:string'/> \n" +
624       " <xsd:element name='street' type='xsd:string'/> \n" +
625       " <xsd:element name='city' type='xsd:string'/> \n" +
626       " <xsd:element name='state' type='xsd:string'/> \n" +
627       " <xsd:element name='zip' type='xsd:integer'/> \n" +
628       " </xsd:sequence> \n" +
629       " <xsd:attribute name='country' type='xsd:string' fixed='US'/> \n" +
630       " </xsd:complexType> \n" +
631       " \n" +
632       " <xsd:complexType name='Items'> \n" +
633       " <xsd:sequence> \n" +
634       " <xsd:element name='item' minOccurs='0' maxOccurs='unbounded'> \n" +
635       " <xsd:complexType> \n" +
636       " <xsd:sequence> \n" +
637       " <xsd:element name='productName' type='xsd:string'/> \n" +
638       " <xsd:element name='quantity'> \n" +
639       " <xsd:simpleType> \n" +
640       " <xsd:restriction base='xsd:positiveInteger'> \n" +
641       " <xsd:maxExclusive value='100'/> \n" +
642       " </xsd:restriction> \n" +
643       " </xsd:simpleType> \n" +
644       " </xsd:element> \n" +
645       " <xsd:element name='USPrice' type='xsd:decimal'/> \n" +
646       " <xsd:element ref='comment' minOccurs='0'/> \n" +
647       " <xsd:element name='shipDate' type='xsd:date' minOccurs='0'/> \n" +
648       " </xsd:sequence> \n" +
649       " <xsd:attribute name='partNum' type='SKU' use='required'/> \n" +
650       " </xsd:complexType> \n" +
651       " </xsd:element> \n" +
652       " </xsd:sequence> \n" +
653       " </xsd:complexType> \n" +
654       " \n" +
655       " <xsd:complexType name='PurchaseOrderType'> \n" +
656       " <xsd:sequence> \n" +
657       " <xsd:element name='shipTo' type='USAddress'/> \n" +
658       " <xsd:element name='billTo' type='USAddress'/> \n" +
659       " <xsd:element ref='comment' minOccurs='0'/> \n" +
660       " <xsd:element name='items' type='Items'/> \n" +
661       " </xsd:sequence> \n" +
662       " <xsd:attribute name='orderDate' type='xsd:date'/> \n" +
663       " </xsd:complexType> \n" +
664       " \n" +
665       " <xsd:element name='purchaseOrder' type='PurchaseOrderType'/> \n" +
666       " \n" +
667       "</xsd:schema> \n";
668
669     SchemaSG jschema = parse(schema, "testPurchaseOrder.xsd");
670
671     TypeSG[] schemaTypes = jschema.getTypes();
672     assertNotNull(schemaTypes);
673     assertEquals(4, schemaTypes.length);
674
675     // SKU
676
TypeSG sku = schemaTypes[0];
677     assertEquals(new XsQName((String JavaDoc) null, "SKU"), sku.getName());
678     assertTrue(sku.isGlobalType());
679     assertTrue(!sku.isComplex());
680     assertTrue(sku.isRestriction());
681     assertEquals(new XsQName(XSParser.XML_SCHEMA_URI, "string"), sku.getRestrictedType().getName());
682
683     // USAddress
684
// <xsd:complexType name='USAddress'>
685
// <xsd:sequence>
686
TypeSG usAddress = schemaTypes[1];
687     assertEquals(new XsQName((String JavaDoc) null, "USAddress"), usAddress.getName());
688     assertTrue(usAddress.isGlobalType());
689     assertTrue(usAddress.isComplex());
690     assertTrue(usAddress.getComplexTypeSG().getComplexContentSG().getRootParticle().getGroupSG().isSequence());
691     // USAddress.country
692
// <xsd:attribute name='country' type='xsd:string' fixed='US'/>
693
// ToDo: test attribute fixed='US'
694
AttributeSG [] usAddressAttributes = usAddress.getComplexTypeSG().getAttributes();
695     assertEquals(1, usAddressAttributes.length);
696     AttributeSG country = usAddressAttributes[0];
697     assertEquals(new XsQName((String JavaDoc) null, "country"), country.getName());
698     assertEquals(JavaQNameImpl.getInstance(String JavaDoc.class), country.getTypeSG().getRuntimeType());
699     // USAddress children
700
String JavaDoc [] nameShouldBe = {"name", "street", "city", "state", "zip"};
701     ParticleSG [] usAddressChildren = usAddress.getComplexTypeSG().getComplexContentSG().getRootParticle().getGroupSG().getParticles();
702     assertEquals(5, usAddressChildren.length);
703     for (int i = 0; i < usAddressChildren.length; i++) {
704       ParticleSG child = usAddressChildren[i];
705       assertTrue(child.isElement());
706       assertEquals(new XsQName((String JavaDoc) null, nameShouldBe[i]), child.getObjectSG().getName());
707       assertTrue(!child.getObjectSG().getTypeSG().isComplex());
708       Class JavaDoc childRuntimeClass = "zip".equals(nameShouldBe[i]) ? BigInteger JavaDoc.class : String JavaDoc.class;
709       assertEquals(JavaQNameImpl.getInstance(childRuntimeClass), child.getObjectSG().getTypeSG().getRuntimeType());
710     }
711
712     // Items
713
TypeSG items = schemaTypes[2];
714     assertEquals(new XsQName((String JavaDoc) null, "Items"), items.getName());
715     assertTrue(items.isGlobalType());
716     assertTrue(items.isComplex());
717     ComplexTypeSG itemsComplex = items.getComplexTypeSG();
718     assertTrue(!itemsComplex.hasSimpleContent());
719     ComplexContentSG itemsComplexContent = itemsComplex.getComplexContentSG();
720     GroupSG group = itemsComplexContent.getRootParticle().getGroupSG();
721     assertTrue(group.isSequence());
722     // Items.item
723
ParticleSG[] itemsChildren = group.getParticles();
724     assertEquals(1, itemsChildren.length);
725     ParticleSG item = itemsChildren[0];
726     assertTrue(item.isElement());
727     assertEquals(new XsQName((String JavaDoc) null, "item"), item.getObjectSG().getName());
728     assertEquals(0, item.getMinOccurs());
729     assertEquals(-1, item.getMaxOccurs());
730     TypeSG itemST = item.getObjectSG().getTypeSG();
731     assertTrue(itemST.isComplex());
732     assertTrue(!itemST.getComplexTypeSG().hasSimpleContent());
733     assertTrue(itemST.getComplexTypeSG().getComplexContentSG().getRootParticle().getGroupSG().isSequence());
734     // Items.item.partNum
735
// <xsd:attribute name='partNum' type='SKU' use='required'/>
736
AttributeSG[] itemAttributes = itemST.getComplexTypeSG().getAttributes();
737     assertEquals(1, itemAttributes.length);
738     AttributeSG partNum = itemAttributes[0];
739     assertEquals(new XsQName((String JavaDoc) null, "partNum"), partNum.getName());
740     assertTrue(partNum.isRequired());
741     // Items.item.USPrice
742
// <xsd:element name='USPrice' type='xsd:decimal'/>
743
ParticleSG[] itemChildren = itemST.getComplexTypeSG().getComplexContentSG().getRootParticle().getGroupSG().getParticles();
744     assertEquals(5, itemChildren.length);
745     ParticleSG usPrice = itemChildren[2];
746     assertTrue(usPrice.isElement());
747     assertEquals(new XsQName((String JavaDoc) null,"USPrice"), usPrice.getObjectSG().getName());
748     TypeSG usPriceSST = usPrice.getObjectSG().getTypeSG();
749     assertTrue(!usPrice.getObjectSG().getTypeSG().isComplex());
750     assertTrue(usPriceSST.getSimpleTypeSG().isAtomic());
751     assertEquals(java.math.BigDecimal JavaDoc.class.getName(), usPriceSST.getRuntimeType().toString());
752     // Items.item.comment
753
// <xsd:element ref='comment' minOccurs='0'/>
754
ParticleSG comment = itemChildren[3];
755     assertTrue(comment.isElement());
756     assertEquals(new XsQName((String JavaDoc) null, "comment"), comment.getObjectSG().getName());
757     assertEquals(0, comment.getMinOccurs());
758     TypeSG commentSST = comment.getObjectSG().getTypeSG();
759     assertTrue(!commentSST.isComplex());
760     assertTrue(commentSST.getSimpleTypeSG().isAtomic());
761     assertEquals(JavaQNameImpl.getInstance(String JavaDoc.class), commentSST.getRuntimeType());
762     // Items.item.productName
763
// <xsd:element name='productName' type='xsd:string'/>
764
ParticleSG productName = itemChildren[0];
765     assertTrue(productName.isElement());
766     assertEquals(new XsQName((String JavaDoc) null, "productName"), productName.getObjectSG().getName());
767     TypeSG productNameSST = productName.getObjectSG().getTypeSG();
768     assertTrue(!productNameSST.isComplex());
769     assertTrue(productNameSST.getSimpleTypeSG().isAtomic());
770     assertEquals(JavaQNameImpl.getInstance(String JavaDoc.class), productNameSST.getRuntimeType());
771     // Items.item.quantity
772
ParticleSG quantity = itemChildren[1];
773     assertTrue(quantity.isElement());
774     assertEquals(new XsQName((String JavaDoc) null, "quantity"), quantity.getObjectSG().getName());
775     TypeSG quantitySST = quantity.getObjectSG().getTypeSG();
776     assertTrue(!quantitySST.isComplex());
777     assertTrue(quantitySST.getSimpleTypeSG().isAtomic());
778     assertEquals(JavaQNameImpl.getInstance(java.math.BigInteger JavaDoc.class), quantitySST.getRuntimeType());
779     // Items.item.shipDate
780
// <xsd:element name='shipDate' type='xsd:date' minOccurs='0'/>
781
ParticleSG shipDate = itemChildren[4];
782     assertTrue(shipDate.isElement());
783     assertEquals(new XsQName((String JavaDoc) null, "shipDate"), shipDate.getObjectSG().getName());
784     TypeSG shipDateSST = shipDate.getObjectSG().getTypeSG();
785     assertTrue(!shipDateSST.isComplex());
786     assertEquals(0, shipDate.getMinOccurs());
787     assertTrue(shipDateSST.getSimpleTypeSG().isAtomic());
788     assertEquals(Calendar JavaDoc.class.getName(), shipDateSST.getRuntimeType().toString());
789
790     // PurchaseOrderType
791
TypeSG purchaseOrderType = schemaTypes[3];
792     assertEquals(new XsQName((String JavaDoc) null, "PurchaseOrderType"), purchaseOrderType.getName());
793     assertTrue(purchaseOrderType.isGlobalType());
794     assertTrue(purchaseOrderType.isComplex());
795     assertTrue(purchaseOrderType.getComplexTypeSG().getComplexContentSG().getRootParticle().getGroupSG().isSequence());
796     // PurchaseOrderType.orderDate
797
// <xsd:attribute name='orderDate' type='xsd:date'/>
798
AttributeSG [] potAttributes = purchaseOrderType.getComplexTypeSG().getAttributes();
799     assertEquals(1, potAttributes.length);
800     AttributeSG orderDate = potAttributes[0];
801     assertEquals(new XsQName((String JavaDoc) null, "orderDate"), orderDate.getName());
802     assertEquals(JavaQNameImpl.getInstance(Calendar JavaDoc.class),
803                  orderDate.getTypeSG().getSimpleTypeSG().getRuntimeType());
804     ParticleSG [] potChildren = purchaseOrderType.getComplexTypeSG().getComplexContentSG().getRootParticle().getGroupSG().getParticles();
805     assertEquals(4, potChildren.length);
806     // PurchaseOrderType.shipTo
807
// <xsd:element name='shipTo' type='USAddress'/>
808
ParticleSG shipTo = potChildren[0];
809     assertTrue(shipTo.isElement());
810     assertEquals(new XsQName((String JavaDoc) null, "shipTo"), shipTo.getObjectSG().getName());
811     TypeSG shipToST = shipTo.getObjectSG().getTypeSG();
812     assertTrue(shipToST.isComplex());
813     assertEquals(new XsQName((String JavaDoc) null, "USAddress"), shipToST.getName());
814     assertTrue(shipToST.isComplex());
815     // PurchaseOrderType.billTo
816
// <xsd:element name='billTo' type='USAddress'/>
817
ParticleSG billTo = potChildren[1];
818     assertTrue(billTo.isElement());
819     assertEquals(new XsQName((String JavaDoc) null, "billTo"), billTo.getObjectSG().getName());
820     TypeSG billToST = billTo.getObjectSG().getTypeSG();
821     assertTrue(billToST.isComplex());
822     assertEquals(new XsQName((String JavaDoc) null, "USAddress"), billToST.getName());
823     assertTrue(billToST.isComplex());
824     // PurchaseOrderType.comment
825
// <xsd:element ref='comment' minOccurs='0'/>
826
ParticleSG potComment = potChildren[2];
827     assertTrue(potComment.isElement());
828     assertEquals(new XsQName((String JavaDoc) null, "comment"), comment.getObjectSG().getName());
829     assertEquals(0, comment.getMinOccurs());
830     TypeSG potCommentST = potComment.getObjectSG().getTypeSG();
831     assertTrue(!potCommentST.isComplex());
832     assertEquals(JavaQNameImpl.getInstance(String JavaDoc.class), potCommentST.getRuntimeType());
833     // PurchaseOrderType.items
834
// <xsd:element name='items' type='Items'/>
835
ParticleSG potItems = potChildren[3];
836     assertTrue(potItems.isElement());
837     assertEquals(new XsQName((String JavaDoc) null, "items"), potItems.getObjectSG().getName());
838     assertTrue(potItems.getObjectSG().getTypeSG().isComplex());
839
840     // purchaseOrder
841
// <xsd:element name='purchaseOrder' type='PurchaseOrderType'/>
842
ObjectSG[] elements = jschema.getElements();
843     assertNotNull(elements);
844     assertEquals(2, elements.length);
845     ObjectSG purchaseOrder = elements[1];
846     assertEquals(new XsQName((String JavaDoc) null, "purchaseOrder"), purchaseOrder.getName());
847     assertTrue(purchaseOrder.getTypeSG().isComplex());
848     assertTrue(purchaseOrder.getTypeSG().isGlobalType());
849     assertEquals(new XsQName((String JavaDoc) null, "PurchaseOrderType"), purchaseOrder.getTypeSG().getName());
850   }
851
852   /** A test case to trigger a previous parser bug. */
853   public void testRestrictionMaxExclusive() throws Exception JavaDoc {
854     final String JavaDoc schema =
855       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> \n" +
856       " <xsd:annotation><xsd:appinfo>\n" +
857       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
858       " <jaxb:package name='parsertest.testrestrictionmaxexclusive'/>\n" +
859       " </jaxb:schemaBindings>\n" +
860       " </xsd:appinfo></xsd:annotation>\n" +
861       " <xsd:element name='quantity'> \n" +
862       " <xsd:simpleType> \n" +
863       " <xsd:restriction base='xsd:decimal'> \n" +
864       " <xsd:maxExclusive value='100'/> \n" +
865       " </xsd:restriction> \n" +
866       " </xsd:simpleType> \n" +
867       " </xsd:element> \n" +
868       "</xsd:schema> \n";
869
870     SchemaSG jschema = parse(schema, "testRestrictionMaxExclusive.xsd");
871     ObjectSG[] elements = jschema.getElements();
872     assertEquals(1, elements.length);
873     ObjectSG quantity = elements[0];
874     String JavaDoc maxExclusive = quantity.getTypeSG().getSimpleTypeSG().getAtomicType().getMaxExclusive();
875     assertNotNull(maxExclusive);
876     assertEquals("100", maxExclusive);
877   }
878
879   /** Test some basic functionality of the builtin datatype nonPositiveInteger. */
880   public void testNonPositiveInteger() throws Exception JavaDoc {
881     final String JavaDoc schema =
882       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> \n" +
883       " <xsd:annotation><xsd:appinfo>\n" +
884       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
885       " <jaxb:package name='parsertest.testnonpositiveinteger'/>\n" +
886       " </jaxb:schemaBindings>\n" +
887       " </xsd:appinfo></xsd:annotation>\n" +
888       " <xsd:element name='non-positive-integer' type='xsd:nonPositiveInteger'/> \n" +
889       "</xsd:schema> \n";
890
891     SchemaSG jschema = parse(schema, "testNonPositiveInteger.xsd");
892
893     // simple, atomic, with restriction and maxExclusive and maxInclusive
894
TypeSG npi = jschema.getElements()[0].getTypeSG();
895     assertTrue(!npi.isComplex());
896     SimpleTypeSG npis = npi.getSimpleTypeSG();
897     assertTrue(npis.isAtomic());
898     assertTrue(!npis.isList());
899     assertTrue(!npis.isUnion());
900     assertEquals(new Long JavaDoc(0), npis.getAtomicType().getFractionDigits());
901     assertEquals("0", npis.getAtomicType().getMaxInclusive());
902     assertEquals("1", npis.getAtomicType().getMaxExclusive());
903   }
904
905   /** Test some basic functionality of the builtin datatype negativeInteger. */
906   public void testNegativeInteger() throws Exception JavaDoc {
907     final String JavaDoc schema =
908       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> \n" +
909       " <xsd:annotation><xsd:appinfo>\n" +
910       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
911       " <jaxb:package name='parsertest.testnegativeinteger'/>\n" +
912       " </jaxb:schemaBindings>\n" +
913       " </xsd:appinfo></xsd:annotation>\n" +
914       " <xsd:element name='negative-integer' type='xsd:negativeInteger'/> \n" +
915       "</xsd:schema> \n";
916
917     SchemaSG jschema = parse(schema, "testNegativeInteger.xsd");
918
919     // simple, atomic, with restrictions on maxInclusive and MaxExclusive
920
TypeSG ni = jschema.getElements()[0].getTypeSG();
921     assertTrue(!ni.isComplex());
922     SimpleTypeSG nis = ni.getSimpleTypeSG();
923     assertTrue(nis.isAtomic());
924     assertTrue(!nis.isList());
925     assertTrue(!nis.isUnion());
926     assertEquals(new Long JavaDoc(0), nis.getAtomicType().getFractionDigits());
927     assertEquals("-1", nis.getAtomicType().getMaxInclusive());
928     assertEquals("0", nis.getAtomicType().getMaxExclusive());
929   }
930
931   public void testNonNegativeIntegerType() throws Exception JavaDoc {
932     final String JavaDoc schema =
933       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> \n" +
934       " <xsd:annotation><xsd:appinfo>\n" +
935       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
936       " <jaxb:package name='parsertest.testnonnegativeinteger'/>\n" +
937       " </jaxb:schemaBindings>\n" +
938       " </xsd:appinfo></xsd:annotation>\n" +
939       " <xsd:element name='non-negative-integer' type='xsd:nonNegativeInteger'/> \n" +
940       "</xsd:schema> \n";
941
942     SchemaSG jschema = parse(schema, "testNonNegativeIntegerType.xsd");
943
944     // simple, atomic, with restriction on minInclusive and minExclusive
945
TypeSG nni = jschema.getElements()[0].getTypeSG();
946     assertTrue(!nni.isComplex());
947     SimpleTypeSG nnis = nni.getSimpleTypeSG();
948     assertTrue(nnis.isAtomic());
949     assertTrue(!nnis.isList());
950     assertTrue(!nnis.isUnion());
951     assertEquals(new Long JavaDoc(0), nnis.getAtomicType().getFractionDigits());
952     assertEquals("-1", nnis.getAtomicType().getMinExclusive());
953     assertEquals("0", nnis.getAtomicType().getMinInclusive());
954   }
955
956   public void testPositiveIntegerType() throws Exception JavaDoc {
957     final String JavaDoc schema =
958       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> \n" +
959       " <xsd:annotation><xsd:appinfo>\n" +
960       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
961       " <jaxb:package name='parsertest.testpositiveinteger'/>\n" +
962       " </jaxb:schemaBindings>\n" +
963       " </xsd:appinfo></xsd:annotation>\n" +
964       " <xsd:element name='positive-integer' type='xsd:positiveInteger'/> \n" +
965       "</xsd:schema> \n";
966
967     SchemaSG jschema = parse(schema, "testPositiveIntegerType.xsd");
968
969     // simple, atomic, with restriction on minInclusive and minExclusive
970
TypeSG pi = jschema.getElements()[0].getTypeSG();
971     assertTrue(!pi.isComplex());
972     SimpleTypeSG pis = pi.getSimpleTypeSG();
973     assertTrue(pis.isAtomic());
974     assertTrue(!pis.isList());
975     assertTrue(!pis.isUnion());
976     assertEquals(new Long JavaDoc(0), pis.getAtomicType().getFractionDigits());
977     assertEquals("0", pis.getAtomicType().getMinExclusive());
978     assertEquals("1", pis.getAtomicType().getMinInclusive());
979   }
980
981   /** Test some basic functionality of the builtin datatype integer. */
982   public void testIntegerType() throws Exception JavaDoc {
983     final String JavaDoc schema =
984       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> \n" +
985       " <xsd:annotation><xsd:appinfo>\n" +
986       " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
987       " <jaxb:package name='parsertest.testintegertype'/>\n" +
988       " </jaxb:schemaBindings>\n" +
989       " </xsd:appinfo></xsd:annotation>\n" +
990       " <xsd:element name='integer' type='xsd:integer'/> \n" +
991       "</xsd:schema> \n";
992
993     SchemaSG jschema = parse(schema, "testIntegerType.xsd");
994
995     // simple, atomic, with restriction on fractionDigits
996
ObjectSG[] elements = jschema.getElements();
997     TypeSG i = elements[0].getTypeSG();
998     assertTrue(!i.isComplex());
999     SimpleTypeSG is = i.getSimpleTypeSG();
1000    assertTrue(is.isAtomic());
1001    assertTrue(!is.isList());
1002    assertTrue(!is.isUnion());
1003    assertEquals(JavaQNameImpl.getInstance(BigInteger JavaDoc.class), is.getRuntimeType());
1004    assertEquals(new Long JavaDoc(0), is.getAtomicType().getFractionDigits());
1005  }
1006
1007  /** Test built-in type NMTOKENS. */
1008  public void testNmTokensType() throws Exception JavaDoc {
1009    final String JavaDoc schema =
1010      "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> \n" +
1011      " <xs:annotation><xs:appinfo>\n" +
1012      " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
1013      " <jaxb:package name='parsertest.testnmtokenstype'/>\n" +
1014      " </jaxb:schemaBindings>\n" +
1015      " </xs:appinfo></xs:annotation>\n" +
1016      " <xs:element name='some-nmtokens' type='xs:NMTOKENS'/> \n" +
1017      "</xs:schema> \n";
1018
1019    SchemaSG jschema = parse(schema, "testNmTokensType.xsd");
1020
1021    // list of one or more nmtoken's
1022
ObjectSG[] elements = jschema.getElements();
1023    assertEquals(1, elements.length);
1024    TypeSG nmts = elements[0].getTypeSG();
1025    assertTrue(!nmts.isComplex());
1026    SimpleTypeSG nmtss = nmts.getSimpleTypeSG();
1027    assertTrue(!nmtss.isAtomic());
1028    assertTrue(nmtss.isList());
1029    assertTrue(!nmtss.isUnion());
1030    ListTypeSG nmtsl = nmtss.getListType();
1031    assertEquals(new Long JavaDoc(1), nmtsl.getMinLength());
1032  }
1033
1034    /** Test for <a HREF="http://issues.apache.org/jira/browse/JAXME-46">JAXME-46</a>
1035     */

1036    public void testJira46() throws Exception JavaDoc {
1037        final String JavaDoc uri = "http://www.cnipa.it/schemas/2003/eGovIT/Busta1_0/";
1038        final String JavaDoc schemaSpec =
1039            "<xs:schema targetNamespace='" + uri + "'\n" +
1040            " xmlns:eGov_IT='" + uri + "'\n" +
1041            " xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
1042            " <xs:element name='Riferimento'>\n" +
1043            " <xs:complexType>\n" +
1044            " <xs:sequence/>\n" +
1045            " <xs:attribute ref='eGov_IT:id' use='required'/>\n" +
1046            " </xs:complexType>\n" +
1047            " </xs:element>\n" +
1048            " <xs:attribute name='id' type='xs:ID'/>\n" +
1049            "</xs:schema>\n";
1050        
1051        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSpec));
1052        isource.setSystemId("jira46.xsd");
1053        JAXBSchemaReader r = getSchemaReader();
1054        XSParser parser = r.getSGFactory().newXSParser();
1055        parser.setValidating(false);
1056        XSSchema schema = parser.parse(isource);
1057        XSAttribute[] attrs = schema.getAttributes();
1058        assertEquals(1, attrs.length);
1059        XSAttribute idAttr = attrs[0];
1060        assertTrue(idAttr instanceof JAXBAttribute);
1061        assertEquals(new XsQName(uri, "id"), idAttr.getName());
1062        assertEquals(XSID.getInstance(), idAttr.getType());
1063        assertTrue(idAttr.isOptional());
1064        XSElement[] elements = schema.getElements();
1065        assertEquals(1, elements.length);
1066        XSElement rifElem = elements[0];
1067        assertFalse(rifElem.getType().isSimple());
1068        XSComplexType ct = rifElem.getType().getComplexType();
1069        XSAttributable[] rifAttrs = ct.getAttributes();
1070        assertEquals(1, rifAttrs.length);
1071        XSAttribute idRef = (XSAttribute) rifAttrs[0];
1072        assertTrue(idRef instanceof JAXBAttribute);
1073        assertFalse(idRef.equals(idAttr));
1074        assertEquals(new XsQName(uri, "id"), idAttr.getName());
1075        assertEquals(XSID.getInstance(), idAttr.getType());
1076        assertFalse(idRef.isOptional());
1077    }
1078
1079    /** Test, whether a choice group with multiplicity > 1 is
1080     * rejected.
1081     */

1082    public void testMultipleGroupRejected() throws Exception JavaDoc {
1083        final String JavaDoc schemaSpec =
1084            "<xs:schema\n" +
1085            " xmlns:xs='http://www.w3.org/2001/XMLSchema'\n" +
1086            " elementFormDefault='qualified'\n" +
1087            " xmlns:jaxb='http://java.sun.com/xml/ns/jaxb'\n" +
1088            " jaxb:version='1.0'>\n" +
1089            " <xs:annotation><xs:appinfo>\n" +
1090            " <jaxb:schemaBindings xmlns:jaxb='" + JAXBParser.JAXB_SCHEMA_URI + "'>\n" +
1091            " <jaxb:package name='parsertest.testmultiplegroupsrejected'/>\n" +
1092            " </jaxb:schemaBindings>\n" +
1093            " </xs:appinfo></xs:annotation>\n" +
1094            " <xs:element name='DIAGJOBS'>\n" +
1095            " <xs:complexType>\n" +
1096            " <xs:choice maxOccurs='unbounded'>\n" +
1097            " <xs:element name='DIAGJOB' type='xs:string'/>\n" +
1098            " <xs:sequence>\n" +
1099            " <xs:element name='DJREF' type='xs:long'/>\n" +
1100            " <xs:element name='DESCRIPTIONS' type='xs:string' minOccurs='0'/>\n" +
1101            " </xs:sequence>\n" +
1102            " </xs:choice>\n" +
1103            " </xs:complexType>\n" +
1104            " </xs:element>\n" +
1105            "</xs:schema>\n";
1106        SchemaSG schema = parse(schemaSpec, "testMultipleGroupRejected.xsd");
1107        try {
1108            schema.generate();
1109            fail("Expected an exception");
1110        } catch (SAXException JavaDoc e) {
1111            assertTrue(e.getMessage().indexOf("Model groups with maxOccurs > 1 are not yet supported.") != -1);
1112        }
1113    }
1114}
1115
Popular Tags