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 GroupTest extends TestCase {
32  
33      /**
34       * This method will test the group.
35       *
36       * @throws Exception Any exception encountered
37       */
38      public void testGroup() 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             <group name="priceGroup">
47               <annotation>
48                 <documentation xml:lang="en">
49                    A price is any one of the following:
50                        * Full Price (with amount)
51                        * Sale Price (with amount and authorization)
52                        * Clearance Price (with amount and authorization)
53                        * Free (with authorization)
54                 </documentation>
55               </annotation>
56               <choice id="pg.choice">
57                 <element name="fullPrice" type="decimal"/>
58                 <element name="salePrice" type="decimal"/>
59                 <element name="clearancePrice" type="decimal"/>
60                 <element name="freePrice" type="decimal"/>
61               </choice>
62             </group>
63    
64             <element name="price">
65               <complexType>
66                 <group ref="tns:priceGroup" />
67               </complexType>
68             </element>
69  
70           </schema>
71          */
72  
73          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
74                                          "price");
75          InputStream is = new FileInputStream(Resources.asURI("group.xsd"));
76          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
77          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
78  
79          XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
80          assertNotNull(elem);
81          assertEquals("price", elem.getName());
82          assertEquals(new QName("http://soapinterop.org/types", "price"),
83                       elem.getQName());
84  
85          XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
86          assertNotNull(cType);
87  
88          XmlSchemaGroupRef ref = (XmlSchemaGroupRef)cType.getParticle();
89          assertEquals(new QName("http://soapinterop.org/types", "priceGroup"),
90                       ref.getRefName());
91  
92          XmlSchemaObjectTable t = schema.getGroups();
93          assertEquals(1, t.getCount());
94  
95          XmlSchemaObject o = t.getItem(ref.getRefName());
96  
97          Set s = new HashSet();
98          s.add("priceGroup");
99          for (Iterator i = t.getNames(); i.hasNext(); ) {
100             String name = ((QName)i.next()).getLocalPart();
101             assertEquals("priceGroup", name);
102             s.remove(name);
103         }
104         assertTrue("The set should have been empty, but instead contained: "
105                    + s + ".",
106                    s.isEmpty());
107 
108         s.clear();
109         s.add("org.apache.ws.commons.schema.XmlSchemaGroup");
110         XmlSchemaGroup xsg = null;
111         for (Iterator i = t.getValues(); i.hasNext(); ) {
112             xsg = (XmlSchemaGroup)i.next();
113             s.remove(xsg.getClass().getName());
114         }
115         assertTrue("The set should have been empty, but instead contained: "
116                    + s + ".",
117                    s.isEmpty());
118         
119         assertEquals("priceGroup", xsg.getName());
120 
121         XmlSchemaChoice xsc = (XmlSchemaChoice)xsg.getParticle();
122         assertNotNull(xsc);
123 
124         s.clear();
125         s.add("fullPrice");
126         s.add("salePrice");
127         s.add("clearancePrice");
128         s.add("freePrice");
129         XmlSchemaObjectCollection items = xsc.getItems();
130         Iterator iterator = items.getIterator();
131         while (iterator.hasNext()) {
132             XmlSchemaElement e = (XmlSchemaElement)iterator.next();
133             String eName = e.getName();
134             if (eName.equals("fullPrice")) {
135                 assertEquals(new QName("", "fullPrice"), e.getQName());
136             } else if (eName.equals("salePrice")) {
137                 assertEquals(new QName("", "salePrice"), e.getQName());
138             } else if (eName.equals("clearancePrice")) {
139                 assertEquals(new QName("", "clearancePrice"), e.getQName());
140             } else if (eName.equals("freePrice")) {
141                 assertEquals(new QName("", "freePrice"), e.getQName());
142             } else {
143                 fail("The name \"" + eName + "\" was found but shouldn't "
144                      + "have been found.");
145             }
146             assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
147                                    "decimal"), e.getSchemaTypeName());
148             assertTrue(s.remove(e.getName()));
149         }
150         assertTrue("The set should have been empty, but instead contained: "
151                    + s + ".",
152                    s.isEmpty());
153 
154     }
155 
156 }