1 7 8 package org.jboss.ejb3.test.clusteredsession.unit; 9 10 import org.jboss.test.JBossClusteredTestCase; 11 12 import javax.naming.Context ; 13 import javax.naming.InitialContext ; 14 import java.util.Date ; 15 import java.util.Properties ; 16 17 import junit.framework.Test; 18 import org.jboss.ejb3.test.clusteredsession.StatefulRemote; 19 import org.jboss.ejb3.test.clusteredsession.NodeAnswer; 20 21 27 public class BeanUnitTestCase 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 BeanUnitTestCase (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(BeanUnitTestCase.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 public void testBasic() 63 throws Exception 64 { 65 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- " 66 +"Trying the context..."); 67 68 InitialContext ctx = getInitialContext(0); 70 71 getLog().debug("Test Stateful Bean"); 72 getLog().debug("=================================="); 73 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- " 74 +"Looking up testBasic..."); 75 StatefulRemote stateful = (StatefulRemote) ctx.lookup("testStateful/remote"); 76 77 stateful.setName("The Code"); 78 _sleep(300); 79 80 NodeAnswer node1 = stateful.getNodeState (); 81 getLog ().debug ("Node 1 ID: " +node1); 82 83 assertEquals("Counter: ", 1, stateful.increment()); 84 assertEquals("Counter: ", 2, stateful.increment()); 85 86 stateful.remove(); 87 getLog().debug("ok"); 88 } 89 90 public void testStatefulBeanCounterFailover() 91 throws Exception 92 { 93 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "+"Trying the context..."); 94 95 InitialContext ctx = getInitialContext(0); 97 98 getLog().debug("Test Stateful Bean Failover"); 99 getLog().debug("=================================="); 100 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- " 101 +"Looking up testStateful..."); 102 StatefulRemote stateful = (StatefulRemote) ctx.lookup("testStateful/remote"); 103 104 stateful.setName("The Code"); 105 NodeAnswer node1 = stateful.getNodeState (); 106 getLog ().debug ("Node 1 ID: " +node1); 107 108 assertEquals("Counter: ", 1, stateful.increment()); 109 assertEquals("Counter: ", 2, stateful.increment()); 110 _sleep(300); 111 112 stateful.setUpFailover("once"); 115 NodeAnswer node2 = stateful.getNodeState (); 116 assertNotNull("State node: ", node2); 117 getLog ().debug ("Node 2 ID : " +node2); 118 119 assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId); 120 121 assertEquals ("Node 1: ", "The Code", node1.answer); 122 assertEquals ("Node 2: ", "The Code", node2.answer); 123 124 assertEquals("Counter: ", 3, stateful.increment()); 125 assertEquals("Counter: ", 4, stateful.increment()); 126 127 stateful.remove(); 128 getLog().debug("ok"); 129 } 130 131 public void testStatefulBeanFailover() 132 throws Exception 133 { 134 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "+"Trying the context..."); 135 136 InitialContext ctx = getInitialContext(0); 138 139 getLog().debug("Test Stateful Bean Failover"); 140 getLog().debug("=================================="); 141 getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- " 142 +"Looking up testStateful..."); 143 StatefulRemote stateful = (StatefulRemote) ctx.lookup("testStateful/remote"); 144 145 stateful.setName("Bupple-Dupple"); 146 _sleep(300); 147 148 NodeAnswer node1 = stateful.getNodeState (); 149 getLog ().debug ("Node 1 ID: " +node1); 150 151 stateful.setUpFailover("once"); 154 NodeAnswer node2 = stateful.getNodeState (); 155 assertNotNull("State node: ", node2); 156 getLog ().debug ("Node 2 ID : " +node2); 157 158 assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId); 159 160 assertEquals ("Node 1: ", "Bupple-Dupple", node1.answer); 161 assertEquals ("Node 2: ", "Bupple-Dupple", node2.answer); 162 163 stateful.setName ("Changed"); 166 _sleep(300); 167 168 stateful.setUpFailover("once"); 171 node1 = stateful.getNodeState (); 172 assertNotNull("State node id should not be null: ", node1.nodeId); 173 getLog ().debug (node1); 174 175 _sleep(300); 176 assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId); 177 178 assertEquals ("Value is not identical on replicated node", "Changed", node1.answer ); 179 180 stateful.remove(); 181 getLog().debug("ok"); 182 } 183 184 protected void _sleep(long time) 185 { 186 try { 187 Thread.sleep(time); 188 } catch (InterruptedException e) { 189 e.printStackTrace(); 190 } 191 } 192 } 193 | Popular Tags |