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.Iterator;
12  import java.util.Set;
13  
14  /*
15   * Copyright 2004,2007 The Apache Software Foundation.
16   * Copyright 2006 International Business Machines Corp.
17   *
18   * Licensed under the Apache License, Version 2.0 (the "License");
19   * you may not use this file except in compliance with the License.
20   * You may obtain a copy of the License at
21   *
22   *      http://www.apache.org/licenses/LICENSE-2.0
23   *
24   * Unless required by applicable law or agreed to in writing, software
25   * distributed under the License is distributed on an "AS IS" BASIS,
26   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27   * See the License for the specific language governing permissions and
28   * limitations under the License.
29   *
30   */
31  public class ChoiceTest extends TestCase {
32  
33      /**
34       * This method will test the choice.
35       *
36       * @throws Exception Any exception encountered
37       */
38      public void testChoice() throws Exception {
39  
40          /*
41          <schema xmlns="http://www.w3.org/2001/XMLSchema"
42                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
43                  xmlns:tns="http://soapinterop.org/types"
44                  targetNamespace="http://soapinterop.org/types">
45  
46            <element name="computer">
47              <complexType>
48                <choice>
49                  <element name="desktop" type="string"/>
50                  <element name="laptop" type="string"/>
51                </choice>
52              </complexType>
53            </element>
54  
55          </schema>
56          */
57  
58          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
59                                          "computer");
60          InputStream is = new FileInputStream(Resources.asURI("choice.xsd"));
61          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
62          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
63  
64          QName WRONG_QNAME = new QName("http://soapinterop.org/types",
65                                        "machine");
66          XmlSchemaElement elem = schemaCol.getElementByQName(WRONG_QNAME);
67          assertNull(elem);
68          elem = schemaCol.getElementByQName(ELEMENT_QNAME);
69          assertEquals("computer", elem.getName());
70          assertEquals(new QName("http://soapinterop.org/types", "computer"),
71                       elem.getQName());
72  
73          XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
74          assertNotNull(cType);
75  
76          XmlSchemaChoice choice = (XmlSchemaChoice)cType.getParticle();
77          assertNotNull(choice);
78  
79          Set s = new HashSet();
80          s.add("desktop");
81          s.add("laptop");
82          XmlSchemaObjectCollection items = choice.getItems();
83          Iterator iterator = items.getIterator();
84          while (iterator.hasNext()) {
85              XmlSchemaElement e = (XmlSchemaElement)iterator.next();
86              String eName = e.getName();
87              if (eName.equals("desktop")) {
88                  assertEquals(new QName("", "desktop"), e.getQName());
89                  assertEquals(e.getName(), "desktop");
90              } else if (eName.equals("laptop")) {
91                  assertEquals(new QName("", "laptop"), e.getQName());
92                  assertEquals(e.getName(), "laptop");
93              } else {
94                  fail("Should have had a name of desktop or laptop, but"
95                       + " instead had " + eName);
96              }
97              assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
98                                     "string"), e.getSchemaTypeName());
99              assertTrue(s.remove(e.getName()));
100         }
101         assertTrue("The set should have been empty, but instead contained: "
102                    + s + ".",
103                    s.isEmpty());
104     }
105 
106 }