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