01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest.spring.integrationtests.tests;
05:
06: import com.tc.test.server.appserver.deployment.AbstractTwoServerDeploymentTest;
07: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
08: import com.tctest.spring.bean.ISimpleInitializingSingleton;
09: import com.tctest.spring.bean.SimpleInitializingSingleton;
10: import com.tctest.spring.integrationtests.SpringTwoServerTestSetup;
11:
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
22: AbstractTwoServerDeploymentTest {
23:
24: private static final String REMOTE_SERVICE_NAME = "Singleton";
25: private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-init2.xml";
26: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/init2-tc-config.xml";
27:
28: private ISimpleInitializingSingleton singleton1;
29: private ISimpleInitializingSingleton singleton2;
30:
31: protected void setUp() throws Exception {
32: super .setUp();
33:
34: singleton1 = (ISimpleInitializingSingleton) server0
35: .getProxy(ISimpleInitializingSingleton.class,
36: REMOTE_SERVICE_NAME);
37: singleton2 = (ISimpleInitializingSingleton) server1
38: .getProxy(ISimpleInitializingSingleton.class,
39: REMOTE_SERVICE_NAME);
40: }
41:
42: public void testInitialization() throws Exception {
43: logger.debug("testing initialization");
44:
45: // test pre-condition
46: assertEquals("Pre-condition check failed.",
47: SimpleInitializingSingleton.ME, singleton1.getName()); // instantiate bean in node1
48: assertEquals("Pre-condition check failed.",
49: SimpleInitializingSingleton.ME, singleton1.getName()); // instantiate bean in node1
50:
51: long id1 = singleton1.getId();
52: long id2 = singleton2.getId();
53: long innerId1 = singleton1.getInnerId();
54: long innerId2 = singleton2.getInnerId();
55:
56: // check initialization in node 1
57: assertEquals(id1, innerId1);
58: assertTrue(singleton1.isTheSameInstance());
59:
60: // check initialization in node 2
61: // node 2 will get shared singlton from node 1
62: // but pupulated with transient local values
63: // including "id" and local "afterPropertiesSetThis"
64: assertEquals(id2, innerId2);
65: assertTrue(id1 != id2);
66: // this is still true since afterPropertiesSet is passed in with the clustered object
67: assertTrue(
68: "Failed to verify that afterPropertiesSet is invoked on the shared bean",
69: singleton2.isTheSameInstance());
70:
71: logger.debug("!!!! Asserts passed !!!");
72: }
73:
74: private static class InitializingBean2Setup extends
75: SpringTwoServerTestSetup {
76: private InitializingBean2Setup() {
77: super (InitializingBean2Test.class, CONFIG_FILE_FOR_TEST,
78: "test-initializingbean2");
79: }
80:
81: protected void configureWar(DeploymentBuilder builder) {
82: builder
83: .addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
84: builder.addRemoteService(REMOTE_SERVICE_NAME,
85: "distributedInitBeanProxy",
86: ISimpleInitializingSingleton.class);
87: }
88:
89: }
90:
91: public static Test suite() {
92: return new InitializingBean2Setup();
93: }
94:
95: }
|