KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jndi > JNDIAddressBookTest


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 jndi;
59
60 import java.util.Iterator JavaDoc;
61
62 import javax.naming.Context JavaDoc;
63 import javax.naming.InitialContext JavaDoc;
64 import javax.naming.NameAlreadyBoundException JavaDoc;
65 import junit.framework.Test;
66 import junit.framework.TestCase;
67 import junit.framework.TestSuite;
68 import junit.textui.TestRunner;
69 import org.apache.wsif.WSIFService;
70 import org.apache.wsif.naming.WSIFServiceRef;
71 import org.apache.wsif.naming.WSIFServiceStubRef;
72 import addressbook.wsifservice.AddressBook;
73 import addressbook.wsiftypes.Address;
74 import addressbook.wsiftypes.Phone;
75 import util.AddressUtility;
76 import util.TestUtilities;
77
78 /**
79  * Junit test to test using JNDI to retrieve a service.
80  * @author Owen Burroughs <owenb@apache.org>
81  */

82 public class JNDIAddressBookTest extends TestCase {
83     String JavaDoc wsdlLocation =
84         TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice")
85             + "AddressBook.wsdl";
86     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
87     
88     static String JavaDoc name1 = "Purdue Boilermaker";
89     static Address addr1 =
90         new Address(
91             1,
92             "University Drive",
93             "West Lafayette",
94             "IN",
95             47907,
96             new Phone(765, "494", "4900"));
97
98     static String JavaDoc firstName2 = "Someone";
99     static String JavaDoc lastName2 = "Else";
100     static Address addr2 =
101         new Address(
102             0,
103             "Somewhere Else",
104             "No Where",
105             "NO",
106             71983,
107             new Phone(600, "391", "5682"));
108
109     public JNDIAddressBookTest(String JavaDoc name) {
110         super(name);
111     }
112
113     public static void main(String JavaDoc[] args) {
114         TestUtilities.startListeners(
115             TestUtilities.ADDRESSBOOK_LISTENER);
116         junit.textui.TestRunner.run(suite());
117         TestUtilities.stopListeners();
118     }
119
120     public static Test suite() {
121         return new TestSuite(JNDIAddressBookTest.class);
122     }
123
124     public void setUp() {
125         if (!TestUtilities.areWeTesting("jndi"))
126             return;
127         TestUtilities.setUpExtensionsAndProviders();
128         try {
129             Context JavaDoc startingContext = new InitialContext JavaDoc();
130             WSIFJndiHelper.setInitialContext(startingContext);
131             WSIFJndiHelper.bindService(
132                 new WSIFServiceRef(
133                     TestUtilities.getWsdlPath("java\\test\\jndi")
134                         + "JNDIAddressBook.wsdl",
135                     "http://wsifservice.addressbook/",
136                     "AddressBookService",
137                     "http://wsifservice.addressbook/",
138                     "AddressBook"),
139                 "comp/env/wsif/addressservice");
140             System.out.println("Binding of service using JNDI successful");
141             WSIFJndiHelper.bindStub(
142                 new WSIFServiceStubRef(
143                     TestUtilities.getWsdlPath("java\\test\\jndi")
144                         + "JNDIAddressBook.wsdl",
145                     "http://wsifservice.addressbook/",
146                     "AddressBookService",
147                     "http://wsifservice.addressbook/",
148                     "AddressBook",
149                     "SOAPPort",
150                     "addressbook.wsifservice.AddressBook"),
151                 "comp/env/wsif/addressservicestub");
152             System.out.println("Binding of service stub using JNDI successful");
153         } catch (NameAlreadyBoundException JavaDoc ignore) {
154         } catch (Exception JavaDoc e) {
155             System.out.println("Error binding server using JNDI: " + e);
156         }
157     }
158
159     public void testAllPorts() {
160         doit();
161     }
162     public void testAxisStub() {
163         doitStub(server+"Port");
164     }
165
166     private void doit() {
167         if (!TestUtilities.areWeTesting("jndi"))
168             return;
169         String JavaDoc portName = "";
170         try {
171             InitialContext JavaDoc ic = new InitialContext JavaDoc();
172
173             // Lookup jndi service name
174
WSIFService service = (WSIFService) ic.lookup("comp/env/wsif/addressservice");
175
176             Iterator JavaDoc it = service.getAvailablePortNames();
177             {
178                 System.out.println("What ports does this service have?");
179                 while (it.hasNext()) {
180                     String JavaDoc p = (String JavaDoc) it.next();
181                     portName = p;
182                     System.out.print("Port found named " + p);
183                     if (p.toUpperCase().indexOf("JMS") >= 0
184                         && !TestUtilities.areWeTesting("jms")) {
185                         System.out.println(
186                             " - Port is a JMS port and I don't like JMS so I refuse to use it!");
187                     } else {
188                         if (("SOAPPort".equals(p) || "AXISPort".equals(p))
189                             && !p.equals(server + "Port"))
190                         {
191                             System.out.println(
192                                 "- not configured to use " + server + " port ");
193                             continue;
194                         }
195                         
196                         System.out.println(" - I like this port, I think I'll use it!");
197                         AddressBook abStub = (AddressBook) service.getStub(p, AddressBook.class);
198
199                         abStub.addEntry(name1, addr1);
200                         abStub.addEntry(firstName2, lastName2, addr2);
201
202                         Address resp1 = abStub.getAddressFromName(name1);
203                         assertTrue(new AddressUtility(addr1).equals(resp1));
204
205                         Address resp2 = abStub.getAddressFromName(firstName2 + " " + lastName2);
206                         assertTrue(new AddressUtility(addr2).equals(resp2));
207                     }
208                 }
209             }
210         } catch (Exception JavaDoc e) {
211             System.err.println(
212                 "JNDIAddressBookTest(" + portName + ") caught exception " + e);
213             e.printStackTrace();
214             assertTrue(false);
215         }
216     }
217
218     private void doitStub(String JavaDoc portName) {
219         if (!TestUtilities.areWeTesting("jndi"))
220             return;
221         try {
222             InitialContext JavaDoc ic = new InitialContext JavaDoc();
223
224             AddressBook abStub =
225                 (AddressBook) ic.lookup("comp/env/wsif/addressservicestub");
226
227             abStub.addEntry(name1, addr1);
228             abStub.addEntry(firstName2, lastName2, addr2);
229
230             Address resp1 = abStub.getAddressFromName(name1);
231             assertTrue(new AddressUtility(addr1).equals(resp1));
232
233             Address resp2 = abStub.getAddressFromName(firstName2 + " " + lastName2);
234             assertTrue(new AddressUtility(addr2).equals(resp2));
235         } catch (Exception JavaDoc e) {
236             System.err.println(
237                 "JNDIAddressBookTest(" + portName + ") caught exception " + e);
238             e.printStackTrace();
239             assertTrue(false);
240         }
241     }
242 }
243
Popular Tags