1 22 package org.jboss.ejb3.test.bank.unit; 23 24 import javax.ejb.EJBAccessException ; 25 import javax.ejb.EJBException ; 26 import javax.ejb.NoSuchEJBException ; 27 import javax.management.ObjectName ; 28 import javax.naming.InitialContext ; 29 30 import org.jboss.ejb3.ClientKernelAbstraction; 31 import org.jboss.ejb3.KernelAbstractionFactory; 32 import org.jboss.ejb3.test.bank.Bank; 33 import org.jboss.ejb3.test.bank.ProxyFactoryInterface; 34 import org.jboss.ejb3.test.bank.Teller; 35 import org.jboss.ejb3.test.bank.TellerInterceptor; 36 import org.jboss.ejb3.test.bank.TestStatus; 37 import org.jboss.logging.Logger; 38 import org.jboss.security.SecurityAssociation; 39 import org.jboss.security.SimplePrincipal; 40 import org.jboss.test.JBossTestCase; 41 import junit.framework.Test; 42 43 49 public class BankDeploymentDescriptorTestCase 50 extends JBossTestCase { 51 53 private static final Logger log = Logger 54 .getLogger(BankDeploymentDescriptorTestCase.class); 55 56 public BankDeploymentDescriptorTestCase(String name) 57 { 58 super(name); 59 } 60 61 public void testEnvEntry() throws Exception 62 { 63 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 64 SecurityAssociation.setCredential("password".toCharArray()); 65 66 InitialContext jndiContext = new InitialContext (); 67 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 68 assertNotNull(bank); 69 String id = bank.getEnvEntryId(); 70 assertEquals(id, "5678"); 71 } 72 73 public void testStatelessTeller() throws Exception 74 { 75 InitialContext jndiContext = new InitialContext (); 76 77 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 78 SecurityAssociation.setCredential("password".toCharArray()); 79 80 String greeting; 81 Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME); 82 assertNotNull(teller); 83 84 greeting = teller.greetWithRequiredTransaction("Hello"); 85 assertNotNull(greeting); 86 assertEquals("Hello", greeting); 87 greeting = teller.greetWithNotSupportedTransaction("Hello"); 88 assertNotNull(greeting); 89 assertEquals("Hello", greeting); 90 greeting = teller.greetUnchecked("Hello"); 91 assertNotNull(greeting); 92 assertEquals("Hello", greeting); 93 94 try { 95 greeting = teller.greetChecked("Hello"); 96 assertTrue(false); 97 } catch (Exception e){ 98 assertTrue(e instanceof EJBAccessException ); 99 } 100 101 SecurityAssociation.setPrincipal(new SimplePrincipal("customer")); 102 SecurityAssociation.setCredential("password".toCharArray()); 103 104 try{ 105 greeting = teller.greetChecked("Hello"); 106 assertNotNull(greeting); 107 assertEquals("Hello", greeting); 108 } catch (Exception e){ 109 e.printStackTrace(); 110 } 111 } 112 113 public void testInjectionAnnotations() throws Exception 114 { 115 InitialContext jndiContext = new InitialContext (); 116 Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME); 117 assertNotNull(teller); 118 119 SecurityAssociation.setPrincipal(new SimplePrincipal("customer")); 120 SecurityAssociation.setCredential("password".toCharArray()); 121 122 String greeting = teller.greetChecked("Hello"); 123 assertNotNull(greeting); 124 assertEquals("Hello", greeting); 125 assertTrue(teller.isConstructed()); 126 127 } 128 129 public void testFieldInject() throws Exception 130 { 131 InitialContext jndiContext = new InitialContext (); 132 133 Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME); 134 assertNotNull(teller); 135 136 String greeting = teller.greetWithServiceTimer("Hello"); 137 assertEquals("Hello", greeting); 138 } 139 140 public void testRunAs() throws Exception 141 { 142 InitialContext jndiContext = new InitialContext (); 143 144 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 145 SecurityAssociation.setCredential("password".toCharArray()); 146 147 String customerId = "CustomerId"; 148 String greeting; 149 Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME); 150 assertNotNull(teller); 151 152 String tmpId = teller.retrieveCustomerId(); 153 assertEquals("defaultId", tmpId); 154 } 155 156 public void testStatefulBank() throws Exception 157 { 158 InitialContext jndiContext = new InitialContext (); 159 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 160 assertNotNull(bank); 161 String customerId = "CustomerId"; 162 163 SecurityAssociation.setPrincipal(new SimplePrincipal("customer")); 164 SecurityAssociation.setCredential("password".toCharArray()); 165 try { 166 bank.storeCustomerId(customerId); 167 assertTrue(false); 168 } catch (Exception e){ 169 assertTrue(e instanceof EJBAccessException ); 170 } 171 172 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 173 SecurityAssociation.setCredential("password".toCharArray()); 174 175 bank.storeCustomerId(customerId); 176 String tmpId = bank.retrieveCustomerId(); 177 assertEquals(customerId, tmpId); 178 } 179 180 public void testStatefulState() throws Exception 181 { 182 InitialContext jndiContext = new InitialContext (); 183 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 184 assertNotNull(bank); 185 String customerId = "CustomerId"; 186 187 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 188 SecurityAssociation.setCredential("password".toCharArray()); 189 190 bank.storeCustomerId(customerId); 191 String tmpId = bank.retrieveCustomerId(); 192 assertEquals(customerId, tmpId); 193 194 bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 195 assertNotNull(bank); 196 tmpId = bank.retrieveCustomerId(); 197 assertEquals("defaultId", tmpId); 198 } 199 200 public void testStatefulBank21() throws Exception 201 { 202 InitialContext jndiContext = new InitialContext (); 203 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME + "21"); 204 assertNotNull(bank); 205 206 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 207 SecurityAssociation.setCredential("password".toCharArray()); 208 209 String activated = bank.isActivated(); 210 assertEquals(activated, "_CREATED"); 211 } 212 213 public void testCallbackListenersAndInteceptors() throws Exception 214 { 215 InitialContext jndiContext = new InitialContext (); 216 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 217 assertNotNull(bank); 218 TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote"); 219 status.clear(); 220 221 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 222 SecurityAssociation.setCredential("password".toCharArray()); 223 224 String id = bank.interceptCustomerId("CustomerId"); 225 assertEquals("CustomerId_SecondInterceptor_FirstInterceptor", id); 226 assertTrue(status.postConstruct()); 227 } 228 229 public void testResource() throws Exception 230 { 231 InitialContext jndiContext = new InitialContext (); 232 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 233 assertNotNull(bank); 234 235 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 236 SecurityAssociation.setCredential("password".toCharArray()); 237 238 bank.testResource(); 239 } 240 241 public void testRemove() throws Exception 242 { 243 InitialContext jndiContext = new InitialContext (); 244 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 245 assertNotNull(bank); 246 247 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 248 SecurityAssociation.setCredential("password".toCharArray()); 249 250 bank.remove(); 251 252 try { 253 bank.testResource(); 254 assertTrue(false); 255 } 256 catch (NoSuchEJBException e) 257 { 258 } 260 } 261 262 public void testInit() throws Exception 263 { 264 InitialContext jndiContext = new InitialContext (); 265 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 266 assertNotNull(bank); 267 268 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 269 SecurityAssociation.setCredential("password".toCharArray()); 270 271 String initialized = bank.isInitialized(); 272 assertEquals("YESYES", initialized); 273 } 274 275 public void testTeller() throws Exception 276 { 277 InitialContext jndiContext = new InitialContext (); 278 Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME); 279 assertNotNull(teller); 280 281 SecurityAssociation.setPrincipal(new SimplePrincipal("customer")); 282 SecurityAssociation.setCredential("password".toCharArray()); 283 284 try { 285 teller.excludedMethod(); 286 assertTrue(false); 287 } catch (Exception e){ 288 assertTrue(e instanceof EJBAccessException ); 289 } 290 } 291 292 public void testRemoteBindingProxyFactory() throws Exception 293 { 294 ProxyFactoryInterface teller = (ProxyFactoryInterface)getInitialContext().lookup(Teller.JNDI_NAME); 295 assertNotNull(teller); 296 } 297 298 public void testRemoteBindingInterceptorStack() throws Exception 299 { 300 Teller teller = (Teller)getInitialContext().lookup(Teller.JNDI_NAME); 301 assertNotNull(teller); 302 assertTrue(TellerInterceptor.accessed); 303 } 304 305 public void testTransactionTimeout() throws Exception 306 { 307 InitialContext jndiContext = new InitialContext (); 308 309 SecurityAssociation.setPrincipal(new SimplePrincipal("customer")); 310 SecurityAssociation.setCredential("password".toCharArray()); 311 312 Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME); 313 assertNotNull(teller); 314 315 boolean exceptionThrown = false; 316 try 317 { 318 teller.testTransactionTimeout(); 319 } 320 catch (Exception e) 321 { 322 exceptionThrown = true; 323 } 324 assertTrue(exceptionThrown); 325 326 } 327 328 public void testStatefulTransactionTimeout() throws Exception 329 { 330 InitialContext jndiContext = new InitialContext (); 331 332 SecurityAssociation.setPrincipal(new SimplePrincipal("teller")); 333 SecurityAssociation.setCredential("password".toCharArray()); 334 335 Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME); 336 assertNotNull(bank); 337 338 try{ 339 bank.testTransactionTimeout(); 340 String state = bank.getTransactionState(); 341 assertEquals("failed", state); 342 } catch (Exception e){ 343 } 344 } 345 346 protected void setUp() throws Exception 347 { 348 } 349 350 protected void tearDown() throws Exception 351 { 352 } 353 354 public static Test suite() throws Exception 355 { 356 ClientKernelAbstraction kernel = KernelAbstractionFactory.getClientInstance(); 357 ObjectName propertiesServiceON = new ObjectName ("jboss:type=Service,name=SystemProperties"); 358 kernel.invoke( 359 propertiesServiceON, 360 "set", 361 new Object []{"test.datasource.jndi","java:/DefaultDS"}, 362 new String []{"java.lang.String", "java.lang.String"} 363 ); 364 365 kernel.invoke( 366 propertiesServiceON, 367 "set", 368 new Object []{"test.transactionmanager.jndi","java:/TransactionManager"}, 369 new String []{"java.lang.String", "java.lang.String"} 370 ); 371 372 return getDeploySetup(BankDeploymentDescriptorTestCase.class, "bank.jar"); 373 } 374 375 } 376 | Popular Tags |