KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISimpleBean;
7 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
8 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
9
10 import java.util.HashSet JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import junit.extensions.TestSetup;
14 import junit.framework.Test;
15
16 /**
17  * Testing the following features
18  * 1. Replication behavior
19  * 2. Bean reference another bean and how the sharing
20  *
21  * Test depends on the order of the instantiation of the parent beans
22  *
23  * @author Liyu Yi
24  */

25 public class ReferenceAndReplicationTest extends AbstractTwoServerDeploymentTest {
26
27   private static final String JavaDoc SHAREDPARENT_SERVICE_NAME = "ShareParent";
28   private static final String JavaDoc LOCALCHILD1_SERVICE_NAME = "LocalChild1";
29   private static final String JavaDoc LOCALCHILD2_SERVICE_NAME = "LocalChild2";
30   private static final String JavaDoc LOCALCHILD3_SERVICE_NAME = "LocalChild3";
31   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-referenceandreplication.xml";
32   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/referenceandreplication-tc-config.xml";
33
34   // for N1
35
private static ISimpleBean sharedParentN1;
36   private static ISimpleBean localChild1N1;
37   private static ISimpleBean localChild2N1;
38   private static ISimpleBean localChild3N1;
39
40   // for N2
41
private static ISimpleBean sharedParentN2;
42   private static ISimpleBean localChild1N2;
43   private static ISimpleBean localChild2N2;
44   private static ISimpleBean localChild3N2;
45
46   public void testReplication() throws Exception JavaDoc {
47     logger.debug("testing replication of shared spring bean");
48     // check pre-conditions
49
long id2 = sharedParentN2.getId();
50     long id1 = sharedParentN1.getId();
51
52     assertFalse("Pre-condition check failed" , id1 == id2);
53
54     long timeStamp1 = sharedParentN1.getTimeStamp();
55     long timeStamp2 = sharedParentN2.getTimeStamp();
56     long timeStampMin = timeStamp1 < timeStamp2 ? timeStamp1 : timeStamp2;
57
58
59     // check shared field replication and also the behavior of
60
// the GetBeanProtocol for the 2nd Node (which is N1) to grab the shared
61
// bean from singletonCache in the mixin
62
assertEquals("Shared field replication failed: " + sharedParentN1.getSharedId(), timeStampMin, sharedParentN1.getSharedId());
63     assertEquals("Shared field replication failed: " + sharedParentN2.getSharedId(), timeStampMin, sharedParentN2.getSharedId());
64
65     // check shared field replication again
66
assertNull("", sharedParentN2.getField());
67     sharedParentN1.setField("" + id1);
68     assertEquals("Shared field replication failed: " + sharedParentN2.getField(), "" + id1, sharedParentN2.getField());
69
70     // check transient field stopping replication
71
assertNull("Pre-condition check failed", sharedParentN1.getTransientField());
72     assertNull("Pre-condition check failed", sharedParentN2.getTransientField());
73     sharedParentN1.setTransientField("" + id1);
74     sharedParentN2.setTransientField("" + id2);
75     assertEquals("Unexpected field replication: " + sharedParentN1.getTransientField(), "" + id1, sharedParentN1.getTransientField());
76     assertEquals("Unexpected field replication: " + sharedParentN2.getTransientField(), "" + id2, sharedParentN2.getTransientField());
77
78     // check "dso transient" field stopping replication
79
assertNull("Pre-condition check failed", sharedParentN1.getDsoTransientField());
80     assertNull("Pre-condition check failed", sharedParentN2.getDsoTransientField());
81     sharedParentN1.setDsoTransientField("" + id1);
82     sharedParentN2.setDsoTransientField("" + id2);
83     assertEquals("Unexpected field replication: " + sharedParentN1.getDsoTransientField(), "" + id1, sharedParentN1.getDsoTransientField());
84     assertEquals("Unexpected field replication: " + sharedParentN2.getDsoTransientField(), "" + id2, sharedParentN2.getDsoTransientField());
85
86     // check static field stopping replication
87
assertNull("Pre-condition check failed", sharedParentN1.getStaticField());
88     assertNull("Pre-condition check failed", sharedParentN2.getStaticField());
89     sharedParentN1.setStaticField("" + id1);
90     sharedParentN2.setStaticField("" + id2);
91     assertEquals("Unexpected field replication: " + sharedParentN1.getStaticField(), "" + id1, sharedParentN1.getStaticField());
92     assertEquals("Unexpected field replication: " + sharedParentN2.getStaticField(), "" + id2, sharedParentN2.getStaticField());
93
94     logger.debug("!!!! Asserts passed !!!");
95   }
96
97   public void testBeanReference() throws Exception JavaDoc {
98     logger.debug("testing shared bean reference other beans");
99
100     // check pre-conditions
101
long id2 = sharedParentN2.getId();
102     long id1 = sharedParentN1.getId();
103
104     assertFalse("Pre-condition check failed" , id1 == id2);
105
106     Set JavaDoc idSet = new HashSet JavaDoc();
107
108     long id11 = localChild1N1.getId(); idSet.add(new Long JavaDoc(id11));
109     long id21 = localChild2N1.getId(); idSet.add(new Long JavaDoc(id21));
110     long id31 = localChild3N1.getId(); idSet.add(new Long JavaDoc(id31));
111     long id12 = localChild1N2.getId(); idSet.add(new Long JavaDoc(id12));
112     long id22 = localChild2N2.getId(); idSet.add(new Long JavaDoc(id22));
113     long id32 = localChild3N2.getId(); idSet.add(new Long JavaDoc(id32));
114
115     assertEquals("Pre-condition check failed: " + idSet, 6, idSet.size());
116
117     // check localChild1 -- bean through shared reference is NOT shared if the reference bean is not declared as shared explicitly
118
// localChild1N1.setField("localChild1N1");
119
// localChild1N2.setField("localChild1N2");
120
// assertEquals("Unexcpected field replication: " + localChild1N1.getField(), "localChild1N1", localChild1N1.getField());
121
// assertEquals("Unexcpected field replication: " + localChild1N2.getField(), "localChild1N2", localChild1N2.getField());
122
//
123
// assertEquals("Singleton semantics broken: " + id11 + " - " + sharedParentN1.getSharedRefId(), id11, sharedParentN1.getSharedRefId());
124
// assertEquals("Singleton semantics broken: " + id12 + " - " + sharedParentN2.getSharedRefId(), id12, sharedParentN2.getSharedRefId());
125

126     // check localChild2 -- bean through transient reference is not shared
127
localChild2N1.setField("localChild2N1");
128     localChild2N2.setField("localChild2N2");
129     assertEquals("Unexcpected field replication: " + localChild2N1.getField(), "localChild2N1", localChild2N1.getField());
130     assertEquals("Unexcpected field replication: " + localChild2N2.getField(), "localChild2N2", localChild2N2.getField());
131
132     // check localChild3 -- bean through dso transient reference is not shared
133
localChild3N1.setField("localChild3N1");
134     localChild3N2.setField("localChild3N2");
135     assertEquals("Unexcpected field replication: " + localChild3N1.getField(), "localChild3N1", localChild3N1.getField());
136     assertEquals("Unexcpected field replication: " + localChild3N2.getField(), "localChild3N2", localChild3N2.getField());
137
138     logger.debug("!!!! Asserts passed !!!");
139   }
140
141   private static class InnerTestSetup extends TwoSvrSetup {
142     private InnerTestSetup() {
143       super(ReferenceAndReplicationTest.class, CONFIG_FILE_FOR_TEST, "test-referenceandreplication");
144     }
145
146     protected void setUp() throws Exception JavaDoc {
147       super.setUp();
148       // for N1
149
localChild1N1 = (ISimpleBean) server1.getProxy(ISimpleBean.class, LOCALCHILD1_SERVICE_NAME);
150       localChild2N1 = (ISimpleBean) server1.getProxy(ISimpleBean.class, LOCALCHILD2_SERVICE_NAME);
151       localChild3N1 = (ISimpleBean) server1.getProxy(ISimpleBean.class, LOCALCHILD3_SERVICE_NAME);
152       sharedParentN1 = (ISimpleBean) server1.getProxy(ISimpleBean.class, SHAREDPARENT_SERVICE_NAME);
153       // for N2
154
localChild1N2 = (ISimpleBean) server2.getProxy(ISimpleBean.class, LOCALCHILD1_SERVICE_NAME);
155       localChild2N2 = (ISimpleBean) server2.getProxy(ISimpleBean.class, LOCALCHILD2_SERVICE_NAME);
156       localChild3N2 = (ISimpleBean) server2.getProxy(ISimpleBean.class, LOCALCHILD3_SERVICE_NAME);
157       sharedParentN2 = (ISimpleBean) server2.getProxy(ISimpleBean.class, SHAREDPARENT_SERVICE_NAME);
158     }
159
160     protected void configureWar(DeploymentBuilder builder) {
161       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
162       builder.addRemoteService(SHAREDPARENT_SERVICE_NAME, "sharedParent", ISimpleBean.class);
163       builder.addRemoteService(LOCALCHILD1_SERVICE_NAME, "localChild1", ISimpleBean.class);
164       builder.addRemoteService(LOCALCHILD2_SERVICE_NAME, "localChild2", ISimpleBean.class);
165       builder.addRemoteService(LOCALCHILD3_SERVICE_NAME, "localChild3", ISimpleBean.class);
166     }
167   }
168
169   /**
170    * JUnit test loader entry point
171    */

172   public static Test suite() {
173     TestSetup setup = new InnerTestSetup();
174     return setup;
175   }
176 }
177
Popular Tags