KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > 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.xs.junit;
18
19 import java.io.IOException JavaDoc;
20 import java.io.StringReader JavaDoc;
21 import java.lang.reflect.InvocationHandler JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.lang.reflect.Proxy JavaDoc;
24
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26
27 import org.apache.ws.jaxme.xs.XSAnnotation;
28 import org.apache.ws.jaxme.xs.XSAppinfo;
29 import org.apache.ws.jaxme.xs.XSAttributable;
30 import org.apache.ws.jaxme.xs.XSAttribute;
31 import org.apache.ws.jaxme.xs.XSAttributeGroup;
32 import org.apache.ws.jaxme.xs.XSComplexType;
33 import org.apache.ws.jaxme.xs.XSElement;
34 import org.apache.ws.jaxme.xs.XSElementOrAttrRef;
35 import org.apache.ws.jaxme.xs.XSEnumeration;
36 import org.apache.ws.jaxme.xs.XSGroup;
37 import org.apache.ws.jaxme.xs.XSIdentityConstraint;
38 import org.apache.ws.jaxme.xs.XSKeyRef;
39 import org.apache.ws.jaxme.xs.XSListType;
40 import org.apache.ws.jaxme.xs.XSModelGroup;
41 import org.apache.ws.jaxme.xs.XSObject;
42 import org.apache.ws.jaxme.xs.XSParser;
43 import org.apache.ws.jaxme.xs.XSParticle;
44 import org.apache.ws.jaxme.xs.XSSchema;
45 import org.apache.ws.jaxme.xs.XSSimpleType;
46 import org.apache.ws.jaxme.xs.XSType;
47 import org.apache.ws.jaxme.xs.XSUnionType;
48 import org.apache.ws.jaxme.xs.XSWildcard;
49 import org.apache.ws.jaxme.xs.jaxb.JAXBGlobalBindings;
50 import org.apache.ws.jaxme.xs.jaxb.JAXBSchema;
51 import org.apache.ws.jaxme.xs.jaxb.JAXBXsObjectFactory;
52 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser;
53 import org.apache.ws.jaxme.xs.types.XSBoolean;
54 import org.apache.ws.jaxme.xs.types.XSDate;
55 import org.apache.ws.jaxme.xs.types.XSDateTime;
56 import org.apache.ws.jaxme.xs.types.XSDecimal;
57 import org.apache.ws.jaxme.xs.types.XSDouble;
58 import org.apache.ws.jaxme.xs.types.XSFloat;
59 import org.apache.ws.jaxme.xs.types.XSID;
60 import org.apache.ws.jaxme.xs.types.XSInt;
61 import org.apache.ws.jaxme.xs.types.XSNMToken;
62 import org.apache.ws.jaxme.xs.types.XSPositiveInteger;
63 import org.apache.ws.jaxme.xs.types.XSString;
64 import org.apache.ws.jaxme.xs.xml.XsNamespaceList;
65 import org.apache.ws.jaxme.xs.xml.XsObjectFactory;
66 import org.apache.ws.jaxme.xs.xml.XsQName;
67 import org.xml.sax.Attributes JavaDoc;
68 import org.xml.sax.EntityResolver JavaDoc;
69 import org.xml.sax.InputSource JavaDoc;
70 import org.xml.sax.SAXException JavaDoc;
71 import org.xml.sax.XMLReader JavaDoc;
72
73
74 /** <p>Implements some basic tests for the Schema generator.</p>
75  */

