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.Set;
31  
32  /*
33   * Copyright 2004,2007 The Apache Software Foundation.
34   * Copyright 2006 International Business Machines Corp.
35   *
36   * Licensed under the Apache License, Version 2.0 (the "License");
37   * you may not use this file except in compliance with the License.
38   * You may obtain a copy of the License at
39   *
40   *      http://www.apache.org/licenses/LICENSE-2.0
41   *
42   * Unless required by applicable law or agreed to in writing, software
43   * distributed under the License is distributed on an "AS IS" BASIS,
44   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45   * See the License for the specific language governing permissions and
46   * limitations under the License.
47   * 
48   * @author Brent Ulbricht 
49   */
50  public class ConstraintsTest extends TestCase {
51  
52      /**
53       * This method will test the unique, key, and
54       * keyref constaints.
55       *
56       * @throws Exception Any exception encountered
57       */
58      public void testConstraints() throws Exception {
59  
60          /*
61           <schema xmlns="http://www.w3.org/2001/XMLSchema"
62                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
63                   xmlns:tns="http://soapinterop.org/types"
64                   targetNamespace="http://soapinterop.org/types"
65                   elementFormDefault="qualified">
66  
67             <element name="constraintTest">
68               <complexType>
69                 <sequence>
70                   <element name="manufacturers" type="tns:ManufacturerType"/>
71                   <element name="products" type="tns:ProductType"/>
72                 </sequence>
73               </complexType>
74  
75               <unique name="uniqueTest">
76                 <selector xpath="tns:manufacturers/tns:location"/>
77                 <field xpath="@district"/>
78               </unique>
79  
80               <key name="keyTest">
81                 <selector xpath="tns:products/tns:productName"/>
82                 <field xpath="@productId"/>
83               </key>
84  
85               <keyref name="keyRefTest" refer="tns:keyTest">
86                 <selector xpath="tns:manufacturers/tns:location/tns:productName"/>
87                 <field xpath="@productId"/>
88               </keyref>
89  
90             </element>
91  
92             <complexType name="ManufacturerType">
93               <sequence>
94                 <element name="location" maxOccurs="unbounded">
95                   <complexType>
96                     <sequence>
97                       <element name="productName" maxOccurs="unbounded"/>
98                         <complexType>
99                           <complexContent>
100                            <extension base="string">
101                              <attribute name="productId" type="integer"/>
102                              <attribute name="units" type="integer"/>
103                            </extension>
104                          </complexContent>
105                        </complexType>
106                      </element>
107                    </sequence>
108                    <attribute name="district" type="integer"/>
109                  </complexType>
110                </element>
111              </sequence>
112            </complexType>
113 
114            <complexType name="ProductType">
115              <sequence>
116                <element name="productName" maxOccurs="unbounded">
117                  <complexType>
118                    <simpleContent>
119                      <extension base="string">
120                        <attribute name="productId" type="integer"/>
121                      </extension>
122                    </simpleContent>
123                  </complexType>
124                </element>
125              </sequence>
126            </complexType>
127 
128          </schema>
129         */
130 
131         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
132                                         "constraintTest");
133         InputStream is = new FileInputStream(Resources.asURI("constraints.xsd"));
134         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
135         schemaCol.read(new StreamSource(is), null);
136 
137         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
138         assertNotNull(elem);
139         assertEquals("constraintTest", elem.getName());
140         assertEquals(new QName("http://soapinterop.org/types", "constraintTest"),
141                      elem.getQName());
142 
143         XmlSchemaObjectCollection c = elem.getConstraints();
144         assertEquals(3, c.getCount());
145                              
146         Set s = new HashSet();
147         s.add(XmlSchemaKey.class.getName());
148         s.add(XmlSchemaKeyref.class.getName());
149         s.add(XmlSchemaUnique.class.getName());
150         for (int i = 0; i < c.getCount(); i++) {
151             Object o = c.getItem(i);
152             if (o instanceof XmlSchemaKey) {
153                 XmlSchemaKey key = (XmlSchemaKey)o;
154                 assertEquals("keyTest", key.getName());
155                 
156                 XmlSchemaXPath selectorXpath = key.getSelector();
157                 assertEquals("tns:products/tns:productName",
158                              selectorXpath.getXPath());
159                 
160                 XmlSchemaObjectCollection fields = key.getFields();
161                 assertEquals(1, fields.getCount());
162                 XmlSchemaXPath fieldXpath = null;
163                 for (int j = 0; j < fields.getCount(); j++) {
164                     fieldXpath = (XmlSchemaXPath)fields.getItem(j);
165                 }
166                 assertNotNull(fieldXpath);
167                 assertEquals("@productId", fieldXpath.getXPath());
168             } else if (o instanceof XmlSchemaKeyref) {
169                 XmlSchemaKeyref keyref = (XmlSchemaKeyref)o;
170                 assertNotNull(keyref);
171                 assertEquals("keyRefTest", keyref.getName());
172                 assertEquals(new QName("http://soapinterop.org/types",
173                                        "keyTest"),
174                              keyref.getRefer());
175                 
176                 XmlSchemaXPath selectorXpath = keyref.getSelector();
177                 assertEquals("tns:manufacturers/tns:location/tns:productName",
178                              selectorXpath.getXPath());
179 
180                 XmlSchemaObjectCollection fields = keyref.getFields();
181                 assertEquals(1, fields.getCount());
182                 XmlSchemaXPath fieldXpath = null;
183                 for (int j = 0; j < fields.getCount(); j++) {
184                     fieldXpath = (XmlSchemaXPath)fields.getItem(j);
185                 }
186                 assertNotNull(fieldXpath);
187                 assertEquals("@productId", fieldXpath.getXPath());
188             } else if (o instanceof XmlSchemaUnique) {
189                 XmlSchemaUnique unique = (XmlSchemaUnique)o;
190                 assertNotNull(unique);
191                 assertEquals("uniqueTest", unique.getName());
192                 XmlSchemaXPath selectorXpath = unique.getSelector();
193                 assertEquals("tns:manufacturers/tns:location",
194                              selectorXpath.getXPath());
195 
196                 XmlSchemaObjectCollection fields = unique.getFields();
197                 assertEquals(1, fields.getCount());
198                 XmlSchemaXPath fieldXpath = null;
199                 for (int j = 0; j < fields.getCount(); j++) {
200                     fieldXpath = (XmlSchemaXPath)fields.getItem(j);
201                 }
202                 assertNotNull(fieldXpath);
203                 assertEquals("@district", fieldXpath.getXPath());
204             } else {
205                 fail("An unexpected constraint of \""
206                      + o.getClass().getName() + "\" was found.");
207             }
208             s.remove(o.getClass().getName());
209         }
210 
211         assertTrue("The set should have been empty, but instead contained: "
212                    + s + ".",
213                    s.isEmpty());
214         
215     }
216 
217 }