KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > schema > SchemaTest


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package schema;
59
60 import java.util.Enumeration;
61 import java.util.Hashtable;
62
63 import javax.wsdl.Definition;
64 import javax.xml.namespace.QName;
65 import junit.framework.Test;
66 import junit.framework.TestCase;
67 import junit.framework.TestSuite;
68
69 import org.apache.wsif.schema.Parser;
70 import org.apache.wsif.util.WSIFUtils;
71 import org.apache.wsif.wsdl.WSIFWSDLLocatorImpl;
72
73 import util.TestUtilities;
74
75 /**
76  * Junit test to test parsing of Schemas to generate xml -> Java mappings
77  * @author Owen Burroughs
78  */

79 public class SchemaTest extends TestCase {
80     String wsdlLocation1 = TestUtilities.getWsdlPath("java\\test\\schema") + "SchemaTest1.wsdl";
81     String wsdlLocation2 = TestUtilities.getWsdlPath("java\\test\\schema") + "SchemaTest2.wsdl";
82     String wsdlLocation3 = TestUtilities.getWsdlPath("java\\test\\schema") + "SchemaTest3.wsdl";
83
84     Hashtable expectedA = new Hashtable();
85     Hashtable expectedB = new Hashtable();
86         
87     public SchemaTest(String name) {
88         super(name);
89     }
90
91     public static void main(String[] args) {
92         junit.textui.TestRunner.run(suite());
93     }
94
95     public static Test suite() {
96         return new TestSuite(SchemaTest.class);
97     }
98
99     public void setUp() {
100         TestUtilities.setUpExtensionsAndProviders();
101         expectedA.put(
102             new QName("http://types.wsiftest/", "addresses"),
103             "[Lwsiftest.types.Address;");
104         expectedA.put(
105             new QName("http://types.wsiftest/", "InfoResponse"),
106             "wsiftest.types.InfoResponseElement");
107         expectedA.put(
108             new QName("http://types.wsiftest/", "ArrayOfArrayOfAddress"),
109             "[[Lwsiftest.types.Address;");
110         expectedA.put(
111             new QName("http://types.wsiftest/", "phone"),
112             "wsiftest.types.Phone");
113         expectedA.put(
114             new QName("http://types.wsiftest/", "ArrayOfLong"),
115             "[[J");
116         expectedA.put(
117             new QName("http://types.wsiftest/", "address"),
118             "wsiftest.types.Address");
119         expectedA.put(
120             new QName("http://types.wsiftest/", "InfoRequest"),
121             "wsiftest.types.InfoRequestElement");
122         expectedA.put(new QName("http://types.wsiftest/", "ArrayOfInt"), "[I");
123         expectedA.put(
124             new QName("http://types.wsiftest/", "ArrayOfAddress"),
125             "[Lwsiftest.types.Address;");
126             
127             
128         // For nested xsd files namepsaces/results are slightly different
129
expectedB.put(
130             new QName("http://types.wsiftest/", "addresses"),
131             "[Lwsiftest.types2.Address;");
132         expectedB.put(
133             new QName("http://types.wsiftest/", "InfoResponse"),
134             "wsiftest.types.InfoResponseElement");
135         expectedB.put(
136             new QName("http://types2.wsiftest/", "ArrayOfArrayOfAddress"),
137             "[[Lwsiftest.types2.Address;");
138         expectedB.put(
139             new QName("http://types2.wsiftest/", "phone"),
140             "wsiftest.types2.Phone");
141         expectedB.put(
142             new QName("http://types.wsiftest/", "ArrayOfLong"),
143             "[[J");
144         expectedB.put(
145             new QName("http://types2.wsiftest/", "address"),
146             "wsiftest.types2.Address");
147         expectedB.put(
148             new QName("http://types.wsiftest/", "InfoRequest"),
149             "wsiftest.types.InfoRequestElement");
150         expectedB.put(new QName("http://types.wsiftest/", "ArrayOfInt"), "[I");
151         expectedB.put(
152             new QName("http://types2.wsiftest/", "ArrayOfAddress"),
153             "[Lwsiftest.types2.Address;");
154     }
155
156     public void testParsing() {
157         doIt(wsdlLocation1, expectedA);
158         doIt(wsdlLocation2, expectedA);
159         doIt(wsdlLocation3, expectedB);
160     }
161
162
163     private void doIt(String wsdl, Hashtable expectedResults) {
164         try {
165             System.out.println("*** " + wsdl + " ***\n");
166             Definition def = WSIFUtils.readWSDL(null, wsdl);
167             Hashtable table = new Hashtable();
168             Parser.getTypeMappings(def, table, false);
169             if (table != null) {
170                 System.out.println("WITHOUT STANDARD MAPPINGS:");
171                 Enumeration k = table.keys();
172                 while(k.hasMoreElements()) {
173                     QName qn = (QName) k.nextElement();
174                     String cl = (String) table.get(qn);
175                     System.out.println(qn + " -> " + cl);
176                 }
177             } else {
178                 System.out.println("WITHOUT STANDARD MAPPINGS RETURNED NULL!!");
179                 assertTrue("With wsdl "+wsdl+", table without standard mappings was null", false);
180             }
181             checkResult(wsdl, table, expectedResults);
182             System.out.println("- - - -");
183             Hashtable table2 = new Hashtable();
184             Parser.getTypeMappings(def, table2);
185             if (table2 != null) {
186                 System.out.println("WITH STANDARD MAPPINGS:");
187                 Enumeration k = table2.keys();
188                 while(k.hasMoreElements()) {
189                     QName qn = (QName) k.nextElement();
190                     String cl = (String) table2.get(qn);
191                     System.out.println(qn + " -> " + cl);
192                 }
193             } else {
194                 System.out.println("WITH STANDARD MAPPINGS RETURNED NULL!!");
195                 assertTrue("With wsdl "+wsdl+", table with standard mappings was null", false);
196             }
197             checkResult(wsdl, table2, expectedResults);
198             System.out.println("-----------------------------------------------------\n");
199         } catch (Exception e) {
200             System.err.println(
201                 "SchemaTest caught exception " + e);
202             e.printStackTrace();
203             assertTrue(false);
204         } finally {
205             TestUtilities.resetDefaultProviders();
206         }
207     }
208     
209     private void doItWithClassLoader(String wsdl, Hashtable expectedResults) {
210         try {
211             System.out.println("*** " + wsdl + " ***\n");
212             ClassLoader loader = this.getClass().getClassLoader();
213             Definition def = WSIFUtils.readWSDL(null, wsdl, loader);
214             Hashtable table = new Hashtable();
215             Parser.getTypeMappings(def, table, loader, false);
216             if (table != null) {
217                 System.out.println("WITHOUT STANDARD MAPPINGS:");
218                 Enumeration k = table.keys();
219                 while(k.hasMoreElements()) {
220                     QName qn = (QName) k.nextElement();
221                     String cl = (String) table.get(qn);
222                     System.out.println(qn + " -> " + cl);
223                 }
224             } else {
225                 System.out.println("WITHOUT STANDARD MAPPINGS RETURNED NULL!!");
226                 assertTrue("With wsdl "+wsdl+" and using a classloader, table without standard mappings was null", false);
227             }
228             checkResult(wsdl, table, expectedResults);
229             System.out.println("- - - -");
230             Hashtable table2 = new Hashtable();
231             Parser.getTypeMappings(def, table2, loader, true);
232             if (table2 != null) {
233                 System.out.println("WITH STANDARD MAPPINGS:");
234                 Enumeration k = table2.keys();
235                 while(k.hasMoreElements()) {
236                     QName qn = (QName) k.nextElement();
237                     String cl = (String) table2.get(qn);
238                     System.out.println(qn + " -> " + cl);
239                 }
240             } else {
241                 System.out.println("WITH STANDARD MAPPINGS RETURNED NULL!!");
242                 assertTrue("With wsdl "+wsdl+" and using a classloader, table with standard mappings was null", false);
243             }
244             checkResult(wsdl, table2, expectedResults);
245             System.out.println("-----------------------------------------------------\n");
246         } catch (Exception e) {
247             System.err.println(
248                 "SchemaTest caught exception " + e);
249             e.printStackTrace();
250             assertTrue(false);
251         } finally {
252             TestUtilities.resetDefaultProviders();
253         }
254     }
255     
256     private void checkResult(String wsdl, Hashtable results, Hashtable expected) {
257         Enumeration enum = expected.keys();
258         while(enum.hasMoreElements()) {
259             QName key = (QName) enum.nextElement();
260             String value = (String) expected.get(key);
261             if (results.containsKey(key)) {
262                 String resultVal = (String) results.get(key);
263                 if (!value.equals(resultVal)) {
264                     assertTrue("Using wsdl "+wsdl+" results did not contain correct value for type "+key
265                     + ", value should be "+value+" but result was "+resultVal, false);
266                 }
267             } else {
268                 assertTrue("Using wsdl "+wsdl+" results did not contain type "+key, false);
269             }
270         }
271     }
272 }
273
Popular Tags