1 22 package org.jboss.ejb3.test.stateless.unit; 23 24 import javax.ejb.EJBAccessException ; 25 26 import org.jboss.ejb3.test.stateless.AnonymousStateless; 27 import org.jboss.ejb3.test.stateless.CheckedStateless; 28 import org.jboss.ejb3.test.stateless.UnsecuredStateless; 29 import org.jboss.ejb3.test.stateless.RunAsStateless; 30 import org.jboss.ejb3.test.stateless.RunAsStatelessLocal; 31 import org.jboss.logging.Logger; 32 import org.jboss.security.SecurityAssociation; 33 import org.jboss.security.SimplePrincipal; 34 import org.jboss.test.JBossTestCase; 35 import junit.framework.Test; 36 37 41 public class StatelessTestCase extends JBossTestCase 42 { 43 private static final Logger log = Logger.getLogger(StatelessTestCase.class); 44 45 static boolean deployed = false; 46 static int test = 0; 47 48 public StatelessTestCase(String name) 49 { 50 super(name); 51 } 52 53 public void testCallerPrincipal() throws Exception 54 { 55 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 56 SecurityAssociation.setCredential("password".toCharArray()); 57 58 RunAsStateless runAs = (RunAsStateless) getInitialContext().lookup("RunAsStatelessEjbName/remote"); 59 assertNotNull(runAs); 60 61 String principal = runAs.getCallerPrincipal(); 62 assertEquals("somebody", principal); 63 64 principal = runAs.getCheckedCallerPrincipal(); 65 assertEquals("somebody", principal); 66 } 67 68 public void testStatelessLocal() throws Exception 69 { 70 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 71 SecurityAssociation.setCredential("password".toCharArray()); 72 73 try 74 { 75 RunAsStatelessLocal runAs = (RunAsStatelessLocal) getInitialContext().lookup("RunAsStatelessEjbName/local"); 76 assertNotNull(runAs); 77 runAs.method(1); 78 fail("EJBException should be thrown"); 79 } 80 catch (javax.ejb.EJBException e) 81 { 82 log.info("Caught EJBException " + e.getMessage()); 83 } 84 } 85 86 public void testRunAs() throws Exception 87 { 88 CheckedStateless checked = (CheckedStateless)getInitialContext().lookup("CheckedStatelessBean/remote"); 89 90 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 91 SecurityAssociation.setCredential("password".toCharArray()); 92 93 int result = checked.method(1); 94 assertEquals(1,result); 95 96 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 97 SecurityAssociation.setCredential("password".toCharArray()); 98 99 try { 100 checked.method(2); 101 assertTrue(false); 102 } catch (Exception e){ 103 assertTrue(e instanceof EJBAccessException ); 104 } 105 106 RunAsStateless runAs = 107 (RunAsStateless) getInitialContext().lookup("RunAsStatelessEjbName/remote"); 108 109 int i = runAs.method(3); 110 assertEquals(3, i); 111 } 112 113 public void testAnonymous() throws Exception 114 { 115 AnonymousStateless anonymous = (AnonymousStateless)getInitialContext().lookup("AnonymousStatelessBean/remote"); 116 117 SecurityAssociation.clear(); 118 119 try 120 { 121 anonymous.method(1); 122 } 124 catch (Exception e) 125 { 126 } 128 } 129 130 public void testUnsecureToSecure() throws Exception 131 { 132 UnsecuredStateless stateless = (UnsecuredStateless)getInitialContext().lookup("UnsecuredStatelessBean/remote"); 133 134 SecurityAssociation.clear(); 135 136 try 137 { 138 stateless.method(1); 139 fail(); 140 } 141 catch (Exception e) 142 { 143 144 } 145 146 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 147 SecurityAssociation.setCredential("password".toCharArray()); 148 149 assertEquals(1, stateless.method(1)); 150 151 } 152 153 public static Test suite() throws Exception 154 { 155 return getDeploySetup(StatelessTestCase.class, "stateless-test.jar"); 156 } 157 } | Popular Tags |