76 public class ParserTest extends ParserTestBase {
77     /** <p>Creates a new instance of <code>ParserTest</code> named
78      * <code>pName</code>.</p>
79      * @param pName The test name
80      */

81     public ParserTest(String JavaDoc pName) { super(pName); }
82
83     protected void testSimpleTypes(XSParser pParser) throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc {
84         final String JavaDoc schemaSource =
85             "<?xml version='1.0'?>\n" +
86             "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
87             " <xs:simpleType name='a'>\n" +
88             " <xs:restriction base='xs:string'>\n" +
89             " <xs:minLength value='3'/>\n" +
90             " </xs:restriction>\n" +
91             " </xs:simpleType>\n" +
92             "\n" +
93             " <xs:simpleType name='b'>\n" +
94             " <xs:list itemType='xs:int'/>\n" +
95             " </xs:simpleType>\n" +
96             "\n" +
97             " <xs:simpleType name='c'>\n" +
98             " <xs:union memberTypes='a b'/>\n" +
99             " </xs:simpleType>\n" +
100             "</xs:schema>\n";
101         
102         InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
103         isource.setSystemId("testSimpleTypes.xsd");
104         XSSchema schema = pParser.parse(isource);
105         
106         XSType[] types = schema.getTypes();
107         assertEquals(3, types.length);
108         
109         XSType t1 = types[0];
110         assertEquals(new XsQName((String JavaDoc) null, "a"), t1.getName());
111         XSSimpleType st1 = assertSimpleType(t1);
112         assertAtomicType(st1);
113         XSType t1_1 = assertRestriction(st1);
114         assertEquals(XSString.getInstance(), t1_1);
115         
116         XSType t2 = types[1];
117         assertEquals(new XsQName((String JavaDoc) null, "b"), t2.getName());
118         XSSimpleType st2 = assertSimpleType(t2);
119         XSListType lt = assertListType(st2);
120         XSType it = lt.getItemType();
121         assertNotNull(it);
122         assertEquals(XSInt.getInstance(), it);
123         XSSimpleType it2 = assertSimpleType(it);
124         assertAtomicType(it2);
125         
126         XSType t3 = types[2];
127         assertEquals(new XsQName((String JavaDoc) null, "c"), t3.getName());
128         XSSimpleType st3 = assertSimpleType(t3);
129         XSUnionType ut3 = assertUnionType(st3);
130         XSType[] memberTypes = ut3.getMemberTypes();
131         assertEquals(2, memberTypes.length);
132         XSType mt3_1 = memberTypes[0];
133         assertEquals(mt3_1, t1);
134         XSType mt3_2 = memberTypes[1];
135         assertEquals(mt3_2, t2);
136     }
137     
138     /** <p>Tests whether the basic simple types can be parsed:
139      * Atomic, List, and Union.</p>
140      * @throws Exception
141      */

142     public void testSimpleTypes() throws Exception JavaDoc {
143         XSParser xsParser = newXSParser();
144         testSimpleTypes(xsParser);
145         JAXBParser jaxbParser = newJAXBParser();
146         testSimpleTypes(jaxbParser);
147     }
148
149   protected void testAttributes(XSParser pParser) throws Exception JavaDoc {
150     final String JavaDoc schemaSource =
151       "<?xml version='1.0'?>\n" +
152       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
153       " <xs:attribute name='a' type='xs:string'/>\n" +
154       "\n" +
155       " <xs:attribute name='b'>\n" +
156       " <xs:simpleType>\n" +
157       " <xs:restriction base='xs:int'/>\n" +
158       " </xs:simpleType>\n" +
159       " </xs:attribute>\n" +
160       "\n" +
161       " <xs:attribute name='c' type='xs:int'/>\n" +
162       "</xs:schema>\n";
163
164     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
165     isource.setSystemId("testAttributes.xsd");
166     XSSchema schema = pParser.parse(isource);
167
168     XSAttributable[] attr = schema.getAttributes();
169     assertEquals(3, attr.length);
170
171     XSAttribute attr1 = (XSAttribute) attr[0];
172     assertTrue(attr1.isGlobal());
173     assertEquals(new XsQName((String JavaDoc) null, "a"), attr1.getName());
174     XSType t1 = attr1.getType();
175     assertEquals(XSString.getInstance(), t1);
176     XSSimpleType st1 = assertSimpleType(attr1.getType());
177     assertAtomicType(st1);
178
179     XSAttribute attr2 = (XSAttribute) attr[1];
180     assertTrue(attr2.isGlobal());
181     assertEquals(new XsQName((String JavaDoc) null, "b"), attr2.getName());
182     XSType t2 = attr2.getType();
183     assertTrue(!XSInt.getInstance().equals(t2));
184     XSType t2_1 = assertRestriction(assertSimpleType(t2));
185     assertEquals(XSInt.getInstance(), t2_1);
186     XSSimpleType st2 = assertSimpleType(t2);
187     assertAtomicType(st2);
188
189     XSAttribute attr3 = (XSAttribute) attr[2];
190     assertTrue(attr3.isGlobal());
191     assertEquals(new XsQName((String JavaDoc) null, "c"), attr3.getName());
192     XSType t3 = attr3.getType();
193     assertEquals(XSInt.getInstance(), t3);
194     XSSimpleType st3 = assertSimpleType(t3);
195     assertAtomicType(st3);
196   }
197
198   /** Tests typical attribute uses.
199    */

200   public void testAttributes() throws Exception JavaDoc {
201     XSParser xsParser = newXSParser();
202     testAttributes(xsParser);
203     JAXBParser jaxbParser = newJAXBParser();
204     testAttributes(jaxbParser);
205   }
206
207   protected void testAttributeGroups(XSParser pParser) throws Exception JavaDoc {
208     final String JavaDoc schemaSource =
209       "<?xml version='1.0'?>\n" +
210       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
211       " <xs:attribute name='a' type='xs:string'/>\n" +
212       " <xs:attribute name='b' type='xs:int'/>\n" +
213       "\n" +
214       " <xs:attributeGroup name='c'>\n" +
215       " <xs:attribute name='d' type='xs:dateTime'/>\n" +
216       " <xs:attribute name='e' type='xs:float'/>\n" +
217       " <xs:attribute ref='a'/>\n" +
218       " </xs:attributeGroup>\n" +
219       "\n" +
220       " <xs:attributeGroup name='f'>\n" +
221       " <xs:attribute name='g' type='xs:double'/>\n" +
222       " <xs:attributeGroup ref='c'/>\n" +
223       " <xs:attribute ref='b'/>\n" +
224       " </xs:attributeGroup>\n" +
225       "</xs:schema>\n";
226
227     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
228     isource.setSystemId("testAttributeGroups.xsd");
229     XSSchema schema = pParser.parse(isource);
230
231     XSAttributeGroup[] groups = schema.getAttributeGroups();
232     assertNotNull(groups);
233     assertEquals(2, groups.length);
234
235     XSAttributeGroup ag1 = groups[0];
236     assertEquals(new XsQName((String JavaDoc) null, "c"), ag1.getName());
237     XSAttributable[] attr1 = ag1.getAttributes();
238     assertEquals(3, attr1.length);
239     XSAttribute attr1_1 = (XSAttribute) attr1[0];
240     assertEquals(new XsQName((String JavaDoc) null, "d"), attr1_1.getName());
241     assertEquals(XSDateTime.getInstance(), attr1_1.getType());
242     XSAttribute attr1_2 = (XSAttribute) attr1[1];
243     assertEquals(new XsQName((String JavaDoc) null, "e"), attr1_2.getName());
244     assertEquals(XSFloat.getInstance(), attr1_2.getType());
245     XSAttribute attr1_3 = (XSAttribute) attr1[2];
246     assertEquals(new XsQName((String JavaDoc) null, "a"), attr1_3.getName());
247     assertEquals(XSString.getInstance(), attr1_3.getType());
248
249     XSAttributeGroup ag2 = groups[1];
250     assertEquals(new XsQName((String JavaDoc) null, "f"), ag2.getName());
251     XSAttributable[] attr2 = ag2.getAttributes();
252     assertEquals(5, attr2.length);
253     XSAttribute attr2_1 = (XSAttribute) attr2[0];
254     assertEquals(new XsQName((String JavaDoc) null, "g"), attr2_1.getName());
255     assertEquals(XSDouble.getInstance(), attr2_1.getType());
256     XSAttribute attr2_2 = (XSAttribute) attr2[1];
257     assertEquals(new XsQName((String JavaDoc) null, "d"), attr2_2.getName());
258     assertEquals(XSDateTime.getInstance(), attr2_2.getType());
259     XSAttribute attr2_3 = (XSAttribute) attr2[2];
260     assertEquals(new XsQName((String JavaDoc) null, "e"), attr2_3.getName());
261     assertEquals(XSFloat.getInstance(), attr2_3.getType());
262     XSAttribute attr2_4 = (XSAttribute) attr2[3];
263     assertEquals(new XsQName((String JavaDoc) null, "a"), attr2_4.getName());
264     assertEquals(XSString.getInstance(), attr2_4.getType());
265     XSAttribute attr2_5 = (XSAttribute) attr2[4];
266     assertEquals(new XsQName((String JavaDoc) null, "b"), attr2_5.getName());
267     assertEquals(XSInt.getInstance(), attr2_5.getType());
268   }
269
270   /** Tests typical attribute group uses.
271    */

272   public void testAttributeGroups() throws Exception JavaDoc {
273     XSParser xsParser = newXSParser();
274     testAttributeGroups(xsParser);
275     JAXBParser jaxbParser = newJAXBParser();
276     testAttributeGroups(jaxbParser);
277   }
278
279   protected void testElements(XSParser pParser) throws Exception JavaDoc {
280     final String JavaDoc schemaSource =
281       "<?xml version='1.0'?>\n" +
282       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
283       " <xs:attribute name='id' type='xs:ID'/>\n" +
284       "\n" +
285       " <xs:element name='a' type='xs:string'/>\n" +
286       "\n" +
287       " <xs:element name='b'>\n" +
288       " <xs:simpleType>\n" +
289       " <xs:restriction base='xs:float'/>\n" +
290       " </xs:simpleType>\n" +
291       " </xs:element>\n" +
292       "\n" +
293       " <xs:element name='c'>\n" +
294       " <xs:complexType>\n" +
295       " <xs:annotation>\n" +
296       " <xs:documentation>\n" +
297       " The type of 'c'.\n" +
298       " </xs:documentation>\n" +
299       " </xs:annotation>\n" +
300       " <xs:sequence>\n" +
301       " <xs:element ref='a'/>\n" +
302       " <xs:element name='d' type='xs:double' minOccurs='0'>\n" +
303       " <xs:annotation>\n" +
304       " <xs:documentation>\n" +
305       " The element 'd'.\n" +
306       " </xs:documentation>\n" +
307       " </xs:annotation>\n" +
308       " </xs:element>\n" +
309       " </xs:sequence>\n" +
310       " <xs:attribute name='e' type='xs:int'>\n" +
311       " <xs:annotation>\n" +
312       " <xs:documentation>\n" +
313       " The attribute 'e'.\n" +
314       " </xs:documentation>\n" +
315       " </xs:annotation>\n" +
316       " </xs:attribute>\n" +
317       " </xs:complexType>\n" +
318       " </xs:element>\n" +
319       "\n" +
320       " <xs:element name='f'>\n" +
321       " <xs:complexType>\n" +
322       " <xs:simpleContent>\n" +
323       " <xs:extension base='xs:int'>\n" +
324       " <xs:attribute name='g' type='xs:boolean'/>\n" +
325       " <xs:attribute ref='id'/>\n" +
326       " </xs:extension>\n" +
327       " </xs:simpleContent>\n" +
328       " </xs:complexType>\n" +
329       " </xs:element>\n" +
330       "</xs:schema>\n";
331
332     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
333     isource.setSystemId("testElements.xsd");
334     XSSchema schema = pParser.parse(isource);
335
336     XSObject[] elements = schema.getElements();
337     assertEquals(4, elements.length);
338
339     XSElement e1 = (XSElement) elements[0];
340     assertEquals(new XsQName((String JavaDoc) null, "a"), e1.getName());
341     XSType t1 = e1.getType();
342     assertEquals(XSString.getInstance(), t1);
343
344     XSElement e2 = (XSElement) elements[1];
345     assertEquals(new XsQName((String JavaDoc) null, "b"), e2.getName());
346     XSType t2 = e2.getType();
347     assertEquals(XSFloat.getInstance(), assertRestriction(assertSimpleType(t2)));
348
349     XSElement e3 = (XSElement) elements[2];
350     assertEquals(new XsQName((String JavaDoc) null, "c"), e3.getName());
351     XSComplexType ct3 = assertComplexType(e3.getType());
352     XSParticle p3 = assertComplexContent(ct3);
353     XSGroup g3 = assertGroup(p3);
354     XSParticle[] particles3 = g3.getParticles();
355     assertEquals(2, particles3.length);
356     XSElement e3_1 = assertElement(particles3[0]);
357     assertEquals(new XsQName((String JavaDoc) null, "a"), e3_1.getName());
358     assertEquals(e1.getType(), e3_1.getType());
359     XSParticle p3_2 = particles3[1];
360     assertEquals(0, p3_2.getMinOccurs());
361     XSElement e3_2 = assertElement(p3_2);
362     assertEquals(new XsQName((String JavaDoc) null, "d"), e3_2.getName());
363     assertEquals(XSDouble.getInstance(), e3_2.getType());
364     XSAttributable[] a3 = ct3.getAttributes();
365     assertEquals(1, a3.length);
366     XSAttribute a3_1 = (XSAttribute) a3[0];
367     assertEquals(new XsQName((String JavaDoc) null, "e"), a3_1.getName());
368     assertEquals(XSInt.getInstance(), a3_1.getType());
369
370     XSElement e4 = (XSElement) elements[3];
371     assertEquals(new XsQName((String JavaDoc) null, "f"), e4.getName());
372     XSComplexType ct4 = assertComplexType(e4.getType());
373     XSType t4 = assertSimpleContent(ct4).getType();
374     assertSimpleType(t4);
375     assertEquals(XSInt.getInstance(), t4);
376     XSAttributable[] a4 = ct4.getAttributes();
377     assertEquals(2, a4.length);
378     XSAttribute a4_1 = (XSAttribute) a4[0];
379     assertEquals(new XsQName((String JavaDoc) null, "g"), a4_1.getName());
380     assertEquals(XSBoolean.getInstance(), a4_1.getType());
381     XSAttribute a4_2 = (XSAttribute) a4[1];
382     assertEquals(new XsQName((String JavaDoc) null, "id"), a4_2.getName());
383     assertEquals(XSID.getInstance(), a4_2.getType());
384   }
385
386   /** Tests typical element uses.
387    */

388   public void testElements() throws Exception JavaDoc {
389     XSParser xsParser = newXSParser();
390     testElements(xsParser);
391     JAXBParser jaxbParser = newJAXBParser();
392     testElements(jaxbParser);
393   }
394
395   protected void testFacets(XSParser pParser) throws Exception JavaDoc {
396     final String JavaDoc schemaSource =
397       "<?xml version='1.0'?>\n" +
398       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
399       " <xs:simpleType name='USState'>\n" +
400       " <xs:restriction base='xs:string'>\n" +
401       " <xs:enumeration value='AK'/>\n" +
402       " <xs:enumeration value='AL'/>\n" +
403       " <xs:enumeration value='AR'/>\n" +
404       " <!-- and so on ... -->\n" +
405       " </xs:restriction>\n" +
406       " </xs:simpleType>\n" +
407       " <xs:element name='a' type='USState'/>\n" +
408       "</xs:schema>\n";
409     
410     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
411     isource.setSystemId("testFacets.xsd");
412     XSSchema schema = pParser.parse(isource);
413
414     XSElement[] elements = schema.getElements();
415     assertEquals(1, elements.length);
416     XSElement e1 = elements[0];
417     assertEquals(new XsQName((String JavaDoc) null, "a"), e1.getName());
418     XSSimpleType st1 = assertSimpleType(e1.getType());
419     assertEquals(XSString.getInstance(), assertRestriction(st1));
420     XSEnumeration[] enumerations = st1.getEnumerations();
421     assertNotNull(enumerations);
422     assertEquals(3, enumerations.length);
423     assertEquals(3, enumerations.length);
424     assertEquals("AK", enumerations[0].getValue());
425     assertEquals("AL", enumerations[1].getValue());
426     assertEquals("AR", enumerations[2].getValue());
427   }
428
429   /** Tests various facets.
430    */

431   public void testFacets() throws Exception JavaDoc {
432     XSParser xsParser = newXSParser();
433     testFacets(xsParser);
434     JAXBParser jaxbParser = newJAXBParser();
435     testFacets(jaxbParser);
436   }
437
438   /** Test of the w3c sample schema PO.
439    */

440   public void testPurchaseOrder() throws Exception JavaDoc {
441     final String JavaDoc schemaSource =
442       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n" +
443       "\n" +
444       " <xsd:annotation>\n" +
445       " <xsd:documentation xml:lang='en'>\n" +
446       " Purchase order schema for Example.com.\n" +
447       " Copyright 2000 Example.com. All rights reserved.\n" +
448       " </xsd:documentation>\n" +
449       " </xsd:annotation>\n" +
450       "\n" +
451       " <xsd:element name='purchaseOrder' type='PurchaseOrderType'/>\n" +
452       "\n" +
453       " <xsd:element name='comment' type='xsd:string'/>\n" +
454       "\n" +
455       " <xsd:complexType name='PurchaseOrderType'>\n" +
456       " <xsd:sequence>\n" +
457       " <xsd:element name='shipTo' type='USAddress'/>\n" +
458       " <xsd:element name='billTo' type='USAddress'/>\n" +
459       " <xsd:element ref='comment' minOccurs='0'/>\n" +
460       " <xsd:element name='items' type='Items'/>\n" +
461       " </xsd:sequence>\n" +
462       " <xsd:attribute name='orderDate' type='xsd:date'/>\n" +
463       " </xsd:complexType>\n" +
464       "\n" +
465       " <xsd:complexType name='USAddress'>\n" +
466       " <xsd:sequence>\n" +
467       " <xsd:element name='name' type='xsd:string'/>\n" +
468       " <xsd:element name='street' type='xsd:string'/>\n" +
469       " <xsd:element name='city' type='xsd:string'/>\n" +
470       " <xsd:element name='state' type='xsd:string'/>\n" +
471       " <xsd:element name='zip' type='xsd:decimal'/>\n" +
472       " </xsd:sequence>\n" +
473       " <xsd:attribute name='country' type='xsd:NMTOKEN'\n" +
474       " fixed='US'/>\n" +
475       " </xsd:complexType>\n" +
476       "\n" +
477       " <xsd:complexType name='Items'>\n" +
478       " <xsd:sequence>\n" +
479       " <xsd:element name='item' minOccurs='0' maxOccurs='unbounded'>\n" +
480       " <xsd:complexType>\n" +
481       " <xsd:sequence>\n" +
482       " <xsd:element name='productName' type='xsd:string'/>\n" +
483       " <xsd:element name='quantity'>\n" +
484       " <xsd:simpleType>\n" +
485       " <xsd:restriction base='xsd:positiveInteger'>\n" +
486       " <xsd:maxExclusive value='100'/>\n" +
487       " </xsd:restriction>\n" +
488       " </xsd:simpleType>\n" +
489       " </xsd:element>\n" +
490       " <xsd:element name='USPrice' type='xsd:decimal'/>\n" +
491       " <xsd:element ref='comment' minOccurs='0'/>\n" +
492       " <xsd:element name='shipDate' type='xsd:date' minOccurs='0'/>\n" +
493       " </xsd:sequence>\n" +
494       " <xsd:attribute name='partNum' type='SKU' use='required'/>\n" +
495       " </xsd:complexType>\n" +
496       " </xsd:element>\n" +
497       " </xsd:sequence>\n" +
498       " </xsd:complexType>\n" +
499       "\n" +
500       " <!-- Stock Keeping Unit, a code for identifying products -->\n" +
501       " <xsd:simpleType name='SKU'>\n" +
502       " <xsd:restriction base='xsd:string'>\n" +
503       " <xsd:pattern value='\\d{3}-[A-Z]{2}'/>\n" +
504       " </xsd:restriction>\n" +
505       " </xsd:simpleType>\n" +
506       "</xsd:schema>\n";
507
508     JAXBParser parser = newJAXBParser();
509     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
510     isource.setSystemId("testPurchaseOrder.xsd");
511     JAXBSchema schema = (JAXBSchema) parser.parse(isource);
512     JAXBGlobalBindings globalBindings = schema.getJAXBGlobalBindings();
513     assertNotNull(globalBindings);
514
515     XSType[] schemaTypes = schema.getTypes();
516     assertNotNull(schemaTypes);
517     assertEquals(4, schemaTypes.length);
518
519     // Items
520
XSType items = schemaTypes[2];
521     assertEquals(new XsQName((String JavaDoc) null, "Items"), items.getName());
522     assertTrue(items.isGlobal());
523     XSComplexType itemsComplexType = assertComplexType(items);
524     XSParticle itemsParticle = assertComplexContent(itemsComplexType);
525     XSGroup itemsGroup = assertGroup(itemsParticle);
526     assertSequence(itemsGroup);
527     // Items.item
528
XSParticle[] itemsChildren = itemsGroup.getParticles();
529     assertEquals(1, itemsChildren.length);
530     XSParticle item = itemsChildren[0];
531     XSElement itemElement = assertElement(item);
532     assertEquals(new XsQName((String JavaDoc) null, "item"), itemElement.getName());
533     assertTrue(!itemElement.isGlobal());
534     XSComplexType itemComplexType = assertComplexType(itemElement.getType());
535     assertEquals(0, item.getMinOccurs());
536     assertEquals(-1, item.getMaxOccurs());
537     XSParticle itemParticle = assertComplexContent(itemComplexType);
538     XSGroup itemGroup = assertGroup(itemParticle);
539     assertSequence(itemGroup);
540     // Items.item.partNum
541
// <xsd:attribute name='partNum' type='SKU' use='required'/>
542
XSAttributable[] itemAttributes = itemComplexType.getAttributes();
543     assertEquals(1, itemAttributes.length);
544     XSAttribute partNum = (XSAttribute) itemAttributes[0];
545     assertEquals(new XsQName((String JavaDoc) null, "partNum"), partNum.getName());
546     assertTrue(!partNum.isOptional());
547     XSType partNumType = partNum.getType();
548     assertEquals(new XsQName((String JavaDoc) null, "SKU"), partNumType.getName());
549     assertTrue(partNumType.isGlobal());
550
551     XSParticle[] itemGroupParticles = itemGroup.getParticles();
552     assertEquals(5, itemGroupParticles.length);
553
554     // Items.item.productName
555
// <xsd:element name='productName' type='xsd:string'/>
556
XSParticle productName = itemGroupParticles[0];
557     assertEquals(1, productName.getMinOccurs());
558     assertEquals(1, productName.getMaxOccurs());
559     XSElement productNameElement = assertElement(productName);
560     assertEquals(new XsQName((String JavaDoc) null, "productName"), productNameElement.getName());
561     assertSimpleType(productNameElement.getType());
562     assertEquals(XSString.getInstance(), productNameElement.getType());
563
564     // Items.item.quantity
565
XSParticle quantity = itemGroupParticles[1];
566     assertEquals(1, quantity.getMinOccurs());
567     assertEquals(1, quantity.getMaxOccurs());
568     XSElement quantityElement = assertElement(quantity);
569     assertEquals(new XsQName((String JavaDoc) null, "quantity"), quantityElement.getName());
570     XSSimpleType quantitySimpleType = assertSimpleType(quantityElement.getType());
571     assertEquals(XSPositiveInteger.getInstance(), assertRestriction(quantitySimpleType));
572
573     // Items.item.USPrice
574
// <xsd:element name='USPrice' type='xsd:decimal'/>
575
XSParticle usPrice = itemGroupParticles[2];
576     assertEquals(1, usPrice.getMinOccurs());
577     assertEquals(1, usPrice.getMaxOccurs());
578     XSElement usPriceElement = assertElement(usPrice);
579     assertEquals(new XsQName((String JavaDoc) null, "USPrice"), usPriceElement.getName());
580     assertEquals(XSDecimal.getInstance(), usPriceElement.getType());
581
582     // Items.item.comment
583
// <xsd:element ref='comment' minOccurs='0'/>
584
XSParticle comment = itemGroupParticles[3];
585     assertEquals(comment.getMinOccurs(), 0);
586     assertEquals(comment.getMaxOccurs(), 1);
587     XSElement commentElement = assertElement(comment);
588     assertEquals(new XsQName((String JavaDoc) null, "comment"), commentElement.getName());
589     assertEquals(XSString.getInstance(), commentElement.getType());
590
591     // Items.item.shipDate
592
// <xsd:element name='shipDate' type='xsd:date' minOccurs='0'/>
593
XSParticle shipDate = itemGroupParticles[4];
594     assertEquals(shipDate.getMinOccurs(), 0);
595     assertEquals(shipDate.getMaxOccurs(), 1);
596     XSElement shipDateElement = assertElement(shipDate);
597     assertEquals(XSDate.getInstance(), shipDateElement.getType());
598
599     // PurchaseOrderType
600
XSType purchaseOrderType = schemaTypes[0];
601     assertTrue(purchaseOrderType.isGlobal());
602     assertEquals(new XsQName((String JavaDoc) null, "PurchaseOrderType"), purchaseOrderType.getName());
603     XSComplexType purchaseOrderTypeComplexType = assertComplexType(purchaseOrderType);
604     XSParticle purchaseOrderTypeParticle = assertComplexContent(purchaseOrderTypeComplexType);
605     XSGroup purchaseOrderTypeGroup = assertGroup(purchaseOrderTypeParticle);
606     assertSequence(purchaseOrderTypeGroup);
607
608     XSAttributable[] potAttributes = purchaseOrderTypeComplexType.getAttributes();
609     assertEquals(1, potAttributes.length);
610     // PurchaseOrderType.orderDate
611
// <xsd:attribute name='orderDate' type='xsd:date'/>
612
XSAttribute orderDate = (XSAttribute) potAttributes[0];
613     assertEquals(new XsQName((String JavaDoc) null, "orderDate"), orderDate.getName());
614     assertEquals(XSDate.getInstance(), orderDate.getType());
615
616     XSParticle[] potChildren = purchaseOrderTypeGroup.getParticles();
617     assertEquals(4, potChildren.length);
618     // PurchaseOrderType.shipTo
619
// <xsd:element name='shipTo' type='USAddress'/>
620
XSParticle shipTo = potChildren[0];
621     assertEquals(1, shipTo.getMinOccurs());
622     assertEquals(1, shipTo.getMaxOccurs());
623     XSElement shipToElement = assertElement(shipTo);
624     assertEquals(new XsQName((String JavaDoc) null, "shipTo"), shipToElement.getName());
625     XSType shipToType = shipToElement.getType();
626     assertTrue(shipToType.isGlobal());
627     assertEquals(new XsQName((String JavaDoc) null, "USAddress"), shipToType.getName());
628     // PurchaseOrderType.billTo
629
// <xsd:element name='billTo' type='USAddress'/>
630
XSParticle billTo = potChildren[1];
631     assertEquals(1, billTo.getMinOccurs());
632     assertEquals(1, billTo.getMaxOccurs());
633     XSElement billToElement = assertElement(billTo);
634     assertEquals(new XsQName((String JavaDoc) null, "billTo"), billToElement.getName());
635     XSType billToType = billToElement.getType();
636     assertEquals(shipToType, billToType);
637     // PurchaseOrderType.comment
638
// <xsd:element ref='comment' minOccurs='0'/>
639
XSParticle potComment = potChildren[2];
640     assertEquals(0, potComment.getMinOccurs());
641     assertEquals(1, potComment.getMaxOccurs());
642     XSElement potCommentElement = assertElement(potComment);
643     assertEquals(new XsQName((String JavaDoc) null, "comment"), potCommentElement.getName());
644     assertEquals(potCommentElement.getType(), commentElement.getType());
645     // PurchaseOrderType.items
646
// <xsd:element name='items' type='Items'/>
647
XSParticle potItems = potChildren[3];
648     assertEquals(1, potItems.getMinOccurs());
649     assertEquals(1, potItems.getMaxOccurs());
650     XSElement potItemsElement = assertElement(potItems);
651     assertEquals(new XsQName((String JavaDoc) null, "items"), potItemsElement.getName());
652     assertEquals(items, potItemsElement.getType());
653
654     // SKU
655
XSType sku = schemaTypes[3];
656     assertTrue(sku.isGlobal());
657     assertEquals(new XsQName((String JavaDoc) null, "SKU"), sku.getName());
658     XSSimpleType skuSimpleType = assertSimpleType(sku);
659     assertEquals(XSString.getInstance(), assertRestriction(skuSimpleType));
660     assertEquals(sku, partNumType);
661
662     // USAddress
663
// <xsd:complexType name='USAddress'>
664
// <xsd:sequence>
665
XSType usAddress = schemaTypes[1];
666     assertTrue(usAddress.isGlobal());
667     assertEquals(new XsQName((String JavaDoc) null, "USAddress"), usAddress.getName());
668     assertTrue(usAddress.isGlobal());
669     XSComplexType usAddressComplexType = assertComplexType(usAddress);
670     XSParticle usAddressParticle = assertComplexContent(usAddressComplexType);
671     XSGroup usAddressGroup = assertGroup(usAddressParticle);
672     assertSequence(usAddressGroup);
673     // USAddress.country
674
// <xsd:attribute name='country' type='xsd:string' fixed='US'/>
675
// ToDo: test attribute fixed='US'
676
XSAttributable[] usAddressAttributes = usAddressComplexType.getAttributes();
677     assertEquals(1, usAddressAttributes.length);
678     XSAttribute country = (XSAttribute) usAddressAttributes[0];
679     assertEquals(new XsQName((String JavaDoc) null, "country"), country.getName());
680     assertEquals(XSNMToken.getInstance(), country.getType());
681     // USAddress children
682
String JavaDoc [] nameShouldBe = {"name", "street", "city", "state", "zip"};
683     XSParticle[] usAddressChildren = usAddressGroup.getParticles();
684     assertEquals(5, usAddressChildren.length);
685     for (int i = 0; i < usAddressChildren.length; i++) {
686       XSParticle child = usAddressChildren[i];
687       assertEquals(1, child.getMinOccurs());
688       assertEquals(1, child.getMaxOccurs());
689       XSElement element = assertElement(child);
690       assertEquals(new XsQName((String JavaDoc) null, nameShouldBe[i]), element.getName());
691       assertEquals(i == 4 ? XSDecimal.getInstance() : XSString.getInstance(), element.getType());
692     }
693
694     // purchaseOrder
695
// <xsd:element name='purchaseOrder' type='PurchaseOrderType'/>
696
XSElement[] schemaElements = schema.getElements();
697     assertEquals(2, schemaElements.length);
698     XSElement purchaseOrder = schemaElements[0];
699     assertEquals(new XsQName((String JavaDoc) null, "purchaseOrder"), purchaseOrder.getName());
700     assertEquals(purchaseOrderType, purchaseOrder.getType());
701     assertEquals(new XsQName((String JavaDoc) null, "comment"), schemaElements[1].getName());
702   }
703
704   /** A test case to trigger a previous parser bug. */
705   public void testRestrictionMaxExclusive() throws Exception JavaDoc {
706     final String JavaDoc schemaSource =
707       "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n" +
708       " <xsd:element name='quantity'> \n" +
709       " <xsd:simpleType> \n" +
710       " <xsd:restriction base='xsd:decimal'> \n" +
711       " <xsd:maxExclusive value='100'/> \n" +
712       " </xsd:restriction> \n" +
713       " </xsd:simpleType> \n" +
714       " </xsd:element> \n" +
715       "</xsd:schema> \n";
716
717     JAXBParser parser = newJAXBParser();
718     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
719     isource.setSystemId("testRestrictionMaxExclusive.xsd");
720     parser.parse(isource);
721   }
722
723   private void verifyLocalNamespaces(String JavaDoc pSchema, boolean pQualified) throws Exception JavaDoc {
724     XsQName fooQualified = new XsQName("http://test.com/namespaces", "foo");
725     XsQName fooUnQualified = new XsQName((String JavaDoc) null, "foo");
726     XsQName barQualified = new XsQName("http://test.com/namespaces", "bar");
727     XsQName barUnQualified = new XsQName((String JavaDoc) null, "bar");
728     
729     XSParser parser = newJAXBParser();
730     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(pSchema));
731     XSSchema schema = parser.parse(isource);
732     XSElement[] schemaElements = schema.getElements();
733     assertEquals(1, schemaElements.length);
734     assertEquals(fooQualified, schemaElements[0].getName());
735     XSAttribute[] schemaAttributes = schema.getAttributes();
736     assertEquals(1, schemaAttributes.length);
737     assertEquals(barQualified, schemaAttributes[0].getName());
738     XSType[] types = schema.getTypes();
739     assertEquals(1, types.length);
740     XSComplexType complexType = assertComplexType(types[0]);
741     XSAttributable[] typeAttributes = complexType.getAttributes();
742     assertEquals(1, typeAttributes.length);
743     assertEquals(pQualified ? barQualified : barUnQualified, ((XSAttribute) typeAttributes[0]).getName());
744     XSGroup group = assertGroup(assertComplexContent(complexType));
745     XSParticle[] particles = group.getParticles();
746     assertEquals(1, particles.length);
747     XSElement typeElement = assertElement(particles[0]);
748     assertEquals(pQualified ? fooQualified : fooUnQualified, typeElement.getName());
749   }
750
751   /** <p>Verify the correct handling of target namespace, and the like.</p>
752    */

