/** * @Package: com.scap.servers.scapserver.ejb * @CreateDate Apr 15, 2005 * @Creator eha * * TODO Add more info */ package com.scap.servers.scapserver.ejb; import java.rmi.RemoteException; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import junit.framework.TestCase; import com.scap.servers.scapserver.interfaces.TimerTest; import com.scap.servers.scapserver.interfaces.TimerTestHome; /** * EJB Test Client */ public class TimerTestClient extends TestCase { /** * */ public TimerTestClient() { super(); // TODO Auto-generated constructor stub } /** * @param name */ public TimerTestClient(String name) { super(name); // TODO Auto-generated constructor stub } /** Home interface */ protected TimerTestHome home; private static final int REQUIRED = 0; private static final int REQUIRES_NEW = 1; private static final int NOT_SUPPORTED = 2; private static final int NEVER = 3; private static final int FIRST = REQUIRED; private static final int LAST = NEVER; private static final String[] TRANSTYPE = { "Required", "RequiresNew", "NotSupported", "Never" }; /** * Get the initial naming context */ protected Context getInitialContext() throws Exception { Hashtable props = new Hashtable(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); props.put(Context.PROVIDER_URL, "jnp://localhost:1099"); Context ctx = new InitialContext(props); return ctx; } /** * Get the home interface */ protected com.scap.servers.scapserver.interfaces.TimerTestHome getHome() throws Exception { Context ctx = this.getInitialContext(); Object o = ctx.lookup("ejb/TimerTest"); com.scap.servers.scapserver.interfaces.TimerTestHome intf = (com.scap.servers.scapserver.interfaces.TimerTestHome) PortableRemoteObject.narrow(o, com.scap.servers.scapserver.interfaces.TimerTestHome.class); return intf; } /** * Set up the test case */ protected void setUp() throws Exception { this.home = this.getHome(); } private void startTimer(TimerTest instance, int transType) throws RemoteException { int timersBefore = instance.listAllTimers(); switch (transType) { case REQUIRED: instance.startTimerInTxRequired(); break; case REQUIRES_NEW: instance.startTimerInTxRequiresNew(); break; case NOT_SUPPORTED: instance.startTimerInTxNotSupported(); break; case NEVER: instance.startTimerInTxNever(); break; } int timersAfter = instance.listAllTimers(); if (timersAfter != timersBefore + 1) { fail("Did not delete timer!\nBefore:" + timersBefore + "\nAfter:" + timersAfter); } } private void cancelTimer(TimerTest instance, int transType) { try { int timersBefore = instance.listAllTimers(); switch (transType) { case REQUIRED: instance.cancelTimerInTxRequired(); break; case REQUIRES_NEW: instance.cancelTimerInTxRequiresNew(); break; case NOT_SUPPORTED: instance.cancelTimerInTxNotSupported(); break; case NEVER: instance.cancelTimerInTxNever(); break; } int timersAfter = instance.listAllTimers(); if (timersBefore != timersAfter + 1) { fail("Did not delete timer!\nBefore:" + timersBefore + "\nAfter:" + timersAfter); } } catch (Exception e) { System.out.println("Failed to cancel timer:" + e); } } /** * Test for com.scap.servers.scapserver.interfaces.TimerTest.startTimerInTxRequired() */ public void testTimer() throws Exception { com.scap.servers.scapserver.interfaces.TimerTest instance; // Parameters // Instance creation instance = this.home.create(); int run = 0; for (int start = FIRST; start <= LAST; start++) { for (int cancel = FIRST; cancel <= LAST; cancel++) { System.out.println("Run " + run + " start:" + TRANSTYPE[start] + " cancel:" + TRANSTYPE[cancel]); startTimer(instance, start); cancelTimer(instance, cancel); } } } }