1   package tests;
2   
3   import junit.framework.TestCase;
4   import org.apache.ws.commons.schema.*;
5   
6   import javax.xml.namespace.QName;
7   import javax.xml.transform.stream.StreamSource;
8   import java.io.FileInputStream;
9   import java.io.InputStream;
10  import java.util.HashSet;
11  import java.util.Iterator;
12  import java.util.Set;
13  
14  /*
15   * Copyright 2004,2007 The Apache Software Foundation.
16   * Copyright 2006 International Business Machines Corp.
17   *
18   * Licensed under the Apache License, Version 2.0 (the "License");
19   * you may not use this file except in compliance with the License.
20   * You may obtain a copy of the License at
21   *
22   *      http://www.apache.org/licenses/LICENSE-2.0
23   *
24   * Unless required by applicable law or agreed to in writing, software
25   * distributed under the License is distributed on an "AS IS" BASIS,
26   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27   * See the License for the specific language governing permissions and
28   * limitations under the License.
29   * 
30   * @author Brent Ulbricht 
31   */
32  public class RedefineTest extends TestCase {
33  
34      /**
35       * This method will test a complex type redefine.
36       *
37       * @throws Exception Any exception encountered
38       */
39      public void testComplexTypeRedefine() throws Exception {
40  
41          /*
42          redefine1.xsd
43          -----------------
44          
45          <schema xmlns="http://www.w3.org/2001/XMLSchema"
46                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
47                  xmlns:tns="http://soapinterop.org/types"
48                  targetNamespace="http://soapinterop.org/types">
49    
50            <complexType name="person">
51              <sequence>
52                <element name="firstname" type="string"/>
53                <element name="lastname" type="string"/>
54              </sequence>
55            </complexType>
56  
57            <element name="customer" type="tns:person"/>
58  
59          </schema>
60          
61                           
62          redefine2.xsd
63          -----------------
64          
65          <schema xmlns="http://www.w3.org/2001/XMLSchema"
66                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
67                  xmlns:tns="http://soapinterop.org/types"
68                  targetNamespace="http://soapinterop.org/types">
69  
70            <redefine schemaLocation="src/test/test-resources/redefine1.xsd">
71              <complexType name="person">
72                <complexContent>
73                  <extension base="tns:person">
74                    <sequence>
75                      <element name="id" type="string"/>
76                    </sequence>
77                  </extension>
78                </complexContent>
79              </complexType>
80            </redefine>
81  
82            <element name="vip" type="tns:person"/>
83  
84          </schema>
85          */
86  
87          InputStream is = new FileInputStream(Resources.asURI("redefine2.xsd"));
88          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
89          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
90  
91          XmlSchemaObjectTable xsot = schema.getElements();
92          assertEquals(1, xsot.getCount());
93  
94          XmlSchemaElement xse = null;
95          for (Iterator i = xsot.getValues(); i.hasNext(); ) {
96              xse = (XmlSchemaElement)i.next();
97          }
98          assertEquals("vip", xse.getName());
99          assertEquals(new QName("http://soapinterop.org/types",
100                                "person"),
101                      xse.getSchemaTypeName());
102 
103         XmlSchemaObjectCollection xsoc = schema.getIncludes();
104         assertEquals(1, xsoc.getCount());
105         
106         XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
107         xsot = xsr.getSchemaTypes();
108         assertEquals(1, xsot.getCount());
109 
110         for (Iterator i = xsot.getNames(); i.hasNext(); ) {
111             QName qname = (QName)i.next();
112             assertEquals(new QName("http://soapinterop.org/types",
113                                    "person"), qname);
114         }
115 
116         XmlSchemaComplexType xsct = null;
117         for (Iterator i = xsot.getValues(); i.hasNext(); ) {
118             xsct = (XmlSchemaComplexType)i.next();
119         }
120         assertNotNull(xsct);
121 
122         XmlSchemaContentModel xscm = xsct.getContentModel();
123         assertNotNull(xscm);
124 
125         XmlSchemaComplexContentExtension xscce =
126             (XmlSchemaComplexContentExtension)xscm.getContent();
127         assertEquals(new QName("http://soapinterop.org/types",
128                                "person"),
129                      xscce.getBaseTypeName());
130 
131         XmlSchemaSequence xsp = (XmlSchemaSequence)xscce.getParticle();
132         assertNotNull(xsp);
133 
134         XmlSchemaObjectCollection c = xsp.getItems();
135         assertEquals(1, c.getCount());
136 
137         xse = null;
138         for (int i = 0; i < c.getCount(); i++) {
139             xse = (XmlSchemaElement)c.getItem(i);
140         }
141         assertEquals("id", xse.getName());
142         assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
143                                "string"),
144                      xse.getSchemaTypeName());
145 
146     }
147 
148     /**
149      * This method will test a simple type redefine.
150      *
151      * @throws Exception Any exception encountered
152      */
153     public void testSimpleTypeRedefine() throws Exception {
154         /*
155         
156         redefine3.xsd
157         -----------------
158         
159         <schema xmlns="http://www.w3.org/2001/XMLSchema"
160                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
161                 xmlns:tns="http://soapinterop.org/types"
162                 targetNamespace="http://soapinterop.org/types">
163   
164           <simpleType name="drinksize">
165             <restriction base="integer"/>
166           </simpleType>
167           
168           <element name="size" type="tns:drinksize"/>
169 
170         </schema>
171         
172                                                   
173         redefine4.xsd
174         -----------------
175         
176         <schema xmlns="http://www.w3.org/2001/XMLSchema"
177                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
178                 xmlns:tns="http://soapinterop.org/types"
179                 targetNamespace="http://soapinterop.org/types">
180 
181           <redefine schemaLocation="test-resources/redefine3.xsd">
182             <simpleType name="drinksize">
183               <restriction base="tns:drinksize">
184                 <minInclusive value="1"/>
185                 <maxInclusive value="3"/>
186               </restriction>
187             </simpleType>
188           </redefine>
189           
190           <element name="childsizedrink" type="tns:drinksize"/>
191 
192         </schema>
193         */
194 
195         InputStream is = new FileInputStream(Resources.asURI("redefine4.xsd"));
196         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
197         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
198 
199         XmlSchemaObjectTable xsot = schema.getElements();
200         assertEquals(1, xsot.getCount());
201 
202         XmlSchemaElement xse = null;
203         for (Iterator i = xsot.getValues(); i.hasNext(); ) {
204             xse = (XmlSchemaElement)i.next();
205         }
206         assertEquals("childsizedrink", xse.getName());
207         assertEquals(new QName("http://soapinterop.org/types",
208                                "drinksize"),
209                      xse.getSchemaTypeName());
210 
211         XmlSchemaObjectCollection xsoc = schema.getIncludes();
212         assertEquals(1, xsoc.getCount());
213         
214         XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
215         xsot = xsr.getSchemaTypes();
216         assertEquals(1, xsot.getCount());
217 
218         for (Iterator i = xsot.getNames(); i.hasNext(); ) {
219             QName qname = (QName)i.next();
220             assertEquals(new QName("http://soapinterop.org/types",
221                                    "drinksize"), qname);
222         }
223 
224         XmlSchemaSimpleType xsst = null;
225         for (Iterator i = xsot.getValues(); i.hasNext(); ) {
226             xsst = (XmlSchemaSimpleType)i.next();
227         }
228         assertNotNull(xsst);
229 
230         XmlSchemaSimpleTypeRestriction xsstr =
231             (XmlSchemaSimpleTypeRestriction)xsst.getContent();
232         assertEquals(new QName("http://soapinterop.org/types",
233                                "drinksize"),
234                      xsstr.getBaseTypeName());
235 
236         xsoc = xsstr.getFacets();
237 
238         Set s = new HashSet();
239         s.add(XmlSchemaMinInclusiveFacet.class.getName());
240         s.add(XmlSchemaMaxInclusiveFacet.class.getName());
241         for (Iterator i  = xsoc.getIterator(); i.hasNext(); ) {
242             Object o = i.next();
243             assertTrue(s.remove(o.getClass().getName()));
244             if (o instanceof XmlSchemaMinInclusiveFacet) {
245                 assertEquals("1", ((XmlSchemaMinInclusiveFacet)o).getValue());
246             } else if (o instanceof XmlSchemaMaxInclusiveFacet) {
247                 assertEquals("3", ((XmlSchemaMaxInclusiveFacet)o).getValue());
248             } else {
249                 fail("Unexpected object encountered: "
250                      + o.getClass().getName());
251             }
252         }
253 
254         assertTrue("The set should have been empty, but instead contained: "
255                    + s + ".",
256                    s.isEmpty());
257 
258     }
259 
260     /**
261      * This method will test a group redefine.
262      *
263      * @throws Exception Any exception encountered
264      */
265     public void testGroupRedefine() throws Exception {
266 
267         /*
268         redefine5.xsd
269         -----------------
270         
271         <schema xmlns="http://www.w3.org/2001/XMLSchema"
272                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
273                 xmlns:tns="http://soapinterop.org/types"
274                 targetNamespace="http://soapinterop.org/types">
275   
276           <group name="PrologGroup">
277             <sequence>
278               <element name="date" type="string"/>
279               <element name="author" type="string"/>
280               <element name="defect" type="integer"/>
281             </sequence>
282           </group>
283           
284         </schema>
285 
286 
287         redefine6.xsd
288         -----------------
289 
290         <schema xmlns="http://www.w3.org/2001/XMLSchema"
291                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
292                 xmlns:tns="http://soapinterop.org/types"
293                 targetNamespace="http://soapinterop.org/types">
294 
295           <redefine schemaLocation="redefine5.xsd">
296             <group name="PrologGroup">
297               <sequence>
298                 <group ref="tns:PrologGroup"/>
299                 <element name="description" type="string"/>
300               </sequence>
301             </group>
302           </redefine>
303 
304         </schema>
305         */
306 
307         InputStream is = new FileInputStream(Resources.asURI("redefine6.xsd"));
308         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
309         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
310 
311         XmlSchemaObjectCollection xsoc = schema.getIncludes();
312         assertEquals(1, xsoc.getCount());
313         
314         XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
315         XmlSchemaObjectTable xsot = xsr.getGroup();
316         assertEquals(1, xsot.getCount());
317 
318         for (Iterator i = xsot.getNames(); i.hasNext(); ) {
319             assertEquals("PrologGroup", (String)i.next());
320         }
321 
322         XmlSchemaGroup xsg = null;
323         for (Iterator i = xsot.getValues(); i.hasNext(); ) {
324             xsg = (XmlSchemaGroup)i.next();
325         }
326 
327         XmlSchemaSequence xss = (XmlSchemaSequence)xsg.getParticle();
328 
329         xsoc = xss.getItems();
330         assertEquals(2, xsoc.getCount());
331         
332         Set s = new HashSet();
333         s.add(XmlSchemaGroupRef.class.getName());
334         s.add(XmlSchemaElement.class.getName());
335         for (Iterator i  = xsoc.getIterator(); i.hasNext(); ) {
336             Object o = i.next();
337             assertTrue(s.remove(o.getClass().getName()));
338             if (o instanceof XmlSchemaGroupRef) {
339                 assertEquals(new QName("http://soapinterop.org/types",
340                                        "PrologGroup"),
341                              ((XmlSchemaGroupRef)o).getRefName());
342             } else if (o instanceof XmlSchemaElement) {
343                 assertEquals("description", ((XmlSchemaElement)o).getName());
344             } else {
345                 fail("Unexpected object encountered: "
346                      + o.getClass().getName());
347             }
348         }
349 
350         assertTrue("The set should have been empty, but instead contained: "
351                    + s + ".",
352                    s.isEmpty());
353 
354     }
355 
356     /**
357      * This method will test a attribute group redefine.
358      *
359      * @throws Exception Any exception encountered
360      */
361     public void testAttributeGroupRedefine() throws Exception {
362 
363         /*
364         redefine7.xsd
365         -----------------
366         
367         <schema xmlns="http://www.w3.org/2001/XMLSchema"
368                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
369                 xmlns:tns="http://soapinterop.org/types"
370                 targetNamespace="http://soapinterop.org/types">
371   
372           <attributeGroup name="AttribGroup">
373             <attribute name="type" type="string"/>
374             <attribute name="units" type="string"/>
375             <attribute name="serialId" type="string"/>
376           </attributeGroup>
377           
378         </schema>
379 
380 
381         redefine8.xsd
382         -----------------
383 
384         <schema xmlns="http://www.w3.org/2001/XMLSchema"
385                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
386                 xmlns:tns="http://soapinterop.org/types"
387                 targetNamespace="http://soapinterop.org/types">
388 
389           <redefine schemaLocation="redefine7.xsd">
390             <attributeGroup name="AttribGroup">
391               <attribute name="type" type="string"/>
392               <attribute name="units" type="string"/>
393             </attributeGroup>
394           </redefine>
395 
396         </schema>
397         */
398 
399         InputStream is = new FileInputStream(Resources.asURI("redefine8.xsd"));
400         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
401         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
402 
403         XmlSchemaObjectCollection xsoc = schema.getIncludes();
404         assertEquals(1, xsoc.getCount());
405         
406         XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
407         XmlSchemaObjectTable xsot = xsr.getAttributeGroup();
408         assertEquals(1, xsot.getCount());
409 
410         for (Iterator i = xsot.getNames(); i.hasNext(); ) {
411             assertEquals("AttribGroup", (String)i.next());
412         }
413 
414         XmlSchemaAttributeGroup xsag = null;
415         for (Iterator i = xsot.getValues(); i.hasNext(); ) {
416             xsag = (XmlSchemaAttributeGroup)i.next();
417         }
418         assertEquals("AttribGroup", xsag.getName());
419         xsoc = xsag.getAttributes();
420 
421         Set s = new HashSet();
422         s.add("type");
423         s.add("units");
424         for (Iterator i  = xsoc.getIterator(); i.hasNext(); ) {
425             XmlSchemaAttribute xsa = (XmlSchemaAttribute)i.next();
426             assertTrue(s.remove(xsa.getName()));
427         }
428 
429         assertTrue("The set should have been empty, but instead contained: "
430                    + s + ".",
431                    s.isEmpty());
432         
433     }
434 
435 
436 }