View Javadoc

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  
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Element;
36  public class NamespaceContextTest extends XMLTestCase {
37      protected boolean whitespace = true;
38      protected void setUp() throws Exception {
39          whitespace = XMLUnit.getIgnoreWhitespace();
40          XMLUnit.setIgnoreWhitespace(true);
41      }
42      protected void tearDown() throws java.lang.Exception {
43          XMLUnit.setIgnoreWhitespace(whitespace);
44      }
45      public void testNamespaceContext() throws Exception {
46          Map namespaceMapFromWSDL = new HashMap();
47          namespaceMapFromWSDL.put("tns", new URI("http://example.org/getBalance/"));
48          namespaceMapFromWSDL.put("xsd", new URI("http://www.w3.org/2001/XMLSchema"));
49          String schema = "\t\t<xsd:schema targetNamespace=\"http://example.org/getBalance/\"\n" +
50                  "attributeFormDefault=\"unqualified\" elementFormDefault=\"unqualified\"" +
51                  " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
52                  " xmlns=\"http://www.w3.org/2001/XMLSchema\"" +
53                  " xmlns:xsd1=\"http://example.org/getBalance/\">" +
54                  "\t\t\t<xsd:include schemaLocation=\"getBalance.xsd\" />\n" +
55                  "\n" +
56                  "\t\t\t<xsd:element name=\"newCustomer\">\n" +
57                  "\t\t\t\t<xsd:complexType>\n" +
58                  "\t\t\t\t\t<xsd:sequence>\n" +
59                  "\t\t\t\t\t\t<xsd:element name=\"details\" type=\"tns:cinfoct\" />\n" +
60                  "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"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\t<xsd:element name=\"customerId\">\n" +
66                  "\t\t\t\t<xsd:complexType>\n" +
67                  "\t\t\t\t\t<xsd:sequence>\n" +
68                  "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"string\" />\n" +
69                  "\t\t\t\t\t</xsd:sequence>\n" +
70                  "\t\t\t\t</xsd:complexType>\n" +
71                  "\t\t\t</xsd:element>\n" +
72                  "\n" +
73                  "\t\t</xsd:schema>";
74          org.xml.sax.InputSource schemaInputSource = new InputSource(new StringReader(schema));
75          XmlSchemaCollection xsc = new XmlSchemaCollection();
76          xsc.setBaseUri(Resources.TEST_RESOURCES);
77  
78          //Set the namespaces explicitly
79          NamespaceMap prefixmap = new NamespaceMap(namespaceMapFromWSDL);
80          xsc.setNamespaceContext(prefixmap);
81          XmlSchema schemaDef = xsc.read(schemaInputSource, null);
82          StringWriter sw = new StringWriter();
83          schemaDef.write(sw);
84          try {
85              assertXMLEqual(sw.toString(), schema.replaceAll("tns:", "xsd1:"));
86          } catch (NullPointerException ex) {
87              System.out.println(">>>> NPE, ignoring assertXMLEqual");
88          }
89      }
90      public void testNamespaceCount() throws Exception {
91          String schema =  "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\"" +
92          		" xmlns:addressing=\"http://schemas.foo.com/references\"" +
93          		" xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\"" +
94          		" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\"" +
95          		" xmlns:tns=\"http://schemas.foo.com/idl/isfx_authn_service.idl\" " +
96          		" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\"" +
97          		" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
98          		" xmlns:xsd1=\"http://schemas.foo.com/idltypes/isfx_authn_service.idl\"" +
99          		" targetNamespace=\"http://schemas.foo.com/idltypes/isf_suppl_info.idl\">" +
100         		"<complexType name=\"IT_ISF.DomainRealmFilter\">" +
101         		"<sequence><element name=\"domain_name\" type=\"string\"/>" +
102         		"<element name=\"realm\" type=\"string\"/></sequence>" +
103         		"</complexType></schema>";
104         org.xml.sax.InputSource schemaInputSource = new InputSource(new StringReader(schema));
105         XmlSchemaCollection xsc = new XmlSchemaCollection();
106         xsc.setBaseUri(Resources.TEST_RESOURCES);
107 
108         //Set the namespaces explicitly
109         XmlSchema schemaDef = xsc.read(schemaInputSource, null);
110         Document doc = schemaDef.getSchemaDocument();
111         Element el = doc.getDocumentElement();
112         String ns = el.getAttribute("xmlns");
113         assertEquals("http://www.w3.org/2001/XMLSchema", ns);
114     }
115     public void testNullNamespaceCtx() throws Exception {
116         String schema = "\t\t<xsd:schema targetNamespace=\"http://example.org/getBalance/\"\n" +
117             "attributeFormDefault=\"unqualified\" elementFormDefault=\"unqualified\"" +
118             " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
119             " xmlns=\"http://www.w3.org/2001/XMLSchema\"" +
120             " xmlns:xsd1=\"http://example.org/getBalance/\">" +
121             "\t\t\t<xsd:include schemaLocation=\"getBalance.xsd\" />\n" +
122             "\n" +
123             "\t\t\t<xsd:element name=\"newCustomer\">\n" +
124             "\t\t\t\t<xsd:complexType>\n" +
125             "\t\t\t\t\t<xsd:sequence>\n" +
126             "\t\t\t\t\t\t<xsd:element name=\"details\" type=\"xsd1:cinfoct\" />\n" +
127             "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"string\" />\n" +
128             "\t\t\t\t\t</xsd:sequence>\n" +
129             "\t\t\t\t</xsd:complexType>\n" +
130             "\t\t\t</xsd:element>\n" +
131             "\n" +
132             "\t\t\t<xsd:element name=\"customerId\">\n" +
133             "\t\t\t\t<xsd:complexType>\n" +
134             "\t\t\t\t\t<xsd:sequence>\n" +
135             "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"string\" />\n" +
136             "\t\t\t\t\t</xsd:sequence>\n" +
137             "\t\t\t\t</xsd:complexType>\n" +
138             "\t\t\t</xsd:element>\n" +
139             "\n" +
140             "\t\t</xsd:schema>";
141         org.xml.sax.InputSource schemaInputSource = new InputSource(new StringReader(schema));
142         XmlSchemaCollection xsc = new XmlSchemaCollection();
143         xsc.setBaseUri(Resources.TEST_RESOURCES);
144 
145         //Set the namespaces explicitly
146         XmlSchema schemaDef = xsc.read(schemaInputSource, null);
147         schemaDef.setNamespaceContext(null);
148         Document doc = schemaDef.getSchemaDocument();
149         Element el = doc.getDocumentElement();
150         String ns = el.getAttribute("xmlns");
151         assertEquals("http://www.w3.org/2001/XMLSchema", ns);
152         ns = el.getAttribute("xmlns:tns");
153         assertEquals("http://example.org/getBalance/", ns);
154     }
155 }