753   public void testTargetNamespace() throws Exception JavaDoc {
754     final String JavaDoc schemaSource1 =
755       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='http://test.com/namespaces'>\n" +
756       " <xs:element name='foo' type='xs:string'/>\n" +
757       "</xs:schema>\n";
758
759     JAXBParser parser = newJAXBParser();
760     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource1));
761     isource.setSystemId("testTargetNamespace1.xsd");
762     XSSchema schema = parser.parse(isource);
763     XSElement[] schemaElements = schema.getElements();
764     assertEquals(1, schemaElements.length);
765     assertEquals(new XsQName("http://test.com/namespaces", "foo"), schemaElements[0].getName());
766     assertNull(schemaElements[0].getName().getPrefix());
767
768     final String JavaDoc schemaSource2 =
769       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:test='http://test.com/namespaces'" +
770       " targetNamespace='http://test.com/namespaces'>\n" +
771       " <xs:element name='foo' type='xs:string'/>\n" +
772       "</xs:schema>\n";
773
774     parser = newJAXBParser();
775     isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource2));
776     isource.setSystemId("testTargetNamespace1.xsd");
777     schema = parser.parse(isource);
778     schemaElements = schema.getElements();
779     assertEquals(1, schemaElements.length);
780     assertEquals(new XsQName("http://test.com/namespaces", "foo"), schemaElements[0].getName());
781     assertEquals("test", schemaElements[0].getName().getPrefix());
782
783     final String JavaDoc namespaceSchemaHeader =
784       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:test='http://test.com/namespaces'" +
785       " targetNamespace='http://test.com/namespaces'";
786     final String JavaDoc namespaceSchemaFooter =
787       " <xs:complexType name='cType'>\n" +
788       " <xs:sequence>\n" +
789       " <xs:element name='foo' type='xs:string'/>\n" +
790       " </xs:sequence>\n" +
791       " <xs:attribute name='bar'/>\n" +
792       " </xs:complexType>\n" +
793       " <xs:element name='foo' type='xs:string'/>\n" +
794       " <xs:attribute name='bar'/>\n" +
795       "</xs:schema>\n";
796
797     verifyLocalNamespaces(namespaceSchemaHeader + " >\n" + namespaceSchemaFooter, false);
798     verifyLocalNamespaces(namespaceSchemaHeader +
799                           " elementFormDefault='unqualified'\n" +
800                           " attributeFormDefault='unqualified'>\n" +
801                           namespaceSchemaFooter, false);
802     verifyLocalNamespaces(namespaceSchemaHeader +
803                           " elementFormDefault='qualified'\n" +
804                           " attributeFormDefault='qualified'>\n" +
805                           namespaceSchemaFooter, true);
806   }
807
808   /** <p>Verify substitution groups.</p>
809    */

