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 com.tc.test.server.appserver.deployment.AbstractTwoServerDeploymentTest;
007: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
008: import com.tctest.spring.bean.ISingleton;
009: import com.tctest.spring.integrationtests.SpringTwoServerTestSetup;
010:
011: import junit.extensions.TestSetup;
012: import junit.framework.Test;
013:
014: /**
015: * Test the clustered bean behavior with multiple contexts;
016: * verify that the clustering behavior is within "same" context - context with the same
017: *
018: * @author Liyu Yi
019: */
020: public class SingletonFromAnotherContextTest extends
021: AbstractTwoServerDeploymentTest {
022:
023: private static final String REMOTE_SERVICE_NAME = "Singleton";
024: private static final String ANOTHER_REMOTE_SERVICE_NAME = "AnotherSingleton";
025: private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-another.xml";
026: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/anothersingleton-tc-config.xml";
027:
028: private ISingleton singleton1N1;
029: private ISingleton singleton2N1;
030:
031: private ISingleton singleton1N2;
032: private ISingleton singleton2N2;
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036:
037: singleton1N1 = (ISingleton) server0.getProxy(ISingleton.class,
038: REMOTE_SERVICE_NAME);
039: singleton2N1 = (ISingleton) server0.getProxy(ISingleton.class,
040: ANOTHER_REMOTE_SERVICE_NAME);
041:
042: singleton1N2 = (ISingleton) server1.getProxy(ISingleton.class,
043: REMOTE_SERVICE_NAME);
044: singleton2N2 = (ISingleton) server1.getProxy(ISingleton.class,
045: ANOTHER_REMOTE_SERVICE_NAME);
046: }
047:
048: public void testSingletonFromAnotherContext() throws Exception {
049: logger.debug("testing singleton from another context");
050:
051: // check pre-condition
052: assertEquals(0, singleton1N1.getCounter());
053: assertEquals(0, singleton1N2.getCounter());
054:
055: assertEquals(0, singleton2N1.getCounter());
056: assertEquals(0, singleton2N2.getCounter());
057:
058: singleton1N1.incrementCounter();
059: singleton1N2.incrementCounter();
060:
061: // only singleton1s are getting the changes
062: assertEquals(2, singleton1N1.getCounter());
063: assertEquals(2, singleton1N2.getCounter());
064:
065: assertEquals(0, singleton2N1.getCounter());
066: assertEquals(0, singleton2N2.getCounter());
067:
068: singleton2N1.incrementCounter();
069: singleton2N2.incrementCounter();
070:
071: // only singleton2s are getting the changes
072: assertEquals(2, singleton2N1.getCounter());
073: assertEquals(2, singleton2N2.getCounter());
074:
075: assertEquals(2, singleton1N1.getCounter());
076: assertEquals(2, singleton1N2.getCounter());
077:
078: logger.debug("!!!! Asserts passed !!!");
079: }
080:
081: private static class InnerTestSetup extends
082: SpringTwoServerTestSetup {
083: private InnerTestSetup() {
084: super (SingletonFromAnotherContextTest.class,
085: CONFIG_FILE_FOR_TEST, "test-anothersingleton");
086: }
087:
088: protected void configureWar(DeploymentBuilder builder) {
089: builder
090: .addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
091: builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton",
092: ISingleton.class);
093: builder.addRemoteService(ANOTHER_REMOTE_SERVICE_NAME,
094: "anotherSingleton", ISingleton.class);
095: }
096: }
097:
098: /**
099: * JUnit test loader entry point
100: */
101: public static Test suite() {
102: TestSetup setup = new InnerTestSetup();
103: return setup;
104: }
105: }
|