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