810   public void testSubstitutionGroups() throws Exception JavaDoc {
811     final String JavaDoc schemaSource1 =
812       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'\n" +
813       " targetNamespace='http://test.com/namespaces'\n" +
814       " xmlns:ns='http://test.com/namespaces'>\n" +
815       " <xs:element name='head' type='xs:string'/>\n" +
816       " <xs:element name='subst1' type='xs:int' substitutionGroup='ns:head'/>\n" +
817       " <xs:element name='subst2' type='xs:float' substitutionGroup='ns:head'/>\n" +
818       " <xs:complexType name='test'>\n" +
819       " <xs:sequence>\n" +
820       " <xs:element ref='ns:head'/>\n" +
821       " </xs:sequence>\n" +
822       " </xs:complexType>\n" +
823       "</xs:schema>\n";
824     
825     JAXBParser parser = newJAXBParser();
826     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource1));
827     isource.setSystemId("testTargetNamespace1.xsd");
828     XSSchema schema = parser.parse(isource);
829     XSElement[] elements = schema.getElements();
830     assertEquals(3, elements.length);
831     XsQName headName = new XsQName("http://test.com/namespaces", "head");
832     XsQName subst1Name = new XsQName("http://test.com/namespaces", "subst1");
833     XsQName subst2Name = new XsQName("http://test.com/namespaces", "subst2");
834     assertEquals(headName, elements[0].getName());
835     assertNotNull(elements[0].getSubstitutionGroup());
836     assertNull(elements[0].getSubstitutionGroupName());
837     assertEquals(subst1Name, elements[1].getName());
838     assertNull(elements[1].getSubstitutionGroup());
839     assertEquals(headName, elements[1].getSubstitutionGroupName());
840     assertEquals(subst2Name, elements[2].getName());
841     assertNull(elements[2].getSubstitutionGroup());
842     assertEquals(headName, elements[2].getSubstitutionGroupName());
843
844     XSType[] types = schema.getTypes();
845     assertEquals(1, types.length);
846     XSGroup group = assertGroup(assertComplexContent(assertComplexType(types[0])));
847     XSParticle[] particles = group.getParticles();
848     assertEquals(1, particles.length);
849     XSGroup substitutedGroup = assertGroup(particles[0]);
850     assertEquals(XSModelGroup.CHOICE, substitutedGroup.getCompositor());
851     XSParticle[] substParticles = substitutedGroup.getParticles();
852     assertEquals(3, substParticles.length);
853     assertEquals(headName, assertElement(substParticles[0]).getName());
854     assertEquals(subst1Name, assertElement(substParticles[1]).getName());
855     assertEquals(subst2Name, assertElement(substParticles[2]).getName());
856   }
857
858   /** <p>Verify extension handling.</p>
859    */

