1 package test.wsdl.multithread; 2 3 import junit.framework.AssertionFailedError; 4 import junit.framework.TestCase; 5 import org.apache.axis.AxisFault; 6 import org.apache.axis.components.logger.LogFactory; 7 import org.apache.commons.logging.Log; 8 import samples.addr.Address; 9 import samples.addr.AddressBook; 10 import samples.addr.AddressBookSOAPBindingStub; 11 import samples.addr.AddressBookServiceLocator; 12 import samples.addr.Phone; 13 import samples.addr.StateType; 14 15 import javax.xml.rpc.ServiceException ; 16 import java.net.ConnectException ; 17 18 25 26 public class MultithreadTestCase extends TestCase { 27 private static Log log = 28 LogFactory.getLog(MultithreadTestCase.class.getName()); 29 30 private AddressBook binding; 31 private static int successCount = 0; 32 33 static synchronized void addSuccess() 34 { 35 successCount++; 36 } 37 38 public MultithreadTestCase(String name) { 39 super(name); 40 } 41 42 private String printAddress (Address ad) { 43 String out; 44 if (ad == null) 45 out = "\t[ADDRESS NOT FOUND!]"; 46 else 47 out ="\t" + ad.getStreetNum () + " " + ad.getStreetName () + "\n\t" + ad.getCity () + ", " + ad.getState () + " " + ad.getZip () + "\n\t" + printPhone (ad.getPhoneNumber ()); 48 return out; 49 } 51 private String printPhone (Phone ph) 52 { 53 String out; 54 if (ph == null) 55 out = "[PHONE NUMBER NOT FOUND!]"; 56 else 57 out ="Phone: (" + ph.getAreaCode () + ") " + ph.getExchange () + "-" + ph.getNumber (); 58 return out; 59 } 61 private AssertionFailedError error = null; 62 63 private synchronized void setError(AssertionFailedError error) { 64 if (this.error == null) { 65 this.error = error; 66 } 67 } 69 private static int var = 0; 70 71 public class Run implements Runnable { 72 public void run() { 73 try { 74 for (int i = 0; i < 4; ++i) { 75 Address address = new Address(); 76 Phone phone = new Phone(); 77 address.setStreetNum(var++); 78 address.setStreetName("2"); 79 address.setCity("3"); 80 address.setState(StateType.TX); 81 address.setZip(var++); 82 phone.setAreaCode(11); 83 phone.setExchange("22"); 84 phone.setNumber("33"); 85 address.setPhoneNumber(phone); 86 87 binding.addEntry("hi", address); 88 Address addressRet = binding.getAddressFromName("hi"); 89 addSuccess(); 91 } 92 } catch (Throwable t) { 93 if (!(t instanceof AxisFault && 98 ((AxisFault) t).detail instanceof ConnectException )) { 99 100 log.fatal("Throwable caught: ", t); 102 103 setError(new AssertionFailedError("Throwable caught: " + t)); 104 } 105 } 106 } } 109 public void testMultithreading() { 110 try { 111 binding = new AddressBookServiceLocator().getAddressBook(); 112 } 113 catch (ServiceException jre) { 114 throw new AssertionFailedError("ServiceException caught: " + jre); 115 } 116 assertTrue("binding is null", binding != null); 117 ((AddressBookSOAPBindingStub) binding).setMaintainSession(true); 118 int NUM_THREADS = 50; 119 Thread [] threads = new Thread [NUM_THREADS]; 120 for (int i = 0; i < NUM_THREADS; ++i) { 121 threads[i] = new Thread (new Run()); 122 threads[i].start(); 123 } 124 for (int i = 0; i < NUM_THREADS; ++i) { 125 try { 126 threads[i].join(); 127 } 128 catch (InterruptedException ie) { 129 } 130 } 131 System.out.println("Had " + successCount + 132 " successes (of a possible " + 133 (NUM_THREADS * 4) + ")"); 134 if (error != null) { 135 throw error; 136 } 137 } 139 public static void main(String [] args) { 140 MultithreadTestCase testCase = new MultithreadTestCase("MultithreadTestCase"); 141 testCase.testMultithreading(); 142 } 143 } 145 | Popular Tags |