View Javadoc

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