View Javadoc

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  package tests;
20  
21  import junit.framework.TestCase;
22  import org.apache.ws.commons.schema.*;
23  import org.w3c.dom.Node;
24  import org.w3c.dom.NodeList;
25  
26  import javax.xml.namespace.QName;
27  import javax.xml.transform.stream.StreamSource;
28  import java.io.FileInputStream;
29  import java.io.InputStream;
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  
34  public class AnnotationTest extends TestCase {
35  
36      /**
37       * This method will test for when the appinfo
38       * element of an annotation doesn't include a
39       * source attribute and doesn't contain any 
40       * content.
41       *
42       * @throws Exception Any exception encountered
43       */
44      public void testEmptyAppInfo() throws Exception {
45  
46          /*
47          <simpleType name="emptyAppinfo">
48            <annotation>
49              <documentation source="http://test/source/doc" xml:lang="en">testing987</documentation>
50              <appinfo/>
51            </annotation>
52            <restriction base="string">
53              <length value="1"/>
54            </restriction>
55          </simpleType>
56          */
57  
58          QName TYPE_QNAME = new QName("http://soapinterop.org/types",
59                                       "emptyAppinfo");
60          InputStream is = new FileInputStream(Resources.asURI("annotation.xsd"));
61          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
62          schemaCol.read(new StreamSource(is), null);
63  
64          XmlSchemaSimpleType simpleType =
65              (XmlSchemaSimpleType)schemaCol.getTypeByQName(TYPE_QNAME);
66          assertNotNull(simpleType);
67  
68          XmlSchemaAnnotation xsa = simpleType.getAnnotation();
69          assertNotNull(xsa);
70  
71          XmlSchemaObjectCollection col = xsa.getItems();
72          assertEquals(1, col.getCount());
73  
74          Set s = new HashSet();
75          s.add(XmlSchemaDocumentation.class.getName());
76          for (int i = 0; i < col.getCount(); i++) {
77              XmlSchemaObject o = col.getItem(i);
78              if (o instanceof XmlSchemaAppInfo) {
79                  fail("The appinfo element did not contain a source"
80                       + " attribute or any content, so this element"
81                       + " was not exptected to be found.");
82              } else if (o instanceof XmlSchemaDocumentation) {
83                  assertEquals("en",
84                               ((XmlSchemaDocumentation)o).getLanguage());
85                  assertEquals("http://test/source/doc",
86                               ((XmlSchemaDocumentation)o).getSource());
87                  NodeList nl = ((XmlSchemaDocumentation)o).getMarkup();
88                  for (int j = 0; j < nl.getLength(); j++) {
89                      Node n = nl.item(j);
90                      if (n.getNodeType() == Node.TEXT_NODE) {
91                          assertEquals("testing987", n.getNodeValue());
92                      }
93                  }
94              }
95              assertTrue(s.remove(o.getClass().getName()));
96          }
97          assertTrue("The set should have been empty, but instead contained: "
98                     + s + ".",
99                     s.isEmpty());
100     }
101 
102     /**
103      * This method will test for when the documentation
104      * element of an annotation doesn't include a
105      * source attribute or xml:lang attribute and doesn't
106      * contain any content.
107      *
108      * @throws Exception Any exception encountered
109      */
110     public void testEmptyDocumentation() throws Exception {
111 
112         /*
113         <simpleType name="emptyDocumentation">
114           <annotation>
115             <documentation/>
116             <appinfo source="http://test/source/appinfo">testing123</appinfo>
117           </annotation>
118           <restriction base="string">
119             <length value="2"/>
120           </restriction>
121         </simpleType>
122         */
123 
124         QName TYPE_QNAME = new QName("http://soapinterop.org/types",
125                                      "emptyDocumentation");
126         InputStream is = new FileInputStream(Resources.asURI("annotation.xsd"));
127         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
128         schemaCol.read(new StreamSource(is), null);
129 
130         XmlSchemaSimpleType simpleType =
131             (XmlSchemaSimpleType)schemaCol.getTypeByQName(TYPE_QNAME);
132         assertNotNull(simpleType);
133 
134         XmlSchemaAnnotation xsa = simpleType.getAnnotation();
135         assertNotNull(xsa);
136 
137         XmlSchemaObjectCollection col = xsa.getItems();
138         assertEquals(1, col.getCount());
139 
140         Set s = new HashSet();
141         s.add(XmlSchemaAppInfo.class.getName());
142         for (int i = 0; i < col.getCount(); i++) {
143             XmlSchemaObject o = col.getItem(i);
144             if (o instanceof XmlSchemaAppInfo) {
145                 assertEquals("http://test/source/appinfo",
146                              ((XmlSchemaAppInfo)o).getSource());
147                 NodeList nl = ((XmlSchemaAppInfo)o).getMarkup();
148                 for (int j = 0; j < nl.getLength(); j++) {
149                     Node n = nl.item(j);
150                     if (n.getNodeType() == Node.TEXT_NODE) {
151                         assertEquals("testing123", n.getNodeValue());
152                     }
153                 }
154             } else if (o instanceof XmlSchemaDocumentation) {
155                 fail("The documentation element did not contain a source"
156                      + " attribute or any content, so this element"
157                      + " was not exptected to be found.");
158             }
159             assertTrue(s.remove(o.getClass().getName()));
160         }
161         assertTrue("The set should have been empty, but instead contained: "
162                    + s + ".",
163                    s.isEmpty());
164     }
165 
166 
167     /**
168      * This method will test for when the documentation
169      * and appinfo elements of an annotation don't include
170      * anything.
171      *
172      * @throws Exception Any exception encountered
173      */
174     public void testEmptyAppinfoDocumentation() throws Exception {
175 
176         /*
177         <simpleType name="emptyAppinfoDocumentation">
178           <annotation>
179             <documentation/>
180             <appinfo/>
181           </annotation>
182           <restriction base="string">
183             <length value="1"/>
184           </restriction>
185         </simpleType>
186         */
187 
188         QName TYPE_QNAME = new QName("http://soapinterop.org/types",
189                                      "emptyAppinfoDocumentation");
190         InputStream is = new FileInputStream(Resources.asURI("annotation.xsd"));
191         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
192         schemaCol.read(new StreamSource(is), null);
193 
194         XmlSchemaSimpleType simpleType =
195             (XmlSchemaSimpleType)schemaCol.getTypeByQName(TYPE_QNAME);
196         assertNotNull(simpleType);
197 
198         XmlSchemaAnnotation xsa = simpleType.getAnnotation();
199         assertNotNull(xsa);
200 
201         XmlSchemaObjectCollection col = xsa.getItems();
202         assertEquals(0, col.getCount());
203 
204     }
205 
206     /**
207      * This method will test for when the documentation
208      * and appinfo elements contain all the information.
209      *
210      * @throws Exception Any exception encountered
211      */
212     public void testFullDocumentationAppinfo() throws Exception {
213 
214         /*
215         <simpleType name="annotationTest">
216           <annotation>
217             <documentation source="http://test/source/doc" xml:lang="en">testing987</documentation>
218             <appinfo source="http://test/source/appinfo">testing123</appinfo>
219           </annotation>
220           <restriction base="string">
221             <length value="1"/>
222           </restriction>
223         </simpleType>
224         */
225 
226         QName TYPE_QNAME = new QName("http://soapinterop.org/types",
227                                      "annotationTest");
228         InputStream is = new FileInputStream(Resources.asURI("annotation.xsd"));
229         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
230         schemaCol.read(new StreamSource(is), null);
231 
232         XmlSchemaSimpleType simpleType =
233             (XmlSchemaSimpleType)schemaCol.getTypeByQName(TYPE_QNAME);
234         assertNotNull(simpleType);
235 
236         XmlSchemaAnnotation xsa = simpleType.getAnnotation();
237         assertNotNull(xsa);
238 
239         XmlSchemaObjectCollection col = xsa.getItems();
240         assertEquals(2, col.getCount());
241 
242         Set s = new HashSet();
243         s.add(XmlSchemaAppInfo.class.getName());
244         s.add(XmlSchemaDocumentation.class.getName());
245         for (int i = 0; i < col.getCount(); i++) {
246             XmlSchemaObject o = col.getItem(i);
247             if (o instanceof XmlSchemaAppInfo) {
248                 assertEquals("http://test/source/appinfo",
249                              ((XmlSchemaAppInfo)o).getSource());
250                 NodeList nl = ((XmlSchemaAppInfo)o).getMarkup();
251                 for (int j = 0; j < nl.getLength(); j++) {
252                     Node n = nl.item(j);
253                     if (n.getNodeType() == Node.TEXT_NODE) {
254                         assertEquals("testing123", n.getNodeValue());
255                     }
256                 }
257             } else if (o instanceof XmlSchemaDocumentation) {
258                 assertEquals("en",
259                              ((XmlSchemaDocumentation)o).getLanguage());
260                 assertEquals("http://test/source/doc",
261                              ((XmlSchemaDocumentation)o).getSource());
262                 NodeList nl = ((XmlSchemaDocumentation)o).getMarkup();
263                 for (int j = 0; j < nl.getLength(); j++) {
264                     Node n = nl.item(j);
265                     if (n.getNodeType() == Node.TEXT_NODE) {
266                         assertEquals("testing987", n.getNodeValue());
267                     }
268                 }
269             }
270             assertTrue(s.remove(o.getClass().getName()));
271         }
272         assertTrue("The set should have been empty, but instead contained: "
273                    + s + ".",
274                    s.isEmpty());
275     }
276 
277     /**
278      * This method will test for when an annotation is added
279      * to the Xml Schema Element.
280      *
281      * @throws Exception Any exception encountered
282      */
283     public void testXmlSchemaElementAnnotation() throws Exception {
284 
285         /*
286         <annotation id="schemaAnnotation">
287           <documentation source="http://test101/source/doc" xml:lang="en">testing101</documentation>
288           <appinfo source="http://test101/source/appinfo">testing101</appinfo>
289         </annotation>
290         */
291 
292         InputStream is = new FileInputStream(Resources.asURI("annotation.xsd"));
293         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
294         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
295         
296         XmlSchemaAnnotation xsa = schema.getAnnotation();
297         XmlSchemaObjectCollection col = xsa.getItems();
298         assertEquals(2, col.getCount());
299 
300         Set s = new HashSet();
301         s.add(XmlSchemaAppInfo.class.getName());
302         s.add(XmlSchemaDocumentation.class.getName());
303         for (int i = 0; i < col.getCount(); i++) {
304             XmlSchemaObject o = col.getItem(i);
305             if (o instanceof XmlSchemaAppInfo) {
306                 assertEquals("http://test101/source/appinfo",
307                              ((XmlSchemaAppInfo)o).getSource());
308                 NodeList nl = ((XmlSchemaAppInfo)o).getMarkup();
309                 for (int j = 0; j < nl.getLength(); j++) {
310                     Node n = nl.item(j);
311                     if (n.getNodeType() == Node.TEXT_NODE) {
312                         assertEquals("testing101", n.getNodeValue());
313                     }
314                 }
315             } else if (o instanceof XmlSchemaDocumentation) {
316                 assertEquals("en",
317                              ((XmlSchemaDocumentation)o).getLanguage());
318                 assertEquals("http://test101/source/doc",
319                              ((XmlSchemaDocumentation)o).getSource());
320                 NodeList nl = ((XmlSchemaDocumentation)o).getMarkup();
321                 for (int j = 0; j < nl.getLength(); j++) {
322                     Node n = nl.item(j);
323                     if (n.getNodeType() == Node.TEXT_NODE) {
324                         assertEquals("testing101", n.getNodeValue());
325                     }
326                 }
327             }
328             assertTrue(s.remove(o.getClass().getName()));
329         }
330         assertTrue("The set should have been empty, but instead contained: "
331                    + s + ".",
332                    s.isEmpty());
333 
334     }
335 
336 }