KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Runs a couple of tests within the same JVM
15  *
16  * @author cer
17  */

18 /**
19  * Testing basic clustered singleton behavior, also serve as an example for using the web testing framework.
20  */

21 public class SingletonV2Test extends AbstractTwoServerDeploymentTest {
22
23   private static final String JavaDoc REMOTE_SERVICE_NAME = "Singleton";
24   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
25   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml";
26
27   private static ISingleton singleton1;
28   private static ISingleton singleton2;
29
30   public void testSharedField() throws Exception JavaDoc {
31
32     logger.debug("testing shared fields");
33
34     assertEquals(singleton1.getCounter(), singleton2.getCounter());
35     singleton1.incrementCounter();
36     assertEquals(singleton1.getCounter(), singleton2.getCounter());
37     singleton2.incrementCounter();
38     assertEquals(singleton2.getCounter(), singleton1.getCounter());
39
40     logger.debug("!!!! Asserts passed !!!");
41   }
42
43   public void testTransientField() throws Exception JavaDoc {
44
45     logger.debug("Testing transient fields");
46     assertEquals("aaa", singleton1.getTransientValue());
47     assertEquals("aaa", singleton2.getTransientValue());
48     singleton1.setTransientValue("s1");
49     assertEquals("aaa", singleton2.getTransientValue());
50     singleton2.setTransientValue("s2");
51     assertEquals("s1", singleton1.getTransientValue());
52     assertEquals("s2", singleton2.getTransientValue());
53     logger.debug("done testing transient fields");
54   }
55
56   public void testSharedBooleanField() throws Exception JavaDoc {
57     assertTrue(singleton1.toggleBoolean());
58     assertFalse(singleton2.toggleBoolean());
59     assertTrue(singleton1.toggleBoolean());
60   }
61
62   private static class SingletonTestSetup extends TwoSvrSetup {
63     private SingletonTestSetup() {
64       super(SingletonV2Test.class, CONFIG_FILE_FOR_TEST, "test-singleton");
65     }
66
67     protected void setUp() throws Exception JavaDoc {
68       try {
69         super.setUp();
70
71         singleton1 = (ISingleton) server1.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
72         singleton2 = (ISingleton) server2.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
73       } catch (Exception JavaDoc e) {
74         e.printStackTrace();
75         throw e;
76       }
77     }
78
79     protected void configureWar(DeploymentBuilder builder) {
80       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
81       builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton", ISingleton.class);
82     }
83
84   }
85
86   /**
87    * JUnit test loader entry point
88    */

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