1 22 package org.jboss.test.txtimer.test; 23 24 import java.security.Principal ; 25 import java.util.Properties ; 26 27 import javax.ejb.TimerHandle ; 28 import javax.management.MBeanServerConnection ; 29 import javax.management.ObjectName ; 30 import javax.naming.InitialContext ; 31 32 import junit.framework.TestSuite; 33 34 import org.jboss.ejb.txtimer.TimerHandleImpl; 35 import org.jboss.test.JBossTestCase; 36 import org.jboss.test.txtimer.interfaces.TimerEntity; 37 import org.jboss.test.txtimer.interfaces.TimerEntityHome; 38 import org.jboss.test.txtimer.interfaces.TimerFacade; 39 import org.jboss.test.txtimer.interfaces.TimerFacadeHome; 40 41 48 public class EntityBeanTestCase extends JBossTestCase 49 { 50 public EntityBeanTestCase(String name) 51 { 52 super(name); 53 } 54 55 public static TestSuite suite() throws Exception 56 { 57 TestSuite ts = new TestSuite(); 58 ts.addTest(getDeploySetup(EntityBeanTestCase.class, "ejb-txtimer.jar")); 59 return ts; 60 } 61 62 65 public void testSingleEventDuration() throws Exception 66 { 67 InitialContext iniCtx = getInitialContext(); 68 TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME); 69 TimerEntity entity = home.create(new Integer (1)); 70 try 71 { 72 entity.createTimer(500, 0, null); 73 sleep(1000); 74 assertEquals("unexpected call count", 1, entity.getCallCount()); 75 } 76 finally 77 { 78 entity.remove(); 79 } 80 } 81 82 85 public void testInstanceAsscociation() throws Exception 86 { 87 InitialContext iniCtx = getInitialContext(); 88 TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME); 89 TimerEntity entity1 = home.create(new Integer (1)); 90 TimerEntity entity2 = home.create(new Integer (2)); 91 92 try 93 { 94 entity1.createTimer(500, 1000, null); 95 entity2.createTimer(500, 1000, null); 96 sleep(1000); 97 assertEquals("unexpected call count", 1, entity1.getCallCount()); 98 assertEquals("unexpected call count", 1, entity2.getCallCount()); 99 } 100 finally 101 { 102 entity1.remove(); 103 entity2.remove(); 104 } 105 } 106 107 110 public void testEjbTimeoutCallerPrincipal() throws Exception 111 { 112 MBeanServerConnection server = getServer(); 113 String defaultPrincipal = (String ) server.getAttribute(new ObjectName ("jboss.security:service=JaasSecurityManager"), "DefaultUnauthenticatedPrincipal"); 114 115 InitialContext iniCtx = getInitialContext(); 116 TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME); 117 TimerEntity entity = home.create(new Integer (1)); 118 try 119 { 120 entity.createTimer(500, 0, null); 121 sleep(1000); 122 assertEquals("unexpected call count", 1, entity.getCallCount()); 123 124 Principal callerPrincipal = entity.getEjbTimeoutCaller(); 125 assertEquals("unexpected principal", defaultPrincipal, callerPrincipal.getName()); 126 } 127 finally 128 { 129 entity.remove(); 130 } 131 } 132 133 136 public void testReturnTimerHandle() throws Exception 137 { 138 InitialContext iniCtx = getInitialContext(); 139 TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME); 140 TimerEntity entity = home.create(new Integer (1)); 141 try 142 { 143 144 try 145 { 146 TimerHandle handle = (TimerHandle ) entity.createTimerReturnHandle(500); 147 fail("TimerHandle should not pass through the remote interface: " + handle); 148 } 149 catch (Exception e) 150 { 151 assertTrue("Timer list should be empty", entity.getTimers().size() == 0); 152 sleep(1000); 153 assertEquals("unexpected call count", 0, entity.getCallCount()); 154 } 155 156 try 157 { 158 TimerHandle handle = TimerHandleImpl.parse("[[id=jboss.j2ee:jndiName=test/txtimer/TimerEntity,service=EJB,pk=1],created=10-Apr-2004 20:16:11.000,first=10-Apr-2004 20:16:11.000,periode=0]"); 159 String retStr = entity.passTimerHandle(handle); 160 fail("TimerHandle should not pass through the remote interface: " + retStr); 161 } 162 catch (Exception e) 163 { 164 assertTrue("Timer list should be empty", entity.getTimers().size() == 0); 165 sleep(1000); 166 assertEquals("unexpected call count", 0, entity.getCallCount()); 167 } 168 } 169 finally 170 { 171 entity.remove(); 172 } 173 } 174 175 178 public void testRollbackAfterCreate() throws Exception 179 { 180 InitialContext iniCtx = getInitialContext(); 181 TimerEntityHome entityHome = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME); 182 TimerEntity entity = entityHome.create(new Integer (1)); 183 184 TimerFacadeHome facadeHome = (TimerFacadeHome) iniCtx.lookup(TimerFacadeHome.JNDI_NAME); 185 TimerFacade facade = facadeHome.create(); 186 try 187 { 188 facade.rollbackAfterCreateEntity(500); 189 assertTrue("Timer list should be empty", entity.getTimers().size() == 0); 190 sleep(1000); 191 assertEquals("unexpected call count", 0, entity.getCallCount()); 192 } 193 finally 194 { 195 entity.remove(); 196 } 197 } 198 199 202 public void testRollbackAfterCancel() throws Exception 203 { 204 InitialContext iniCtx = getInitialContext(); 205 TimerEntityHome entityHome = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME); 206 TimerEntity entity = entityHome.create(new Integer (1)); 207 208 TimerFacadeHome facadeHome = (TimerFacadeHome) iniCtx.lookup(TimerFacadeHome.JNDI_NAME); 209 TimerFacade facade = facadeHome.create(); 210 try 211 { 212 entity.createTimer(500, 0, null); 213 facade.rollbackAfterCancelEntity(); 214 sleep(1000); 215 assertEquals("unexpected call count", 1, entity.getCallCount()); 216 } 217 finally 218 { 219 entity.remove(); 220 } 221 } 222 223 226 public void testRetryAfterRollback() throws Exception 227 { 228 InitialContext iniCtx = getInitialContext(); 229 TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME); 230 TimerEntity entity = home.create(new Integer (1)); 231 232 try 233 { 234 Properties props = new Properties (); 235 props.setProperty("rollback", "true"); 236 237 entity.createTimer(500, 0, props); 238 sleep(1000); 239 240 assertEquals("unexpected call count", 1, entity.getCallCount()); 243 } 244 finally 245 { 246 entity.remove(); 247 } 248 } 249 } 250 | Popular Tags |