KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > timer > test > SecureTimerUnitTestCase


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.test.timer.test;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.rmi.ServerException JavaDoc;
26 import javax.ejb.EJBHome JavaDoc;
27 import javax.ejb.NoSuchObjectLocalException JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.security.auth.login.LoginContext JavaDoc;
31
32 import junit.framework.Test;
33
34 import org.jboss.security.auth.callback.AppCallbackHandler;
35 import org.jboss.test.JBossTestCase;
36 import org.jboss.test.timer.interfaces.TimerEntity;
37 import org.jboss.test.timer.interfaces.TimerEntityHome;
38 import org.jboss.test.timer.interfaces.TimerSFSB;
39 import org.jboss.test.timer.interfaces.TimerSFSBHome;
40 import org.jboss.test.timer.interfaces.TimerSLSB;
41 import org.jboss.test.timer.interfaces.TimerSLSBHome;
42
43 /**
44  * Simple unit tests for the EJB Timer service that uses secured ejbs.
45  */

46 public class SecureTimerUnitTestCase extends JBossTestCase
47 {
48
49    private static final String JavaDoc EJB_TIMER_XAR = "ejb-timer.ear";
50
51    private static final int SHORT_PERIOD = 1 * 1000; // 1s
52
private LoginContext JavaDoc lc;
53
54    /**
55     * Setup the test suite.
56     */

57    public static Test suite() throws Exception JavaDoc
58    {
59       return JBossTestCase.getDeploySetup(SecureTimerUnitTestCase.class, EJB_TIMER_XAR);
60    }
61
62    /**
63     * Constructor for the BasicTimerUnitTest object
64     */

65    public SecureTimerUnitTestCase(String JavaDoc pName)
66    {
67       super(pName);
68    }
69
70    public void tearDown()
71    {
72    }
73
74    /**
75     * Test that a Stafeful Session Bean cannot access a Timer Service
76     *
77     * @throws Exception Unexpected Exception indicating an error
78     */

79    public void testSecuredStatefulSessionBeanTimer()
80       throws Exception JavaDoc
81    {
82       login();
83       TimerSFSBHome home = (TimerSFSBHome) getEJBHome(TimerSFSBHome.SECURED_JNDI_NAME);
84       TimerSFSB lBean = home.create();
85       try
86       {
87          lBean.checkTimerService();
88          fail("Stateful Session Bean is not allowed to get a Timer Service");
89       }
90       catch (RemoteException JavaDoc re)
91       {
92          Throwable JavaDoc lCause = re.detail;
93          if (lCause instanceof ServerException JavaDoc)
94          {
95             lCause = ((ServerException JavaDoc) lCause).detail;
96             if (lCause instanceof IllegalStateException JavaDoc)
97             {
98                // This exception is expected -> ignore
99
}
100             else
101             {
102                throw re;
103             }
104          }
105       }
106       logout();
107    }
108
109    /**
110     * Test that a repetitive Stafeless Session Bean Timer
111     *
112     * @throws Exception Unexpected Exception indicating an error
113     */

114    public void testSecuredStatelessSessionBeanTimer()
115       throws Exception JavaDoc
116    {
117       login();
118       TimerSLSBHome home = (TimerSLSBHome) getEJBHome(TimerSLSBHome.SECURED_JNDI_NAME);
119       TimerSLSB bean = home.create();
120       byte[] handle = bean.startTimer(SHORT_PERIOD);
121       Thread.sleep(12 * SHORT_PERIOD + SHORT_PERIOD);
122       int count = bean.getTimeoutCount(handle);
123       bean.stopTimer(handle);
124       assertTrue("Timeout was expected to be called at least 10 times but was "
125          + "only called: " + count + " times",
126          count >= 10);
127       Thread.sleep(5 * SHORT_PERIOD);
128       int count2 = bean.getTimeoutCount(handle);
129       assertTrue("After the timer was stopped no timeout should happen but "
130          + "it was called " + count2 + " more times",
131          count2 == 0);
132       bean.remove();
133       logout();
134    }
135
136    /**
137     * Test that a single Stafeless Session Bean Timer
138     *
139     * @throws Exception Unexpected Exception indicating an error
140     */

141    public void testSecuredStatelessSessionBeanSingleTimer()
142       throws Exception JavaDoc
143    {
144       login();
145       TimerSLSBHome home = (TimerSLSBHome) getEJBHome(TimerSLSBHome.SECURED_JNDI_NAME);
146       TimerSLSB bean = home.create();
147       byte[] handle = bean.startSingleTimer(SHORT_PERIOD);
148       Thread.sleep(5 * SHORT_PERIOD);
149       int count = bean.getTimeoutCount(handle);
150       assertTrue("Timeout was expected to be called only once but was called: "
151          + count + " times",
152          count == 1);
153       try
154       {
155          bean.stopTimer(handle);
156          fail("A single timer should expire after the first event and therefore this "
157             + "has to throw an NoSuchObjectLocalException");
158       }
159       catch (RemoteException JavaDoc re)
160       {
161          Throwable JavaDoc lCause = re.detail;
162          if (lCause instanceof ServerException JavaDoc)
163          {
164             lCause = ((ServerException JavaDoc) lCause).detail;
165             if (lCause instanceof NoSuchObjectLocalException JavaDoc)
166             {
167                // This exception is expected -> ignore
168
}
169             else
170             {
171                throw re;
172             }
173          }
174       }
175       bean.remove();
176       logout();
177    }
178
179    /**
180     * Test that a repetitive Entity Bean Timer
181     *
182     * @throws Exception Unexpected Exception indicating an error
183     */

184    public void testSecuredEntityBeanTimer()
185       throws Exception JavaDoc
186    {
187       login();
188       TimerEntityHome home = (TimerEntityHome) getEJBHome(TimerEntityHome.SECURED_JNDI_NAME);
189       TimerEntity entity = home.create(new Integer JavaDoc(111));
190       entity.startTimer(SHORT_PERIOD);
191       Thread.sleep(12 * SHORT_PERIOD);
192       entity.stopTimer();
193       int count = entity.getTimeoutCount();
194       assertTrue("Timeout was expected to be called at least 10 times but was "
195          + "only called: " + count + " times",
196          count >= 10);
197       Thread.sleep(5 * SHORT_PERIOD);
198       int count2 = entity.getTimeoutCount();
199       assertTrue("After the timer was stopped no timeout should happen but "
200          + "it was called " + (count2 - count) + " more times",
201          count == count2);
202       entity.remove();
203       logout();
204    }
205
206    /**
207     * Test that a single Entity Bean Timer
208     *
209     * @throws Exception Unexpected Exception indicating an error
210     */

211    public void testSecuredEntityBeanSingleTimer()
212       throws Exception JavaDoc
213    {
214       login();
215       TimerEntityHome home = (TimerEntityHome) getEJBHome(TimerEntityHome.SECURED_JNDI_NAME);
216       TimerEntity entity = home.create(new Integer JavaDoc(222));
217       entity.startSingleTimer(SHORT_PERIOD);
218       Thread.sleep(5 * SHORT_PERIOD);
219       int count = entity.getTimeoutCount();
220       assertTrue("Timeout was expected to be called only once but was called: "
221          + count + " times",
222          count == 1);
223       try
224       {
225          entity.stopTimer();
226          fail("A single timer should expire after the first event and therefore this "
227             + "has to throw an NoSuchObjectLocalException");
228       }
229       catch (RemoteException JavaDoc re)
230       {
231          Throwable JavaDoc lCause = re.detail;
232          if (lCause instanceof ServerException JavaDoc)
233          {
234             lCause = ((ServerException JavaDoc) lCause).detail;
235             if (lCause instanceof NoSuchObjectLocalException JavaDoc)
236             {
237                // This exception is expected -> ignore
238
}
239             else
240             {
241                throw re;
242             }
243          }
244       }
245       entity.remove();
246       logout();
247    }
248
249    private EJBHome JavaDoc getEJBHome(String JavaDoc pJNDIName)
250       throws NamingException JavaDoc
251    {
252       InitialContext JavaDoc lContext = new InitialContext JavaDoc();
253       return (EJBHome JavaDoc) lContext.lookup(pJNDIName);
254    }
255
256    private void login() throws Exception JavaDoc
257    {
258       lc = null;
259       String JavaDoc username = "jduke";
260       char[] password = "theduke".toCharArray();
261       AppCallbackHandler handler = new AppCallbackHandler(username, password);
262       log.debug("Creating LoginContext(ejb-timers)");
263       lc = new LoginContext JavaDoc("ejb-timers", handler);
264       lc.login();
265       log.debug("Created LoginContext, subject="+lc.getSubject());
266    }
267    private void logout() throws Exception JavaDoc
268    {
269       if( lc != null )
270       {
271          lc.logout();
272          lc = null;
273       }
274    }
275 }
276
Popular Tags