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