001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package schema;
059:
060: import java.util.Enumeration;
061: import java.util.Hashtable;
062:
063: import javax.wsdl.Definition;
064: import javax.xml.namespace.QName;
065: import junit.framework.Test;
066: import junit.framework.TestCase;
067: import junit.framework.TestSuite;
068:
069: import org.apache.wsif.schema.Parser;
070: import org.apache.wsif.util.WSIFUtils;
071: import org.apache.wsif.wsdl.WSIFWSDLLocatorImpl;
072:
073: import util.TestUtilities;
074:
075: /**
076: * Junit test to test parsing of Schemas to generate xml -> Java mappings
077: * @author Owen Burroughs
078: */
079: public class SchemaTest extends TestCase {
080: String wsdlLocation1 = TestUtilities
081: .getWsdlPath("java\\test\\schema")
082: + "SchemaTest1.wsdl";
083: String wsdlLocation2 = TestUtilities
084: .getWsdlPath("java\\test\\schema")
085: + "SchemaTest2.wsdl";
086: String wsdlLocation3 = TestUtilities
087: .getWsdlPath("java\\test\\schema")
088: + "SchemaTest3.wsdl";
089:
090: Hashtable expectedA = new Hashtable();
091: Hashtable expectedB = new Hashtable();
092:
093: public SchemaTest(String name) {
094: super (name);
095: }
096:
097: public static void main(String[] args) {
098: junit.textui.TestRunner.run(suite());
099: }
100:
101: public static Test suite() {
102: return new TestSuite(SchemaTest.class);
103: }
104:
105: public void setUp() {
106: TestUtilities.setUpExtensionsAndProviders();
107: expectedA.put(new QName("http://types.wsiftest/", "addresses"),
108: "[Lwsiftest.types.Address;");
109: expectedA.put(new QName("http://types.wsiftest/",
110: "InfoResponse"), "wsiftest.types.InfoResponseElement");
111: expectedA.put(new QName("http://types.wsiftest/",
112: "ArrayOfArrayOfAddress"), "[[Lwsiftest.types.Address;");
113: expectedA.put(new QName("http://types.wsiftest/", "phone"),
114: "wsiftest.types.Phone");
115: expectedA.put(
116: new QName("http://types.wsiftest/", "ArrayOfLong"),
117: "[[J");
118: expectedA.put(new QName("http://types.wsiftest/", "address"),
119: "wsiftest.types.Address");
120: expectedA.put(
121: new QName("http://types.wsiftest/", "InfoRequest"),
122: "wsiftest.types.InfoRequestElement");
123: expectedA
124: .put(new QName("http://types.wsiftest/", "ArrayOfInt"),
125: "[I");
126: expectedA.put(new QName("http://types.wsiftest/",
127: "ArrayOfAddress"), "[Lwsiftest.types.Address;");
128:
129: // For nested xsd files namepsaces/results are slightly different
130: expectedB.put(new QName("http://types.wsiftest/", "addresses"),
131: "[Lwsiftest.types2.Address;");
132: expectedB.put(new QName("http://types.wsiftest/",
133: "InfoResponse"), "wsiftest.types.InfoResponseElement");
134: expectedB
135: .put(new QName("http://types2.wsiftest/",
136: "ArrayOfArrayOfAddress"),
137: "[[Lwsiftest.types2.Address;");
138: expectedB.put(new QName("http://types2.wsiftest/", "phone"),
139: "wsiftest.types2.Phone");
140: expectedB.put(
141: new QName("http://types.wsiftest/", "ArrayOfLong"),
142: "[[J");
143: expectedB.put(new QName("http://types2.wsiftest/", "address"),
144: "wsiftest.types2.Address");
145: expectedB.put(
146: new QName("http://types.wsiftest/", "InfoRequest"),
147: "wsiftest.types.InfoRequestElement");
148: expectedB
149: .put(new QName("http://types.wsiftest/", "ArrayOfInt"),
150: "[I");
151: expectedB.put(new QName("http://types2.wsiftest/",
152: "ArrayOfAddress"), "[Lwsiftest.types2.Address;");
153: }
154:
155: public void testParsing() {
156: doIt(wsdlLocation1, expectedA);
157: doIt(wsdlLocation2, expectedA);
158: doIt(wsdlLocation3, expectedB);
159: }
160:
161: private void doIt(String wsdl, Hashtable expectedResults) {
162: try {
163: System.out.println("*** " + wsdl + " ***\n");
164: Definition def = WSIFUtils.readWSDL(null, wsdl);
165: Hashtable table = new Hashtable();
166: Parser.getTypeMappings(def, table, false);
167: if (table != null) {
168: System.out.println("WITHOUT STANDARD MAPPINGS:");
169: Enumeration k = table.keys();
170: while (k.hasMoreElements()) {
171: QName qn = (QName) k.nextElement();
172: String cl = (String) table.get(qn);
173: System.out.println(qn + " -> " + cl);
174: }
175: } else {
176: System.out
177: .println("WITHOUT STANDARD MAPPINGS RETURNED NULL!!");
178: assertTrue("With wsdl " + wsdl
179: + ", table without standard mappings was null",
180: false);
181: }
182: checkResult(wsdl, table, expectedResults);
183: System.out.println("- - - -");
184: Hashtable table2 = new Hashtable();
185: Parser.getTypeMappings(def, table2);
186: if (table2 != null) {
187: System.out.println("WITH STANDARD MAPPINGS:");
188: Enumeration k = table2.keys();
189: while (k.hasMoreElements()) {
190: QName qn = (QName) k.nextElement();
191: String cl = (String) table2.get(qn);
192: System.out.println(qn + " -> " + cl);
193: }
194: } else {
195: System.out
196: .println("WITH STANDARD MAPPINGS RETURNED NULL!!");
197: assertTrue("With wsdl " + wsdl
198: + ", table with standard mappings was null",
199: false);
200: }
201: checkResult(wsdl, table2, expectedResults);
202: System.out
203: .println("-----------------------------------------------------\n");
204: } catch (Exception e) {
205: System.err.println("SchemaTest caught exception " + e);
206: e.printStackTrace();
207: assertTrue(false);
208: } finally {
209: TestUtilities.resetDefaultProviders();
210: }
211: }
212:
213: private void doItWithClassLoader(String wsdl,
214: Hashtable expectedResults) {
215: try {
216: System.out.println("*** " + wsdl + " ***\n");
217: ClassLoader loader = this .getClass().getClassLoader();
218: Definition def = WSIFUtils.readWSDL(null, wsdl, loader);
219: Hashtable table = new Hashtable();
220: Parser.getTypeMappings(def, table, loader, false);
221: if (table != null) {
222: System.out.println("WITHOUT STANDARD MAPPINGS:");
223: Enumeration k = table.keys();
224: while (k.hasMoreElements()) {
225: QName qn = (QName) k.nextElement();
226: String cl = (String) table.get(qn);
227: System.out.println(qn + " -> " + cl);
228: }
229: } else {
230: System.out
231: .println("WITHOUT STANDARD MAPPINGS RETURNED NULL!!");
232: assertTrue(
233: "With wsdl "
234: + wsdl
235: + " and using a classloader, table without standard mappings was null",
236: false);
237: }
238: checkResult(wsdl, table, expectedResults);
239: System.out.println("- - - -");
240: Hashtable table2 = new Hashtable();
241: Parser.getTypeMappings(def, table2, loader, true);
242: if (table2 != null) {
243: System.out.println("WITH STANDARD MAPPINGS:");
244: Enumeration k = table2.keys();
245: while (k.hasMoreElements()) {
246: QName qn = (QName) k.nextElement();
247: String cl = (String) table2.get(qn);
248: System.out.println(qn + " -> " + cl);
249: }
250: } else {
251: System.out
252: .println("WITH STANDARD MAPPINGS RETURNED NULL!!");
253: assertTrue(
254: "With wsdl "
255: + wsdl
256: + " and using a classloader, table with standard mappings was null",
257: false);
258: }
259: checkResult(wsdl, table2, expectedResults);
260: System.out
261: .println("-----------------------------------------------------\n");
262: } catch (Exception e) {
263: System.err.println("SchemaTest caught exception " + e);
264: e.printStackTrace();
265: assertTrue(false);
266: } finally {
267: TestUtilities.resetDefaultProviders();
268: }
269: }
270:
271: private void checkResult(String wsdl, Hashtable results, Hashtable expected) {
272: Enumeration enum = expected.keys();
273: while(enum.hasMoreElements()) {
274: QName key = (QName) enum.nextElement();
275: String value = (String) expected.get(key);
276: if (results.containsKey(key)) {
277: String resultVal = (String) results.get(key);
278: if (!value.equals(resultVal)) {
279: assertTrue("Using wsdl "+wsdl+" results did not contain correct value for type "+key
280: + ", value should be "+value+" but result was "+resultVal, false);
281: }
282: } else {
283: assertTrue("Using wsdl "+wsdl+" results did not contain type "+key, false);
284: }
285: }
286: }
287: }
|