1 4 package com.tctest.spring.integrationtests.tests; 5 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import com.tctest.spring.bean.ISimpleBean; 9 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest; 10 import com.tctest.spring.integrationtests.framework.DeploymentBuilder; 11 12 import java.util.ArrayList ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 import javax.servlet.ServletContextEvent ; 19 import javax.servlet.ServletContextListener ; 20 21 import junit.extensions.TestSetup; 22 import junit.framework.Test; 23 24 29 public class MultipleContextsTest extends AbstractTwoServerDeploymentTest { 30 31 private static final String REMOTE_SERVICE_NAME = "SimpleBean"; 32 private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/multicontext-tc-config.xml"; 33 34 private static ISimpleBean bean11; private static ISimpleBean bean12; private static ISimpleBean bean13; 38 private static ISimpleBean bean21; private static ISimpleBean bean22; private static ISimpleBean bean23; 42 public void testBeansFrom2ClusteredContexts() throws Exception { 43 Set s = new HashSet (); 45 s.add(new Long (bean11.getId())); 46 s.add(new Long (bean12.getId())); 47 s.add(new Long (bean21.getId())); 48 s.add(new Long (bean22.getId())); 49 50 assertEquals("Pre-condition checking failed" + " - " + bean11.getId() + " - " + bean12.getId() + " - " + bean21.getId() + " - " + bean22.getId(), 4, s.size()); 51 52 assertEquals("Replication test faled", bean11.getSharedId(), bean21.getSharedId()); 54 assertEquals("Replication test faled", bean12.getSharedId(), bean22.getSharedId()); 55 56 assertTrue("Replication test faled", bean11.getSharedId() != bean12.getSharedId()); 58 } 59 60 public void testBeansFrom1Clustered1LocalContexts() throws Exception { 61 Set s = new HashSet (); 63 s.add(new Long (bean11.getId())); 64 s.add(new Long (bean13.getId())); 65 s.add(new Long (bean21.getId())); 66 s.add(new Long (bean23.getId())); 67 68 assertEquals("Pre-condition checking failed" + " - " + bean11.getId() + " - " + bean13.getId() + " - " + bean21.getId() + " - " + bean23.getId(), 69 4, s.size()); 70 71 assertEquals("Replication test faled", bean11.getSharedId(), bean21.getSharedId()); 73 assertTrue("Replication test faled", bean13.getSharedId() != bean23.getSharedId()); 74 75 assertTrue("Replication test faled", bean11.getSharedId() != bean13.getSharedId()); 77 assertTrue("Replication test faled", bean21.getSharedId() != bean23.getSharedId()); 78 } 79 80 private static class MultipleContextsTestSetup extends TwoSvrSetup { 81 82 private MultipleContextsTestSetup() { 83 super(MultipleContextsTest.class, CONFIG_FILE_FOR_TEST, "test-multicontext"); 84 } 85 86 protected void setUp() throws Exception { 87 super.setUp(); 88 bean11 = (ISimpleBean) server1.getProxy(ISimpleBean.class, REMOTE_SERVICE_NAME + "1"); 89 bean12 = (ISimpleBean) server1.getProxy(ISimpleBean.class, REMOTE_SERVICE_NAME + "2"); 90 bean13 = (ISimpleBean) server1.getProxy(ISimpleBean.class, REMOTE_SERVICE_NAME + "3"); 91 92 bean21 = (ISimpleBean) server2.getProxy(ISimpleBean.class, REMOTE_SERVICE_NAME + "1"); 93 bean22 = (ISimpleBean) server2.getProxy(ISimpleBean.class, REMOTE_SERVICE_NAME + "2"); 94 bean23 = (ISimpleBean) server2.getProxy(ISimpleBean.class, REMOTE_SERVICE_NAME + "3"); 95 } 96 97 protected void configureWar(DeploymentBuilder builder) { 98 builder.addListener(MultiContextLoaderListener.class); 99 } 100 } 101 102 public static class MultiContextLoaderListener implements ServletContextListener { 103 104 private List contexts = new ArrayList (); 105 106 public void contextInitialized(ServletContextEvent event) { 107 try { 108 contexts.add(new ClassPathXmlApplicationContext( 109 new String [] { "classpath:/com/tctest/spring/multictx-beanfactory1.xml" })); 110 contexts.add(new ClassPathXmlApplicationContext( 111 new String [] { "classpath:/com/tctest/spring/multictx-beanfactory2.xml" })); 112 contexts.add(new ClassPathXmlApplicationContext( 113 new String [] { "classpath:/com/tctest/spring/multictx-beanfactory3.xml" })); 114 } catch (Exception ex) { 115 ex.printStackTrace(); 116 } 117 } 118 119 public void contextDestroyed(ServletContextEvent event) { 120 for (Iterator iter = contexts.iterator(); iter.hasNext();) { 121 ((ClassPathXmlApplicationContext) iter.next()).close(); 122 } 123 } 124 } 125 126 129 public static Test suite() { 130 TestSetup setup = new MultipleContextsTestSetup(); 131 return setup; 132 } 133 } 134 | Popular Tags |