1   package tests;
2   
3   import junit.framework.TestCase;
4   import org.apache.ws.commons.schema.*;
5   import org.xml.sax.InputSource;
6   
7   import javax.xml.namespace.QName;
8   import javax.xml.transform.stream.StreamSource;
9   import java.io.FileInputStream;
10  import java.io.InputStream;
11  import java.util.HashSet;
12  import java.util.Set;
13  
14  /*
15   * Copyright 2004,2007 The Apache Software Foundation.
16   * Copyright 2006 International Business Machines Corp.
17   *
18   * Licensed under the Apache License, Version 2.0 (the "License");
19   * you may not use this file except in compliance with the License.
20   * You may obtain a copy of the License at
21   *
22   *      http://www.apache.org/licenses/LICENSE-2.0
23   *
24   * Unless required by applicable law or agreed to in writing, software
25   * distributed under the License is distributed on an "AS IS" BASIS,
26   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27   * See the License for the specific language governing permissions and
28   * limitations under the License.
29   *
30   */
31  public class IncludeTest extends TestCase {
32  
33      /**
34       * This method will test the include.
35       *
36       * @throws Exception Any exception encountered
37       */
38      public void testInclude() throws Exception {
39  
40          /*
41          <schema xmlns="http://www.w3.org/2001/XMLSchema"
42                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
43                  xmlns:tns="http://soapinterop.org/types"
44                  targetNamespace="http://soapinterop.org/types">
45    
46            <include schemaLocation="include2.xsd"/>
47            <include schemaLocation="include3.xsd"/>
48  
49          </schema>
50  
51          
52          <schema xmlns="http://www.w3.org/2001/XMLSchema"
53                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
54                  xmlns:tns="http://soapinterop.org/types"
55                  targetNamespace="http://soapinterop.org/types">
56    
57            <element name="test1include" type="string"/>
58  
59          </schema>
60  
61  
62          <schema xmlns="http://www.w3.org/2001/XMLSchema"
63                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
64                  xmlns:tns="http://soapinterop.org/types"
65                  targetNamespace="http://soapinterop.org/types">
66    
67            <element name="test2include" type="integer"/>
68  
69          </schema>
70          */
71  
72          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
73                                          "test1include");
74          InputStream is = new FileInputStream(Resources.asURI("include.xsd"));
75          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
76          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
77  
78          XmlSchemaObjectCollection c = schema.getIncludes();
79          assertEquals(2, c.getCount());
80  
81          Set set = new HashSet();
82          set.add(Resources.asURI("include2.xsd"));
83          set.add(Resources.asURI("include3.xsd"));
84          for (int i = 0; i < c.getCount(); i++) {
85              XmlSchemaInclude include = (XmlSchemaInclude)c.getItem(i);
86              assertNotNull(include);
87              XmlSchema s = include.getSchema();
88              assertNotNull(s);
89              String schemaLocation = include.getSchemaLocation();
90              if (schemaLocation.equals(Resources.asURI("include2.xsd"))) {
91                  XmlSchemaElement xse =
92                      s.getElementByName(new
93                          QName("http://soapinterop.org/types", "test1include"));
94                  assertEquals("test1include", xse.getName());
95                  assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "string"),
96                               xse.getSchemaTypeName());
97              } else if (schemaLocation.equals(Resources.asURI("include3.xsd"))) {
98                  XmlSchemaElement xse =
99                      s.getElementByName(new 
100                         QName("http://soapinterop.org/types", "test2include"));
101                 assertEquals("test2include", xse.getName());
102                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
103                              xse.getSchemaTypeName());
104             } else {
105                 fail("The schemaLocation of \"" + schemaLocation + "\" was"
106                      + " not expected.");
107             }
108             set.remove(schemaLocation);
109         }
110 
111         assertTrue("The set should have been empty, but instead contained: "
112                    + set + ".",
113                    set.isEmpty());
114 
115     }
116 
117 	/**
118 	 * Test importing a schema without namespace into a schema
119 	 * with namespace.
120 	 */
121 	public void testImportSchemaWithoutNamespace() throws Exception {
122         InputStream is = new FileInputStream(Resources.asURI("includingWithNamespace.xsd"));
123         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
124         schemaCol.read(new StreamSource(is), null);
125 
126         assertNotNull(schemaCol.getTypeByQName(new QName("http://tns.demo.org", "XdwsGroupId")));
127 	}
128 
129     /**
130      * Schema included defined xmlns="http://www.w3.org/2001/XMLSchema"
131      * @throws Exception
132      */
133     public void testSchemaInclude() throws Exception{
134         String uri = Resources.asURI("WSCOMMONS-87/includeBase.xsd");
135         InputSource isource = new InputSource(new FileInputStream(uri));
136         isource.setSystemId(uri);
137         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
138         XmlSchema schema = schemaCol.read(isource, null);
139         assertNotNull(schema);
140     }
141     
142     /**
143      * Schema included does not define xmlns="http://www.w3.org/2001/XMLSchema"
144      * @throws Exception
145      */
146     public void testSchemaIncludeNoDefaultNS() throws Exception{
147         String uri = Resources.asURI("WSCOMMONS-87/includeBaseNoDefaultNS.xsd");
148         InputSource isource = new InputSource(new FileInputStream(uri));
149         isource.setSystemId(uri);
150         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
151         XmlSchema schema = schemaCol.read(isource, null);
152         assertNotNull(schema);
153     }
154 }