1 4 package com.tctest.spring.integrationtests.tests; 5 6 import com.tctest.spring.bean.FooService; 7 import com.tctest.spring.bean.ISingleton; 8 import com.tctest.spring.integrationtests.framework.AbstractDeploymentTest; 9 import com.tctest.spring.integrationtests.framework.Deployment; 10 import com.tctest.spring.integrationtests.framework.DeploymentBuilder; 11 import com.tctest.spring.integrationtests.framework.Server; 12 import com.tctest.spring.integrationtests.framework.WebApplicationServer; 13 14 17 public class DeploymentTest extends AbstractDeploymentTest { 18 19 private static final String FOO_SERVICE_NAME = "Foo"; 20 private static final String SINGLETON_SERVICE_NAME = "Singleton"; 21 private Deployment singletonWAR; 22 private Deployment fooServiceWAR; 23 24 protected void setUp() throws Exception { 25 super.setUp(); 26 singletonWAR = makeSingletonWAR(); 27 fooServiceWAR = makeFooServiceWAR(); 28 } 29 30 protected void runTest() throws Throwable { 31 System.err.println("Running test method: " + this.getName()); 32 super.runTest(); 33 } 34 35 private Deployment makeSingletonWAR() throws Exception { 36 DeploymentBuilder builder = makeDeploymentBuilder("test-singleton.war"); 37 builder.addBeanDefinitionFile("classpath:/com/tctest/spring/beanfactory.xml"); 38 builder.addRemoteService(SINGLETON_SERVICE_NAME, "singleton", ISingleton.class); 39 builder.addDirectoryOrJARContainingClass(ISingleton.class); 40 builder.addDirectoryContainingResource("/tc-config-files/singleton-tc-config.xml"); 41 return builder.makeDeployment(); 42 } 43 44 private Deployment makeFooServiceWAR() throws Exception { 45 DeploymentBuilder builder = makeDeploymentBuilder("test-parent-child.war"); 46 builder.addBeanDefinitionFile("classpath:/com/tctest/spring/beanfactory-parent-child.xml"); 47 builder.addRemoteService(FOO_SERVICE_NAME, "service1", FooService.class); 48 builder.addDirectoryOrJARContainingClass(ISingleton.class); 49 builder.addDirectoryContainingResource("/tc-config-files/singleton-tc-config.xml"); 50 return builder.makeDeployment(); 51 } 52 53 public void testStartAndStopOneWebAppAndThenStartAnother() throws Exception { 54 startPingAndStop("test-singleton", singletonWAR, "/tc-config-files/singleton-tc-config.xml", 55 new TestServerCallback() { 56 public void test(Server server) throws Exception { 57 ISingleton singleton1 = (ISingleton) server.getProxy(ISingleton.class, SINGLETON_SERVICE_NAME); 58 singleton1.incrementCounter(); 59 60 } 61 }); 62 63 startPingAndStop("test-parent-child", fooServiceWAR, "/tc-config-files/parent-child-tc-config.xml", 64 new TestServerCallback() { 65 public void test(Server server) throws Exception { 66 FooService foo = (FooService) server.getProxy(FooService.class, FOO_SERVICE_NAME); 67 foo.serviceMethod(); 68 } 69 }); 70 } 71 72 private void startPingAndStop(String context, Deployment warFile, String tcConfigPath, TestServerCallback testCallback) 73 throws Exception { 74 WebApplicationServer server = makeWebApplicationServer(tcConfigPath); 75 server.addWarDeployment(warFile, context); 76 server.start(); 77 78 testCallback.test(server); 79 80 server.stopIgnoringExceptions(); 81 } 82 83 public void testDeployingTwoWARsOneDistributedOneNot() throws Exception { 84 Server server1 = makeServerWithTwoWARsOneDistributed(); 85 Server server2 = makeServerWithTwoWARsOneDistributed(); 86 87 server1.start(); 88 server2.start(); 89 90 SingletonStateUtil.assertSingletonShared(server1, server2, SINGLETON_SERVICE_NAME); 91 92 assertFooServiceTransient(server1, server2); 93 } 94 95 private void assertFooServiceTransient(Server server1, Server server2) throws Exception { 96 FooService foo1a = (FooService) server1.getProxy(FooService.class, FOO_SERVICE_NAME); 97 FooService foo2a = (FooService) server2.getProxy(FooService.class, FOO_SERVICE_NAME); 98 assertEquals("rawValue-0", foo1a.serviceMethod()); 99 assertEquals("rawValue-0", foo2a.serviceMethod()); 100 } 101 102 private Server makeServerWithTwoWARsOneDistributed() throws Exception { 103 return makeWebApplicationServer("/tc-config-files/singleton-tc-config.xml").addWarDeployment(singletonWAR, 104 "test-singleton") 105 .addWarDeployment(fooServiceWAR, "test-parent-child"); 106 } 107 108 public void testDeployingTwoDistributedWARs() throws Exception { 109 Server server1 = makeServerWithTwoDistributedWARs(); 110 Server server2 = makeServerWithTwoDistributedWARs(); 111 112 server1.start(); 113 server2.start(); 114 115 SingletonStateUtil.assertSingletonShared(server1, server2, SINGLETON_SERVICE_NAME); 116 117 assertFooServiceShared(server1, server2); 118 } 119 120 private void assertFooServiceShared(Server server1, Server server2) throws Exception { 121 FooService foo1a = (FooService) server1.getProxy(FooService.class, FOO_SERVICE_NAME); 122 FooService foo2a = (FooService) server2.getProxy(FooService.class, FOO_SERVICE_NAME); 123 assertEquals("rawValue-0", foo1a.serviceMethod()); 124 assertEquals("rawValue-1", foo2a.serviceMethod()); 125 } 126 127 private Server makeServerWithTwoDistributedWARs() throws Exception { 128 return makeWebApplicationServer("/tc-config-files/singleton-parent-child-tc-config.xml") 129 .addWarDeployment(singletonWAR, "test-singleton").addWarDeployment(fooServiceWAR, "test-parent-child"); 130 } 131 132 } 133 | Popular Tags |