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