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 SimpleContentExtensionTest extends TestCase {
31  
32      /**
33       * This method will test the simple content extension.
34       *
35       * @throws Exception Any exception encountered
36       */
37      public void testSimpleContentExtension() 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                   attributeFormDefault="qualified">
45    
46             <element name="height">
47               <complexType>
48                 <simpleContent>
49                   <extension base="integer">
50                     <attribute name="units" type="string" use="required"/>
51                     <attribute name="id" type="integer" use="required" default="001"/>
52                     <attribute name="desc" type="decimal" fixed="1.1"/>
53                   </extension>
54                 </simpleContent>
55               </complexType>
56             </element>                                                           
57  
58           </schema>
59          */                                                                      
60  
61          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
62                                          "height");
63          InputStream is = new FileInputStream(Resources.asURI("simplecontentextension.xsd"));
64          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
65          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
66  
67          XmlSchemaElement elem = schema.getElementByName(ELEMENT_QNAME);
68          assertNotNull(elem);
69          assertEquals("height", elem.getName());
70          assertEquals(new QName("http://soapinterop.org/types", "height"),
71                       elem.getQName());
72  
73          XmlSchemaComplexType xsct = (XmlSchemaComplexType)elem.getSchemaType();
74          assertNotNull(xsct);
75          XmlSchemaSimpleContent xssc = (XmlSchemaSimpleContent)xsct.getContentModel();
76          assertNotNull(xssc);
77          
78          XmlSchemaSimpleContentExtension xssce 
79              = (XmlSchemaSimpleContentExtension)xssc.getContent();
80          assertNotNull(xssce);
81          assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
82                       xssce.getBaseTypeName());
83  
84          XmlSchemaObjectCollection xsoc = xssce.getAttributes();
85          assertEquals(3, xsoc.getCount());
86  
87          Set s = new HashSet();
88          s.add("units");
89          s.add("id");
90          s.add("desc");
91          for (int i = 0; i < xsoc.getCount(); i++) {
92              XmlSchemaAttribute xsa = (XmlSchemaAttribute)xsoc.getItem(i);
93              String name = xsa.getName();
94              if (name.equals("units")) {
95                  assertEquals(new QName("http://soapinterop.org/types", "units"),
96                               xsa.getQName());
97                  assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "string"),
98                               xsa.getSchemaTypeName());
99                  assertNull(xsa.getDefaultValue());
100                 assertEquals("required", xsa.getUse().getValue());
101                 assertNull(xsa.getFixedValue());
102             } else if (name.equals("id")) {
103                 assertEquals(new QName("http://soapinterop.org/types", "id"),
104                              xsa.getQName());
105                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
106                              xsa.getSchemaTypeName());
107                 assertEquals("001", xsa.getDefaultValue());
108                 assertEquals("required", xsa.getUse().getValue());
109                 assertNull(xsa.getFixedValue());
110             } else if (name.equals("desc")) {
111                 assertEquals(new QName("http://soapinterop.org/types", "desc"),
112                              xsa.getQName());
113                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "decimal"),
114                              xsa.getSchemaTypeName());
115                 assertEquals("none", xsa.getUse().getValue());
116                 assertEquals("1.1", xsa.getFixedValue());
117             } else {
118                 fail("The name \"" + name + "\" was not expected.");
119             }
120             s.remove(name);
121         }
122 
123         assertTrue("The set should have been empty, but instead contained: "
124                    + s + ".",
125                    s.isEmpty());
126 
127     }
128 
129 }