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  import org.apache.ws.commons.schema.XmlSchema;
22  import org.apache.ws.commons.schema.XmlSchemaCollection;
23  import org.apache.ws.commons.schema.utils.NamespaceMap;
24  import org.custommonkey.xmlunit.XMLTestCase;
25  import org.custommonkey.xmlunit.XMLUnit;
26  import org.xml.sax.InputSource;
27  
28  import java.io.StringReader;
29  import java.io.StringWriter;
30  import java.net.URI;
31  import java.util.HashMap;
32  import java.util.Map;
33  public class NamespaceContextTest extends XMLTestCase {
34      protected boolean whitespace = true;
35      protected void setUp() throws Exception {
36          whitespace = XMLUnit.getIgnoreWhitespace();
37          XMLUnit.setIgnoreWhitespace(true);
38      }
39      protected void tearDown() throws java.lang.Exception {
40          XMLUnit.setIgnoreWhitespace(whitespace);
41      }
42      public void testNamespaceContext() throws Exception {
43          Map namespaceMapFromWSDL = new HashMap();
44          namespaceMapFromWSDL.put("tns", new URI("http://example.org/getBalance/"));
45          namespaceMapFromWSDL.put("xsd", new URI("http://www.w3.org/2001/XMLSchema"));
46          String schema = "\t\t<xsd:schema targetNamespace=\"http://example.org/getBalance/\"\n" +
47                  "attributeFormDefault=\"unqualified\" elementFormDefault=\"unqualified\"" +
48                  " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
49                  "\t\t\t<xsd:include schemaLocation=\"getBalance.xsd\" />\n" +
50                  "\n" +
51                  "\t\t\t<xsd:element name=\"newCustomer\">\n" +
52                  "\t\t\t\t<xsd:complexType>\n" +
53                  "\t\t\t\t\t<xsd:sequence>\n" +
54                  "\t\t\t\t\t\t<xsd:element name=\"details\" type=\"tns:cinfoct\" />\n" +
55                  "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"xsd:string\" />\n" +
56                  "\t\t\t\t\t</xsd:sequence>\n" +
57                  "\t\t\t\t</xsd:complexType>\n" +
58                  "\t\t\t</xsd:element>\n" +
59                  "\n" +
60                  "\t\t\t<xsd:element name=\"customerId\">\n" +
61                  "\t\t\t\t<xsd:complexType>\n" +
62                  "\t\t\t\t\t<xsd:sequence>\n" +
63                  "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"xsd:string\" />\n" +
64                  "\t\t\t\t\t</xsd:sequence>\n" +
65                  "\t\t\t\t</xsd:complexType>\n" +
66                  "\t\t\t</xsd:element>\n" +
67                  "\n" +
68                  "\t\t</xsd:schema>";
69          org.xml.sax.InputSource schemaInputSource = new InputSource(new StringReader(schema));
70          XmlSchemaCollection xsc = new XmlSchemaCollection();
71          xsc.setBaseUri(Resources.TEST_RESOURCES);
72  
73          //Set the namespaces explicitly
74          NamespaceMap prefixmap = new NamespaceMap(namespaceMapFromWSDL);
75          xsc.setNamespaceContext(prefixmap);
76          XmlSchema schemaDef = xsc.read(schemaInputSource, null);
77          StringWriter sw = new StringWriter();
78          schemaDef.write(sw);
79                  
80          assertXMLEqual(sw.toString(), schema);
81      }
82  }