001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.spring.integrationtests.tests;
006:
007: import com.tc.test.server.appserver.deployment.Deployment;
008: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
009: import com.tc.test.server.appserver.deployment.Server;
010: import com.tc.test.server.appserver.deployment.ServerTestSetup;
011: import com.tc.test.server.appserver.deployment.WebApplicationServer;
012: import com.tctest.spring.bean.ISingleton;
013: import com.tctest.spring.integrationtests.SpringDeploymentTest;
014:
015: import java.util.ArrayList;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import junit.framework.Test;
020:
021: public class SingletonTest extends SpringDeploymentTest {
022:
023: private static final String REMOTE_SERVICE_NAME = "Singleton";
024: private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
025: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml";
026:
027: private Deployment deployment;
028:
029: private String context = "test-singleton";
030: private String url = "/test-singleton";
031:
032: public static Test suite() {
033: return new ServerTestSetup(SingletonTest.class);
034: }
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: if (deployment == null)
039: deployment = makeDeployment();
040: }
041:
042: public void testSingleton4() throws Exception {
043: runNodes(4);
044: }
045:
046: private void runNodes(int nodeCount) throws Exception {
047: List servers = new ArrayList();
048:
049: for (int i = 0; i < nodeCount; i++) {
050: WebApplicationServer server = makeWebApplicationServer(CONFIG_FILE_FOR_TEST);
051: server.addWarDeployment(deployment, context);
052: server.start();
053: servers.add(server);
054: }
055:
056: // ((WebApplicationServer) servers.get(0)).ping(url);
057:
058: WebApplicationServer prev = null;
059: for (Iterator iter = servers.iterator(); iter.hasNext();) {
060: if (prev == null) {
061: prev = (WebApplicationServer) iter.next();
062: } else {
063: WebApplicationServer server = (WebApplicationServer) iter
064: .next();
065: SingletonStateUtil.assertSingletonShared(prev, server,
066: REMOTE_SERVICE_NAME);
067: assertTransientState(prev, server);
068: prev = server;
069: }
070: }
071: }
072:
073: public static void assertTransientState(Server server1,
074: Server server2) throws Exception {
075: ISingleton singleton1 = (ISingleton) server1.getProxy(
076: ISingleton.class, REMOTE_SERVICE_NAME);
077: ISingleton singleton2 = (ISingleton) server2.getProxy(
078: ISingleton.class, REMOTE_SERVICE_NAME);
079: String originalValue = "aaa";
080:
081: assertEquals(originalValue, singleton1.getTransientValue());
082: assertEquals(originalValue, singleton2.getTransientValue());
083: singleton1.setTransientValue("s1");
084: assertEquals(originalValue, singleton2.getTransientValue());
085: singleton2.setTransientValue("s2");
086: assertEquals("s1", singleton1.getTransientValue());
087: assertEquals("s2", singleton2.getTransientValue());
088:
089: // reset - for 2,4,8-node testing
090: singleton1.setTransientValue(originalValue);
091: singleton2.setTransientValue(originalValue);
092: }
093:
094: private Deployment makeDeployment() throws Exception {
095: DeploymentBuilder builder = makeDeploymentBuilder(context
096: + ".war");
097:
098: builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
099:
100: builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton",
101: ISingleton.class);
102:
103: builder.addDirectoryOrJARContainingClass(ISingleton.class);
104: builder.addDirectoryContainingResource(CONFIG_FILE_FOR_TEST);
105:
106: return builder.makeDeployment();
107: }
108:
109: /*
110: * public StandardTerracottaAppServerConfig buildTCConfig() { StandardTerracottaAppServerConfig tcConfigBuilder =
111: * getConfigBuilder(); SpringConfigBuilder springConfigBuilder =
112: * tcConfigBuilder.getConfigBuilder().getApplication().getSpring(); SpringApplicationConfigBuilder application =
113: * springConfigBuilder.getApplications()[0]; application.setName("test-singleton");
114: * SpringApplicationContextConfigBuilder applicationContext = application.getApplicationContexts()[0];
115: * applicationContext.setPaths(new String[] { "*.xml" }); applicationContext.addBean("singleton");
116: * tcConfigBuilder.build(); logger.debug(tcConfigBuilder.toString()); return tcConfigBuilder; }
117: */
118:
119: // Causes a hang
120: // public void testSingleton2() throws Exception {
121: // testSingleton();
122: // }
123: }
|