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  import org.w3c.dom.Node;
25  import org.w3c.dom.NodeList;
26  
27  import javax.xml.namespace.QName;
28  import javax.xml.transform.stream.StreamSource;
29  import java.io.FileInputStream;
30  import java.io.InputStream;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.Set;
34  
35  /*
36   * Copyright 2004,2007 The Apache Software Foundation.
37   * Copyright 2006 International Business Machines Corp.
38   *
39   * Licensed under the Apache License, Version 2.0 (the "License");
40   * you may not use this file except in compliance with the License.
41   * You may obtain a copy of the License at
42   *
43   *      http://www.apache.org/licenses/LICENSE-2.0
44   *
45   * Unless required by applicable law or agreed to in writing, software
46   * distributed under the License is distributed on an "AS IS" BASIS,
47   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48   * See the License for the specific language governing permissions and
49   * limitations under the License.
50   * 
51   * @author Brent Ulbricht 
52   */
53  public class NotationTest extends TestCase {
54  
55      /**
56       * This method will test the notation.
57       *
58       * @throws Exception Any exception encountered
59       */
60      public void testNotation() throws Exception {
61  
62          /*
63           <schema xmlns="http://www.w3.org/2001/XMLSchema"
64                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
65                   xmlns:tns="http://soapinterop.org/types"
66                   targetNamespace="http://soapinterop.org/types">
67    
68             <notation name="teamLogo"
69                       system="com/team/graphics/teamLogo"
70                       public="http://www.team.com/graphics/teamLogo"
71                       id="notation.teamLogo">
72               <annotation>
73                 <documentation xml:lang="en">Location of the corporate logo.</documentation>
74               </annotation>
75             </notation>
76  
77             <notation name="teamMascot"
78                       system="com/team/graphics/teamMascot"
79                       public="http://www.team.com/graphics/teamMascot"
80                       id="notation.teamMascot">
81               <annotation>
82                 <documentation xml:lang="en">Location of the corporate mascot.</documentation>
83               </annotation>
84             </notation>
85  
86             <element name="demoNotation">
87               <simpleType>
88                 <restriction base="NOTATION">
89                   <enumeration value="tns:teamLogo"/>
90                   <enumeration value="tns:teamMascot"/>
91                 </restriction>
92               </simpleType>
93             </element>
94  
95           </schema>
96          */
97  
98          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
99                                          "demoNotation");
100         QName notationName = new QName("http://soapinterop.org/types",
101                                                 "teamLogo");
102 
103 
104 
105         InputStream is = new FileInputStream(Resources.asURI("notation.xsd"));
106         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
107         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
108 
109         XmlSchemaObjectTable notations = schema.getNotations();
110         assertNotNull(notations.getItem(notationName));
111 
112         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
113         assertNotNull(elem);
114         assertEquals("demoNotation", elem.getName());
115         assertEquals(new QName("http://soapinterop.org/types", "demoNotation"),
116                      elem.getQName());
117 
118         XmlSchemaSimpleType type =
119             (XmlSchemaSimpleType)elem.getSchemaType();
120         assertNotNull(type);
121 
122         XmlSchemaSimpleTypeRestriction xsstc =
123             (XmlSchemaSimpleTypeRestriction)type.getContent();
124         assertEquals(new QName("http://www.w3.org/2001/XMLSchema","NOTATION"),
125                      xsstc.getBaseTypeName());
126 
127         XmlSchemaObjectCollection xsoc = xsstc.getFacets();
128         assertEquals(2, xsoc.getCount());
129         Set s = new HashSet();
130         s.add("tns:teamLogo");
131         s.add("tns:teamMascot");
132         for (int i = 0; i < xsoc.getCount(); i++) {
133             XmlSchemaEnumerationFacet xsef =
134                 (XmlSchemaEnumerationFacet)xsoc.getItem(i);
135             String value = (String)xsef.getValue();
136             if (!(value.equals("tns:teamLogo")
137                    || value.equals("tns:teamMascot"))) {
138                 fail("An unexpected value of \"" + value
139                      + "\" was found.");
140             }
141             assertTrue(s.remove(value));
142         }
143         assertTrue("The set should have been empty, but instead contained: "
144                    + s + ".",
145                    s.isEmpty());
146 
147         XmlSchemaObjectTable xsot = schema.getNotations();
148         assertEquals(2, xsot.getCount());
149         
150         s.clear();
151         s.add("teamMascot");
152         s.add("teamLogo");
153         for (Iterator i = xsot.getNames(); i.hasNext(); ) {
154             String name = ((QName)i.next()).getLocalPart();
155             if (!(name.equals("teamLogo")
156                    || name.equals("teamMascot"))) {
157                 fail("An unexpected name of \"" + name
158                      + "\" was found.");
159             }
160             assertTrue(s.remove(name));
161         }
162         assertTrue("The set should have been empty, but instead contained: "
163                    + s + ".",
164                    s.isEmpty());
165 
166         s.clear();
167         s.add("teamMascot");
168         s.add("teamLogo");
169         for (Iterator i = xsot.getValues(); i.hasNext(); ) {
170             XmlSchemaNotation xsn = (XmlSchemaNotation)i.next();
171             String name = xsn.getName();
172             XmlSchemaAnnotation xsa = xsn.getAnnotation();
173             XmlSchemaObjectCollection col = xsa.getItems();
174             assertEquals(1, col.getCount());
175             XmlSchemaDocumentation xsd = null; 
176             for (int k = 0; k < col.getCount(); k++) {
177                 xsd = (XmlSchemaDocumentation)col.getItem(k);
178             }
179             if (name.equals("teamMascot")) {
180                 assertEquals("http://www.team.com/graphics/teamMascot",
181                              xsn.getPublic());
182                 assertEquals("com/team/graphics/teamMascot",
183                              xsn.getSystem());
184                 assertEquals("notation.teamMascot", xsn.getId());
185                 assertEquals("en", xsd.getLanguage());
186                 NodeList nl = xsd.getMarkup();
187                 for (int j = 0; j < nl.getLength(); j++) {
188                     Node n = nl.item(j);
189                     if (n.getNodeType() == Node.TEXT_NODE) {
190                         assertEquals("Location of the corporate mascot.",
191                                      n.getNodeValue());
192                     }
193                 }
194             } else if (name.equals("teamLogo")) {
195                 assertEquals("http://www.team.com/graphics/teamLogo",
196                              xsn.getPublic());
197                 assertEquals("com/team/graphics/teamLogo",
198                              xsn.getSystem());
199                 assertEquals("notation.teamLogo", xsn.getId());
200                 assertEquals("en", xsd.getLanguage());
201                 NodeList nl = xsd.getMarkup();
202                 for (int j = 0; j < nl.getLength(); j++) {
203                     Node n = nl.item(j);
204                     if (n.getNodeType() == Node.TEXT_NODE) {
205                         assertEquals("Location of the corporate logo.",
206                                      n.getNodeValue());
207                     }
208                 }
209             } else {
210                 fail("An unexpected name of \"" + name
211                      + "\" was found.");
212             }
213             assertTrue(s.remove(name));
214         }
215         assertTrue("The set should have been empty, but instead contained: "
216                    + s + ".",
217                    s.isEmpty());
218 
219     }
220 
221 }