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