860   public void testExtensions() throws Exception JavaDoc {
861     final String JavaDoc schemaSource =
862       "<?xml version='1.0'?>\n" +
863       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
864       " <xs:complexType name='a'>\n" +
865       " <xs:sequence>\n" +
866       " <xs:element name='ae1' type='xs:int'/>\n" +
867       " <xs:element name='ae2' type='xs:dateTime'/>\n" +
868       " </xs:sequence>\n" +
869       " <xs:attribute name='aa1' type='xs:boolean'/>\n" +
870       " </xs:complexType>\n" +
871       "\n" +
872       " <xs:complexType name='b'>\n" +
873       " <xs:complexContent>\n" +
874       " <xs:extension base='a'>\n" +
875       " <xs:choice>\n" +
876       " <xs:element name='be1' type='xs:anyURI'/>\n" +
877       " <xs:element name='be2' type='xs:double'/>\n" +
878       " </xs:choice>\n" +
879       " <xs:attribute name='ba1'/>\n" +
880       " </xs:extension>\n" +
881       " </xs:complexContent>\n" +
882       " </xs:complexType>\n" +
883       "</xs:schema>\n";
884
885     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
886     isource.setSystemId("testElements.xsd");
887     XSParser xsParser = newXSParser();
888     XSSchema schema = xsParser.parse(isource);
889
890     XSType[] types = schema.getTypes();
891     assertEquals(2, types.length);
892     XSType a = types[0];
893     assertEquals(new XsQName((String JavaDoc) null, "a"), a.getName());
894     XSComplexType aComplexType = assertComplexType(a);
895     XSAttributable[] aAttributes = aComplexType.getAttributes();
896     assertEquals(1, aAttributes.length);
897     XSAttribute aa1 = (XSAttribute) aAttributes[0];
898     assertEquals(new XsQName((String JavaDoc) null, "aa1"), aa1.getName());
899
900     XSType b = types[1];
901     assertEquals(new XsQName((String JavaDoc) null, "b"), b.getName());
902     XSComplexType bComplexType = assertComplexType(b);
903     assertTrue(bComplexType.isExtension());
904     assertEquals(bComplexType.getExtendedType(), a);
905     XSAttributable[] bAttributes = bComplexType.getAttributes();
906     assertEquals(2, bAttributes.length);
907     assertEquals(aa1, bAttributes[0]);
908     XSAttribute ba1 = (XSAttribute) bAttributes[1];
909     assertEquals(new XsQName((String JavaDoc) null, "ba1"), ba1.getName());
910   }
911
912   /** Tests setting attributes, which aren't specified in the
913    * schema.
914    */

915   public void testAdditionalAttributes() throws Exception JavaDoc {
916     final String JavaDoc schemaSource =
917       "<?xml version='1.0'?>\n" +
918       "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:foo='x' foo:a='y'>\n" +
919       " <xs:element name='test' type='xs:string' foo:b='z'/>\n" +
920       "</xs:schema>\n";
921
922     InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
923     isource.setSystemId("testElements.xsd");
924     XSParser xsParser = newXSParser();
925     XSSchema schema = xsParser.parse(isource);
926
927     Attributes JavaDoc schemaOpenAttrs = schema.getOpenAttributes();
928     assertNotNull(schemaOpenAttrs);
929     assertEquals(1, schemaOpenAttrs.getLength());
930     assertEquals("x", schemaOpenAttrs.getURI(0));
931     assertEquals("a", schemaOpenAttrs.getLocalName(0));
932     assertEquals("y", schemaOpenAttrs.getValue(0));
933
934     XSElement[] elements = schema.getElements();
935     assertEquals(1, elements.length);
936     Attributes JavaDoc elementOpenAttrs = elements[0].getOpenAttributes();
937     assertNotNull(elementOpenAttrs);
938     assertEquals(1, elementOpenAttrs.getLength());
939     assertEquals("x", elementOpenAttrs.getURI(0));
940     assertEquals("b", elementOpenAttrs.getLocalName(0));
941     assertEquals("z", elementOpenAttrs.getValue(0));
942   }
943
944   /** Tests restriction of a simple type.
945    */

