1   /*
2    * Copyright 2004,2007 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tests;
18  import org.apache.ws.commons.schema.XmlSchema;
19  import org.apache.ws.commons.schema.XmlSchemaCollection;
20  import org.apache.ws.commons.schema.utils.NamespaceMap;
21  import org.custommonkey.xmlunit.XMLTestCase;
22  import org.custommonkey.xmlunit.XMLUnit;
23  import org.xml.sax.InputSource;
24  
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  import java.net.URI;
28  import java.util.HashMap;
29  import java.util.Map;
30  public class NamespaceContextTest extends XMLTestCase {
31      protected boolean whitespace = true;
32      protected void setUp() throws Exception {
33          whitespace = XMLUnit.getIgnoreWhitespace();
34          XMLUnit.setIgnoreWhitespace(true);
35      }
36      protected void tearDown() throws java.lang.Exception {
37          XMLUnit.setIgnoreWhitespace(whitespace);
38      }
39      public void testNamespaceContext() throws Exception {
40          Map namespaceMapFromWSDL = new HashMap();
41          namespaceMapFromWSDL.put("tns", new URI("http://example.org/getBalance/"));
42          namespaceMapFromWSDL.put("xsd", new URI("http://www.w3.org/2001/XMLSchema"));
43          String schema = "\t\t<xsd:schema targetNamespace=\"http://example.org/getBalance/\"\n" +
44                  "attributeFormDefault=\"unqualified\" elementFormDefault=\"unqualified\"" +
45                  " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
46                  "\t\t\t<xsd:include schemaLocation=\"getBalance.xsd\" />\n" +
47                  "\n" +
48                  "\t\t\t<xsd:element name=\"newCustomer\">\n" +
49                  "\t\t\t\t<xsd:complexType>\n" +
50                  "\t\t\t\t\t<xsd:sequence>\n" +
51                  "\t\t\t\t\t\t<xsd:element name=\"details\" type=\"tns:cinfoct\" />\n" +
52                  "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"xsd:string\" />\n" +
53                  "\t\t\t\t\t</xsd:sequence>\n" +
54                  "\t\t\t\t</xsd:complexType>\n" +
55                  "\t\t\t</xsd:element>\n" +
56                  "\n" +
57                  "\t\t\t<xsd:element name=\"customerId\">\n" +
58                  "\t\t\t\t<xsd:complexType>\n" +
59                  "\t\t\t\t\t<xsd:sequence>\n" +
60                  "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"xsd:string\" />\n" +
61                  "\t\t\t\t\t</xsd:sequence>\n" +
62                  "\t\t\t\t</xsd:complexType>\n" +
63                  "\t\t\t</xsd:element>\n" +
64                  "\n" +
65                  "\t\t</xsd:schema>";
66          org.xml.sax.InputSource schemaInputSource = new InputSource(new StringReader(schema));
67          XmlSchemaCollection xsc = new XmlSchemaCollection();
68          xsc.setBaseUri(Resources.TEST_RESOURCES);
69  
70          //Set the namespaces explicitly
71          NamespaceMap prefixmap = new NamespaceMap(namespaceMapFromWSDL);
72          xsc.setNamespaceContext(prefixmap);
73          XmlSchema schemaDef = xsc.read(schemaInputSource, null);
74          StringWriter sw = new StringWriter();
75          schemaDef.write(sw);
76  
77          assertXMLEqual(sw.toString(), schema);
78      }
79  }