KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > standalone > security > unit > SecurityUnitTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

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 JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.ejb.EJBAccessException JavaDoc;
34 import java.util.Hashtable JavaDoc;
35
36 /**
37  * Sample client for the jboss container.
38  *
39  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
40  * @version $Id: SecurityUnitTestCase.java 42748 2006-03-24 21:22:04Z bill $
41  */

42
43 public class SecurityUnitTestCase
44         extends TestCase
45 {
46
47    public SecurityUnitTestCase(String JavaDoc name)
48    {
49
50       super(name);
51
52    }
53
54    public void testSecurity() throws Exception JavaDoc
55    {
56       Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc ctx = new InitialContext JavaDoc(env);
61       Secured test = (Secured)ctx.lookup("SecuredBean/local");
62       boolean exceptionThrown = false;
63       try
64       {
65          test.echo("bill");
66       }
67       catch (EJBAccessException JavaDoc 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 JavaDoc(env);
76       test.echo("bill");
77    }
78
79
80    public static Test suite() throws Exception JavaDoc
81    {
82       TestSuite suite = new TestSuite();
83       suite.addTestSuite(SecurityUnitTestCase.class);
84
85       // setup test so that embedded JBoss is started/stopped once for all tests here.
86
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