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  
25  import javax.xml.namespace.QName;
26  import javax.xml.transform.stream.StreamSource;
27  import java.io.FileInputStream;
28  import java.io.InputStream;
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.Set;
32  
33  /*
34   * Copyright 2004,2007 The Apache Software Foundation.
35   * Copyright 2006 International Business Machines Corp.
36   *
37   * Licensed under the Apache License, Version 2.0 (the "License");
38   * you may not use this file except in compliance with the License.
39   * You may obtain a copy of the License at
40   *
41   *      http://www.apache.org/licenses/LICENSE-2.0
42   *
43   * Unless required by applicable law or agreed to in writing, software
44   * distributed under the License is distributed on an "AS IS" BASIS,
45   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46   * See the License for the specific language governing permissions and
47   * limitations under the License.
48   *
49   */
50  public class GroupTest extends TestCase {
51  
52      /**
53       * This method will test the group.
54       *
55       * @throws Exception Any exception encountered
56       */
57      public void testGroup() throws Exception {
58  
59          /*
60           <schema xmlns="http://www.w3.org/2001/XMLSchema"
61                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
62                   xmlns:tns="http://soapinterop.org/types"
63                   targetNamespace="http://soapinterop.org/types">
64  
65             <group name="priceGroup">
66               <annotation>
67                 <documentation xml:lang="en">
68                    A price is any one of the following:
69                        * Full Price (with amount)
70                        * Sale Price (with amount and authorization)
71                        * Clearance Price (with amount and authorization)
72                        * Free (with authorization)
73                 </documentation>
74               </annotation>
75               <choice id="pg.choice">
76                 <element name="fullPrice" type="decimal"/>
77                 <element name="salePrice" type="decimal"/>
78                 <element name="clearancePrice" type="decimal"/>
79                 <element name="freePrice" type="decimal"/>
80               </choice>
81             </group>
82    
83             <element name="price">
84               <complexType>
85                 <group ref="tns:priceGroup" />
86               </complexType>
87             </element>
88  
89           </schema>
90          */
91  
92          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
93                                          "price");
94          InputStream is = new FileInputStream(Resources.asURI("group.xsd"));
95          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
96          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
97  
98          XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
99          assertNotNull(elem);
100         assertEquals("price", elem.getName());
101         assertEquals(new QName("http://soapinterop.org/types", "price"),
102                      elem.getQName());
103 
104         XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
105         assertNotNull(cType);
106 
107         XmlSchemaGroupRef ref = (XmlSchemaGroupRef)cType.getParticle();
108         assertEquals(new QName("http://soapinterop.org/types", "priceGroup"),
109                      ref.getRefName());
110 
111         XmlSchemaObjectTable t = schema.getGroups();
112         assertEquals(1, t.getCount());
113 
114         Set s = new HashSet();
115         s.add("priceGroup");
116         for (Iterator i = t.getNames(); i.hasNext(); ) {
117             String name = ((QName)i.next()).getLocalPart();
118             assertEquals("priceGroup", name);
119             s.remove(name);
120         }
121         assertTrue("The set should have been empty, but instead contained: "
122                    + s + ".",
123                    s.isEmpty());
124 
125         s.clear();
126         s.add("org.apache.ws.commons.schema.XmlSchemaGroup");
127         XmlSchemaGroup xsg = null;
128         for (Iterator i = t.getValues(); i.hasNext(); ) {
129             xsg = (XmlSchemaGroup)i.next();
130             s.remove(xsg.getClass().getName());
131         }
132         assertTrue("The set should have been empty, but instead contained: "
133                    + s + ".",
134                    s.isEmpty());
135         
136         assertNotNull(xsg);
137         assertEquals("priceGroup", xsg.getName().getLocalPart());
138 
139         XmlSchemaChoice xsc = (XmlSchemaChoice)xsg.getParticle();
140         assertNotNull(xsc);
141 
142         s.clear();
143         s.add("fullPrice");
144         s.add("salePrice");
145         s.add("clearancePrice");
146         s.add("freePrice");
147         XmlSchemaObjectCollection items = xsc.getItems();
148         Iterator iterator = items.getIterator();
149         while (iterator.hasNext()) {
150             XmlSchemaElement e = (XmlSchemaElement)iterator.next();
151             String eName = e.getName();
152             if (eName.equals("fullPrice")) {
153                 assertEquals(new QName("", "fullPrice"), e.getQName());
154             } else if (eName.equals("salePrice")) {
155                 assertEquals(new QName("", "salePrice"), e.getQName());
156             } else if (eName.equals("clearancePrice")) {
157                 assertEquals(new QName("", "clearancePrice"), e.getQName());
158             } else if (eName.equals("freePrice")) {
159                 assertEquals(new QName("", "freePrice"), e.getQName());
160             } else {
161                 fail("The name \"" + eName + "\" was found but shouldn't "
162                      + "have been found.");
163             }
164             assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
165                                    "decimal"), e.getSchemaTypeName());
166             assertTrue(s.remove(e.getName()));
167         }
168         assertTrue("The set should have been empty, but instead contained: "
169                    + s + ".",
170                    s.isEmpty());
171 
172     }
173 
174 }