1 22 package org.jboss.ejb3.test.service.unit; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import javax.management.Attribute ; 27 import javax.management.AttributeList ; 28 import javax.management.MBeanServerConnection ; 29 import javax.management.ObjectName ; 30 31 import org.jboss.ejb3.test.service.ServiceOneRemote; 32 import org.jboss.ejb3.test.service.ServiceSevenRemote; 33 import org.jboss.ejb3.test.service.ServiceSixRemote; 34 import org.jboss.ejb3.test.service.ServiceTwoRemote; 35 import org.jboss.ejb3.test.service.SessionRemote; 36 import org.jboss.logging.Logger; 37 import org.jboss.security.SecurityAssociation; 38 import org.jboss.security.SimplePrincipal; 39 import org.jboss.test.JBossTestCase; 40 import junit.framework.Test; 41 42 48 49 public class ServiceUnitTestCase 50 extends JBossTestCase 51 { 52 private static final Logger log = Logger.getLogger(ServiceUnitTestCase.class); 53 54 static boolean deployed = false; 55 static int test = 0; 56 57 public ServiceUnitTestCase(String name) 58 { 59 60 super(name); 61 62 } 63 64 public void testEjbJar() throws Exception 65 { 66 SessionRemote session = (SessionRemote)getInitialContext().lookup("SessionBean/remote"); 67 assertNotNull(session); 68 } 69 70 public void testEjbInjection() throws Exception 71 { 72 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 73 SecurityAssociation.setCredential("password".toCharArray()); 74 75 ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote"); 76 test.testEjbInjection(); 77 } 78 79 public void testSecurityDomain() throws Exception 80 { 81 try 82 { 83 SecurityAssociation.clear(); 84 85 ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote"); 86 test.testEjbInjection(); 87 fail("Should have thrown EJBAccessException"); 88 } 89 catch (javax.ejb.EJBAccessException e) 90 { 91 92 } 93 } 94 95 public void testServiceWithDefaultRemoteJNDIName() throws Exception 96 { 97 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 98 SecurityAssociation.setCredential("password".toCharArray()); 99 100 ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote"); 101 test.setRemoteMethodCalls(0); 102 final int count = 15; 103 Thread [] threads = new Thread [count]; 104 for (int i = 0 ; i < count ; i++) 105 { 106 final int outer = i; 107 threads[i] = new Thread ( 108 new Runnable () 109 { 110 public void run() 111 { 112 try 113 { 114 ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote"); 115 for (int j = 0 ; j < count ; j++) 116 { 117 String s = outer + "_" + j; 118 test.remoteMethod(s); 120 } 121 } 122 catch(Exception e) 123 { 124 throw new RuntimeException (e); 125 } 126 } 127 } 128 ); 129 threads[i].start(); 130 } 131 132 Thread.sleep(5000); 133 for (int i = 0 ; i < count ; i++) 134 { 135 threads[i].join(); 136 } 137 assertEquals("There should only ever be one instance of the service", 1, test.getInstances()); 138 assertEquals("Wrong number of remote method calls", count * count, test.getRemoteMethodCalls()); 139 } 140 141 144 public void testEJB3_587() throws Exception 145 { 146 MBeanServerConnection server = getServer(); 147 ObjectName testerName = new ObjectName ("jboss.ejb3.bugs:service=TestResourceInjectionService"); 148 boolean success = (Boolean )server.getAttribute(testerName, "TestedSuccessful"); 149 assertTrue(success); 150 } 151 152 155 public void testEJB3_587_2() throws Exception 156 { 157 MBeanServerConnection server = getServer(); 158 ObjectName testerName = new ObjectName ("jboss.ejb3.bugs:service=TestResourceInjectionService"); 159 boolean success = (Boolean )server.getAttribute(testerName, "TestedSuccessfulNow"); 160 assertTrue(success); 161 } 162 163 public void testServiceWithDefaultLocalJNDIName() throws Exception 164 { 165 MBeanServerConnection server = getServer(); 166 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=service"); 167 Object [] params = {}; 168 String [] sig = {}; 169 server.invoke(testerName, "testServiceWithDefaultLocalJNDIName", params, sig); 170 } 171 172 private class RemoteTest extends Thread 173 { 174 private int count; 175 private int outer; 176 177 public Exception exception; 178 179 public RemoteTest(int outer, int count) 180 { 181 this.outer = outer; 182 this.count = count; 183 } 184 185 public void run() 186 { 187 try 188 { 189 ServiceSevenRemote test = (ServiceSevenRemote) getInitialContext().lookup("ServiceSeven/remote"); 190 for (int j = 0 ; j < count ; j++) 191 { 192 String s = outer + "_" + j; 193 test.remoteMethod(s); 195 } 196 } 197 catch(Exception e) 198 { 199 exception = e; 200 } 201 } 202 } 203 204 public void testRemoteServiceWithInterfaceAnnotations() throws Exception 205 { 206 ServiceSevenRemote test = (ServiceSevenRemote) getInitialContext().lookup("ServiceSeven/remote"); 207 test.setRemoteMethodCalls(0); 208 final int count = 15; 209 RemoteTest[] threads = new RemoteTest[count]; 210 for (int i = 0 ; i < count ; i++) 211 { 212 final int outer = i; 213 threads[i] = new RemoteTest(outer, count); 214 threads[i].start(); 215 } 216 217 Thread.sleep(5000); 218 for (int i = 0 ; i < count ; i++) 219 { 220 threads[i].join(); 221 if (threads[i].exception != null) throw threads[i].exception; 222 } 223 assertEquals("There should only ever be one instance of the service", 1, test.getInstances()); 224 assertEquals("Wrong number of remote method calls", count * count, test.getRemoteMethodCalls()); 225 } 226 227 public void testLocalServiceWithInterfaceAnnotation() throws Exception 228 { 229 MBeanServerConnection server = getServer(); 230 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=service"); 231 Object [] params = {}; 232 String [] sig = {}; 233 server.invoke(testerName, "testLocalServiceWithInterfaceAnnotation", params, sig); 234 } 235 236 public void testServiceWithRemoteBinding() throws Exception 237 { 238 ServiceTwoRemote test = (ServiceTwoRemote) getInitialContext().lookup("serviceTwo/remote"); 239 test.setCalled(false); 240 assertFalse("Called should be false", test.getCalled()); 241 test.remoteMethod(); 242 assertTrue("Called should be true", test.getCalled()); 243 } 244 245 public void testServiceWithLocalBinding() throws Exception 246 { 247 MBeanServerConnection server = getServer(); 248 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=service"); 249 Object [] params = {}; 250 String [] sig = {}; 251 server.invoke(testerName, "testServiceWithLocalBinding", params, sig); 252 } 253 254 public void testDeploymentDescriptorServiceInjection() throws Exception 255 { 256 ServiceSixRemote test = (ServiceSixRemote) getInitialContext().lookup("serviceSix/remote"); 257 assertNotNull(test); 258 test.testInjection(); 259 } 260 261 public void testDeploymentDescriptorServiceWithRemoteBinding() throws Exception 262 { 263 ServiceSixRemote test = (ServiceSixRemote) getInitialContext().lookup("serviceSix/remote"); 264 test.setCalled(false); 265 assertFalse("Called should be false", test.getCalled()); 266 test.remoteMethod(); 267 assertTrue("Called should be true", test.getCalled()); 268 } 269 270 public void testDeploymentDescriptorServiceWithLocalBinding() throws Exception 271 { 272 MBeanServerConnection server = getServer(); 273 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=service"); 274 Object [] params = {}; 275 String [] sig = {}; 276 server.invoke(testerName, "testDeploymentDescriptorServiceWithLocalBinding", params, sig); 277 } 278 279 public void testDeploymentDescriptorManagementServiceWithDefaultName() throws Exception 280 { 281 MBeanServerConnection server = getServer(); 282 ObjectName testerName = new ObjectName ("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceSix,type=ManagementInterface"); 283 server.setAttribute(testerName, new Attribute ("Attribute", new Integer (1234))); 284 int ret = ((Integer )server.getAttribute(testerName, "Attribute")); 285 assertEquals("wrong value for Attribute", 1234, ret); 286 287 String s = (String )server.invoke(testerName, "jmxOperation", new Object []{"1"}, new String []{String .class.getName()}); 288 assertEquals("wrong return value", "x1x", s); 289 290 String [] sa = (String [])server.invoke(testerName, "jmxOperation", new Object []{new String []{"0", "1"}}, new String []{String [].class.getName()}); 291 assertEquals("wrong number of return values", 2, sa.length); 292 for (int i = 0 ; i < sa.length ; i++) 293 { 294 assertEquals("Wrong return value index " + i, "x" + i + "x", sa[i]); 295 } 296 297 AttributeList attributes = new AttributeList (); 298 attributes.add(new Attribute ("SomeAttr", new Integer (72))); 299 attributes.add(new Attribute ("OtherAttr", new Integer (99))); 300 attributes = server.setAttributes(testerName, attributes); 301 302 String [] attrs = new String []{"Attribute", "SomeAttr", "OtherAttr"}; 303 AttributeList list = server.getAttributes(testerName, attrs); 304 305 assertEquals("Wrong number of attributes returned", 3, list.size()); 306 for (Iterator it = list.iterator() ; it.hasNext() ; ) 307 { 308 Attribute attr = (Attribute )it.next(); 309 String name = attr.getName(); 310 if (name.equals("Attribute")) 311 { 312 assertEquals("Wrong number for Attribute", 1234, attr.getValue()); 313 } 314 else if (name.equals("SomeAttr")) 315 { 316 assertEquals("Wrong number for SomeAttr", 72, attr.getValue()); 317 } 318 else if (name.equals("OtherAttr")) 319 { 320 assertEquals("Wrong number for OtherAttr", 99, attr.getValue()); 321 } 322 else 323 { 324 throw new RuntimeException ("Unknown attribute returned: " + name); 325 } 326 } 327 328 server.setAttribute(testerName, new Attribute ("WriteOnly", new Integer (2525))); 329 assertEquals("Wrong read only value", 2525, ((Integer )server.getAttribute(testerName, "ReadOnly")).intValue()); 330 } 331 332 public void testManagementServiceWithDefaultName() throws Exception 333 { 334 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 335 SecurityAssociation.setCredential("password".toCharArray()); 336 337 MBeanServerConnection server = getServer(); 338 ObjectName testerName = new ObjectName ("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceOne,type=ManagementInterface"); 339 server.setAttribute(testerName, new Attribute ("Attribute", new Integer (1234))); 340 int ret = ((Integer )server.getAttribute(testerName, "Attribute")); 341 assertEquals("wrong value for Attribute", 1234, ret); 342 343 String s = (String )server.invoke(testerName, "jmxOperation", new Object []{"1"}, new String []{String .class.getName()}); 344 assertEquals("wrong return value", "x1x", s); 345 346 String [] sa = (String [])server.invoke(testerName, "jmxOperation", new Object []{new String []{"0", "1"}}, new String []{String [].class.getName()}); 347 assertEquals("wrong number of return values", 2, sa.length); 348 for (int i = 0 ; i < sa.length ; i++) 349 { 350 assertEquals("Wrong return value index " + i, "x" + i + "x", sa[i]); 351 } 352 353 AttributeList attributes = new AttributeList (); 354 attributes.add(new Attribute ("SomeAttr", new Integer (72))); 355 attributes.add(new Attribute ("OtherAttr", new Integer (99))); 356 attributes = server.setAttributes(testerName, attributes); 357 358 String [] attrs = new String []{"Attribute", "SomeAttr", "OtherAttr"}; 359 AttributeList list = server.getAttributes(testerName, attrs); 360 361 assertEquals("Wrong number of attributes returned", 3, list.size()); 362 for (Iterator it = list.iterator() ; it.hasNext() ; ) 363 { 364 Attribute attr = (Attribute )it.next(); 365 String name = attr.getName(); 366 if (name.equals("Attribute")) 367 { 368 assertEquals("Wrong number for Attribute", 1234, attr.getValue()); 369 } 370 else if (name.equals("SomeAttr")) 371 { 372 assertEquals("Wrong number for SomeAttr", 72, attr.getValue()); 373 } 374 else if (name.equals("OtherAttr")) 375 { 376 assertEquals("Wrong number for OtherAttr", 99, attr.getValue()); 377 } 378 else 379 { 380 throw new RuntimeException ("Unknown attribute returned: " + name); 381 } 382 } 383 384 server.setAttribute(testerName, new Attribute ("WriteOnly", new Integer (2525))); 385 assertEquals("Wrong read only value", 2525, ((Integer )server.getAttribute(testerName, "ReadOnly")).intValue()); 386 } 387 388 public void testManagementServiceWithInterfaceAnnotations() throws Exception 389 { 390 MBeanServerConnection server = getServer(); 391 ObjectName testerName = new ObjectName ("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceSeven,type=ManagementInterface"); 392 server.setAttribute(testerName, new Attribute ("Attribute", new Integer (1234))); 393 int ret = ((Integer )server.getAttribute(testerName, "Attribute")); 394 assertEquals("wrong value for Attribute", 1234, ret); 395 396 String s = (String )server.invoke(testerName, "jmxOperation", new Object []{"1"}, new String []{String .class.getName()}); 397 assertEquals("wrong return value", "x1x", s); 398 399 String [] sa = (String [])server.invoke(testerName, "jmxOperation", new Object []{new String []{"0", "1"}}, new String []{String [].class.getName()}); 400 assertEquals("wrong number of return values", 2, sa.length); 401 for (int i = 0 ; i < sa.length ; i++) 402 { 403 assertEquals("Wrong return value index " + i, "x" + i + "x", sa[i]); 404 } 405 406 AttributeList attributes = new AttributeList (); 407 attributes.add(new Attribute ("SomeAttr", new Integer (72))); 408 attributes.add(new Attribute ("OtherAttr", new Integer (99))); 409 attributes = server.setAttributes(testerName, attributes); 410 411 String [] attrs = new String []{"Attribute", "SomeAttr", "OtherAttr"}; 412 AttributeList list = server.getAttributes(testerName, attrs); 413 414 assertEquals("Wrong number of attributes returned", 3, list.size()); 415 for (Iterator it = list.iterator() ; it.hasNext() ; ) 416 { 417 Attribute attr = (Attribute )it.next(); 418 String name = attr.getName(); 419 if (name.equals("Attribute")) 420 { 421 assertEquals("Wrong number for Attribute", 1234, attr.getValue()); 422 } 423 else if (name.equals("SomeAttr")) 424 { 425 assertEquals("Wrong number for SomeAttr", 72, attr.getValue()); 426 } 427 else if (name.equals("OtherAttr")) 428 { 429 assertEquals("Wrong number for OtherAttr", 99, attr.getValue()); 430 } 431 else 432 { 433 throw new RuntimeException ("Unknown attribute returned: " + name); 434 } 435 } 436 437 server.setAttribute(testerName, new Attribute ("WriteOnly", new Integer (2525))); 438 assertEquals("Wrong read only value", 2525, ((Integer )server.getAttribute(testerName, "ReadOnly")).intValue()); 439 } 440 441 public void testServiceDependencyInjectionAndInterception() throws Exception 442 { 443 MBeanServerConnection server = getServer(); 444 ObjectName testerName = new ObjectName ("jboss.ejb3:management=interface,with=customName,is=serviceThree"); 445 Boolean injected = (Boolean )server.getAttribute(testerName, "Injected"); 446 assertTrue("Injected should have been true", injected); 447 448 Integer intercepted = (Integer )server.getAttribute(testerName, "Intercepted");; 449 assertEquals("Wrong number of interceptions", 2, intercepted.intValue()); 450 } 451 452 public void testCreationOrder() throws Exception 453 { 454 MBeanServerConnection server = getServer(); 455 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=service"); 456 ArrayList creates = (ArrayList )server.getAttribute(testerName, "Creates"); 457 ArrayList starts = (ArrayList )server.getAttribute(testerName, "Starts"); 458 459 int expectedLength = 4; 461 assertEquals("Wrong length of created MBeans", expectedLength, creates.size()); 462 assertEquals("Wrong length of started MBeans", expectedLength, starts.size()); 463 464 String createAll = " "; 465 String startAll = " "; 466 for (int i = 0; i < expectedLength; i++) 467 { 468 createAll += creates.get(i) + " "; 469 startAll += starts.get(i) + " "; 470 } 471 472 for (int i = 0 ; i < expectedLength ; i++) 473 { 474 int start = Integer.parseInt((String )starts.get(i)); 477 int expected = (i == 0) ? i + 1 : i + 2; 478 assertEquals("Start of Service" + start + "appears at the wronmg place" + startAll, expected, start); 480 } 481 } 482 483 public void testDependsInjection() throws Exception 484 { 485 MBeanServerConnection server = getServer(); 486 ObjectName testerName = new ObjectName ("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceFive,type=ManagementInterface"); 487 assertTrue("Proxy field was not injected", (Boolean )server.getAttribute(testerName, "InjectedProxyField")); 488 assertTrue("Proxy method was not injected", (Boolean )server.getAttribute(testerName, "InjectedProxyMethod")); 489 assertTrue("ObjectName field was not injected", (Boolean )server.getAttribute(testerName, "InjectedObjectNameField")); 490 assertTrue("ObjectName method was not injected", (Boolean )server.getAttribute(testerName, "InjectedObjectNameMethod")); 491 SessionRemote test = (SessionRemote) getInitialContext().lookup("Session/remote"); 492 assertTrue("StatelessBean @Depends was not injected", test.injectedDepends()); 493 } 494 495 496 public static Test suite() throws Exception 497 { 498 return getDeploySetup(ServiceUnitTestCase.class, "service-test.sar, service-test.jar"); 499 } 500 501 } 502 | Popular Tags |