946   public void testSimpleTypeRestriction() throws Exception JavaDoc {
947       final String JavaDoc schemaSource =
948         "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'\n" +
949         " xmlns='http://teamconnect.com'\n" +
950         " targetNamespace='http://teamconnect.com'>\n" +
951         " <xs:simpleType name='ZNSecurityTypeIID'>\n" +
952         " <xs:annotation>\n" +
953         " <xs:documentation/>\n" +
954         " </xs:annotation>\n" +
955         " <xs:restriction base='xs:int'>\n" +
956         " <xs:enumeration value='0'/>\n" +
957         " <xs:enumeration value='2'/>\n" +
958         " </xs:restriction>\n" +
959         " </xs:simpleType>\n" +
960         "</xs:schema>\n";
961       InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
962       isource.setSystemId("testSimpleTypeRestriction.xsd");
963       XSParser xsParser = newXSParser();
964       XSSchema schema = xsParser.parse(isource);
965       XSType[] types = schema.getTypes();
966       assertEquals(1, types.length);
967       XSSimpleType simpleType = assertSimpleType(types[0]);
968       assertAtomicType(simpleType);
969       XSEnumeration[] enumerations = simpleType.getEnumerations();
970       assertEquals(2, enumerations.length);
971       assertEquals("0", enumerations[0].getValue());
972       assertEquals("2", enumerations[1].getValue());
973   }
974
975   private void testSimpleKey(XSParser parser) throws Exception JavaDoc {
976     final String JavaDoc schemaSource =
977       "<?xml version='1.0' encoding='UTF-8'?>" +
978       "<xs:schema targetNamespace='http://www.teamconnect.com' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns='http://www.teamconnect.com'>" +
979       " <xs:element name='library'>" +
980       " <xs:complexType>" +
981       " <xs:sequence>" +
982       " <xs:element name='book' minOccurs='0' maxOccurs='unbounded'>" +
983       " <xs:complexType>" +
984       " <xs:attribute name='id' type='xs:int' use='optional'/>" +
985       " <xs:attribute name='title' type='xs:string' use='required'/>" +
986       " <xs:attribute name='author' type='xs:string' use='optional'/>" +
987       " </xs:complexType>" +
988       " </xs:element>" +
989       " </xs:sequence>" +
990       " </xs:complexType>" +
991       " <xs:key name='book-key'>" +
992       " <xs:selector xpath='./book'/>" +
993       " <xs:field xpath='@id'/>" +
994       " <xs:field xpath='.'/>" +
995       " </xs:key>" +
996       " <xs:keyref name='book-key-ref' refer='book-key'>" +
997       " <xs:selector xpath='./book'/>" +
998       " <xs:field xpath='@title'/>" +
999       " <xs:field xpath='.'/>" +
1000      " </xs:keyref>" +
1001      " </xs:element>" +
1002      "</xs:schema>";
1003
1004      InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1005      isource.setSystemId("testSimpleKey.xsd");
1006      XSSchema schema = parser.parse(isource);
1007
1008      XSElement[] elements = schema.getElements();
1009      assertEquals( 1, elements.length );
1010
1011      XSElement libraryElement = elements[0];
1012      XSIdentityConstraint[] ics = libraryElement.getIdentityConstraints();
1013
1014      assertEquals( 1, ics.length );
1015
1016      XSIdentityConstraint ic = ics[0];
1017      assertEquals( "book-key", ic.getName() );
1018
1019      XSElementOrAttrRef[][] icMatchCriteria = ic.getMatchCriteria();
1020
1021      assertEquals( 2, icMatchCriteria.length );
1022      assertEquals( 1, icMatchCriteria[0].length );
1023      assertEquals( 1, icMatchCriteria[1].length );
1024
1025      assertEquals(
1026        "id",
1027        icMatchCriteria[0][0].getAttribute().getName().getLocalName()
1028      );
1029      assertEquals(
1030        "book",
1031        icMatchCriteria[1][0].getElement().getName().getLocalName()
1032      );
1033
1034      XSKeyRef[] rfs = libraryElement.getKeyRefs();
1035      assertEquals( 1, rfs.length );
1036
1037      XSKeyRef rf = rfs[0];
1038      assertEquals( "book-key-ref", rf.getName() );
1039
1040      XSElementOrAttrRef[][] rfMatchCriteria = rf.getMatchCriteria();
1041      assertEquals( 2, rfMatchCriteria.length );
1042      assertEquals( 1, rfMatchCriteria[0].length );
1043      assertEquals( 1, rfMatchCriteria[1].length );
1044
1045      assertEquals(
1046        "title",
1047        rfMatchCriteria[0][0].getAttribute().getName().getLocalName()
1048      );
1049      assertEquals(
1050        "book",
1051        rfMatchCriteria[1][0].getElement().getName().getLocalName()
1052      );
1053
1054  }
1055
1056    /** Tests definition of a key.
1057     */

1058    public void testSimpleKey() throws Exception JavaDoc {
1059        XSParser xsParser = newXSParser();
1060        testSimpleKey(xsParser);
1061        JAXBParser jaxbParser = newJAXBParser();
1062        testSimpleKey(jaxbParser);
1063    }
1064
1065    private void testDocumentationChilds(XSParser pParser) throws Exception JavaDoc {
1066        final String JavaDoc schemaSource =
1067            "<?xml version='1.0' encoding='UTF-8'?>" +
1068            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' >\n" +
1069            " <xs:annotation>\n" +
1070            " <xs:documentation xmlns:cc='http://www.dummy-namespace.org/'>\n" +
1071            " <cc:foo/>\n" +
1072            " </xs:documentation>\n" +
1073            " </xs:annotation>\n" +
1074            "</xs:schema>";
1075
1076            InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1077            isource.setSystemId("testSimpleKey.xsd");
1078            pParser.parse(isource);
1079    }
1080
1081    /** <p>Tests whether xs:documentation may have arbitrary childs.</p>
1082     */

1083    public void testDocumentationChilds() throws Exception JavaDoc {
1084        XSParser xsParser = newXSParser();
1085        testDocumentationChilds(xsParser);
1086        JAXBParser jaxbParser = newJAXBParser();
1087        testDocumentationChilds(jaxbParser);
1088    }
1089
1090    private void testNamespaceLists(XSParser pParser) throws Exception JavaDoc {
1091        final String JavaDoc schemaSource =
1092            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
1093            " <xs:complexType name='foo'>\n" +
1094            " <xs:sequence>\n" +
1095            " <xs:any namespace='http://purl.org/dc/elements/1.1/' processContents='strict' minOccurs='0' maxOccurs='unbounded'/>\n" +
1096            " <xs:any namespace='http://www.jeckle.de/rss' processContents='strict' minOccurs='0'/>\n" +
1097            " </xs:sequence>\n" +
1098            " <xs:anyAttribute namespace='http://www.w3.org/1999/02/22-rdf-syntax-ns#' processContents='strict'/>\n" +
1099            " </xs:complexType>\n" +
1100            "</xs:schema>\n";
1101        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1102        isource.setSystemId("testNamespaceLists.xsd");
1103        XSSchema schema = pParser.parse(isource);
1104        XSParticle[] particles = assertGroup(assertComplexContent(assertComplexType(schema.getTypes()[0]))).getParticles();
1105        assertEquals(2, particles.length);
1106        assertTrue(particles[0].isWildcard());
1107        XSWildcard wildcard = particles[0].getWildcard();
1108        XsNamespaceList namespaceList = wildcard.getNamespaceList();
1109        assertTrue(!namespaceList.isAny());
1110        assertTrue(!namespaceList.isAny());
1111        assertEquals(1, namespaceList.getUris().length);
1112        assertEquals("http://purl.org/dc/elements/1.1/", namespaceList.getUris()[0].getURI());
1113        assertTrue(particles[1].isWildcard());
1114        wildcard = particles[1].getWildcard();
1115        namespaceList = wildcard.getNamespaceList();
1116        assertTrue(!namespaceList.isAny());
1117        assertTrue(!namespaceList.isAny());
1118        assertEquals(1, namespaceList.getUris().length);
1119        assertEquals("http://www.jeckle.de/rss", namespaceList.getUris()[0].getURI());
1120        XSAttributable[] attributes = assertComplexType(schema.getTypes()[0]).getAttributes();
1121        assertEquals(1, attributes.length);
1122        assertTrue(attributes[0] instanceof XSWildcard);
1123        wildcard = (XSWildcard) attributes[0];
1124        namespaceList = wildcard.getNamespaceList();
1125        assertTrue(!namespaceList.isAny());
1126        assertTrue(!namespaceList.isAny());
1127        assertEquals(1, namespaceList.getUris().length);
1128        assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#", namespaceList.getUris()[0].getURI());
1129    }
1130        
1131    /** <p>Test the handling of namespace lists.</p>
1132     */

1133    public void testNamespaceLists() throws Exception JavaDoc {
1134        XSParser xsParser = newXSParser();
1135        testNamespaceLists(xsParser);
1136        JAXBParser jaxbParser = newJAXBParser();
1137        testNamespaceLists(jaxbParser);
1138    }
1139
1140    private void testSimpleTypeRestriction2(XSParser pParser) throws Exception JavaDoc {
1141        final String JavaDoc schemaSource =
1142            "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" +
1143            " targetNamespace='http://asi.sbc.com/cpsosasos/trouble/data'\n" +
1144            " xmlns:s='http://asi.sbc.com/cpsosasos/trouble/data'>\n" +
1145            " <element name='foo'>\n" +
1146            " <complexType>\n" +
1147            " <attribute name='bar' type='s:ServiceIDType'/>\n" +
1148            " </complexType>\n" +
1149            " </element>\n" +
1150            " <simpleType name='ServiceIDType'>\n" +
1151            " <restriction base='s:NameTypeType'/>\n" +
1152            " </simpleType>\n" +
1153            " <simpleType name='NameTypeType'>\n" +
1154            " <union memberTypes='integer string'/>\n" +
1155            " </simpleType>\n" +
1156            "</schema>\n";
1157
1158        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1159        isource.setSystemId("testSimpleTypeRestriction2.xsd");
1160        XSSchema schema = pParser.parse(isource);
1161        XSType[] types = schema.getTypes();
1162        assertEquals(2, types.length);
1163        assertEquals(new XsQName("http://asi.sbc.com/cpsosasos/trouble/data", "NameTypeType"),
1164                     types[1].getName());
1165        XSSimpleType nameTypeType = assertSimpleType(types[1]);
1166        assertUnionType(nameTypeType);
1167        assertEquals(new XsQName("http://asi.sbc.com/cpsosasos/trouble/data", "ServiceIDType"),
1168                     types[0].getName());
1169        XSSimpleType serviceIDType = assertSimpleType(types[0]);
1170        assertUnionType(serviceIDType);
1171        assertTrue(serviceIDType.isRestriction());
1172    }
1173
1174    /** <p>Tests restrictions of simple types.</p>
1175     */

