1 22 package org.jboss.ejb3.test.service; 23 24 25 import org.jboss.system.ServiceMBeanSupport; 26 27 import javax.naming.InitialContext ; 28 29 import java.io.File ; 30 import java.util.ArrayList ; 31 32 38 public class Tester extends ServiceMBeanSupport implements TesterMBean 39 { 40 public static ArrayList creates = new ArrayList (); 41 public static ArrayList starts = new ArrayList (); 42 43 public void testLocalServiceWithInterfaceAnnotation() throws Exception 44 { 45 final int count = 15; 46 final InitialContext ctx = new InitialContext (); 47 ServiceSevenLocal test = (ServiceSevenLocal) ctx.lookup("ServiceSeven/local"); 48 test.setLocalMethodCalls(0); 49 50 Thread [] threads = new Thread [count]; 51 for (int i = 0 ; i < count ; i++) 52 { 53 final int outer = i; 54 threads[i] = new Thread ( 55 new Runnable () 56 { 57 public void run() 58 { 59 try 60 { 61 ServiceSevenLocal test = (ServiceSevenLocal) ctx.lookup("ServiceSeven/local"); 62 for (int j = 0 ; j < count ; j++) 63 { 64 String s = outer + "_" + j; 65 test.localMethod(s); 67 } 68 } 69 catch(Exception e) 70 { 71 throw new RuntimeException (e); 72 } 73 } 74 } 75 ); 76 threads[i].start(); 77 } 78 79 Thread.sleep(5000); 80 for (int i = 0 ; i < count ; i++) 81 { 82 threads[i].join(); 83 } 84 85 if (test.getInstances() != 1) 86 { 87 throw new RuntimeException ("There should only ever be one instance of the service. We have " + test.getInstances()); 88 } 89 90 int localCalls = test.getLocalMethodCalls(); 91 if (localCalls != (count * count)) 92 { 93 throw new RuntimeException ("There should be " + count * count + " local method calls, not " + localCalls); 94 } 95 } 96 97 public void testServiceWithDefaultLocalJNDIName() throws Exception 98 { 99 final int count = 15; 100 final InitialContext ctx = new InitialContext (); 101 ServiceOneLocal test = (ServiceOneLocal) ctx.lookup("ServiceOne/local"); 102 test.setLocalMethodCalls(0); 103 104 Thread [] threads = new Thread [count]; 105 for (int i = 0 ; i < count ; i++) 106 { 107 final int outer = i; 108 threads[i] = new Thread ( 109 new Runnable () 110 { 111 public void run() 112 { 113 try 114 { 115 ServiceOneLocal test = (ServiceOneLocal) ctx.lookup("ServiceOne/local"); 116 for (int j = 0 ; j < count ; j++) 117 { 118 String s = outer + "_" + j; 119 test.localMethod(s); 121 } 122 } 123 catch(Exception e) 124 { 125 throw new RuntimeException (e); 126 } 127 } 128 } 129 ); 130 threads[i].start(); 131 } 132 133 Thread.sleep(5000); 134 for (int i = 0 ; i < count ; i++) 135 { 136 threads[i].join(); 137 } 138 139 if (test.getInstances() != 1) 140 { 141 throw new RuntimeException ("There should only ever be one instance of the service. We have " + test.getInstances()); 142 } 143 144 int localCalls = test.getLocalMethodCalls(); 145 if (localCalls != (count * count)) 146 { 147 throw new RuntimeException ("There should be " + count * count + " local method calls, not " + localCalls); 148 } 149 } 150 151 public void testServiceWithLocalBinding() throws Exception 152 { 153 InitialContext ctx = new InitialContext (); 154 ServiceTwoLocal test = (ServiceTwoLocal) ctx.lookup("serviceTwo/local"); 155 test.setCalled(false); 156 if (test.getCalled()) throw new RuntimeException ("Called should be false, not " + test.getCalled()); 157 test.localMethod(); 158 if (!test.getCalled()) throw new RuntimeException ("Called should be true, not " + test.getCalled()); 159 } 160 161 public void testDeploymentDescriptorServiceWithLocalBinding() throws Exception 162 { 163 InitialContext ctx = new InitialContext (); 164 ServiceSixLocal test = (ServiceSixLocal) ctx.lookup("serviceSix/local"); 165 test.setCalled(false); 166 if (test.getCalled()) throw new RuntimeException ("Called should be false, not " + test.getCalled()); 167 test.localMethod(); 168 if (!test.getCalled()) throw new RuntimeException ("Called should be true, not " + test.getCalled()); 169 } 170 171 public ArrayList getCreates() 172 { 173 return creates; 174 } 175 176 public ArrayList getStarts() 177 { 178 return starts; 179 } 180 181 } 182 | Popular Tags |