1 22 package org.jboss.ejb3.test.standalone.security.unit; 23 24 import junit.extensions.TestSetup; 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 29 import org.jboss.ejb3.test.standalone.security.Secured; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.ejb.EJBAccessException ; 34 import java.util.Hashtable ; 35 36 42 43 public class SecurityUnitTestCase 44 extends TestCase 45 { 46 47 public SecurityUnitTestCase(String name) 48 { 49 50 super(name); 51 52 } 53 54 public void testSecurity() throws Exception 55 { 56 Hashtable env = new Hashtable (); 57 env.put(Context.SECURITY_PRINCIPAL, "bill"); 58 env.put(Context.SECURITY_CREDENTIALS, "password"); 59 env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory"); 60 InitialContext ctx = new InitialContext (env); 61 Secured test = (Secured)ctx.lookup("SecuredBean/local"); 62 boolean exceptionThrown = false; 63 try 64 { 65 test.echo("bill"); 66 } 67 catch (EJBAccessException e) 68 { 69 exceptionThrown = true; 70 e.printStackTrace(); 71 } 72 assertTrue(exceptionThrown); 73 env.put(Context.SECURITY_PRINCIPAL, "kabir"); 74 env.put(Context.SECURITY_CREDENTIALS, "password"); 75 ctx = new InitialContext (env); 76 test.echo("bill"); 77 } 78 79 80 public static Test suite() throws Exception 81 { 82 TestSuite suite = new TestSuite(); 83 suite.addTestSuite(SecurityUnitTestCase.class); 84 85 TestSetup wrapper = new TestSetup(suite) 87 { 88 protected void setUp() 89 { 90 SecurityUnitTestCase.startupEmbeddedJboss(); 91 } 92 93 protected void tearDown() 94 { 95 SecurityUnitTestCase.shutdownEmbeddedJboss(); 96 } 97 }; 98 99 return wrapper; 100 } 101 102 public static void startupEmbeddedJboss() 103 { 104 EJB3StandaloneBootstrap.boot(null); 105 EJB3StandaloneBootstrap.deployXmlResource("security-beans.xml"); 106 EJB3StandaloneBootstrap.scanClasspath("embedded-security.jar"); 107 } 108 109 public static void shutdownEmbeddedJboss() 110 { 111 EJB3StandaloneBootstrap.shutdown(); 112 } 113 114 } 115 | Popular Tags |