1176    public void testSimpleTypeRestriction2() throws Exception JavaDoc {
1177        testSimpleTypeRestriction2(newXSParser());
1178        testSimpleTypeRestriction2(newJAXBParser());
1179    }
1180
1181    private void testAppInfoEmbeddedText(XSParser pParser) throws Exception JavaDoc {
1182        final String JavaDoc schemaSource =
1183            "<schema xmlns='http://www.w3.org/2001/XMLSchema'>\n" +
1184            " <annotation><appinfo>foo</appinfo></annotation>\n" +
1185            "</schema>\n";
1186        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1187        isource.setSystemId("testAppInfoEmbeddedText.xsd");
1188        XSSchema schema = pParser.parse(isource);
1189        XSAnnotation[] annotations = schema.getAnnotations();
1190        assertEquals(1, annotations.length);
1191        XSAppinfo[] appinfos = annotations[0].getAppinfos();
1192        assertEquals(1, appinfos.length);
1193        Object JavaDoc[] childs = appinfos[0].getChilds();
1194        assertEquals(1, childs.length);
1195        assertEquals("foo", (String JavaDoc) childs[0]);
1196    }
1197
1198    /** <p>Does the <code>xs:appinfo</code> element support embedded text?</p>
1199     */

1200    public void testAppInfoEmbeddedText() throws Exception JavaDoc {
1201        testAppInfoEmbeddedText(newXSParser());
1202        testAppInfoEmbeddedText(newJAXBParser());
1203    }
1204
1205    private void testImportSchemaWithoutNamespace(XSParser pParser) throws Exception JavaDoc {
1206        final String JavaDoc schemaSource =
1207            "<schema xmlns='http://www.w3.org/2001/XMLSchema'" +
1208            " targetNamespace='xyz' xmlns:p='xyz'>\n" +
1209            " <include schemaLocation='abc.xsd'/>\n" +
1210            " <element name='a' type='string'/>\n" +
1211            " <element name='b'>\n" +
1212            " <complexType>\n" +
1213            " <sequence>\n" +
1214            " <element ref='p:a'/>\n" +
1215            " </sequence>\n" +
1216            " </complexType>\n" +
1217            " </element>\n" +
1218            "</schema>\n";
1219        final String JavaDoc importedSchema =
1220            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
1221            " <xs:element name='c' type='xs:string'/>\n" +
1222            " <xs:element name='d'>\n" +
1223            " <xs:complexType>\n" +
1224            " <xs:sequence>\n" +
1225            " <xs:element ref='c'/>\n" +
1226            " </xs:sequence>\n" +
1227            " </xs:complexType>\n" +
1228            " </xs:element>\n" +
1229            "\n" +
1230            " <xs:element name='AnyAttribute'>\n" +
1231            " <xs:complexType>\n" +
1232            " <xs:anyAttribute namespace='##any'/>\n" +
1233            " </xs:complexType>\n" +
1234            " </xs:element>\n" +
1235            "\n" +
1236            " <xs:element name='OtherAttribute'>\n" +
1237            " <xs:complexType>\n" +
1238            " <xs:anyAttribute namespace='##other'/>\n" +
1239            " </xs:complexType>\n" +
1240            " </xs:element>\n" +
1241            "\n" +
1242            " <xs:element name='ListAttribute'>\n" +
1243            " <xs:complexType>\n" +
1244            " <xs:anyAttribute namespace='##targetNamespace http://ws.apache.org/jaxme/test/misc/wildcards/2'/>\n" +
1245            " </xs:complexType>\n" +
1246            " </xs:element>\n" +
1247            "</xs:schema>\n";
1248
1249        EntityResolver JavaDoc resolver = new EntityResolver JavaDoc(){
1250            public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
1251                if ("abc.xsd".equals(systemId)) {
1252                    return new InputSource JavaDoc(new StringReader JavaDoc(importedSchema));
1253                } else {
1254                    throw new SAXException JavaDoc("Invalid systemId: " + systemId);
1255                }
1256            }
1257        };
1258        pParser.getContext().setXsObjectFactory(getXsObjectFactoryProxy(pParser.getContext().getXsObjectFactory(), resolver));
1259        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1260        isource.setSystemId("testImportSchemaWithoutNamespace.xsd");
1261        XSSchema schema = pParser.parse(isource);
1262        XSElement[] elements = schema.getElements();
1263        assertEquals(7, elements.length);
1264        assertEquals(new XsQName("xyz", "c"), elements[0].getName());
1265        assertEquals(new XsQName("xyz", "d"), elements[1].getName());
1266        XSComplexType anyAttrElem = assertComplexType(elements[2].getType());
1267        XSAttributable[] attrs = anyAttrElem.getAttributes();
1268        assertEquals(1, attrs.length);
1269        assertTrue(attrs[0] instanceof XSWildcard);
1270        XSWildcard wc = (XSWildcard) attrs[0];
1271        XsNamespaceList nsl = wc.getNamespaceList();
1272        assertTrue(nsl.isAny());
1273        assertNull(nsl.getUris());
1274        XSComplexType otherAttrElem = assertComplexType(elements[3].getType());
1275        attrs = otherAttrElem.getAttributes();
1276        assertEquals(1, attrs.length);
1277        assertTrue(attrs[0] instanceof XSWildcard);
1278        nsl = ((XSWildcard) attrs[0]).getNamespaceList();
1279        assertTrue(nsl.isOther());
1280        assertEquals(1, nsl.getUris().length);
1281        assertEquals("xyz", nsl.getUris()[0].toString());
1282        XSComplexType listAttrElem = assertComplexType(elements[4].getType());
1283        attrs = listAttrElem.getAttributes();
1284        assertEquals(1, attrs.length);
1285        assertTrue(attrs[0] instanceof XSWildcard);
1286        nsl = ((XSWildcard) attrs[0]).getNamespaceList();
1287        assertTrue(!nsl.isOther());
1288        assertTrue(!nsl.isAny());
1289        assertEquals(2, nsl.getUris().length);
1290        assertEquals("xyz", nsl.getUris()[0].toString());
1291        assertEquals("http://ws.apache.org/jaxme/test/misc/wildcards/2", nsl.getUris()[1].toString());
1292        assertEquals(new XsQName("xyz", "a"), elements[5].getName());
1293        assertEquals(new XsQName("xyz", "b"), elements[6].getName());
1294    }
1295
1296    /** <p>Tests, whether importing a schema without namespace
1297     * changes the imported schemas namespace to the local
1298     * namespace.</p>
1299     */

1300    public void testImportSchemaWithoutNamespace() throws Exception JavaDoc {
1301        testImportSchemaWithoutNamespace(newXSParser());
1302        testImportSchemaWithoutNamespace(newJAXBParser());
1303    }
1304
1305    private void testElementReferenceGlobal(XSParser pParser) throws Exception JavaDoc {
1306        final String JavaDoc schemaSource =
1307            "<?xml version='1.0' encoding='UTF-8'?>\n" +
1308            "<schema xmlns='http://www.w3.org/2001/XMLSchema'" +
1309            " xmlns:jaxb='http://java.sun.com/xml/ns/jaxb'" +
1310            " targetNamespace='http://ws.apache.org/jaxme/test/recursion'" +
1311            " xmlns:rec='http://ws.apache.org/jaxme/test/recursion'" +
1312            " elementFormDefault='qualified' attributeFormDefault='unqualified'>\n" +
1313            " <element name='Attribute'>\n" +
1314            " <complexType>\n" +
1315            " <attribute name='id' type='string' use='required'/>\n" +
1316            " <attribute name='value' type='string' use='optional'/>\n" +
1317            " </complexType>\n" +
1318            " </element>\n" +
1319            "\n" +
1320            " <element name='AttributeList'>\n" +
1321            " <complexType>\n" +
1322            " <sequence>\n" +
1323            " <element ref='rec:Attribute'/>\n" +
1324            " <element ref='rec:Attribute' minOccurs='0' maxOccurs='unbounded'/>\n" +
1325            " </sequence>\n" +
1326            " </complexType>\n" +
1327            " </element>\n" +
1328            "</schema>\n";
1329
1330        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1331        isource.setSystemId("testElementReferenceGlobal.xsd");
1332        XSSchema schema = pParser.parse(isource);
1333        XSElement[] elements = schema.getElements();
1334        assertEquals(2, elements.length);
1335        assertEquals(new XsQName("http://ws.apache.org/jaxme/test/recursion", "Attribute"), elements[0].getName());
1336        assertTrue(elements[0].isGlobal());
1337        assertEquals(new XsQName("http://ws.apache.org/jaxme/test/recursion", "AttributeList"), elements[1].getName());
1338        assertTrue(elements[1].isGlobal());
1339        XSComplexType complexType = assertComplexType(elements[1].getType());
1340        XSParticle particle = assertComplexContent(complexType);
1341        XSGroup group = assertGroup(particle);
1342        XSParticle[] particles = group.getParticles();
1343        assertEquals(2, particles.length);
1344        assertEquals(1, particles[0].getMinOccurs());
1345        assertEquals(1, particles[0].getMaxOccurs());
1346        XSElement refElement = assertElement(particles[0]);
1347        assertTrue(refElement.isGlobal());
1348        assertEquals(refElement.getName(), elements[0].getName());
1349        assertEquals(0, particles[1].getMinOccurs());
1350        assertEquals(-1, particles[1].getMaxOccurs());
1351        refElement = assertElement(particles[1]);
1352        assertTrue(refElement.isGlobal());
1353        assertEquals(refElement.getName(), elements[0].getName());
1354    }
1355
1356    /** Tests whether an element reference is flagged as a global
1357     * element.
1358     */

