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.ISimpleBean;
009: import com.tctest.spring.integrationtests.SpringTwoServerTestSetup;
010:
011: import java.util.HashSet;
012: import java.util.Set;
013:
014: import junit.extensions.TestSetup;
015: import junit.framework.Test;
016:
017: /**
018: * Testing the following features
019: * 1. Replication behavior
020: * 2. Bean reference another bean and how the sharing
021: *
022: * Test depends on the order of the instantiation of the parent beans
023: *
024: * @author Liyu Yi
025: */
026: public class ReferenceAndReplicationTest extends
027: AbstractTwoServerDeploymentTest {
028:
029: private static final String SHAREDPARENT_SERVICE_NAME = "ShareParent";
030: private static final String LOCALCHILD1_SERVICE_NAME = "LocalChild1";
031: private static final String LOCALCHILD2_SERVICE_NAME = "LocalChild2";
032: private static final String LOCALCHILD3_SERVICE_NAME = "LocalChild3";
033: private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-referenceandreplication.xml";
034: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/referenceandreplication-tc-config.xml";
035:
036: // for N1
037: private ISimpleBean sharedParentN1;
038: private ISimpleBean localChild1N1;
039: private ISimpleBean localChild2N1;
040: private ISimpleBean localChild3N1;
041:
042: // for N2
043: private ISimpleBean sharedParentN2;
044: private ISimpleBean localChild1N2;
045: private ISimpleBean localChild2N2;
046: private ISimpleBean localChild3N2;
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050: // for N1
051: localChild1N1 = (ISimpleBean) server0.getProxy(
052: ISimpleBean.class, LOCALCHILD1_SERVICE_NAME);
053: localChild2N1 = (ISimpleBean) server0.getProxy(
054: ISimpleBean.class, LOCALCHILD2_SERVICE_NAME);
055: localChild3N1 = (ISimpleBean) server0.getProxy(
056: ISimpleBean.class, LOCALCHILD3_SERVICE_NAME);
057: sharedParentN1 = (ISimpleBean) server0.getProxy(
058: ISimpleBean.class, SHAREDPARENT_SERVICE_NAME);
059: // for N2
060: localChild1N2 = (ISimpleBean) server1.getProxy(
061: ISimpleBean.class, LOCALCHILD1_SERVICE_NAME);
062: localChild2N2 = (ISimpleBean) server1.getProxy(
063: ISimpleBean.class, LOCALCHILD2_SERVICE_NAME);
064: localChild3N2 = (ISimpleBean) server1.getProxy(
065: ISimpleBean.class, LOCALCHILD3_SERVICE_NAME);
066: sharedParentN2 = (ISimpleBean) server1.getProxy(
067: ISimpleBean.class, SHAREDPARENT_SERVICE_NAME);
068: }
069:
070: public void testReplication() throws Exception {
071: logger.debug("testing replication of shared spring bean");
072: // check pre-conditions
073: long id2 = sharedParentN2.getId();
074: long id1 = sharedParentN1.getId();
075:
076: assertFalse("Pre-condition check failed", id1 == id2);
077:
078: long timeStamp1 = sharedParentN1.getTimeStamp();
079: long timeStamp2 = sharedParentN2.getTimeStamp();
080: long timeStampMin = timeStamp1 < timeStamp2 ? timeStamp1
081: : timeStamp2;
082:
083: // check shared field replication and also the behavior of
084: // the GetBeanProtocol for the 2nd Node (which is N1) to grab the shared
085: // bean from singletonCache in the mixin
086: assertEquals("Shared field replication failed: "
087: + sharedParentN1.getSharedId(), timeStampMin,
088: sharedParentN1.getSharedId());
089: assertEquals("Shared field replication failed: "
090: + sharedParentN2.getSharedId(), timeStampMin,
091: sharedParentN2.getSharedId());
092:
093: // check shared field replication again
094: assertNull("", sharedParentN2.getField());
095: sharedParentN1.setField("" + id1);
096: assertEquals("Shared field replication failed: "
097: + sharedParentN2.getField(), "" + id1, sharedParentN2
098: .getField());
099:
100: // check transient field stopping replication
101: assertNull("Pre-condition check failed", sharedParentN1
102: .getTransientField());
103: assertNull("Pre-condition check failed", sharedParentN2
104: .getTransientField());
105: sharedParentN1.setTransientField("" + id1);
106: sharedParentN2.setTransientField("" + id2);
107: assertEquals("Unexpected field replication: "
108: + sharedParentN1.getTransientField(), "" + id1,
109: sharedParentN1.getTransientField());
110: assertEquals("Unexpected field replication: "
111: + sharedParentN2.getTransientField(), "" + id2,
112: sharedParentN2.getTransientField());
113:
114: // check "dso transient" field stopping replication
115: assertNull("Pre-condition check failed", sharedParentN1
116: .getDsoTransientField());
117: assertNull("Pre-condition check failed", sharedParentN2
118: .getDsoTransientField());
119: sharedParentN1.setDsoTransientField("" + id1);
120: sharedParentN2.setDsoTransientField("" + id2);
121: assertEquals("Unexpected field replication: "
122: + sharedParentN1.getDsoTransientField(), "" + id1,
123: sharedParentN1.getDsoTransientField());
124: assertEquals("Unexpected field replication: "
125: + sharedParentN2.getDsoTransientField(), "" + id2,
126: sharedParentN2.getDsoTransientField());
127:
128: // check static field stopping replication
129: assertNull("Pre-condition check failed", sharedParentN1
130: .getStaticField());
131: assertNull("Pre-condition check failed", sharedParentN2
132: .getStaticField());
133: sharedParentN1.setStaticField("" + id1);
134: sharedParentN2.setStaticField("" + id2);
135: assertEquals("Unexpected field replication: "
136: + sharedParentN1.getStaticField(), "" + id1,
137: sharedParentN1.getStaticField());
138: assertEquals("Unexpected field replication: "
139: + sharedParentN2.getStaticField(), "" + id2,
140: sharedParentN2.getStaticField());
141:
142: logger.debug("!!!! Asserts passed !!!");
143: }
144:
145: public void testBeanReference() throws Exception {
146: logger.debug("testing shared bean reference other beans");
147:
148: // check pre-conditions
149: long id2 = sharedParentN2.getId();
150: long id1 = sharedParentN1.getId();
151:
152: assertFalse("Pre-condition check failed", id1 == id2);
153:
154: Set idSet = new HashSet();
155:
156: long id11 = localChild1N1.getId();
157: idSet.add(new Long(id11));
158: long id21 = localChild2N1.getId();
159: idSet.add(new Long(id21));
160: long id31 = localChild3N1.getId();
161: idSet.add(new Long(id31));
162: long id12 = localChild1N2.getId();
163: idSet.add(new Long(id12));
164: long id22 = localChild2N2.getId();
165: idSet.add(new Long(id22));
166: long id32 = localChild3N2.getId();
167: idSet.add(new Long(id32));
168:
169: assertEquals("Pre-condition check failed: " + idSet, 6, idSet
170: .size());
171:
172: // check localChild1 -- bean through shared reference is NOT shared if the reference bean is not declared as shared explicitly
173: // localChild1N1.setField("localChild1N1");
174: // localChild1N2.setField("localChild1N2");
175: // assertEquals("Unexcpected field replication: " + localChild1N1.getField(), "localChild1N1", localChild1N1.getField());
176: // assertEquals("Unexcpected field replication: " + localChild1N2.getField(), "localChild1N2", localChild1N2.getField());
177: //
178: // assertEquals("Singleton semantics broken: " + id11 + " - " + sharedParentN1.getSharedRefId(), id11, sharedParentN1.getSharedRefId());
179: // assertEquals("Singleton semantics broken: " + id12 + " - " + sharedParentN2.getSharedRefId(), id12, sharedParentN2.getSharedRefId());
180:
181: // check localChild2 -- bean through transient reference is not shared
182: localChild2N1.setField("localChild2N1");
183: localChild2N2.setField("localChild2N2");
184: assertEquals("Unexcpected field replication: "
185: + localChild2N1.getField(), "localChild2N1",
186: localChild2N1.getField());
187: assertEquals("Unexcpected field replication: "
188: + localChild2N2.getField(), "localChild2N2",
189: localChild2N2.getField());
190:
191: // check localChild3 -- bean through dso transient reference is not shared
192: localChild3N1.setField("localChild3N1");
193: localChild3N2.setField("localChild3N2");
194: assertEquals("Unexcpected field replication: "
195: + localChild3N1.getField(), "localChild3N1",
196: localChild3N1.getField());
197: assertEquals("Unexcpected field replication: "
198: + localChild3N2.getField(), "localChild3N2",
199: localChild3N2.getField());
200:
201: logger.debug("!!!! Asserts passed !!!");
202: }
203:
204: private static class InnerTestSetup extends
205: SpringTwoServerTestSetup {
206: private InnerTestSetup() {
207: super (ReferenceAndReplicationTest.class,
208: CONFIG_FILE_FOR_TEST,
209: "test-referenceandreplication");
210: }
211:
212: protected void configureWar(DeploymentBuilder builder) {
213: builder
214: .addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
215: builder.addRemoteService(SHAREDPARENT_SERVICE_NAME,
216: "sharedParent", ISimpleBean.class);
217: builder.addRemoteService(LOCALCHILD1_SERVICE_NAME,
218: "localChild1", ISimpleBean.class);
219: builder.addRemoteService(LOCALCHILD2_SERVICE_NAME,
220: "localChild2", ISimpleBean.class);
221: builder.addRemoteService(LOCALCHILD3_SERVICE_NAME,
222: "localChild3", ISimpleBean.class);
223: }
224: }
225:
226: /**
227: * JUnit test loader entry point
228: */
229: public static Test suite() {
230: TestSetup setup = new InnerTestSetup();
231: return setup;
232: }
233: }
|