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