KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISingleton;
7 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
8 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
9
10 import junit.extensions.TestSetup;
11 import junit.framework.Test;
12
13 /**
14  * Test the clustered bean behavior with multiple contexts;
15  * verify that the clustering behavior is within "same" context - context with the same
16  *
17  * @author Liyu Yi
18  */

19 public class SingletonFromAnotherContextTest extends AbstractTwoServerDeploymentTest {
20
21   private static final String JavaDoc REMOTE_SERVICE_NAME = "Singleton";
22   private static final String JavaDoc ANOTHER_REMOTE_SERVICE_NAME = "AnotherSingleton";
23   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-another.xml";
24   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/anothersingleton-tc-config.xml";
25
26   private static ISingleton singleton1N1;
27   private static ISingleton singleton2N1;
28
29   private static ISingleton singleton1N2;
30   private static ISingleton singleton2N2;
31
32   public void testSingletonFromAnotherContext() throws Exception JavaDoc {
33
34     logger.debug("testing singleton from another context");
35     
36     // check pre-condition
37
assertEquals(0, singleton1N1.getCounter());
38     assertEquals(0, singleton1N2.getCounter());
39     
40     assertEquals(0, singleton2N1.getCounter());
41     assertEquals(0, singleton2N2.getCounter());
42     
43     singleton1N1.incrementCounter();
44     singleton1N2.incrementCounter();
45     
46     // only singleton1s are getting the changes
47
assertEquals(2, singleton1N1.getCounter());
48     assertEquals(2, singleton1N2.getCounter());
49     
50     assertEquals(0, singleton2N1.getCounter());
51     assertEquals(0, singleton2N2.getCounter());
52
53     singleton2N1.incrementCounter();
54     singleton2N2.incrementCounter();
55     
56     // only singleton2s are getting the changes
57
assertEquals(2, singleton2N1.getCounter());
58     assertEquals(2, singleton2N2.getCounter());
59        
60     assertEquals(2, singleton1N1.getCounter());
61     assertEquals(2, singleton1N2.getCounter());
62     
63     logger.debug("!!!! Asserts passed !!!");
64   }
65
66   private static class InnerTestSetup extends TwoSvrSetup {
67     private InnerTestSetup() {
68       super(SingletonFromAnotherContextTest.class, CONFIG_FILE_FOR_TEST, "test-anothersingleton");
69     }
70
71     protected void setUp() throws Exception JavaDoc {
72       super.setUp();
73
74       singleton1N1 = (ISingleton) server1.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
75       singleton2N1 = (ISingleton) server1.getProxy(ISingleton.class, ANOTHER_REMOTE_SERVICE_NAME);
76
77       singleton1N2 = (ISingleton) server2.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
78       singleton2N2 = (ISingleton) server2.getProxy(ISingleton.class, ANOTHER_REMOTE_SERVICE_NAME);
79     }
80
81     protected void configureWar(DeploymentBuilder builder) {
82       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
83       builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton", ISingleton.class);
84       builder.addRemoteService(ANOTHER_REMOTE_SERVICE_NAME, "anotherSingleton", ISingleton.class);
85     }
86   }
87
88   /**
89    * JUnit test loader entry point
90    */

91   public static Test suite() {
92     TestSetup setup = new InnerTestSetup();
93     return setup;
94   }
95 }
96
Popular Tags