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 wsdl;
059:
060: /**
061: * Junit test to test using ClassLoader to find wsdl and more importantly
062: * imports in the wsdl
063: *
064: * -----------------------------------------------------------------
065: * NOTE: YOU MUST HAVE THE ClassloaderTest.jar ON YOUR CLASSPATH TO
066: * BE ABLE TO RUN THIS TEST
067: * -----------------------------------------------------------------
068: *
069: * @author Owen Burroughs
070: */
071:
072: import java.io.InputStream;
073: import java.io.InputStreamReader;
074: import java.io.Reader;
075: import java.net.URL;
076:
077: import javax.wsdl.Definition;
078: import junit.framework.Test;
079: import junit.framework.TestCase;
080: import junit.framework.TestSuite;
081:
082: import org.apache.wsif.WSIFService;
083: import org.apache.wsif.WSIFServiceFactory;
084: import org.apache.wsif.util.WSIFUtils;
085: import util.TestUtilities;
086:
087: public class ClassLoaderTest extends TestCase {
088: String wsdlLocation = "test/ImportingTest.wsdl";
089: String wsdlLocation2 = "test/ImportingTest2.wsdl";
090: String wsdlLocation3 = "test/ImportingTest3.wsdl";
091: String wsdlLocation4 = "ImportingTest4.wsdl";
092: String wsdlLocation5 = "test/subtest/ImportingTest5.wsdl";
093: String wsdlLocation6 = "test/subtest/subtest2/ImportingTest6.wsdl";
094: String wsdlLocation7 = TestUtilities
095: .getWsdlPath("java\\test\\addressbook")
096: + "ImportingAddressBook.wsdl";
097: String wsdlLocationError = "test/subtest/subtest2/ImportingTestError.wsdl";
098: String wsdlLocationA = "ImportingTestA.wsdl";
099:
100: public ClassLoaderTest(String name) {
101: super (name);
102: }
103:
104: public static void main(String[] 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 wsdlLoc) {
178: try {
179: WSIFServiceFactory factory = WSIFServiceFactory
180: .newInstance();
181: WSIFService service = factory.getService(wsdlLoc, this
182: .getClass().getClassLoader(), null,
183: // serviceNS
184: null, // serviceName
185: "http://wsifservice.addressbook/", // portTypeNS
186: "AddressBook"); // portTypeName
187: } catch (Exception e) {
188: System.err.println("ClassLoaderTest(" + wsdlLoc
189: + ") caught exception " + e);
190: e.printStackTrace();
191: assertTrue(false);
192: }
193: }
194:
195: private void doItError(String wsdlLoc) {
196: try {
197: WSIFServiceFactory factory = WSIFServiceFactory
198: .newInstance();
199: WSIFService service = factory.getService(wsdlLoc, this
200: .getClass().getClassLoader(), null,
201: // serviceNS
202: null, // serviceName
203: "http://wsifservice.addressbook/", // portTypeNS
204: "AddressBook"); // portTypeName
205: assertTrue(
206: "Import location of '../../../../AddressBook.wsdl'"
207: + "should have caused an error", false);
208: } catch (Exception e) {
209: // Excpected so ignore
210: }
211: }
212:
213: private void doItWithReader(String wsdlLoc) {
214: InputStream in = null;
215: ClassLoader loader = this .getClass().getClassLoader();
216: try {
217: URL url = null;
218: if (wsdlLoc.indexOf(":") == -1)
219: url = new URL("file", null, wsdlLoc);
220: else
221: url = new URL(wsdlLoc);
222: String wsdlRelativeLocation = url.getPath();
223: if (wsdlRelativeLocation.startsWith("/"))
224: wsdlRelativeLocation = wsdlRelativeLocation
225: .substring(1);
226: in = loader.getResourceAsStream(wsdlRelativeLocation);
227: Reader reader = new InputStreamReader(in);
228: Definition def = WSIFUtils.readWSDL(url, reader, loader);
229: assertNotNull("Definition is null!!", def);
230: } catch (Exception e) {
231: System.err.println("ClassLoaderTest(" + wsdlLoc
232: + ") caught exception " + e);
233: assertTrue(false);
234: }
235: }
236: }
|