1 7 package org.jboss.test.remoting.deploy; 8 9 import javax.management.MBeanServer ; 10 import javax.management.MBeanServerFactory ; 11 import javax.management.ObjectName ; 12 import org.jboss.remoting.Client; 13 import org.jboss.remoting.InvalidConfigurationException; 14 import org.jboss.remoting.InvocationRequest; 15 import org.jboss.remoting.InvokerLocator; 16 import org.jboss.remoting.ServerInvocationHandler; 17 import org.jboss.remoting.ServerInvoker; 18 import org.jboss.remoting.callback.InvokerCallbackHandler; 19 import org.jboss.remoting.transport.Connector; 20 21 import junit.framework.TestCase; 22 23 26 public class DualDeploymentTestCase extends TestCase 27 { 28 private static int idCounter = 1; 29 30 public DualDeploymentTestCase(String name) 31 { 32 super(name); 33 } 34 35 42 public void testSameLocator() throws Throwable 43 { 44 MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer(); 45 46 String locator1 = "socket://localhost:5701"; 47 String locator2 = "socket://localhost:5701"; 48 49 int id1 = 1; 50 int id2 = 2; 51 52 Connector connector1 = setupServer(locator1, mbeanServer); 53 SampleInvocationHandler invocationHandler1 = new SampleInvocationHandler(id1); 54 connector1.addInvocationHandler("sub1", invocationHandler1); 55 56 Connector connector2 = null; 57 try 58 { 59 connector2 = setupServer(locator2, mbeanServer); 60 SampleInvocationHandler invocationHandler2 = new SampleInvocationHandler(id2); 61 connector2.addInvocationHandler("sub2", invocationHandler2); 62 } 63 catch(InvalidConfigurationException e) 64 { 65 assertTrue("Got InvalidConfigurationException as expected.", true); 66 return; 67 } 68 finally 69 { 70 connector1.stop(); 71 connector1.destroy(); 72 if(connector2 != null) 73 { 74 connector2.stop(); 75 connector2.destroy(); 76 } 77 } 78 assertTrue("Did not get InvalidConfigurationException as expected.", false); 79 80 } 81 82 88 public void testSameSubsystem() throws Throwable 89 { 90 MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer(); 91 92 String locator1 = "socket://localhost:5703"; 93 94 int id1 = 1; 95 int id2 = 2; 96 97 Connector connector1 = setupServer(locator1, mbeanServer); 98 SampleInvocationHandler invocationHandler1 = new SampleInvocationHandler(id1); 99 ServerInvocationHandler previous = connector1.addInvocationHandler("sub1", invocationHandler1); 100 101 assertNull(previous); 102 103 SampleInvocationHandler invocationHandler2 = new SampleInvocationHandler(id2); 104 previous = connector1.addInvocationHandler("sub1", invocationHandler2); 105 106 assertEquals(invocationHandler1, previous); 107 108 ServerInvocationHandler[] handlers1 = connector1.getInvocationHandlers(); 109 110 assertEquals(1, handlers1.length); 111 112 connector1.stop(); 113 connector1.destroy(); 114 115 } 116 117 123 public void testNoSubsystem() throws Throwable 124 { 125 MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer(); 126 127 String locator1 = "socket://localhost:5704"; 128 String locator2 = "socket://localhost:5705"; 129 130 int id1 = 1; 131 int id2 = 2; 132 int id3 = 3; 133 int id4 = 4; 134 135 Connector connector1 = setupServer(locator1, mbeanServer); 136 SampleInvocationHandler invocationHandler1 = new SampleInvocationHandler(id1); 137 connector1.addInvocationHandler("sub1", invocationHandler1); 138 139 SampleInvocationHandler invocationHandler2 = new SampleInvocationHandler(id2); 140 connector1.addInvocationHandler("sub2", invocationHandler2); 141 SampleInvocationHandler invocationHandler3 = new SampleInvocationHandler(id3); 142 connector1.addInvocationHandler("sub3", invocationHandler3); 143 144 Connector connector2 = setupServer(locator2, mbeanServer); 145 SampleInvocationHandler invocationHandler4 = new SampleInvocationHandler(id4); 146 connector2.addInvocationHandler("sub4", invocationHandler4); 147 148 Client client = new Client(new InvokerLocator(locator1)); 149 Client client2 = new Client(new InvokerLocator(locator2)); 150 151 Object ret1 = client.invoke("Do something"); 152 Object ret2 = client2.invoke("Do something"); 153 154 assertEquals("" + id3, ret1); 156 assertEquals("" + id4, ret2); 157 158 connector1.stop(); 159 connector1.destroy(); 160 connector2.stop(); 161 connector2.destroy(); 162 163 } 164 165 public Connector setupServer(String locatorURI, MBeanServer mbeanServer) throws Exception 166 { 167 InvokerLocator locator = new InvokerLocator(locatorURI); 168 System.out.println("Starting remoting server with locator uri of: " + locatorURI); 169 Connector connector = new Connector(); 170 connector.setInvokerLocator(locator.getLocatorURI()); 171 int randomInt = idCounter++; 172 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol() + ",id=" + randomInt); 173 mbeanServer.registerMBean(connector, obj); 174 connector.start(); 175 176 return connector; 177 } 178 179 182 public static class SampleInvocationHandler implements ServerInvocationHandler 183 { 184 private int id = 0; 185 186 public SampleInvocationHandler(int id) 187 { 188 this.id = id; 189 } 190 191 public int getId() 192 { 193 return id; 194 } 195 196 203 public Object invoke(InvocationRequest invocation) throws Throwable 204 { 205 System.out.println("Invocation request is: " + invocation.getParameter()); 207 208 return "" + id; 210 } 211 212 218 public void addListener(InvokerCallbackHandler callbackHandler) 219 { 220 } 222 223 229 public void removeListener(InvokerCallbackHandler callbackHandler) 230 { 231 } 233 234 239 public void setMBeanServer(MBeanServer server) 240 { 241 } 243 244 249 public void setInvoker(ServerInvoker invoker) 250 { 251 } 253 254 } 255 256 257 } | Popular Tags |