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 org.springframework.context.support.ClassPathXmlApplicationContext;
007:
008: import com.tc.test.server.appserver.deployment.AbstractTwoServerDeploymentTest;
009: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
010: import com.tctest.spring.bean.ISimpleBean;
011: import com.tctest.spring.integrationtests.SpringTwoServerTestSetup;
012:
013: import java.util.ArrayList;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Set;
018:
019: import javax.servlet.ServletContextEvent;
020: import javax.servlet.ServletContextListener;
021:
022: import junit.framework.Test;
023:
024: /**
025: * Test behavior of distributed and non-distributed application contexts in one web application
026: *
027: * @author Liyu Yi
028: */
029: public class MultipleContextsTest extends
030: AbstractTwoServerDeploymentTest {
031:
032: private static final String REMOTE_SERVICE_NAME = "SimpleBean";
033: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/multicontext-tc-config.xml";
034:
035: private ISimpleBean bean11; // shared
036: private ISimpleBean bean12; // shared
037: private ISimpleBean bean13; // NOT shared
038:
039: private ISimpleBean bean21; // shared
040: private ISimpleBean bean22; // shared
041: private ISimpleBean bean23; // NOT shared
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045: bean11 = (ISimpleBean) server0.getProxy(ISimpleBean.class,
046: REMOTE_SERVICE_NAME + "1");
047: bean12 = (ISimpleBean) server0.getProxy(ISimpleBean.class,
048: REMOTE_SERVICE_NAME + "2");
049: bean13 = (ISimpleBean) server0.getProxy(ISimpleBean.class,
050: REMOTE_SERVICE_NAME + "3");
051:
052: bean21 = (ISimpleBean) server1.getProxy(ISimpleBean.class,
053: REMOTE_SERVICE_NAME + "1");
054: bean22 = (ISimpleBean) server1.getProxy(ISimpleBean.class,
055: REMOTE_SERVICE_NAME + "2");
056: bean23 = (ISimpleBean) server1.getProxy(ISimpleBean.class,
057: REMOTE_SERVICE_NAME + "3");
058: }
059:
060: public void testBeansFrom2ClusteredContexts() throws Exception {
061: // all of them have unique id
062: Set s = new HashSet();
063: s.add(new Long(bean11.getId()));
064: s.add(new Long(bean12.getId()));
065: s.add(new Long(bean21.getId()));
066: s.add(new Long(bean22.getId()));
067:
068: assertEquals("Pre-condition checking failed" + " - "
069: + bean11.getId() + " - " + bean12.getId() + " - "
070: + bean21.getId() + " - " + bean22.getId(), 4, s.size());
071:
072: // beans are clustered
073: assertEquals("Replication test faled", bean11.getSharedId(),
074: bean21.getSharedId());
075: assertEquals("Replication test faled", bean12.getSharedId(),
076: bean22.getSharedId());
077:
078: // but NOT across contexts
079: assertTrue("Replication test faled",
080: bean11.getSharedId() != bean12.getSharedId());
081: }
082:
083: public void testBeansFrom1Clustered1LocalContexts()
084: throws Exception {
085: // all of them have unique id
086: Set s = new HashSet();
087: s.add(new Long(bean11.getId()));
088: s.add(new Long(bean13.getId()));
089: s.add(new Long(bean21.getId()));
090: s.add(new Long(bean23.getId()));
091:
092: assertEquals("Pre-condition checking failed" + " - "
093: + bean11.getId() + " - " + bean13.getId() + " - "
094: + bean21.getId() + " - " + bean23.getId(), 4, s.size());
095:
096: // only the clustered have the same value
097: assertEquals("Replication test faled", bean11.getSharedId(),
098: bean21.getSharedId());
099: assertTrue("Replication test faled",
100: bean13.getSharedId() != bean23.getSharedId());
101:
102: // and NOT across contexts
103: assertTrue("Replication test faled",
104: bean11.getSharedId() != bean13.getSharedId());
105: assertTrue("Replication test faled",
106: bean21.getSharedId() != bean23.getSharedId());
107: }
108:
109: private static class MultipleContextsTestSetup extends
110: SpringTwoServerTestSetup {
111:
112: private MultipleContextsTestSetup() {
113: super (MultipleContextsTest.class, CONFIG_FILE_FOR_TEST,
114: "test-multicontext");
115: }
116:
117: protected void configureWar(DeploymentBuilder builder) {
118: builder.addListener(MultiContextLoaderListener.class);
119: }
120:
121: }
122:
123: public static class MultiContextLoaderListener implements
124: ServletContextListener {
125: private List contexts = new ArrayList();
126:
127: public void contextInitialized(ServletContextEvent event) {
128: try {
129: contexts
130: .add(new ClassPathXmlApplicationContext(
131: new String[] { "classpath:/com/tctest/spring/multictx-beanfactory1.xml" }));
132: contexts
133: .add(new ClassPathXmlApplicationContext(
134: new String[] { "classpath:/com/tctest/spring/multictx-beanfactory2.xml" }));
135: contexts
136: .add(new ClassPathXmlApplicationContext(
137: new String[] { "classpath:/com/tctest/spring/multictx-beanfactory3.xml" }));
138: } catch (Exception ex) {
139: ex.printStackTrace();
140: }
141: }
142:
143: public void contextDestroyed(ServletContextEvent event) {
144: for (Iterator iter = contexts.iterator(); iter.hasNext();) {
145: ((ClassPathXmlApplicationContext) iter.next()).close();
146: }
147: }
148: }
149:
150: public static Test suite() {
151: return new MultipleContextsTestSetup();
152: }
153: }
|