KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISimpleInitializingSingleton;
7 import com.tctest.spring.bean.SimpleInitializingSingleton;
8 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
9 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
10
11 import junit.extensions.TestSetup;
12 import junit.framework.Test;
13
14 /**
15  * Testing the following behavior
16  * 1. afterPropertiesSet is invoked properly on the clustered bean instance
17  * 2. afterPropertiesSet is invoked on the clustered bean in node 2, instead of the locally created bean
18  *
19  * @author Liyu Yi
20  */

21 public class InitializingBean2Test extends AbstractTwoServerDeploymentTest {
22
23   private static final String JavaDoc REMOTE_SERVICE_NAME = "Singleton";
24   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-init2.xml";
25   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/init2-tc-config.xml";
26
27   private static ISimpleInitializingSingleton singleton1;
28   private static ISimpleInitializingSingleton singleton2;
29
30   public void testInitialization() throws Exception JavaDoc {
31
32     logger.debug("testing initialization");
33     
34     // test pre-condition
35
assertEquals("Pre-condition check failed.", SimpleInitializingSingleton.ME, singleton1.getName()); // instantiate bean in node1
36
assertEquals("Pre-condition check failed.", SimpleInitializingSingleton.ME, singleton1.getName()); // instantiate bean in node1
37

38     long id1 = singleton1.getId();
39     long id2 = singleton2.getId();
40     long innerId1 = singleton1.getInnerId();
41     long innerId2 = singleton2.getInnerId();
42     
43     // check initialization in node 1
44
assertEquals(id1, innerId1);
45     assertTrue(singleton1.isTheSameInstance());
46     
47     // check initialization in node 2
48
// node 2 will get shared singlton from node 1
49
// but pupulated with transient local values
50
// including "id" and local "afterPropertiesSetThis"
51
assertEquals(id2, innerId2);
52     assertTrue(id1!=id2);
53     // this is still true since afterPropertiesSet is passed in with the clustered object
54
assertTrue("Failed to verify that afterPropertiesSet is invoked on the shared bean", singleton2.isTheSameInstance());
55     
56     logger.debug("!!!! Asserts passed !!!");
57   }
58
59   private static class InitializingBean2Setup extends TwoSvrSetup {
60     private InitializingBean2Setup() {
61       super(InitializingBean2Test.class, CONFIG_FILE_FOR_TEST, "test-initializingbean2");
62     }
63
64     protected void setUp() throws Exception JavaDoc {
65       super.setUp();
66
67       singleton1 = (ISimpleInitializingSingleton) server1.getProxy(ISimpleInitializingSingleton.class, REMOTE_SERVICE_NAME);
68       singleton2 = (ISimpleInitializingSingleton) server2.getProxy(ISimpleInitializingSingleton.class, REMOTE_SERVICE_NAME);
69     }
70
71     protected void configureWar(DeploymentBuilder builder) {
72       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
73       builder.addRemoteService(REMOTE_SERVICE_NAME, "distributedInitBeanProxy", ISimpleInitializingSingleton.class);
74     }
75
76   }
77
78   /**
79    * JUnit test loader entry point
80    */

81   public static Test suite() {
82     TestSetup setup = new InitializingBean2Setup();
83     return setup;
84   }
85
86 }
87
Popular Tags