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   * @author Brent Ulbricht 
30   */
31  public class ConstraintsTest extends TestCase {
32  
33      /**
34       * This method will test the unique, key, and
35       * keyref constaints.
36       *
37       * @throws Exception Any exception encountered
38       */
39      public void testConstraints() throws Exception {
40  
41          /*
42           <schema xmlns="http://www.w3.org/2001/XMLSchema"
43                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
44                   xmlns:tns="http://soapinterop.org/types"
45                   targetNamespace="http://soapinterop.org/types"
46                   elementFormDefault="qualified">
47  
48             <element name="constraintTest">
49               <complexType>
50                 <sequence>
51                   <element name="manufacturers" type="tns:ManufacturerType"/>
52                   <element name="products" type="tns:ProductType"/>
53                 </sequence>
54               </complexType>
55  
56               <unique name="uniqueTest">
57                 <selector xpath="tns:manufacturers/tns:location"/>
58                 <field xpath="@district"/>
59               </unique>
60  
61               <key name="keyTest">
62                 <selector xpath="tns:products/tns:productName"/>
63                 <field xpath="@productId"/>
64               </key>
65  
66               <keyref name="keyRefTest" refer="tns:keyTest">
67                 <selector xpath="tns:manufacturers/tns:location/tns:productName"/>
68                 <field xpath="@productId"/>
69               </keyref>
70  
71             </element>
72  
73             <complexType name="ManufacturerType">
74               <sequence>
75                 <element name="location" maxOccurs="unbounded">
76                   <complexType>
77                     <sequence>
78                       <element name="productName" maxOccurs="unbounded"/>
79                         <complexType>
80                           <complexContent>
81                             <extension base="string">
82                               <attribute name="productId" type="integer"/>
83                               <attribute name="units" type="integer"/>
84                             </extension>
85                           </complexContent>
86                         </complexType>
87                       </element>
88                     </sequence>
89                     <attribute name="district" type="integer"/>
90                   </complexType>
91                 </element>
92               </sequence>
93             </complexType>
94  
95             <complexType name="ProductType">
96               <sequence>
97                 <element name="productName" maxOccurs="unbounded">
98                   <complexType>
99                     <simpleContent>
100                      <extension base="string">
101                        <attribute name="productId" type="integer"/>
102                      </extension>
103                    </simpleContent>
104                  </complexType>
105                </element>
106              </sequence>
107            </complexType>
108 
109          </schema>
110         */
111 
112         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
113                                         "constraintTest");
114         InputStream is = new FileInputStream(Resources.asURI("constraints.xsd"));
115         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
116         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
117 
118         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
119         assertNotNull(elem);
120         assertEquals("constraintTest", elem.getName());
121         assertEquals(new QName("http://soapinterop.org/types", "constraintTest"),
122                      elem.getQName());
123 
124         XmlSchemaObjectCollection c = elem.getConstraints();
125         assertEquals(3, c.getCount());
126                              
127         Set s = new HashSet();
128         s.add(XmlSchemaKey.class.getName());
129         s.add(XmlSchemaKeyref.class.getName());
130         s.add(XmlSchemaUnique.class.getName());
131         for (int i = 0; i < c.getCount(); i++) {
132             Object o = c.getItem(i);
133             if (o instanceof XmlSchemaKey) {
134                 XmlSchemaKey key = (XmlSchemaKey)o;
135                 assertEquals("keyTest", key.getName());
136                 
137                 XmlSchemaXPath selectorXpath = key.getSelector();
138                 assertEquals("tns:products/tns:productName",
139                              selectorXpath.getXPath());
140                 
141                 XmlSchemaObjectCollection fields = key.getFields();
142                 assertEquals(1, fields.getCount());
143                 XmlSchemaXPath fieldXpath = null;
144                 for (int j = 0; j < fields.getCount(); j++) {
145                     fieldXpath = (XmlSchemaXPath)fields.getItem(j);
146                 }
147                 assertNotNull(fieldXpath);
148                 assertEquals("@productId", fieldXpath.getXPath());
149             } else if (o instanceof XmlSchemaKeyref) {
150                 XmlSchemaKeyref keyref = (XmlSchemaKeyref)o;
151                 assertNotNull(keyref);
152                 assertEquals("keyRefTest", keyref.getName());
153                 assertEquals(new QName("http://soapinterop.org/types",
154                                        "keyTest"),
155                              keyref.getRefer());
156                 
157                 XmlSchemaXPath selectorXpath = keyref.getSelector();
158                 assertEquals("tns:manufacturers/tns:location/tns:productName",
159                              selectorXpath.getXPath());
160 
161                 XmlSchemaObjectCollection fields = keyref.getFields();
162                 assertEquals(1, fields.getCount());
163                 XmlSchemaXPath fieldXpath = null;
164                 for (int j = 0; j < fields.getCount(); j++) {
165                     fieldXpath = (XmlSchemaXPath)fields.getItem(j);
166                 }
167                 assertNotNull(fieldXpath);
168                 assertEquals("@productId", fieldXpath.getXPath());
169             } else if (o instanceof XmlSchemaUnique) {
170                 XmlSchemaUnique unique = (XmlSchemaUnique)o;
171                 assertNotNull(unique);
172                 assertEquals("uniqueTest", unique.getName());
173                 XmlSchemaXPath selectorXpath = unique.getSelector();
174                 assertEquals("tns:manufacturers/tns:location",
175                              selectorXpath.getXPath());
176 
177                 XmlSchemaObjectCollection fields = unique.getFields();
178                 assertEquals(1, fields.getCount());
179                 XmlSchemaXPath fieldXpath = null;
180                 for (int j = 0; j < fields.getCount(); j++) {
181                     fieldXpath = (XmlSchemaXPath)fields.getItem(j);
182                 }
183                 assertNotNull(fieldXpath);
184                 assertEquals("@district", fieldXpath.getXPath());
185             } else {
186                 fail("An unexpected constraint of \""
187                      + o.getClass().getName() + "\" was found.");
188             }
189             s.remove(o.getClass().getName());
190         }
191 
192         assertTrue("The set should have been empty, but instead contained: "
193                    + s + ".",
194                    s.isEmpty());
195         
196     }
197 
198 }