1 8 9 package org.jboss.ejb3.test.simplecluster.unit; 10 11 13 14 import java.util.Properties ; 15 16 import javax.management.ObjectName ; 17 import javax.naming.InitialContext ; 18 19 import junit.framework.Test; 20 import junit.framework.TestSuite; 21 22 import org.jboss.ejb3.test.simplecluster.Session; 23 import org.jboss.ejb3.test.simplecluster.StatefulRemote; 24 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 25 import org.jboss.test.JBossClusteredTestCase; 26 import org.jboss.test.JBossTestClusteredSetup; 27 28 34 35 public class SimpleClusterUnitTestCase 36 extends JBossClusteredTestCase 37 { 38 org.apache.log4j.Category log = getLog(); 39 40 static String stoppedAddress; 41 static Properties prop = new Properties (); 42 43 44 public SimpleClusterUnitTestCase(String name) 45 { 46 super(name); 47 48 prop.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); 49 prop.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); 50 } 51 52 protected static void sleepThread(long msecs) 53 { 54 try { 55 Thread.sleep(msecs); 56 } catch (InterruptedException e) { 57 e.printStackTrace(); 58 } 59 } 60 61 62 public void testAll() throws Exception 63 { 64 System.out.println("Starting test" + new java.util.Date ()); 65 String node0 = System.getProperty("jbosstest.cluster.node0"); 66 String node1 = System.getProperty("jbosstest.cluster.node1"); 67 68 prop.put("java.naming.provider.url", "jnp://" + node0 + ":1099"); 69 70 InitialContext ctx = new InitialContext (prop); 71 72 StatefulRemote stateful = (StatefulRemote) ctx.lookup(StatefulRemote.class.getName()); 73 Session stateless = (Session)ctx.lookup(Session.class.getName()); 74 75 int last = 0; 76 for (int i = 0 ; i < 20 ; i++) 77 { 78 stateless.test(); 79 int current = stateful.increment(); 80 System.out.println("Got value: " + current); 81 assertEquals("Wrong return value", current, last + 1); 82 last = current; 83 sleepThread(500); 84 85 if (i == 10) 86 { 87 takeDownActiveInstance(stateful, prop); 88 sleepThread(2000); 89 } 90 91 } 92 } 93 94 protected void takeDownActiveInstance(StatefulRemote stateful, Properties prop) throws Exception 95 { 96 String address = stateful.getHostAddress(); 97 stopJBossInstance(address); 98 stoppedAddress = address; 99 } 100 101 protected static void stopJBossInstance(String address)throws Exception 102 { 103 prop.put("java.naming.provider.url", "jnp://" + address + ":1099"); 104 InitialContext ctx = new InitialContext (prop); 105 RMIAdaptor server = (RMIAdaptor)ctx.lookup("jmx/rmi/RMIAdaptor"); 106 server.invoke(new ObjectName ("jboss.system:type=Server"), "shutdown", new Object [0], new String [0]); 107 108 sleepThread(10000); 109 } 110 111 public static Test suite() throws Exception 112 { 113 Class clazz = SimpleClusterUnitTestCase.class; 114 final String jarName = "simplecluster-test.ejb3"; 115 TestSuite suite = new TestSuite(); 116 117 suite.addTest(new SimpleClusterUnitTestCase("testServerFound")); 121 suite.addTest(new SimpleClusterUnitTestCase("testAll")); 122 123 JBossTestClusteredSetup wrapper = new JBossTestClusteredSetup(suite) 124 { 125 protected void setUp() throws Exception 126 { 127 if (jarName == null) return; 128 deploymentException = null; 129 try 130 { 131 this.deploy(jarName); 132 this.getLog().debug("deployed package: " + jarName); 133 } 134 catch (Exception ex) 135 { 136 deploymentException = ex; 138 } 139 140 synchronized (this) 142 { 143 wait(2000); 144 } 145 } 146 147 protected void tearDown() throws Exception 148 { 149 String node0 = System.getProperty("jbosstest.cluster.node0"); 150 String node1 = System.getProperty("jbosstest.cluster.node1"); 151 if (node0.equals(stoppedAddress)) 152 { 153 stopJBossInstance(node1); 154 } 155 else if (node1.equals(stoppedAddress)) 156 { 157 stopJBossInstance(node0); 158 } 159 } 160 }; 161 162 return wrapper; 163 } 164 } 165 | Popular Tags |