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