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  
24  import javax.xml.namespace.QName;
25  import javax.xml.transform.stream.StreamSource;
26  import java.io.InputStream;
27  import java.io.FileInputStream;
28  import java.util.Set;
29  import java.util.HashSet;
30  import java.util.Iterator;
31  
32  import org.apache.ws.commons.schema.*;
33  
34  /*
35   * Copyright 2004,2007 The Apache Software Foundation.
36   * Copyright 2006 International Business Machines Corp.
37   *
38   * Licensed under the Apache License, Version 2.0 (the "License");
39   * you may not use this file except in compliance with the License.
40   * You may obtain a copy of the License at
41   *
42   *      http://www.apache.org/licenses/LICENSE-2.0
43   *
44   * Unless required by applicable law or agreed to in writing, software
45   * distributed under the License is distributed on an "AS IS" BASIS,
46   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47   * See the License for the specific language governing permissions and
48   * limitations under the License.
49   *
50   */
51  public class SequenceTest extends TestCase {
52  
53      /**
54       * This method will test the sequence - the min and max occurences.
55       *
56       * @throws Exception Any exception encountered
57       */
58      public void testChoice() throws Exception {
59  
60          /*
61          <schema xmlns="http://www.w3.org/2001/XMLSchema"
62                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
63                  xmlns:tns="http://soapinterop.org/types"
64                  targetNamespace="http://soapinterop.org/types">
65  
66             <element name="computer">
67              <complexType>
68                <sequence minOccurs="4" maxOccurs="50">
69                  <element name="desktop" type="string"/>
70                  <element name="laptop" type="string"/>
71                </sequence>
72              </complexType>
73            </element>
74  
75          </schema>
76          */
77  
78          QName computerElementQname = new QName("http://soapinterop.org/types",
79                                          "computer");
80  
81  
82          InputStream is = new FileInputStream(Resources.asURI("sequence.xsd"));
83          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
84          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
85  
86          QName WRONG_QNAME = new QName("http://soapinterop.org/types",
87                                        "machine");
88          XmlSchemaElement elem = schemaCol.getElementByQName(WRONG_QNAME);
89          assertNull(elem);
90          elem = schemaCol.getElementByQName(computerElementQname);
91          assertEquals("computer", elem.getName());
92          assertEquals(new QName("http://soapinterop.org/types", "computer"),
93                       elem.getQName());
94  
95          XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
96          assertNotNull(cType);
97  
98          XmlSchemaSequence sequence = (XmlSchemaSequence)cType.getParticle();
99          assertNotNull(sequence);
100 
101         //values from the XML file
102         assertEquals(sequence.getMinOccurs(),4);
103         assertEquals(sequence.getMaxOccurs(),50);
104 
105     }
106 
107 }