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 AnyTest extends TestCase {
31  
32      /**
33       * This method will test the any.
34       *
35       * @throws Exception Any exception encountered
36       */
37      public void testAny() 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                   elementFormDefault="qualified">
45  
46             <element name="department">
47               <complexType>
48                 <sequence>
49                   <element name="id" type="xsd:integer"/>
50                   <element name="name" type="xsd:string"/>
51                   <any minOccurs="5" maxOccurs="10"/>
52                 </sequence>
53               </complexType>
54             </element>
55  
56           </schema>
57          */
58  
59          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
60                                          "department");
61          InputStream is = new FileInputStream(Resources.asURI("any.xsd"));
62          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
63          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
64  
65          XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
66          assertNotNull(elem);
67          assertEquals("department", elem.getName());
68          assertEquals(new QName("http://soapinterop.org/types", "department"),
69                       elem.getQName());
70  
71          XmlSchemaComplexType type =
72              (XmlSchemaComplexType)elem.getSchemaType();
73          assertNotNull(type);
74          
75          XmlSchemaSequence xss = (XmlSchemaSequence)type.getParticle();
76          assertNotNull(xss);
77  
78          XmlSchemaObjectCollection c = xss.getItems();
79          assertEquals(3, c.getCount());
80  
81          Set s = new HashSet();
82          s.add("id");
83          s.add("name");
84          Object o = null;
85          for (int i = 0; i < c.getCount(); i++) {
86              o = c.getItem(i);
87              if (o instanceof XmlSchemaElement) {
88                  String name = ((XmlSchemaElement)o).getName();
89                  if (name.equals("id")) {
90                      assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
91                                             "integer"),
92                                   ((XmlSchemaElement)o).getSchemaTypeName());
93                  } else if (name.equals("name")) {
94                      assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
95                                             "string"),
96                                   ((XmlSchemaElement)o).getSchemaTypeName());
97                  }
98                  s.remove(name);
99              } else if (o instanceof XmlSchemaAny) {
100                 XmlSchemaContentProcessing xscp =
101                     ((XmlSchemaAny)o).getProcessContent();
102                 assertEquals("none", xscp.toString());
103                 assertEquals(5L, ((XmlSchemaAny)o).getMinOccurs());
104                 assertEquals(10L, ((XmlSchemaAny)o).getMaxOccurs());
105             }
106         }
107         
108         assertTrue("The set should have been empty, but instead contained: "
109                    + s + ".",
110                    s.isEmpty());
111 
112     }
113 
114 }