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.ILifeCycle;
009: import com.tctest.spring.integrationtests.SpringTwoServerTestSetup;
010:
011: import java.util.List;
012:
013: import junit.framework.Test;
014:
015: /**
016: * Test the clustered bean behavior with those life cycle methods 1. setBeanName 2. setApplicationContext 3.
017: * afterPropertiesSet 4. destroy
018: *
019: * @author Liyu Yi
020: */
021: public class LifeCycleTest extends AbstractTwoServerDeploymentTest {
022:
023: private static final String REMOTE_SERVICE_NAME = "LifeCycle";
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/lifecycle-tc-config.xml";
026:
027: private ILifeCycle mLifeCycleBean1;
028: private ILifeCycle mLifeCycleBean2;
029:
030: protected void setUp() throws Exception {
031: super .setUp();
032:
033: mLifeCycleBean1 = (ILifeCycle) server0.getProxy(
034: ILifeCycle.class, REMOTE_SERVICE_NAME);
035: mLifeCycleBean2 = (ILifeCycle) server1.getProxy(
036: ILifeCycle.class, REMOTE_SERVICE_NAME);
037: }
038:
039: public void test() throws Exception {
040: logger.debug("testing bean life cycle");
041:
042: long systemId1 = mLifeCycleBean1.getSystemId();
043: long systemId2 = mLifeCycleBean2.getSystemId();
044:
045: assertTrue("Transient properties also share the same value.",
046: systemId1 != systemId2);
047:
048: List prop1 = mLifeCycleBean1.getProp();
049: List prop2 = mLifeCycleBean2.getProp();
050:
051: // property might be referenced by other beans
052: assertEquals(prop1, prop2);
053: assertTrue(prop1.contains("" + systemId1));
054: assertTrue(prop1.contains("" + systemId2));
055:
056: List records1 = mLifeCycleBean1.getInvocationRecords();
057: List records2 = mLifeCycleBean2.getInvocationRecords();
058:
059: // check regular contract
060: assertTrue("Bean1 afterPropertiesSet not invoked: " + records1,
061: records1.contains("afterPropertiesSet-" + systemId1));
062: assertTrue("Bean2 afterPropertiesSet not invoked: " + records2,
063: records2.contains("afterPropertiesSet-" + systemId2));
064:
065: assertTrue("Bean1 setBeanName not invoked: " + records1,
066: records1.contains("setBeanName-" + systemId1));
067: assertTrue("Bean2 setBeanName not invoked: " + records2,
068: records2.contains("setBeanName-" + systemId2));
069:
070: assertTrue("Bean1 setApplicationContext not invoked: "
071: + records1, records1.contains("setApplicationContext-"
072: + systemId1));
073: assertTrue("Bean2 setApplicationContext not invoked: "
074: + records2, records2.contains("setApplicationContext-"
075: + systemId2));
076:
077: // check distributed behavior
078: assertTrue("Replication failure: " + records1, records1
079: .contains("afterPropertiesSet-" + systemId2));
080: assertTrue("Replication failure: " + records2, records2
081: .contains("afterPropertiesSet-" + systemId1));
082:
083: assertTrue("Replication failure: " + records1, records1
084: .contains("setBeanName-" + systemId2));
085: assertTrue("Replication failure: " + records2, records2
086: .contains("setBeanName-" + systemId1));
087:
088: assertTrue("Replication failure: " + records1, records1
089: .contains("setApplicationContext-" + systemId2));
090: assertTrue("Replication failure: " + records2, records2
091: .contains("setApplicationContext-" + systemId1));
092:
093: mLifeCycleBean1.closeAppCtx();
094: mLifeCycleBean2.closeAppCtx();
095:
096: records1 = mLifeCycleBean1.getInvocationRecords();
097: records2 = mLifeCycleBean2.getInvocationRecords();
098:
099: assertTrue("Bean1 destroy not invoked: " + records1, records1
100: .contains("destroy-" + systemId1));
101: assertTrue("Bean2 destroy not invoked: " + records2, records2
102: .contains("destroy-" + systemId2));
103:
104: assertTrue("Replication failure: " + records1, records1
105: .contains("destroy-" + systemId2));
106: assertTrue("Replication failure: " + records2, records2
107: .contains("destroy-" + systemId1));
108:
109: logger.debug("!!!! Asserts passed !!!");
110: }
111:
112: private static class LifeCycleTestSetup extends
113: SpringTwoServerTestSetup {
114: private LifeCycleTestSetup() {
115: super (LifeCycleTest.class, CONFIG_FILE_FOR_TEST,
116: "test-lifecycle");
117: }
118:
119: protected void configureWar(DeploymentBuilder builder) {
120: builder
121: .addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
122:
123: builder.addRemoteService(RmiServiceExporter.class,
124: "LifeCycle", "lifeCycleBean", ILifeCycle.class);
125:
126: builder
127: .addRemoteServiceBlock("<bean id=\"lifeCycleBean\" class=\"com.tctest.spring.bean.LifeCycleBean\">"
128: + "\n"
129: + "<property name=\"prop\" ref=\"recorder\"/>"
130: + "\n" + "</bean>");
131:
132: }
133: }
134:
135: private static class RmiServiceExporter extends
136: org.springframework.remoting.rmi.RmiServiceExporter {
137: public void destroy() {
138: logger
139: .info("destroy method override in RmiServiceExporter.");
140: }
141: }
142:
143: public static Test suite() {
144: return new LifeCycleTestSetup();
145: }
146:
147: }
|