1 4 package com.tctest.spring.integrationtests.tests; 5 6 import com.tctest.spring.bean.ISingleton; 7 import com.tctest.spring.integrationtests.framework.AbstractDeploymentTest; 8 import com.tctest.spring.integrationtests.framework.Deployment; 9 import com.tctest.spring.integrationtests.framework.DeploymentBuilder; 10 import com.tctest.spring.integrationtests.framework.Server; 11 import com.tctest.spring.integrationtests.framework.WebApplicationServer; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 public class SingletonTest extends AbstractDeploymentTest { 18 19 private static final String REMOTE_SERVICE_NAME = "Singleton"; 20 private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml"; 21 private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml"; 22 23 private Deployment deployment; 24 25 private String context = "test-singleton"; 26 private String url = "/test-singleton"; 27 28 protected void setUp() throws Exception { 29 super.setUp(); 30 if (deployment == null) deployment = makeDeployment(); 31 } 32 33 public void testSingleton2() throws Exception { 34 runNodes(2); 35 } 36 37 public void testSingleton4() throws Exception { 38 runNodes(4); 39 } 40 41 public void testSingleton8() throws Exception { 42 runNodes(8); 43 } 44 45 private void runNodes(int nodeCount) throws Exception { 46 List servers = new ArrayList (); 47 48 for (int i = 0; i < nodeCount; i++) { 49 WebApplicationServer server = makeWebApplicationServer(CONFIG_FILE_FOR_TEST); 50 server.addWarDeployment(deployment, context); 51 server.start(); 52 servers.add(server); 53 } 54 55 57 WebApplicationServer prev = null; 58 for (Iterator iter = servers.iterator(); iter.hasNext();) { 59 if (prev == null) { 60 prev = (WebApplicationServer) iter.next(); 61 } else { 62 WebApplicationServer server = (WebApplicationServer) iter.next(); 63 SingletonStateUtil.assertSingletonShared(prev, server, REMOTE_SERVICE_NAME); 64 assertTransientState(prev, server); 65 prev = server; 66 } 67 } 68 } 69 70 public static void assertTransientState(Server server1, Server server2) throws Exception { 71 ISingleton singleton1 = (ISingleton) server1.getProxy(ISingleton.class, REMOTE_SERVICE_NAME); 72 ISingleton singleton2 = (ISingleton) server2.getProxy(ISingleton.class, REMOTE_SERVICE_NAME); 73 String originalValue = "aaa"; 74 75 assertEquals(originalValue, singleton1.getTransientValue()); 76 assertEquals(originalValue, singleton2.getTransientValue()); 77 singleton1.setTransientValue("s1"); 78 assertEquals(originalValue, singleton2.getTransientValue()); 79 singleton2.setTransientValue("s2"); 80 assertEquals("s1", singleton1.getTransientValue()); 81 assertEquals("s2", singleton2.getTransientValue()); 82 83 singleton1.setTransientValue(originalValue); 85 singleton2.setTransientValue(originalValue); 86 } 87 88 private Deployment makeDeployment() throws Exception { 89 DeploymentBuilder builder = makeDeploymentBuilder(context + ".war"); 90 91 builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST); 92 93 builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton", ISingleton.class); 94 95 builder.addDirectoryOrJARContainingClass(ISingleton.class); 96 builder.addDirectoryContainingResource(CONFIG_FILE_FOR_TEST); 97 98 return builder.makeDeployment(); 99 } 100 101 110 111 } 116 | Popular Tags |