1359    public void testElementReferenceGlobal() throws Exception JavaDoc {
1360        testElementReferenceGlobal(newXSParser());
1361        testElementReferenceGlobal(newJAXBParser());
1362    }
1363
1364    private void checkMailTemplateGroup(String JavaDoc pURI, XSType pType) throws SAXException JavaDoc {
1365        XSComplexType ct = assertComplexType(pType);
1366        assertFalse(ct.isEmpty());
1367        XSParticle particle = assertComplexContent(ct);
1368        XSGroup group = assertGroup(particle);
1369        assertSequence(group);
1370        XSParticle[] particles = group.getParticles();
1371        assertEquals(2, particles.length);
1372        assertElement(particles[0]);
1373        assertEquals(new XsQName(pURI, "subject"), particles[0].getElement().getName());
1374        XSGroup choice = assertGroup(particles[1]);
1375        assertChoice(choice);
1376        XSParticle[] choiceParticles = choice.getParticles();
1377        assertEquals(2, choiceParticles.length);
1378        XSGroup sequence = assertGroup(choiceParticles[0]);
1379        assertSequence(sequence);
1380        XSParticle[] sequenceParticles = sequence.getParticles();
1381        assertEquals(2, sequenceParticles.length);
1382        assertEquals(new XsQName(pURI, "prepend"), sequenceParticles[0].getElement().getName());
1383        assertEquals(new XsQName(pURI, "append"), sequenceParticles[1].getElement().getName());
1384        assertEquals(new XsQName(pURI, "body"), choiceParticles[1].getElement().getName());
1385    }
1386
1387    private void testMailTemplateMixed(XSParser pParser) throws Exception JavaDoc {
1388        final String JavaDoc uri = "http://ws.apache.org/jaxme/test/nestedGroups";
1389        final String JavaDoc schemaSource =
1390            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'\n" +
1391            " xmlns:ng='" + uri + "'\n" +
1392            " targetNamespace='http://ws.apache.org/jaxme/test/nestedGroups'\n" +
1393            " elementFormDefault='qualified'\n" +
1394            " attributeFormDefault='unqualified'>\n" +
1395            " <xs:group name='MailTemplateGroup'>\n" +
1396            " <xs:sequence>\n" +
1397            " <xs:element name='subject' type='xs:string'/>\n" +
1398            " <xs:choice>\n" +
1399            " <xs:sequence>\n" +
1400            " <xs:element name='prepend' type='xs:string'/>\n" +
1401            " <xs:element name='append' type='xs:string'/>\n" +
1402            " </xs:sequence>\n" +
1403            " <xs:element name='body' maxOccurs='unbounded'>\n" +
1404            " <xs:complexType>\n" +
1405            " <xs:simpleContent>\n" +
1406            " <xs:extension base='xs:string'>\n" +
1407            " <xs:attribute name='delivery' use='required'>\n" +
1408            " <xs:simpleType>\n" +
1409            " <xs:restriction base='xs:string'>\n" +
1410            " <xs:enumeration value='dailyDigest'/>\n" +
1411            " <xs:enumeration value='immediate'/>\n" +
1412            " </xs:restriction>\n" +
1413            " </xs:simpleType>\n" +
1414            " </xs:attribute>\n" +
1415            " </xs:extension>\n" +
1416            " </xs:simpleContent>\n" +
1417            " </xs:complexType>\n" +
1418            " </xs:element>\n" +
1419            " </xs:choice>\n" +
1420            " </xs:sequence>\n" +
1421            " </xs:group>\n" +
1422            "\n" +
1423            " <xs:element name='MailTemplate'>\n" +
1424            " <xs:complexType>\n" +
1425            " <xs:group ref='ng:MailTemplateGroup'/>\n" +
1426            " <xs:attribute name='language' type='xs:string' use='optional' default='EN'/>\n" +
1427            " <xs:attribute name='name' type='xs:string' use='required'/>\n" +
1428            " </xs:complexType>\n" +
1429            " </xs:element>\n" +
1430            "\n" +
1431            " <xs:element name='MailTemplateMixed'>\n" +
1432            " <xs:complexType mixed='true'>\n" +
1433            " <xs:group ref='ng:MailTemplateGroup'/>\n" +
1434            " <xs:attribute name='language' type='xs:string' use='optional' default='EN'/>\n" +
1435            " <xs:attribute name='name' type='xs:string' use='required'/>\n" +
1436            " </xs:complexType>\n" +
1437            " </xs:element>\n" +
1438            "</xs:schema>\n";
1439        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1440        isource.setSystemId("testMailTemplateMixed.xsd");
1441        XSSchema schema = pParser.parse(isource);
1442        XSElement[] elements = schema.getElements();
1443        assertEquals(2, elements.length);
1444        assertEquals(new XsQName(uri, "MailTemplate"), elements[0].getName());
1445        assertFalse(assertComplexType(elements[0].getType()).isMixed());
1446        checkMailTemplateGroup(uri, elements[0].getType());
1447        assertEquals(new XsQName(uri, "MailTemplateMixed"), elements[1].getName());
1448        assertTrue(assertComplexType(elements[1].getType()).isMixed());
1449        checkMailTemplateGroup(uri, elements[1].getType());
1450    }
1451
1452    /** Tests handling of nested groups with and/or without mixed
1453     * content.
1454     */

1455    public void testMailTemplateMixed() throws Exception JavaDoc {
1456        testMailTemplateMixed(newXSParser());
1457        testMailTemplateMixed(newJAXBParser());
1458    }
1459
1460    private XsObjectFactory getXsObjectFactoryProxy(final XsObjectFactory pFactory,
1461                                                    final EntityResolver JavaDoc pResolver) {
1462        InvocationHandler JavaDoc h = new InvocationHandler JavaDoc() {
1463            public Object JavaDoc invoke(Object JavaDoc pProxy, Method JavaDoc pMethod, Object JavaDoc[] pArgs)
1464                    throws Throwable JavaDoc {
1465                if (Object JavaDoc.class.equals(pMethod.getDeclaringClass())) {
1466                    return pMethod.invoke(pProxy, pArgs);
1467                }
1468                Object JavaDoc result = pMethod.invoke(pFactory, pArgs);
1469                if ("newXMLReader".equals(pMethod.getName()) && result instanceof XMLReader JavaDoc) {
1470                    XMLReader JavaDoc xr = (XMLReader JavaDoc) result;
1471                    xr.setEntityResolver(pResolver);
1472                }
1473                return result;
1474            }
1475        };
1476        Class JavaDoc[] classes;
1477        if (pFactory instanceof JAXBXsObjectFactory) {
1478            classes = new Class JavaDoc[]{JAXBXsObjectFactory.class};
1479        } else {
1480            classes = new Class JavaDoc[]{XsObjectFactory.class};
1481        }
1482        return (XsObjectFactory) Proxy.newProxyInstance(getClass().getClassLoader(), classes, h);
1483    }
1484
1485    private void testRecursiveXsInclude(XSParser pParser) throws Exception JavaDoc {
1486        final String JavaDoc a = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n"
1487            + " <xs:include schemaLocation='b.xsd'/>\n"
1488            + " <xs:element name='a' type='xs:int'/>\n"
1489            + "</xs:schema>\n";
1490        final String JavaDoc b = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n"
1491            + " <xs:include schemaLocation='a.xsd'/>\n"
1492            + " <xs:element name='b' type='xs:int'/>\n"
1493            + "</xs:schema>\n";
1494        final EntityResolver JavaDoc resolver = new EntityResolver JavaDoc(){
1495            public InputSource JavaDoc resolveEntity(String JavaDoc pPublicId, String JavaDoc pSystemId) throws SAXException JavaDoc, IOException JavaDoc {
1496                final String JavaDoc xml;
1497                if ("a.xsd".equals(pSystemId)) {
1498                    xml = a;
1499                } else if ("b.xsd".equals(pSystemId)) {
1500                    xml = b;
1501                } else {
1502                    return null;
1503                }
1504                InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(xml));
1505                isource.setSystemId(pSystemId);
1506                return isource;
1507            }
1508        };
1509        pParser.getContext().setXsObjectFactory(getXsObjectFactoryProxy(pParser.getContext().getXsObjectFactory(), resolver));
1510        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(a));
1511        isource.setSystemId("a.xsd");
1512        XSSchema schema = pParser.parse(isource);
1513        assertEquals(2, schema.getElements().length);
1514        assertEquals(0, schema.getTypes().length);
1515    }
1516
1517    /** Tests, whether schemas can include each other recursively.
1518     */

1519    public void testRecursiveXsInclude() throws Exception JavaDoc {
1520        testRecursiveXsInclude(newXSParser());
1521        testRecursiveXsInclude(newJAXBParser());
1522    }
1523
1524
1525    private void testGroupMultiplicity(XSParser pParser) throws Exception JavaDoc {
1526        final String JavaDoc schemaSource =
1527            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n"
1528            + "<xs:element name='PARAMETERS'>\n"
1529            + " <xs:complexType>\n"
1530            + " <xs:choice maxOccurs='unbounded'>\n"
1531            + " <xs:element name='PARAMETER'/>\n"
1532            + " <xs:element name='SYSTEMPARAMETER'/>\n"
1533            + " <xs:element name='PREPREF'/>\n"
1534            + " </xs:choice>\n"
1535            + " </xs:complexType>\n"
1536            + "</xs:element></xs:schema>\n";
1537        InputSource JavaDoc isource = new InputSource JavaDoc(new StringReader JavaDoc(schemaSource));
1538        isource.setSystemId("testElementReferenceGlobal.xsd");
1539        XSSchema schema = pParser.parse(isource);
1540        XSElement[] elements = schema.getElements();
1541        assertEquals(1, elements.length);
1542        XSElement parameters = elements[0];
1543        XSParticle particle = assertComplexContent(assertComplexType(parameters.getType()));
1544        assertEquals(1, particle.getMinOccurs());
1545        assertEquals(-1, particle.getMaxOccurs());
1546    }
1547
1548    /** Test the representation of groups with multiplicity > 1.
1549     */

1550    public void testGroupMultiplicity() throws Exception JavaDoc {
1551        testGroupMultiplicity(newXSParser());
1552        testGroupMultiplicity(newJAXBParser());
1553    }
1554}
1555
Popular Tags