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 SimpleContentRestrictionTest extends TestCase {
31  
32      /**
33       * This method will test the simple content restriction.
34       *
35       * @throws Exception Any exception encountered
36       */
37      public void testSimpleContentRestriction() 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             <simpleType name="drinksize">
47               <restriction base="string">
48                 <enumeration value="small"/>
49                 <enumeration value="medium"/>
50                 <enumeration value="large"/>
51               </restriction>
52             </simpleType>
53  
54             <complexType name="dietdrinksize">
55               <simpleContent>
56                 <restriction base="tns:drinksize">
57                   <enumeration value="small"/>
58                   <enumeration value="medium"/>
59                   <attribute name="units" type="string" use="required"/>
60                   <attribute name="id" type="integer" use="required" default="001"/>
61                 </restriction>
62               </simpleContent>
63             </complexType>
64             
65           </schema>
66          */                                                                      
67  
68          QName TYPE_QNAME = new QName("http://soapinterop.org/types",
69                                       "dietdrinksize");
70          InputStream is = new FileInputStream(Resources.asURI("screstriction.xsd"));
71          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
72          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
73  
74          XmlSchemaComplexType xsct =
75              (XmlSchemaComplexType)schema.getTypeByName(TYPE_QNAME);
76          assertNotNull(xsct);
77  
78          XmlSchemaSimpleContent xssc = (XmlSchemaSimpleContent)xsct.getContentModel();
79          assertNotNull(xssc);
80          
81          XmlSchemaSimpleContentRestriction xsscr 
82              = (XmlSchemaSimpleContentRestriction)xssc.getContent();
83          assertNotNull(xsscr);
84          assertEquals(new QName("http://soapinterop.org/types", "drinksize"),
85                       xsscr.getBaseTypeName());
86          XmlSchemaObjectCollection xsoc = xsscr.getAttributes();
87          assertNotNull(xsoc);
88          assertEquals(2, xsoc.getCount());
89  
90          Set s = new HashSet();
91          s.add("units");
92          s.add("id");
93          for (int i = 0; i < xsoc.getCount(); i++) {
94              XmlSchemaAttribute xsa = (XmlSchemaAttribute)xsoc.getItem(i);
95              String name = xsa.getName();
96              if (name.equals("units")) {
97                  assertEquals(new QName("http://soapinterop.org/types", "units"),
98                               xsa.getQName());
99                  assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "string"),
100                              xsa.getSchemaTypeName());
101                 assertNull(xsa.getDefaultValue());
102                 assertEquals("required", xsa.getUse().getValue());
103                 assertNull(xsa.getFixedValue());
104             } else if (name.equals("id")) {
105                 assertEquals(new QName("http://soapinterop.org/types", "id"),
106                              xsa.getQName());
107                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
108                              xsa.getSchemaTypeName());
109                 assertEquals("001", xsa.getDefaultValue());
110                 assertEquals("required", xsa.getUse().getValue());
111                 assertNull(xsa.getFixedValue());
112             } else {
113                 fail("The name \"" + name + "\" was not expected.");
114             }
115             assertTrue(s.remove(name));
116         }
117         assertTrue("The set should have been empty, but instead contained: "
118                    + s + ".",
119                    s.isEmpty());
120         
121         XmlSchemaObjectCollection xsoc2 = xsscr.getFacets();
122         assertNotNull(xsoc2);
123         assertEquals(2, xsoc2.getCount());
124 
125         s.clear();
126         s.add("small");
127         s.add("medium");
128         for (int i = 0; i < xsoc2.getCount(); i++) {
129             XmlSchemaEnumerationFacet xsef =
130                 (XmlSchemaEnumerationFacet)xsoc2.getItem(i);
131             String value = (String)xsef.getValue();
132             if (!(value.equals("small") || value.equals("medium"))) {
133                 fail("Unexpected enumeration value of \"" + value
134                      + "\" found.");
135             }
136             assertTrue(s.remove(value));
137         }
138         assertTrue("The set should have been empty, but instead contained: "
139                    + s + ".",
140                    s.isEmpty());
141 
142     }
143 
144 }