FileDocCategorySizeDatePackage
BeanUnitTestCase.javaAPI DocJBoss 4.2.19005Fri Jul 13 20:53:10 BST 2007org.jboss.ejb3.test.clusteredsession.unit

BeanUnitTestCase

public class BeanUnitTestCase extends org.jboss.test.JBossClusteredTestCase
Test SFSB for load-balancing and failover behaviour
author
Ben.Wang@jboss.org
author
Brian Stansberry
version
$Revision: 60310 $

Fields Summary
static boolean
deployed
public static int
test
static Date
startDate
protected final String
namingFactory
protected final String
providerURL
Constructors Summary
public BeanUnitTestCase(String name)


      
   
      super(name);
   
Methods Summary
protected void_sleep(long time)

      try {
         Thread.sleep(time);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   
private voidbasicTest(java.lang.String jndiBinding)

      getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
              +"Trying the context...");

      // Connect to the server0 JNDI
      InitialContext ctx = getInitialContext(0);

      getLog().debug("Basic Test with " + jndiBinding);
      getLog().debug("==================================");
      getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
            +"Looking up " + jndiBinding + "...");
      StatefulRemote stateful = (StatefulRemote) ctx.lookup(jndiBinding);

      stateful.setName("The Code");
      _sleep(300);

      NodeAnswer node1 = stateful.getNodeState ();
      getLog ().debug ("Node 1 ID: " +node1);

      assertEquals("Counter: ", 1, stateful.increment());
      assertEquals("Counter: ", 2, stateful.increment());

      stateful.remove();
      getLog().debug("ok");
   
protected javax.naming.InitialContextgetInitialContext(int node)

      // Connect to the server0 JNDI
      String[] urls = getNamingURLs();
      Properties env1 = new Properties();
      env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
         "org.jnp.interfaces.NamingContextFactory");
      env1.setProperty(Context.PROVIDER_URL, urls[node]);
      return new InitialContext(env1);
   
private voidstatefulBeanCounterFailoverTest(java.lang.String jndiBinding)

  
      getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "+"Trying the context...");

      // Connect to the server0 JNDI
      InitialContext ctx = getInitialContext(0);

      getLog().debug("Test Stateful Bean Counter Failover with " + jndiBinding);
      getLog().debug("=========================================================");
      getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
              +"Looking up " + jndiBinding + "...");
      StatefulRemote stateful = (StatefulRemote) ctx.lookup(jndiBinding);

      stateful.setName("The Code");
      NodeAnswer node1 = stateful.getNodeState ();
      getLog ().debug ("Node 1 ID: " +node1);

      assertEquals("Counter: ", 1, stateful.increment());
      assertEquals("Counter: ", 2, stateful.increment());
      _sleep(300);

      // Now we switch to the other node, simulating a failure on node 1
      //
      stateful.setUpFailover("once");
      NodeAnswer node2 = stateful.getNodeState ();
      assertNotNull("State node: ", node2);
      getLog ().debug ("Node 2 ID : " +node2);

      assertFalse("Failover has occured", node1.nodeId.equals(node2.nodeId));

      assertEquals ("Node 1: ", "The Code", node1.answer);
      assertEquals ("Node 2: ", "The Code", node2.answer);

      assertEquals("Counter: ", 3, stateful.increment());
      assertEquals("Counter: ", 4, stateful.increment());

      stateful.remove();
      getLog().debug("ok");
   
private voidstatefulBeanFailoverTest(java.lang.String jndiBinding)

  
      getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "+"Trying the context...");

      // Connect to the server0 JNDI
      InitialContext ctx = getInitialContext(0);

      getLog().debug("Test Stateful Bean Failover with " + jndiBinding);
      getLog().debug("================================================");
      getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
            +"Looking up " + jndiBinding + "...");
      StatefulRemote stateful = (StatefulRemote) ctx.lookup(jndiBinding);

      stateful.setName("Bupple-Dupple");
      _sleep(300);

      NodeAnswer node1 = stateful.getNodeState ();
      getLog ().debug ("Node 1 ID: " +node1);

      // Now we switch to the other node, simulating a failure on node 1
      //
      stateful.setUpFailover("once");
      NodeAnswer node2 = stateful.getNodeState ();
      assertNotNull("State node: ", node2);
      getLog ().debug ("Node 2 ID : " +node2);

      assertFalse("Failover has occured", node1.nodeId.equals(node2.nodeId));

      assertEquals ("Node 1: ", "Bupple-Dupple", node1.answer);
      assertEquals ("Node 2: ", "Bupple-Dupple", node2.answer);

      // we change our name to see if it replicates to node 1
      //
      stateful.setName ("Changed");
      _sleep(300);

      // now we travel back on node 1
      //
      stateful.setUpFailover("once");
      node1 = stateful.getNodeState ();
      assertNotNull("State node id should not be null: ", node1.nodeId);
      getLog ().debug (node1);

      _sleep(300);
      assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId);

      assertEquals ("Value is not identical on replicated node", "Changed", node1.answer );

      stateful.remove();
      getLog().debug("ok");
   
public static junit.framework.Testsuite()

      final String jarName = "clusteredsession-test.jar";
      Test t1 = JBossClusteredTestCase.getDeploySetup(BeanUnitTestCase.class,
              jarName);
      return t1;
   
public voidtestBasic()

      basicTest("testStateful/remote");
   
public voidtestBasicWithEjbJarXMLOverride()

      basicTest("EjbJarOverrideAnnotationStateful/remote");
   
public voidtestBasicWithXMLOverride()

      basicTest("OverrideAnnotationStateful/remote");
   
public voidtestBasicWithoutAnnotation()

      basicTest("NonAnnotationStateful/remote");
   
public voidtestStatefulBeanCounterFailover()

      statefulBeanCounterFailoverTest("testStateful/remote");
   
public voidtestStatefulBeanCounterFailoverWithEjbJarXMLOverride()

      statefulBeanCounterFailoverTest("EjbJarOverrideAnnotationStateful/remote");
   
public voidtestStatefulBeanCounterFailoverWithXMLOverride()

      statefulBeanCounterFailoverTest("OverrideAnnotationStateful/remote");
   
public voidtestStatefulBeanCounterFailoverWithoutAnnotation()

      statefulBeanCounterFailoverTest("NonAnnotationStateful/remote");
   
public voidtestStatefulBeanFailover()

      statefulBeanFailoverTest("testStateful/remote");
   
public voidtestStatefulBeanFailoverWithEjbJarXMLOverride()

      statefulBeanFailoverTest("EjbJarOverrideAnnotationStateful/remote");
   
public voidtestStatefulBeanFailoverWithXMLOverride()

      statefulBeanFailoverTest("OverrideAnnotationStateful/remote");
   
public voidtestStatefulBeanFailoverWithoutAnnotation()

      statefulBeanFailoverTest("NonAnnotationStateful/remote");