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  
28  import java.io.ByteArrayInputStream;
29  import java.io.ByteArrayOutputStream;
30  import java.io.FileInputStream;
31  import java.io.InputStream;
32  import java.util.HashSet;
33  import java.util.Set;
34  
35  /*
36   * Copyright 2004,2007 The Apache Software Foundation.
37   * Copyright 2006 International Business Machines Corp.
38   *
39   * Licensed under the Apache License, Version 2.0 (the "License");
40   * you may not use this file except in compliance with the License.
41   * You may obtain a copy of the License at
42   *
43   *      http://www.apache.org/licenses/LICENSE-2.0
44   *
45   * Unless required by applicable law or agreed to in writing, software
46   * distributed under the License is distributed on an "AS IS" BASIS,
47   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48   * See the License for the specific language governing permissions and
49   * limitations under the License.
50   *
51   */
52  public class AnyTest extends TestCase {
53  
54      /**
55       * This method will test the any.
56       *
57       * @throws Exception Any exception encountered
58       */
59      public void testAny() throws Exception {
60  
61          /*
62           <schema xmlns="http://www.w3.org/2001/XMLSchema"
63                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
64                   xmlns:tns="http://soapinterop.org/types"
65                   targetNamespace="http://soapinterop.org/types"
66                   elementFormDefault="qualified">
67  
68             <element name="department">
69               <complexType>
70                 <sequence>
71                   <element name="id" type="xsd:integer"/>
72                   <element name="name" type="xsd:string"/>
73                   <any minOccurs="5" maxOccurs="10"/>
74                 </sequence>
75               </complexType>
76             </element>
77  
78           </schema>
79          */
80  
81          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
82                                          "department");
83          InputStream is = new FileInputStream(Resources.asURI("any.xsd"));
84          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
85          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
86  
87          verifyAccuracy(ELEMENT_QNAME, schemaCol,5L,10L);
88  
89      }
90  
91      
92      public void testAnyZeroOccurs() throws Exception {
93  
94          /*
95           <schema xmlns="http://www.w3.org/2001/XMLSchema"
96                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
97                   xmlns:tns="http://soapinterop.org/types"
98                   targetNamespace="http://soapinterop.org/types"
99                   elementFormDefault="qualified">
100 
101            <element name="department">
102              <complexType>
103                <sequence>
104                  <element name="id" type="xsd:integer"/>
105                  <element name="name" type="xsd:string"/>
106                  <any minOccurs="5" maxOccurs="10"/>
107                </sequence>
108              </complexType>
109            </element>
110 
111          </schema>
112         */
113 
114         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
115                                         "department");
116         InputStream is = new FileInputStream(Resources.asURI("anyZero.xsd"));
117         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
118         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
119 
120         ByteArrayOutputStream baos = new ByteArrayOutputStream();
121         schema.write(baos);
122         
123         XmlSchemaCollection schemaCol2 = new XmlSchemaCollection();
124         XmlSchema schema2 = schemaCol2.read(new StreamSource(new ByteArrayInputStream(baos.toByteArray())), null);
125         
126         verifyAccuracy(ELEMENT_QNAME, schemaCol2,0,0);
127        
128 
129     }
130     
131 	private void verifyAccuracy(QName ELEMENT_QNAME,
132 			XmlSchemaCollection schemaCol,long minCount, long maxCount) {
133 		XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
134         assertNotNull(elem);
135         assertEquals("department", elem.getName());
136         assertEquals(new QName("http://soapinterop.org/types", "department"),
137                      elem.getQName());
138 
139         XmlSchemaComplexType type =
140             (XmlSchemaComplexType)elem.getSchemaType();
141         assertNotNull(type);
142         
143         XmlSchemaSequence xss = (XmlSchemaSequence)type.getParticle();
144         assertNotNull(xss);
145 
146         XmlSchemaObjectCollection c = xss.getItems();
147         assertEquals(3, c.getCount());
148 
149         Set s = new HashSet();
150         s.add("id");
151         s.add("name");
152         Object o = null;
153         for (int i = 0; i < c.getCount(); i++) {
154             o = c.getItem(i);
155             if (o instanceof XmlSchemaElement) {
156                 String name = ((XmlSchemaElement)o).getName();
157                 if (name.equals("id")) {
158                     assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
159                                            "integer"),
160                                  ((XmlSchemaElement)o).getSchemaTypeName());
161                 } else if (name.equals("name")) {
162                     assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
163                                            "string"),
164                                  ((XmlSchemaElement)o).getSchemaTypeName());
165                 }
166                 s.remove(name);
167             } else if (o instanceof XmlSchemaAny) {
168                 XmlSchemaContentProcessing xscp =
169                     ((XmlSchemaAny)o).getProcessContent();
170                 assertEquals("none", xscp.toString());
171                 assertEquals(minCount, ((XmlSchemaAny)o).getMinOccurs());
172                 assertEquals(maxCount, ((XmlSchemaAny)o).getMaxOccurs());
173             }
174         }
175         
176         assertTrue("The set should have been empty, but instead contained: "
177                    + s + ".",
178                    s.isEmpty());
179 	}
180 
181 }