KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wsdl > ClassLoaderTest


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 wsdl;
59
60 /**
61  * Junit test to test using ClassLoader to find wsdl and more importantly
62  * imports in the wsdl
63  *
64  * -----------------------------------------------------------------
65  * NOTE: YOU MUST HAVE THE ClassloaderTest.jar ON YOUR CLASSPATH TO
66  * BE ABLE TO RUN THIS TEST
67  * -----------------------------------------------------------------
68  *
69  * @author Owen Burroughs
70  */

71
72 import java.io.InputStream JavaDoc;
73 import java.io.InputStreamReader JavaDoc;
74 import java.io.Reader JavaDoc;
75 import java.net.URL JavaDoc;
76
77 import javax.wsdl.Definition;
78 import junit.framework.Test;
79 import junit.framework.TestCase;
80 import junit.framework.TestSuite;
81
82 import org.apache.wsif.WSIFService;
83 import org.apache.wsif.WSIFServiceFactory;
84 import org.apache.wsif.util.WSIFUtils;
85 import util.TestUtilities;
86
87 public class ClassLoaderTest extends TestCase {
88     String JavaDoc wsdlLocation = "test/ImportingTest.wsdl";
89     String JavaDoc wsdlLocation2 = "test/ImportingTest2.wsdl";
90     String JavaDoc wsdlLocation3 = "test/ImportingTest3.wsdl";
91     String JavaDoc wsdlLocation4 = "ImportingTest4.wsdl";
92     String JavaDoc wsdlLocation5 = "test/subtest/ImportingTest5.wsdl";
93     String JavaDoc wsdlLocation6 = "test/subtest/subtest2/ImportingTest6.wsdl";
94     String JavaDoc wsdlLocation7 =
95         TestUtilities.getWsdlPath("java\\test\\addressbook")
96             + "ImportingAddressBook.wsdl";
97     String JavaDoc wsdlLocationError = "test/subtest/subtest2/ImportingTestError.wsdl";
98     String JavaDoc wsdlLocationA = "ImportingTestA.wsdl";
99
100     public ClassLoaderTest(String JavaDoc name) {
101         super(name);
102     }
103
104     public static void main(String JavaDoc[] args) {
105         junit.textui.TestRunner.run(suite());
106     }
107
108     public static Test suite() {
109         return new TestSuite(ClassLoaderTest.class);
110     }
111
112     public void setUp() {
113         TestUtilities.setUpExtensionsAndProviders();
114     }
115
116     // wsdl location = test/ImportingTest.wsdl
117
// import location = AddressBookTest.wsdl
118
public void test() {
119         doIt(wsdlLocation);
120     }
121
122     // wsdl location = test/ImportingTest2.wsdl
123
// import location = /test/subtest/subtest2/AddressBookTest2.wsdl
124
public void test2() {
125         doIt(wsdlLocation2);
126     }
127
128     // wsdl location = test/ImportingTest3.wsdl
129
// import location = subtest/subtest2/AddressBookTest3.wsdl
130
public void test3() {
131         doIt(wsdlLocation3);
132     }
133
134     // wsdl location = ImportingTest4.wsdl
135
// import location = test/subtest/subtest2/AddressBookTest4.wsdl
136
public void test4() {
137         doIt(wsdlLocation4);
138     }
139
140     // wsdl location = test/subtest/ImportingTest5.wsdl
141
// import location = ../AddressBookTest.wsdl
142
public void test5() {
143         doIt(wsdlLocation5);
144     }
145
146     // wsdl location = test/subtest/subtest2/ImportingTest6.wsdl
147
// import location = ../../AddressBookTest.wsdl
148
public void test6() {
149         doIt(wsdlLocation6);
150     }
151
152     // wsdl location = <wsdlPath>/java/test/addressbook/ImportingAddressBook.wsdl
153
// import location = http://localhost:8080/wsdl/AddressBook.wsdl
154
public void test7() {
155         if (TestUtilities.areWeTesting("remotewsdl")) {
156             doIt(wsdlLocation7);
157         }
158     }
159     
160     public void test8() {
161         doItWithReader(wsdlLocation2);
162     }
163
164     // wsdl location = ImportingTestA.wsdl
165
// import location = /test/ImportingTest.wsdl
166
// import location2 = AddressBookTest.wsdl
167
public void test9() {
168         doIt(wsdlLocationA);
169     }
170
171     // wsdl location = test/subtest/subtest2/ImportingTestError.wsdl
172
// import location = ../../../../AddressBookTest.wsdl
173
public void testError() {
174         doItError(wsdlLocationError);
175     }
176
177     private void doIt(String JavaDoc wsdlLoc) {
178         try {
179             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
180             WSIFService service =
181                 factory
182                     .getService(wsdlLoc, this.getClass().getClassLoader(), null,
183                                 // serviceNS
184
null, // serviceName
185
"http://wsifservice.addressbook/", // portTypeNS
186
"AddressBook"); // portTypeName
187
} catch (Exception JavaDoc e) {
188             System.err.println(
189                 "ClassLoaderTest(" + wsdlLoc + ") caught exception " + e);
190             e.printStackTrace();
191             assertTrue(false);
192         }
193     }
194
195     private void doItError(String JavaDoc wsdlLoc) {
196         try {
197             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
198             WSIFService service =
199                 factory
200                     .getService(wsdlLoc, this.getClass().getClassLoader(), null,
201                                 // serviceNS
202
null, // serviceName
203
"http://wsifservice.addressbook/", // portTypeNS
204
"AddressBook"); // portTypeName
205
assertTrue("Import location of '../../../../AddressBook.wsdl'" +
206                         "should have caused an error", false);
207         } catch (Exception JavaDoc e) {
208             // Excpected so ignore
209
}
210     }
211
212     private void doItWithReader(String JavaDoc wsdlLoc) {
213         InputStream JavaDoc in = null;
214         ClassLoader JavaDoc loader = this.getClass().getClassLoader();
215         try {
216             URL JavaDoc url = null;
217             if (wsdlLoc.indexOf(":") == -1)
218                 url = new URL JavaDoc("file", null, wsdlLoc);
219             else
220                 url = new URL JavaDoc(wsdlLoc);
221             String JavaDoc wsdlRelativeLocation = url.getPath();
222             if (wsdlRelativeLocation.startsWith("/"))
223                 wsdlRelativeLocation = wsdlRelativeLocation.substring(1);
224             in = loader.getResourceAsStream(wsdlRelativeLocation);
225             Reader JavaDoc reader = new InputStreamReader JavaDoc(in);
226             Definition def = WSIFUtils.readWSDL(url, reader, loader);
227             assertNotNull("Definition is null!!", def);
228         } catch (Exception JavaDoc e) {
229             System.err.println(
230                 "ClassLoaderTest(" + wsdlLoc + ") caught exception " + e);
231             assertTrue(false);
232         }
233     }
234 }
235
Popular Tags