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: /**
022: * This is a simple smoke test for the Spring testing framework extensions. Don't make it more elaborate, i.e. spawn
023: * more than 2 servers etc
024: */
025: public class SpringTestingFrameworkSmokeTest extends
026: SpringDeploymentTest {
027:
028: private static final String APP_NAME = "test-singleton";
029: private static final String REMOTE_SERVICE_NAME = "Singleton";
030: private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
031: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml";
032:
033: private Deployment deployment;
034:
035: String context = APP_NAME;
036: String url = "/" + context;
037:
038: public static Test suite() {
039: return new ServerTestSetup(
040: SpringTestingFrameworkSmokeTest.class);
041: }
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045: if (deployment == null)
046: deployment = makeDeployment();
047: }
048:
049: public void testSingleton2() throws Exception {
050: runNodes(2);
051: }
052:
053: private void runNodes(int nodeCount) throws Exception {
054: List servers = new ArrayList();
055:
056: for (int i = 0; i < nodeCount; i++) {
057: WebApplicationServer server = makeWebApplicationServer(CONFIG_FILE_FOR_TEST);
058: server.addWarDeployment(deployment, context);
059: server.start();
060: servers.add(server);
061: }
062:
063: // ((WebApplicationServer) servers.get(0)).ping(url);
064:
065: WebApplicationServer prev = null;
066: for (Iterator iter = servers.iterator(); iter.hasNext();) {
067: if (prev == null) {
068: prev = (WebApplicationServer) iter.next();
069: } else {
070: WebApplicationServer server = (WebApplicationServer) iter
071: .next();
072: SingletonStateUtil.assertSingletonShared(prev, server,
073: REMOTE_SERVICE_NAME);
074: assertTransientState(prev, server);
075: prev = server;
076: }
077: }
078: }
079:
080: public static void assertTransientState(Server server1,
081: Server server2) throws Exception {
082: ISingleton singleton1 = (ISingleton) server1.getProxy(
083: ISingleton.class, REMOTE_SERVICE_NAME);
084: ISingleton singleton2 = (ISingleton) server2.getProxy(
085: ISingleton.class, REMOTE_SERVICE_NAME);
086: String originalValue = "aaa";
087:
088: assertEquals(originalValue, singleton1.getTransientValue());
089: assertEquals(originalValue, singleton2.getTransientValue());
090: singleton1.setTransientValue("s1");
091: assertEquals(originalValue, singleton2.getTransientValue());
092: singleton2.setTransientValue("s2");
093: assertEquals("s1", singleton1.getTransientValue());
094: assertEquals("s2", singleton2.getTransientValue());
095:
096: // reset - for 2,4,8-node testing
097: singleton1.setTransientValue(originalValue);
098: singleton2.setTransientValue(originalValue);
099: }
100:
101: private Deployment makeDeployment() throws Exception {
102: DeploymentBuilder builder = makeDeploymentBuilder(APP_NAME
103: + ".war");
104:
105: builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
106:
107: builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton",
108: ISingleton.class);
109:
110: builder.addDirectoryOrJARContainingClass(ISingleton.class);
111: builder.addDirectoryContainingResource(CONFIG_FILE_FOR_TEST);
112:
113: return builder.makeDeployment();
114: }
115: }
|