KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > initial > unit > RemoteUnitTestCase


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.initial.unit;
23
24 import javax.ejb.EJBAccessException JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
27 import org.jboss.ejb3.test.initial.ClassInjected;
28 import org.jboss.ejb3.test.initial.InterceptedSFTest;
29 import org.jboss.ejb3.test.initial.SecuredTest;
30 import org.jboss.ejb3.test.initial.StatefulTestRemote;
31 import org.jboss.ejb3.test.initial.TestRemote;
32 import org.jboss.logging.Logger;
33 import org.jboss.security.SecurityAssociation;
34 import org.jboss.security.SimplePrincipal;
35 import org.jboss.test.JBossTestCase;
36 import junit.framework.Test;
37
38 /**
39  * Sample client for the jboss container.
40  *
41  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
42  * @version $Id: RemoteUnitTestCase.java 58418 2006-11-15 21:54:30Z anil.saldhana@jboss.com $
43  */

44
45 public class RemoteUnitTestCase
46 extends JBossTestCase
47 {
48    private static Logger log = Logger.getLogger(RemoteUnitTestCase.class);
49
50    static boolean deployed = false;
51    static int test = 0;
52
53    public RemoteUnitTestCase(String JavaDoc name)
54    {
55
56       super(name);
57
58    }
59
60    public void testStateful() throws Exception JavaDoc
61    {
62       StatefulTestRemote test = (StatefulTestRemote) getInitialContext().lookup("StatefulTestBean/remote");
63       test.setState("hello world");
64       assertEquals(test.getState(), "hello world");
65       test.endSession();
66
67    }
68
69    public void testSimple() throws Exception JavaDoc
70    {
71       TestRemote test = (TestRemote) this.getInitialContext().lookup("TestBean/remote");
72       String JavaDoc echo = test.testMe("echo");
73       assertEquals(echo, "echo");
74    }
75
76    public void testTx() throws Exception JavaDoc
77    {
78       // Test basic tx propagation
79
UserTransaction JavaDoc ut = (UserTransaction JavaDoc) getInitialContext().lookup("UserTransaction");
80       TestRemote test = (TestRemote) this.getInitialContext().lookup("TestBean/remote");
81       ut.begin();
82       test.mandatory();
83       // call mandatory a second time to test that TX has been dissacciated correctly
84
test.mandatory();
85       ut.commit();
86
87    }
88
89    public void testSecurity() throws Exception JavaDoc
90    {
91       // Test basic security propagation
92
InitialContext JavaDoc ctx = getInitialContext();
93       SecuredTest test = (SecuredTest) ctx.lookup("SecuredTestBean/remote");
94
95       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
96       SecurityAssociation.setCredential("password".toCharArray());
97
98       System.out.println("Calling initial security tests....");
99       test.unchecked();
100       System.out.println("Calling testDefault()....");
101       test.testDefault();
102       System.out.println("Calling secured()....");
103       test.secured();
104
105       System.out.println("Calling security fine grain tests....");
106       SecurityAssociation.setPrincipal(new SimplePrincipal("authfail"));
107
108       boolean securityFailure = true;
109       try
110       {
111          test.secured();
112       }
113       catch (EJBAccessException JavaDoc ignored)
114       {
115          System.out.println("log="+log+":ignored:"+ignored);
116          log.info(ignored.getMessage());
117          securityFailure = false;
118       }
119
120       if (securityFailure) throw new RuntimeException JavaDoc("auth failure was not caught for method");
121
122       securityFailure = true;
123       SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail"));
124       try
125       {
126          test.secured();
127       }
128       catch (EJBAccessException JavaDoc ignored)
129       {
130          log.info(ignored.getMessage());
131          securityFailure = false;
132       }
133       if (securityFailure) throw new RuntimeException JavaDoc("role failure was not caught for method");
134
135       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
136       log.info("test exclusion");
137       securityFailure = true;
138       try
139       {
140          test.excluded();
141       }
142       catch (EJBAccessException JavaDoc ignored)
143       {
144          log.info(ignored.getMessage());
145          securityFailure = false;
146       }
147       if (securityFailure) throw new RuntimeException JavaDoc("excluded failure was not caught for method");
148
149    }
150
151    public void testInterceptors() throws Exception JavaDoc
152    {
153       InterceptedSFTest test = (InterceptedSFTest) this.getInitialContext().lookup("InterceptedSFTestBean/remote");
154       int ret = test.testMethod(5);
155       int expected = 1010;
156       if (ret != expected) throw new Exception JavaDoc("return value was not " + expected + ", it was: " + ret);
157    }
158
159    public void testCallbacks() throws Exception JavaDoc
160    {
161       org.jboss.ejb3.test.initial.TestStatus status =
162       (org.jboss.ejb3.test.initial.TestStatus) getInitialContext().lookup("TestStatusBean/remote");
163       status.clear();
164       InterceptedSFTest test = (InterceptedSFTest) getInitialContext().lookup("InterceptedSFTestBean/remote");
165
166       test.testMethod(5);
167       int val = test.getVal();
168       if (val != 5) throw new Exception JavaDoc("test.getVal() returned " + val + " instead of 5");
169       if (!status.postConstruct()) throw new Exception JavaDoc("PostConstruct should be called for SFSB");
170
171       //Exhaust cache to force passivation
172
InterceptedSFTest test2 = (InterceptedSFTest) getInitialContext().lookup("InterceptedSFTestBean/remote");
173       test2.testMethod(3);
174
175       if (!status.prePassivate()) throw new Exception JavaDoc("PrePassivate should be called for SFSB");
176
177       //Activate original bean
178
val = test.getVal();
179       if (val != 5) throw new Exception JavaDoc("test.getVal() returned " + val + " instead of 5");
180       if (!status.postActivate()) throw new Exception JavaDoc("PostActivate should be called for SFSB");
181
182       //Remove method
183
test.clear();
184       if (!status.preDestroy()) throw new Exception JavaDoc("PreDestroy should be called for SFSB");
185
186       test2.clear();
187    }
188
189    public void testClassInjected() throws Exception JavaDoc
190    {
191       ClassInjected test = (ClassInjected)getInitialContext().lookup("ClassInjectedBean/remote");
192       assertTrue(test.isInjected());
193    }
194
195    public static Test suite() throws Exception JavaDoc
196    {
197       return getDeploySetup(RemoteUnitTestCase.class, "initial-ejb3-test.sar");
198    }
199
200 }
201
Popular Tags