1 22 package org.jboss.test.timer.test; 23 24 import java.rmi.RemoteException ; 25 import java.rmi.ServerException ; 26 import javax.ejb.EJBHome ; 27 import javax.ejb.NoSuchObjectLocalException ; 28 import javax.naming.InitialContext ; 29 import javax.naming.NamingException ; 30 import javax.security.auth.login.LoginContext ; 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 46 public class SecureTimerUnitTestCase extends JBossTestCase 47 { 48 49 private static final String EJB_TIMER_XAR = "ejb-timer.ear"; 50 51 private static final int SHORT_PERIOD = 1 * 1000; private LoginContext lc; 53 54 57 public static Test suite() throws Exception 58 { 59 return JBossTestCase.getDeploySetup(SecureTimerUnitTestCase.class, EJB_TIMER_XAR); 60 } 61 62 65 public SecureTimerUnitTestCase(String pName) 66 { 67 super(pName); 68 } 69 70 public void tearDown() 71 { 72 } 73 74 79 public void testSecuredStatefulSessionBeanTimer() 80 throws Exception 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 re) 91 { 92 Throwable lCause = re.detail; 93 if (lCause instanceof ServerException ) 94 { 95 lCause = ((ServerException ) lCause).detail; 96 if (lCause instanceof IllegalStateException ) 97 { 98 } 100 else 101 { 102 throw re; 103 } 104 } 105 } 106 logout(); 107 } 108 109 114 public void testSecuredStatelessSessionBeanTimer() 115 throws Exception 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 141 public void testSecuredStatelessSessionBeanSingleTimer() 142 throws Exception 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 re) 160 { 161 Throwable lCause = re.detail; 162 if (lCause instanceof ServerException ) 163 { 164 lCause = ((ServerException ) lCause).detail; 165 if (lCause instanceof NoSuchObjectLocalException ) 166 { 167 } 169 else 170 { 171 throw re; 172 } 173 } 174 } 175 bean.remove(); 176 logout(); 177 } 178 179 184 public void testSecuredEntityBeanTimer() 185 throws Exception 186 { 187 login(); 188 TimerEntityHome home = (TimerEntityHome) getEJBHome(TimerEntityHome.SECURED_JNDI_NAME); 189 TimerEntity entity = home.create(new Integer (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 211 public void testSecuredEntityBeanSingleTimer() 212 throws Exception 213 { 214 login(); 215 TimerEntityHome home = (TimerEntityHome) getEJBHome(TimerEntityHome.SECURED_JNDI_NAME); 216 TimerEntity entity = home.create(new Integer (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 re) 230 { 231 Throwable lCause = re.detail; 232 if (lCause instanceof ServerException ) 233 { 234 lCause = ((ServerException ) lCause).detail; 235 if (lCause instanceof NoSuchObjectLocalException ) 236 { 237 } 239 else 240 { 241 throw re; 242 } 243 } 244 } 245 entity.remove(); 246 logout(); 247 } 248 249 private EJBHome getEJBHome(String pJNDIName) 250 throws NamingException 251 { 252 InitialContext lContext = new InitialContext (); 253 return (EJBHome ) lContext.lookup(pJNDIName); 254 } 255 256 private void login() throws Exception 257 { 258 lc = null; 259 String 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 ("ejb-timers", handler); 264 lc.login(); 265 log.debug("Created LoginContext, subject="+lc.getSubject()); 266 } 267 private void logout() throws Exception 268 { 269 if( lc != null ) 270 { 271 lc.logout(); 272 lc = null; 273 } 274 } 275 } 276 | Popular Tags |