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.Set;
12  
13  /*
14   * Copyright 2004,2007 The Apache Software Foundation.
15   * Copyright 2006 International Business Machines Corp.
16   *
17   * Licensed under the Apache License, Version 2.0 (the "License");
18   * you may not use this file except in compliance with the License.
19   * You may obtain a copy of the License at
20   *
21   *      http://www.apache.org/licenses/LICENSE-2.0
22   *
23   * Unless required by applicable law or agreed to in writing, software
24   * distributed under the License is distributed on an "AS IS" BASIS,
25   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   * See the License for the specific language governing permissions and
27   * limitations under the License.
28   *
29   */
30  public class UnionTest extends TestCase {
31  
32      /**
33       * This method will test the union.
34       *
35       * @throws Exception Any exception encountered
36       */
37      public void testUnion() throws Exception {
38  
39          /*
40           <schema xmlns="http://www.w3.org/2001/XMLSchema"
41                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
42                   xmlns:tns="http://soapinterop.org/types"
43                   targetNamespace="http://soapinterop.org/types">
44    
45             <element name="unionTest">
46               <simpleType>
47                 <union memberTypes="float decimal"/>
48               </simpleType>
49             </element>
50  
51           </schema>
52          */
53  
54          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
55                                          "unionTest");
56          InputStream is = new FileInputStream(Resources.asURI("union.xsd"));
57          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
58          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
59  
60  
61          XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
62          assertNotNull(elem);
63          assertEquals("unionTest", elem.getName());
64          assertEquals(new QName("http://soapinterop.org/types", "unionTest"),
65                       elem.getQName());
66  
67          XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
68          assertNotNull(simpleType);
69  
70          XmlSchemaSimpleTypeUnion xsstu =
71              (XmlSchemaSimpleTypeUnion)simpleType.getContent();
72          assertNotNull(xsstu);
73  
74          QName[] qname = xsstu.getMemberTypesQNames();
75          Set s = new HashSet();
76          s.add(new QName("http://www.w3.org/2001/XMLSchema", "float"));
77          s.add(new QName("http://www.w3.org/2001/XMLSchema", "decimal"));
78          for (int i = 0; i < qname.length; i++) {
79              assertTrue(s.remove(qname[i]));
80          }
81          assertTrue("The set should have been empty, but instead contained: "
82                     + s + ".",
83                     s.isEmpty());
84  
85          assertEquals("float decimal", xsstu.getMemberTypesSource());
86  
87      }
88  
89  }