1 7 package org.jboss.remoting.oneway; 8 9 import org.apache.log4j.Level; 10 import org.jboss.dtf.MultipleTestRunner; 11 import org.jboss.logging.Logger; 12 import org.jboss.remoting.AbstractInvokerTest; 13 import org.jboss.remoting.Client; 14 import org.jboss.remoting.InvokerLocator; 15 import org.jboss.remoting.invocation.NameBasedInvocation; 16 17 import java.rmi.server.UID ; 18 19 24 public class OnewayInvokerClientTest extends AbstractInvokerTest 25 { 26 private static final Logger log = Logger.getLogger(OnewayInvokerClientTest.class); 27 28 private String sessionId = new UID ().toString(); 29 30 private Client client; 31 32 private static final String NAME = "OnewayInvokerClientTest.class"; 33 34 public OnewayInvokerClientTest(String name) 35 { 36 super(NAME); 37 } 38 39 public OnewayInvokerClientTest(int numberOfInstances) 40 { 41 super(NAME, numberOfInstances); 42 } 43 44 public OnewayInvokerClientTest(String transport, int port) 45 { 46 super(NAME, transport, port); 47 } 48 49 public OnewayInvokerClientTest(String transport, int port, int numberOfInstances) 50 { 51 super(NAME, transport, port, numberOfInstances); 52 } 53 54 public void init() 55 { 56 try 57 { 58 InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port); 59 client = new Client(locator, "test"); 60 client.connect(); 61 } 62 catch(Exception e) 63 { 64 log.error(e.getMessage(), e); 65 } 66 } 67 68 public void runInvokers() throws Throwable 69 { 70 startup(getNumberOfInstances()); 71 try 72 { 73 log.debug("\n\n*****************************" + 74 "Calling testOnewayServerInvocation" + 75 "*******************************\n"); 76 testOnewayServerInvocation(); 77 log.debug("\n\n*****************************" + 78 "Calling testOnewayClientInvocation" + 79 "*******************************\n"); 80 testOnewayClientInvocation(); 81 } 82 finally 83 { 84 shutdown(); 85 } 86 } 87 88 93 public void testOnewayServerInvocation() throws Throwable 94 { 95 try 96 { 97 log.debug("running testOnewayClientCallback()"); 98 99 sessionId = new UID ().toString(); 100 init(); 101 102 sessionId = client.getSessionId(); 103 104 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 105 106 String param = "bar"; 108 makeServerOnewayInvocation("saveInvocationParameter", param); 109 Thread.currentThread().sleep(1000); 110 Object obj = makeInvocation("getLastInvocationParameter", null); 111 112 checkAssertion(param, obj); 113 } 114 finally 115 { 116 if(client != null) 117 { 118 client.disconnect(); 119 } 120 } 121 } 122 123 protected void checkAssertion(String param, Object obj) 124 { 125 assertEquals(param, obj); 126 } 127 128 protected void makeServerOnewayInvocation(String method, String param) throws Throwable 129 { 130 client.invokeOneway(new NameBasedInvocation(method, 131 new Object []{param}, 132 new String []{String .class.getName()}), 133 null, 134 false); 135 136 } 137 138 143 public void testOnewayClientInvocation() throws Throwable 144 { 145 try 146 { 147 log.debug("running testOnewayClientCallback()"); 148 149 sessionId = new UID ().toString(); 150 init(); 151 152 sessionId = client.getSessionId(); 153 154 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 155 156 157 String param = "bar"; 159 makeClientOnewayInvocation("saveInvocationParameter", param); 160 Thread.currentThread().sleep(1000); 161 Object obj = makeInvocation("getLastInvocationParameter", null); 162 163 checkAssertion(param, obj); 164 165 } 166 finally 167 { 168 if(client != null) 169 { 170 client.disconnect(); 171 } 172 } 173 } 174 175 180 public void testClientInvocation() throws Throwable 181 { 182 try 183 { 184 log.debug("running testInvocation()"); 185 186 sessionId = new UID ().toString(); 187 init(); 188 189 sessionId = client.getSessionId(); 190 191 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 192 193 194 String param = "bar"; 196 Object resp = makeClientInvocation("saveInvocationParameter", param); 197 198 Object obj = makeInvocation("getLastInvocationParameter", null); 199 Thread.currentThread().sleep(1000); 200 checkAssertion(param, obj); 201 202 } 203 finally 204 { 205 if(client != null) 206 { 207 client.disconnect(); 208 } 209 } 210 } 211 212 protected void makeClientOnewayInvocation(String method, String param) throws Throwable 213 { 214 client.invokeOneway(new NameBasedInvocation(method, 215 new Object []{param}, 216 new String []{String .class.getName()}), 217 null, 218 true); 219 220 } 221 222 protected Object makeClientInvocation(String method, String param) throws Throwable 223 { 224 Object ret = client.invoke(new NameBasedInvocation(method, 225 new Object []{param}, 226 new String []{String .class.getName()}), 227 null); 228 229 return ret; 230 } 231 232 233 protected Object makeInvocation(String method, String param) throws Throwable 234 { 235 Object ret = client.invoke(new NameBasedInvocation(method, 236 new Object []{param}, 237 new String []{String .class.getName()}), 238 null); 239 240 return ret; 241 } 242 243 244 public static void main(String [] args) 245 { 246 247 org.apache.log4j.BasicConfigurator.configure(); 248 org.apache.log4j.Category.getRoot().setLevel(Level.INFO); 249 org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO); 250 org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG); 251 org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG); 252 253 OnewayInvokerClientTest client = null; 254 if(args.length == 1) 255 { 256 int instances = Integer.parseInt(args[0]); 257 client = new OnewayInvokerClientTest(instances); 258 } 259 else if(args.length == 2) 260 { 261 String transport = args[0]; 262 int port = Integer.parseInt(args[1]); 263 client = new OnewayInvokerClientTest(transport, port); 264 } 265 else if(args.length == 3) 266 { 267 String transport = args[0]; 268 int port = Integer.parseInt(args[1]); 269 int instances = Integer.parseInt(args[2]); 270 client = new OnewayInvokerClientTest(transport, port, instances); 271 } 272 else 273 { 274 client = new OnewayInvokerClientTest(OnewayInvokerClientTest.class.getName()); 275 System.out.println("Using default transport (" + client.getTransport() + 276 ") and default port (" + client.getPort() + ") and " + 277 "default number of instances (" + client.getNumberOfInstances() + ")" + 278 "\nCan enter transport, port, and instances via command line."); 279 } 280 281 try 282 { 283 MultipleTestRunner runner = new MultipleTestRunner(); 286 runner.doRun(client, true); 287 } 288 catch(Throwable e) 289 { 290 e.printStackTrace(); 291 System.exit(1); 292 } 293 System.exit(0); 294 } 295 } 296 | Popular Tags |