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 jndi;
059:
060: import java.util.Iterator;
061:
062: import javax.naming.Context;
063: import javax.naming.InitialContext;
064: import javax.naming.NameAlreadyBoundException;
065: import junit.framework.Test;
066: import junit.framework.TestCase;
067: import junit.framework.TestSuite;
068: import junit.textui.TestRunner;
069: import org.apache.wsif.WSIFService;
070: import org.apache.wsif.naming.WSIFServiceRef;
071: import org.apache.wsif.naming.WSIFServiceStubRef;
072: import addressbook.wsifservice.AddressBook;
073: import addressbook.wsiftypes.Address;
074: import addressbook.wsiftypes.Phone;
075: import util.AddressUtility;
076: import util.TestUtilities;
077:
078: /**
079: * Junit test to test using JNDI to retrieve a service.
080: * @author Owen Burroughs <owenb@apache.org>
081: */
082: public class JNDIAddressBookTest extends TestCase {
083: String wsdlLocation = TestUtilities
084: .getWsdlPath("java\\test\\addressbook\\wsifservice")
085: + "AddressBook.wsdl";
086: static String server = TestUtilities.getSoapServer().toUpperCase();
087:
088: static String name1 = "Purdue Boilermaker";
089: static Address addr1 = new Address(1, "University Drive",
090: "West Lafayette", "IN", 47907,
091: new Phone(765, "494", "4900"));
092:
093: static String firstName2 = "Someone";
094: static String lastName2 = "Else";
095: static Address addr2 = new Address(0, "Somewhere Else", "No Where",
096: "NO", 71983, new Phone(600, "391", "5682"));
097:
098: public JNDIAddressBookTest(String name) {
099: super (name);
100: }
101:
102: public static void main(String[] args) {
103: TestUtilities
104: .startListeners(TestUtilities.ADDRESSBOOK_LISTENER);
105: junit.textui.TestRunner.run(suite());
106: TestUtilities.stopListeners();
107: }
108:
109: public static Test suite() {
110: return new TestSuite(JNDIAddressBookTest.class);
111: }
112:
113: public void setUp() {
114: if (!TestUtilities.areWeTesting("jndi"))
115: return;
116: TestUtilities.setUpExtensionsAndProviders();
117: try {
118: Context startingContext = new InitialContext();
119: WSIFJndiHelper.setInitialContext(startingContext);
120: WSIFJndiHelper.bindService(new WSIFServiceRef(TestUtilities
121: .getWsdlPath("java\\test\\jndi")
122: + "JNDIAddressBook.wsdl",
123: "http://wsifservice.addressbook/",
124: "AddressBookService",
125: "http://wsifservice.addressbook/", "AddressBook"),
126: "comp/env/wsif/addressservice");
127: System.out
128: .println("Binding of service using JNDI successful");
129: WSIFJndiHelper.bindStub(new WSIFServiceStubRef(
130: TestUtilities.getWsdlPath("java\\test\\jndi")
131: + "JNDIAddressBook.wsdl",
132: "http://wsifservice.addressbook/",
133: "AddressBookService",
134: "http://wsifservice.addressbook/", "AddressBook",
135: "SOAPPort", "addressbook.wsifservice.AddressBook"),
136: "comp/env/wsif/addressservicestub");
137: System.out
138: .println("Binding of service stub using JNDI successful");
139: } catch (NameAlreadyBoundException ignore) {
140: } catch (Exception e) {
141: System.out.println("Error binding server using JNDI: " + e);
142: }
143: }
144:
145: public void testAllPorts() {
146: doit();
147: }
148:
149: public void testAxisStub() {
150: doitStub(server + "Port");
151: }
152:
153: private void doit() {
154: if (!TestUtilities.areWeTesting("jndi"))
155: return;
156: String portName = "";
157: try {
158: InitialContext ic = new InitialContext();
159:
160: // Lookup jndi service name
161: WSIFService service = (WSIFService) ic
162: .lookup("comp/env/wsif/addressservice");
163:
164: Iterator it = service.getAvailablePortNames();
165: {
166: System.out
167: .println("What ports does this service have?");
168: while (it.hasNext()) {
169: String p = (String) it.next();
170: portName = p;
171: System.out.print("Port found named " + p);
172: if (p.toUpperCase().indexOf("JMS") >= 0
173: && !TestUtilities.areWeTesting("jms")) {
174: System.out
175: .println(" - Port is a JMS port and I don't like JMS so I refuse to use it!");
176: } else {
177: if (("SOAPPort".equals(p) || "AXISPort"
178: .equals(p))
179: && !p.equals(server + "Port")) {
180: System.out
181: .println("- not configured to use "
182: + server + " port ");
183: continue;
184: }
185:
186: System.out
187: .println(" - I like this port, I think I'll use it!");
188: AddressBook abStub = (AddressBook) service
189: .getStub(p, AddressBook.class);
190:
191: abStub.addEntry(name1, addr1);
192: abStub.addEntry(firstName2, lastName2, addr2);
193:
194: Address resp1 = abStub
195: .getAddressFromName(name1);
196: assertTrue(new AddressUtility(addr1)
197: .equals(resp1));
198:
199: Address resp2 = abStub
200: .getAddressFromName(firstName2 + " "
201: + lastName2);
202: assertTrue(new AddressUtility(addr2)
203: .equals(resp2));
204: }
205: }
206: }
207: } catch (Exception e) {
208: System.err.println("JNDIAddressBookTest(" + portName
209: + ") caught exception " + e);
210: e.printStackTrace();
211: assertTrue(false);
212: }
213: }
214:
215: private void doitStub(String portName) {
216: if (!TestUtilities.areWeTesting("jndi"))
217: return;
218: try {
219: InitialContext ic = new InitialContext();
220:
221: AddressBook abStub = (AddressBook) ic
222: .lookup("comp/env/wsif/addressservicestub");
223:
224: abStub.addEntry(name1, addr1);
225: abStub.addEntry(firstName2, lastName2, addr2);
226:
227: Address resp1 = abStub.getAddressFromName(name1);
228: assertTrue(new AddressUtility(addr1).equals(resp1));
229:
230: Address resp2 = abStub.getAddressFromName(firstName2 + " "
231: + lastName2);
232: assertTrue(new AddressUtility(addr2).equals(resp2));
233: } catch (Exception e) {
234: System.err.println("JNDIAddressBookTest(" + portName
235: + ") caught exception " + e);
236: e.printStackTrace();
237: assertTrue(false);
238: }
239: }
240: }
|