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.Set;
12  
13  /*
14   * Copyright 2004,2007 The Apache Software Foundation.
15   * Copyright 2006 International Business Machines Corp.
16   *
17   * Licensed under the Apache License, Version 2.0 (the "License");
18   * you may not use this file except in compliance with the License.
19   * You may obtain a copy of the License at
20   *
21   *      http://www.apache.org/licenses/LICENSE-2.0
22   *
23   * Unless required by applicable law or agreed to in writing, software
24   * distributed under the License is distributed on an "AS IS" BASIS,
25   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   * See the License for the specific language governing permissions and
27   * limitations under the License.
28   *
29   */
30  public class ComplexContentRestrictionTest extends TestCase {
31  
32      /**
33       * This method will test complex content restriction.
34       *
35       * @throws Exception Any exception encountered
36       */
37      public void testComplexContentRestriction() throws Exception {
38  
39          /*
40          <schema xmlns="http://www.w3.org/2001/XMLSchema"
41                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
42                  xmlns:tns="http://soapinterop.org/types"
43                  targetNamespace="http://soapinterop.org/types">
44  
45  
46            <complexType name="AssemblyRequiredProduct">
47              <sequence>
48                <element name="Name" type="string"/>
49                <element name="Description" type="string" nillable="true"/>
50                <element name="Parts" type="string" maxOccurs="unbounded"/>
51              </sequence>
52            </complexType>
53    
54            <complexType name="NoAssemblyRequiredProduct">
55              <complexContent>
56                <restriction base="tns:AssemblyRequiredProduct">
57                  <sequence>
58                    <element name="Name" type="string"/>
59                    <element name="Description" type="string" nillable="true"/>
60                    <element name="Parts" type="string"/>
61                  </sequence>
62                </restriction>
63              </complexContent>
64            </complexType>  
65  
66          </schema>
67          */
68  
69          QName TYPE_QNAME = new QName("http://soapinterop.org/types",
70                                       "NoAssemblyRequiredProduct");
71          InputStream is = new FileInputStream(Resources.asURI("deriverestriction.xsd"));
72          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
73          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
74  
75          XmlSchemaComplexType cType =
76              (XmlSchemaComplexType)schemaCol.getTypeByQName(TYPE_QNAME);
77          assertNotNull(cType);
78          
79          XmlSchemaContentModel xscm = cType.getContentModel();
80          assertNotNull(xscm);
81  
82          XmlSchemaComplexContentRestriction xsccr =
83              (XmlSchemaComplexContentRestriction)xscm.getContent();
84          assertEquals(new QName("http://soapinterop.org/types",
85                                 "AssemblyRequiredProduct"),
86                       xsccr.getBaseTypeName());
87  
88          XmlSchemaSequence xsp = (XmlSchemaSequence)xsccr.getParticle();
89          assertNotNull(xsp);
90  
91          XmlSchemaObjectCollection col = xsp.getItems();
92  
93          Set s = new HashSet();
94          s.add("Name");
95          s.add("Description");
96          s.add("Parts");
97          for (int i = 0; i < col.getCount(); i++) {
98              XmlSchemaElement xse = (XmlSchemaElement)col.getItem(i);
99              String name = xse.getName();
100             if (name.equals("Name")) {
101                 assertEquals(new QName("", "Name"),
102                              xse.getQName());
103                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
104                                        "string"),
105                                        xse.getSchemaTypeName());
106                 assertTrue(!xse.isAbstract());
107                 assertTrue(!xse.isNillable());
108             } else if (name.equals("Description")) {
109                 assertEquals(new QName("", "Description"),
110                              xse.getQName());
111                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
112                                        "string"),
113                                        xse.getSchemaTypeName());
114                 assertTrue(!xse.isAbstract());
115                 assertTrue(xse.isNillable());
116             } else if (name.equals("Parts")) {
117                 assertEquals(new QName("", "Parts"),
118                              xse.getQName());
119                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
120                                        "string"),
121                                        xse.getSchemaTypeName());
122             } else {
123                 fail("An invalid name of \"" + name + "\" was found.");
124             }
125             s.remove(name);
126         }
127 
128         assertTrue("The set should have been empty, but instead contained: "
129                    + s + ".",
130                    s.isEmpty());
131 
132     }
133 
134 }