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.Element;
24  import org.w3c.dom.Node;
25  import org.w3c.dom.NodeList;
26  
27  import javax.xml.transform.stream.StreamSource;
28  import java.io.FileInputStream;
29  import java.io.InputStream;
30  
31  public class AnnotationDeepTest extends TestCase {
32  	
33      /**
34       * The appinfo element has no source attribute
35       * but it has content.
36       *
37       * @throws Exception Any exception encountered
38       */
39      public void testAppInfoNoSource() throws Exception {
40      	
41          /*
42  		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
43  		        targetNamespace="http://www.abc.com/validation"
44  		        xmlns="http://www.abc.com/validation"
45  		        xmlns:xsns="http://www.abc.com/validation"
46  		        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
47  		        elementFormDefault="qualified">
48  		    <xs:annotation>
49  		        <xs:appinfo>
50  		            <jaxb:schemaBindings>
51  		                <jaxb:package  name="com.abc.validation"/>
52  		            </jaxb:schemaBindings>
53  		        </xs:appinfo>
54  		    </xs:annotation>
55  		
56  		    <simpleType name="emptyAppinfo">
57  		        <restriction base="string">
58  		            <length value="1"/>
59  		        </restriction>
60  		    </simpleType>
61  		
62  		</xs:schema>
63          */
64  
65          InputStream is = new FileInputStream(Resources.asURI("annotation-appinfo-no-source.xsd"));
66          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
67          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
68          
69          XmlSchemaAnnotation annotation = schema.getAnnotation();
70          assertTrue("annotation is retrieved ok", null != annotation);
71          XmlSchemaObjectCollection items = annotation.getItems();
72          assertEquals("Annotation contains an appinfo and yet this fails", 1, items.getCount());
73  
74      }
75  
76      /**
77       * The appinfo element has a source attribute
78       * but the content is deep markup.
79       *
80       * @throws Exception Any exception encountered
81       */
82      public void testAppInfoDeep() throws Exception {
83      	
84          /*
85  		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
86  		        targetNamespace="http://www.abc.com/validation"
87  		        xmlns="http://www.abc.com/validation"
88  		        xmlns:xsns="http://www.abc.com/validation"
89  		        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
90  		        elementFormDefault="qualified">
91  		    <xs:annotation>
92  		        <xs:appinfo source="anything">
93  		            <jaxb:schemaBindings>
94  		                <jaxb:package  name="com.abc.validation"/>
95  		            </jaxb:schemaBindings>
96  		        </xs:appinfo>
97  		    </xs:annotation>
98  		
99  		    <simpleType name="emptyAppinfo">
100 		        <restriction base="string">
101 		            <length value="1"/>
102 		        </restriction>
103 		    </simpleType>
104 		
105 		</xs:schema>
106         */
107 
108         InputStream is = new FileInputStream(Resources.asURI("annotation-appinfo-deep.xsd"));
109         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
110         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
111         
112         XmlSchemaAnnotation annotation = schema.getAnnotation();
113         assertTrue("annotation is retrieved ok", null != annotation);
114         XmlSchemaObjectCollection items = annotation.getItems();
115         assertTrue(items.getItem(0) instanceof XmlSchemaAppInfo);
116         XmlSchemaAppInfo appInfo = (XmlSchemaAppInfo) items.getItem(0);
117         NodeList markup = appInfo.getMarkup();
118         assertTrue("The markup exists", null != markup);
119         Node node = markup.item(1);
120         assertTrue(node instanceof Element);
121         Element el = (Element) node;
122         assertEquals("First level child is retrieved ok",
123         		"http://java.sun.com/xml/ns/jaxb", node.getNamespaceURI());
124         assertEquals("First level child is retrieved ok",
125         		"schemaBindings", node.getLocalName());
126         assertTrue("schemaBindings should have a child", el.getChildNodes().getLength() > 0);
127         NodeList l = el.getElementsByTagNameNS("http://java.sun.com/xml/ns/jaxb", "package");
128         assertTrue("ok this is actually working",l.getLength() > 0);
129 
130     }
131 }