KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractDeploymentTest;
8 import com.tctest.spring.integrationtests.framework.Deployment;
9 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
10 import com.tctest.spring.integrationtests.framework.WebApplicationServer;
11
12 public class StateMaintainedTest extends AbstractDeploymentTest {
13
14   private static final String JavaDoc APP_NAME = "test-singleton";
15   private static final String JavaDoc REMOTE_SERVICE_NAME = "Singleton";
16   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
17   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml";
18
19   private WebApplicationServer server1;
20   private WebApplicationServer server2;
21
22   
23   protected void setUp() throws Exception JavaDoc {
24     super.setUp();
25     Deployment deployment = makeDeployment();
26
27     server1 = makeWebApplicationServer(CONFIG_FILE_FOR_TEST);
28     server1.addWarDeployment(deployment, APP_NAME);
29     
30     server2 = makeWebApplicationServer(CONFIG_FILE_FOR_TEST);
31     server2.addWarDeployment(deployment, APP_NAME);
32   }
33
34   public void testThatStatePreservedAcrossServerRestart() throws Exception JavaDoc {
35     startServer1();
36     
37     server1.restart();
38     
39     ISingleton singleton1 = (ISingleton) server1.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
40     assertEquals(1, singleton1.getCounter());
41   }
42
43   private void startServer1() throws Exception JavaDoc {
44     server1.start();
45     ISingleton singleton1 = (ISingleton) server1.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
46     assertEquals(0, singleton1.getCounter());
47     singleton1.incrementCounter();
48     assertEquals(1, singleton1.getCounter());
49   }
50   
51   public void testThatNewlyStartedServerGetsDistributedState() throws Exception JavaDoc {
52     
53     startServer1();
54     
55     server2.start();
56     ISingleton singleton2 = (ISingleton) server2.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
57     assertEquals(1, singleton2.getCounter());
58   }
59   
60   private Deployment makeDeployment() throws Exception JavaDoc {
61     DeploymentBuilder builder = makeDeploymentBuilder(APP_NAME + ".war");
62     addBeanDefinitions(builder);
63     configureRemoteInterfaces(builder);
64     addClassesAndLibraries(builder);
65     return builder.makeDeployment();
66   }
67
68   private void addBeanDefinitions(DeploymentBuilder builder) {
69     builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
70   }
71
72   private void configureRemoteInterfaces(DeploymentBuilder builder) {
73     builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton", ISingleton.class);
74   }
75
76   private void addClassesAndLibraries(DeploymentBuilder builder) {
77     builder.addDirectoryOrJARContainingClass(ISingleton.class);
78     builder.addDirectoryContainingResource(CONFIG_FILE_FOR_TEST);
79   }
80
81 }
82
Popular Tags