1 7 8 package org.jboss.ejb3.test.clusteredsession.unit; 9 10 import org.jboss.test.JBossClusteredTestCase; 11 import org.jboss.ejb3.test.clusteredsession.NodeAnswer; 12 import org.jboss.ejb3.test.clusteredsession.nested.ParentStatefulRemote; 13 14 import javax.naming.Context ; 15 import javax.naming.InitialContext ; 16 import java.util.Date ; 17 import java.util.Properties ; 18 19 import junit.framework.Test; 20 21 27 public class NestedBeanUnitTestCase extends JBossClusteredTestCase 28 { 29 static boolean deployed = false; 30 public static int test = 0; 31 static Date startDate = new Date (); 32 33 protected final String namingFactory = 34 System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 35 36 protected final String providerURL = 37 System.getProperty(Context.PROVIDER_URL); 38 39 public NestedBeanUnitTestCase (String name) 40 { 41 super(name); 42 } 43 44 public static Test suite() throws Exception 45 { 46 final String jarName = "clusteredsession-test.jar"; 47 Test t1 = JBossClusteredTestCase.getDeploySetup(NestedBeanUnitTestCase.class, 48 jarName); 49 return t1; 50 } 51 52 protected InitialContext getInitialContext(int node) throws Exception { 53 String [] urls = getNamingURLs(); 55 Properties env1 = new Properties (); 56 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 57 "org.jnp.interfaces.NamingContextFactory"); 58 env1.setProperty(Context.PROVIDER_URL, urls[node]); 59 return new InitialContext (env1); 60 } 61 62 63 public void testBasic() 64 throws Exception 65 { 66 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- " 67 +"Trying the context..."); 68 69 InitialContext ctx = getInitialContext(0); 71 72 getLog().debug("Test Nested Stateful Bean"); 73 getLog().debug("=================================="); 74 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- " 75 +"Looking up testBasic..."); 76 ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote"); 77 78 stateful.reset(); 79 stateful.setName("The Code"); 80 _sleep(300); 81 82 NodeAnswer node1 = stateful.getNodeState (); 83 getLog ().debug ("Node 1 ID: " +node1); 84 85 assertEquals("Counter: ", 1, stateful.increment()); 86 assertEquals("Counter: ", 2, stateful.increment()); 87 88 stateful.remove(); 89 getLog().debug("ok"); 90 _sleep(11000); 91 } 92 93 public void testPassivation() 94 throws Exception 95 { 96 getLog().debug(++NestedBeanUnitTestCase.test +"- " 97 +"Trying the context..."); 98 99 InitialContext ctx = getInitialContext(0); 101 102 getLog().debug("Test Nested Stateful Bean"); 103 getLog().debug("=================================="); 104 getLog().debug(++NestedBeanUnitTestCase.test +"- " 105 +"Looking up remote..."); 106 ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote"); 107 108 stateful.setName("The Code"); 109 _sleep(300); 110 111 NodeAnswer node1 = stateful.getNodeState (); 112 getLog ().debug ("Node 1 ID: " +node1); 113 assertEquals("Counter: ", 1, stateful.increment()); 115 assertEquals("Counter: ", 2, stateful.increment()); 116 assertEquals("SLSB state: ", "test", stateful.getStatelessState().getAnswer()); 117 stateful.reset(); 118 _sleep(11000); 119 assertEquals("Counter: ", 3, stateful.increment()); 120 assertEquals("SLSB state: ", "test", stateful.getStatelessState().getAnswer()); 121 assertEquals(1, stateful.getPrePassivate()); 122 assertEquals(1, stateful.getPostActivate()); 123 assertEquals(1, stateful.getNestedPrePassivate()); 124 assertEquals(1, stateful.getNestedPostActivate()); 125 126 stateful.remove(); 127 getLog().debug("ok: done"); 128 _sleep(12000); 129 } 130 131 public void testStatefulBeanCounterFailover() 132 throws Exception 133 { 134 getLog().debug(++NestedBeanUnitTestCase.test +"- "+"Trying the context..."); 135 136 InitialContext ctx = getInitialContext(0); 138 139 getLog().debug("Test Stateful Bean Failover"); 140 getLog().debug("=================================="); 141 getLog().debug(++NestedBeanUnitTestCase.test +"- " 142 +"Looking up testParentStateful..."); 143 ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote"); 144 145 stateful.setName("The Code"); 146 NodeAnswer node1 = stateful.getNodeState (); 147 getLog ().debug ("Node 1 ID: " +node1); 148 149 assertEquals("Counter: ", 1, stateful.increment()); 150 assertEquals("Counter: ", 2, stateful.increment()); 151 _sleep(300); 152 153 stateful.setUpFailover("once"); 156 NodeAnswer node2 = stateful.getNodeState (); 157 assertNotNull("State node: ", node2); 158 getLog ().debug ("Node 2 ID : " +node2); 159 160 assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId); 161 162 assertEquals ("Node 1: ", "The Code", node1.answer); 163 assertEquals ("Node 2: ", "The Code", node2.answer); 164 165 assertEquals("Counter: ", 3, stateful.increment()); 166 assertEquals("Counter: ", 4, stateful.increment()); 167 168 stateful.remove(); 169 getLog().debug("ok"); 170 } 171 172 protected void _sleep(long time) 173 { 174 try { 175 Thread.sleep(time); 176 } catch (InterruptedException e) { 177 e.printStackTrace(); 178 } 179 } 180 } 181 | Popular Tags |