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          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
92                                          "test1include");
93          InputStream is = new FileInputStream(Resources.asURI("include.xsd"));
94          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
95          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
96  
97          XmlSchemaObjectCollection c = schema.getIncludes();
98          assertEquals(2, c.getCount());
99  
100         Set set = new HashSet();
101         set.add(Resources.asURI("include2.xsd"));
102         set.add(Resources.asURI("include3.xsd"));
103         for (int i = 0; i < c.getCount(); i++) {
104             XmlSchemaInclude include = (XmlSchemaInclude)c.getItem(i);
105             assertNotNull(include);
106             XmlSchema s = include.getSchema();
107             assertNotNull(s);
108             String schemaLocation = include.getSchemaLocation();
109             if (schemaLocation.equals(Resources.asURI("include2.xsd"))) {
110                 XmlSchemaElement xse =
111                     s.getElementByName(new
112                         QName("http://soapinterop.org/types", "test1include"));
113                 assertEquals("test1include", xse.getName());
114                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "string"),
115                              xse.getSchemaTypeName());
116             } else if (schemaLocation.equals(Resources.asURI("include3.xsd"))) {
117                 XmlSchemaElement xse =
118                     s.getElementByName(new 
119                         QName("http://soapinterop.org/types", "test2include"));
120                 assertEquals("test2include", xse.getName());
121                 assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
122                              xse.getSchemaTypeName());
123             } else {
124                 fail("The schemaLocation of \"" + schemaLocation + "\" was"
125                      + " not expected.");
126             }
127             set.remove(schemaLocation);
128         }
129 
130         assertTrue("The set should have been empty, but instead contained: "
131                    + set + ".",
132                    set.isEmpty());
133 
134     }
135 
136 	/**
137 	 * Test importing a schema without namespace into a schema
138 	 * with namespace.
139 	 */
140 	public void testImportSchemaWithoutNamespace() throws Exception {
141         InputStream is = new FileInputStream(Resources.asURI("includingWithNamespace.xsd"));
142         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
143         schemaCol.read(new StreamSource(is), null);
144 
145         assertNotNull(schemaCol.getTypeByQName(new QName("http://tns.demo.org", "XdwsGroupId")));
146 	}
147 
148     /**
149      * Schema included defined xmlns="http://www.w3.org/2001/XMLSchema"
150      * @throws Exception
151      */
152     public void testSchemaInclude() throws Exception{
153         String uri = Resources.asURI("WSCOMMONS-87/includeBase.xsd");
154         InputSource isource = new InputSource(new FileInputStream(uri));
155         isource.setSystemId(uri);
156         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
157         XmlSchema schema = schemaCol.read(isource, null);
158         assertNotNull(schema);
159     }
160     
161     /**
162      * Schema included does not define xmlns="http://www.w3.org/2001/XMLSchema"
163      * @throws Exception
164      */
165     public void testSchemaIncludeNoDefaultNS() throws Exception{
166         String uri = Resources.asURI("WSCOMMONS-87/includeBaseNoDefaultNS.xsd");
167         InputSource isource = new InputSource(new FileInputStream(uri));
168         isource.setSystemId(uri);
169         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
170         XmlSchema schema = schemaCol.read(isource, null);
171         assertNotNull(schema);
172     }
173 }