1 57 58 package jndi; 59 60 import java.util.Iterator ; 61 62 import javax.naming.Context ; 63 import javax.naming.InitialContext ; 64 import javax.naming.NameAlreadyBoundException ; 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 82 public class JNDIAddressBookTest extends TestCase { 83 String wsdlLocation = 84 TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice") 85 + "AddressBook.wsdl"; 86 static String server = TestUtilities.getSoapServer().toUpperCase(); 87 88 static String 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 firstName2 = "Someone"; 99 static String 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 name) { 110 super(name); 111 } 112 113 public static void main(String [] 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 startingContext = new InitialContext (); 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 ignore) { 154 } catch (Exception 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 portName = ""; 170 try { 171 InitialContext ic = new InitialContext (); 172 173 WSIFService service = (WSIFService) ic.lookup("comp/env/wsif/addressservice"); 175 176 Iterator it = service.getAvailablePortNames(); 177 { 178 System.out.println("What ports does this service have?"); 179 while (it.hasNext()) { 180 String p = (String ) 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 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 portName) { 219 if (!TestUtilities.areWeTesting("jndi")) 220 return; 221 try { 222 InitialContext ic = new InitialContext (); 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 e) { 236 System.err.println( 237 "JNDIAddressBookTest(" + portName + ") caught exception " + e); 238 e.printStackTrace(); 239 assertTrue(false); 240 } 241 } 242 } 243 | Popular Tags |