001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest.spring.integrationtests.tests;
005:
006: import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
007: import org.springframework.web.servlet.DispatcherServlet;
008:
009: import com.tc.test.server.appserver.deployment.AbstractTwoServerDeploymentTest;
010: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
011: import com.tc.test.server.appserver.deployment.ProxyBuilder;
012: import com.tctest.spring.bean.ISingleton;
013: import com.tctest.spring.integrationtests.SpringTwoServerTestSetup;
014:
015: import java.util.HashMap;
016: import java.util.Map;
017:
018: import junit.framework.Test;
019:
020: /**
021: * Serve as an example for using the HttpInvoker framework.
022: */
023: public class SingletonV3Test extends AbstractTwoServerDeploymentTest {
024: private static final String APP_NAME = "test-singleton";
025: private static final String REMOTE_SERVICE_NAME = "singletonservice";
026: private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
027: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml";
028:
029: private ISingleton singleton1;
030: private ISingleton singleton2;
031:
032: protected void setUp() throws Exception {
033: super .setUp();
034: try {
035: String name = APP_NAME + "/http/" + REMOTE_SERVICE_NAME;
036: Map initCtx = new HashMap();
037: initCtx.put(ProxyBuilder.EXPORTER_TYPE_KEY,
038: HttpInvokerServiceExporter.class);
039: singleton1 = (ISingleton) server0.getProxy(
040: ISingleton.class, name, initCtx);
041: singleton2 = (ISingleton) server1.getProxy(
042: ISingleton.class, name, initCtx);
043: } catch (Exception ex) {
044: ex.printStackTrace();
045: throw ex;
046: }
047: }
048:
049: public void testSharedField() throws Exception {
050: logger.debug("testing shared fields");
051:
052: assertEquals(singleton1.getCounter(), singleton2.getCounter());
053: singleton1.incrementCounter();
054: assertEquals(singleton1.getCounter(), singleton2.getCounter());
055: singleton2.incrementCounter();
056: assertEquals(singleton2.getCounter(), singleton1.getCounter());
057:
058: logger.debug("!!!! Asserts passed !!!");
059: }
060:
061: public void testTransientField() throws Exception {
062: logger.debug("Testing transient fields");
063: assertEquals("aaa", singleton1.getTransientValue());
064: assertEquals("aaa", singleton2.getTransientValue());
065: singleton1.setTransientValue("s1");
066: assertEquals("aaa", singleton2.getTransientValue());
067: singleton2.setTransientValue("s2");
068: assertEquals("s1", singleton1.getTransientValue());
069: assertEquals("s2", singleton2.getTransientValue());
070: logger.debug("done testing transient fields");
071: }
072:
073: public void testSharedBooleanField() throws Exception {
074: assertTrue(singleton1.toggleBoolean());
075: assertFalse(singleton2.toggleBoolean());
076: assertTrue(singleton1.toggleBoolean());
077: }
078:
079: private static class SingletonTestSetup extends
080: SpringTwoServerTestSetup {
081:
082: private SingletonTestSetup() {
083: super (SingletonV3Test.class, CONFIG_FILE_FOR_TEST, APP_NAME);
084: }
085:
086: protected void configureWar(DeploymentBuilder builder) {
087: builder
088: .addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
089: builder.addRemoteService(HttpInvokerServiceExporter.class,
090: REMOTE_SERVICE_NAME, "singleton", ISingleton.class);
091: builder.setDispatcherServlet("httpinvoker", "/http/*",
092: DispatcherServlet.class, null, true);
093: }
094:
095: }
096:
097: public static Test suite() {
098: return new SingletonTestSetup();
099: }
100:
101: }
|