KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > stateless > unit > StatelessTestCase


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.stateless.unit;
23
24 import javax.ejb.EJBAccessException JavaDoc;
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 /**
38  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
39  * @version $Revision: 44722 $
40  */

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 JavaDoc name)
49    {
50       super(name);
51    }
52  
53    public void testCallerPrincipal() throws Exception JavaDoc
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 JavaDoc principal = runAs.getCallerPrincipal();
62       assertEquals("somebody", principal);
63       
64       principal = runAs.getCheckedCallerPrincipal();
65       assertEquals("somebody", principal);
66    }
67    
68    public void testStatelessLocal() throws Exception JavaDoc
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 JavaDoc e)
81       {
82          log.info("Caught EJBException " + e.getMessage());
83       }
84    }
85
86    public void testRunAs() throws Exception JavaDoc
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 JavaDoc e){
103          assertTrue(e instanceof EJBAccessException JavaDoc);
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 JavaDoc
114    {
115       AnonymousStateless anonymous = (AnonymousStateless)getInitialContext().lookup("AnonymousStatelessBean/remote");
116       
117       SecurityAssociation.clear();
118       
119       try
120       {
121          anonymous.method(1);
122 // fail();
123
}
124       catch (Exception JavaDoc e)
125       {
126  // throw e;
127
}
128    }
129    
130    public void testUnsecureToSecure() throws Exception JavaDoc
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 JavaDoc 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 JavaDoc
154    {
155       return getDeploySetup(StatelessTestCase.class, "stateless-test.jar");
156    }
157 }
Popular Tags