KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > tests > LifeCycleTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.spring.integrationtests.tests;
5
6 import com.tctest.spring.bean.ILifeCycle;
7 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
8 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
9
10 import java.util.List JavaDoc;
11
12 import junit.extensions.TestSetup;
13 import junit.framework.Test;
14
15 /**
16  * Test the clustered bean behavior with those life cycle methods 1. setBeanName 2. setApplicationContext 3.
17  * afterPropertiesSet 4. destroy
18  *
19  * @author Liyu Yi
20  */

21 public class LifeCycleTest extends AbstractTwoServerDeploymentTest {
22
23   private static final String JavaDoc REMOTE_SERVICE_NAME = "LifeCycle";
24   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
25   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/lifecycle-tc-config.xml";
26
27   private static ILifeCycle mLifeCycleBean1;
28   private static ILifeCycle mLifeCycleBean2;
29
30   public void test() throws Exception JavaDoc {
31
32     logger.debug("testing bean life cycle");
33
34     long systemId1 = mLifeCycleBean1.getSystemId();
35     long systemId2 = mLifeCycleBean2.getSystemId();
36
37     assertTrue("Transient properties also share the same value.", systemId1 != systemId2);
38
39     List JavaDoc prop1 = mLifeCycleBean1.getProp();
40     List JavaDoc prop2 = mLifeCycleBean2.getProp();
41
42     // property might be referenced by other beans
43
assertEquals(prop1, prop2);
44     assertTrue(prop1.contains("" + systemId1));
45     assertTrue(prop1.contains("" + systemId2));
46
47     List JavaDoc records1 = mLifeCycleBean1.getInvocationRecords();
48     List JavaDoc records2 = mLifeCycleBean2.getInvocationRecords();
49
50     // check regular contract
51
assertTrue("Bean1 afterPropertiesSet not invoked: " + records1, records1
52         .contains("afterPropertiesSet-" + systemId1));
53     assertTrue("Bean2 afterPropertiesSet not invoked: " + records2, records2
54         .contains("afterPropertiesSet-" + systemId2));
55
56     assertTrue("Bean1 setBeanName not invoked: " + records1, records1.contains("setBeanName-" + systemId1));
57     assertTrue("Bean2 setBeanName not invoked: " + records2, records2.contains("setBeanName-" + systemId2));
58
59     assertTrue("Bean1 setApplicationContext not invoked: " + records1, records1.contains("setApplicationContext-"
60         + systemId1));
61     assertTrue("Bean2 setApplicationContext not invoked: " + records2, records2.contains("setApplicationContext-"
62         + systemId2));
63
64     // check distributed behavior
65
assertTrue("Replication failure: " + records1, records1.contains("afterPropertiesSet-" + systemId2));
66     assertTrue("Replication failure: " + records2, records2.contains("afterPropertiesSet-" + systemId1));
67
68     assertTrue("Replication failure: " + records1, records1.contains("setBeanName-" + systemId2));
69     assertTrue("Replication failure: " + records2, records2.contains("setBeanName-" + systemId1));
70
71     assertTrue("Replication failure: " + records1, records1.contains("setApplicationContext-" + systemId2));
72     assertTrue("Replication failure: " + records2, records2.contains("setApplicationContext-" + systemId1));
73
74     mLifeCycleBean1.closeAppCtx();
75     mLifeCycleBean2.closeAppCtx();
76
77     records1 = mLifeCycleBean1.getInvocationRecords();
78     records2 = mLifeCycleBean2.getInvocationRecords();
79
80     assertTrue("Bean1 destroy not invoked: " + records1, records1.contains("destroy-" + systemId1));
81     assertTrue("Bean2 destroy not invoked: " + records2, records2.contains("destroy-" + systemId2));
82
83     assertTrue("Replication failure: " + records1, records1.contains("destroy-" + systemId2));
84     assertTrue("Replication failure: " + records2, records2.contains("destroy-" + systemId1));
85
86     logger.debug("!!!! Asserts passed !!!");
87   }
88
89   private static class LifeCycleTestSetup extends TwoSvrSetup {
90     private LifeCycleTestSetup() {
91       super(LifeCycleTest.class, CONFIG_FILE_FOR_TEST, "test-lifecycle");
92     }
93
94     protected void setUp() throws Exception JavaDoc {
95       super.setUp();
96
97       mLifeCycleBean1 = (ILifeCycle) server1.getProxy(ILifeCycle.class, REMOTE_SERVICE_NAME);
98       mLifeCycleBean2 = (ILifeCycle) server2.getProxy(ILifeCycle.class, REMOTE_SERVICE_NAME);
99     }
100
101     protected void configureWar(DeploymentBuilder builder) {
102       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
103
104       builder.addRemoteService(RmiServiceExporter.class, "LifeCycle", "lifeCycleBean", ILifeCycle.class);
105
106       builder.addRemoteServiceBlock("<bean id=\"lifeCycleBean\" class=\"com.tctest.spring.bean.LifeCycleBean\">" + "\n"
107           + "<property name=\"prop\" ref=\"recorder\"/>" + "\n" + "</bean>");
108
109     }
110   }
111
112   private static class RmiServiceExporter extends org.springframework.remoting.rmi.RmiServiceExporter {
113     public void destroy() {
114       logger.info("destroy method override in RmiServiceExporter.");
115     }
116   }
117
118   /**
119    * JUnit test loader entry point
120    */

121   public static Test suite() {
122     TestSetup setup = new LifeCycleTestSetup();
123     return setup;
124   }
125
126 }
127
Popular Tags