KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
7 import org.springframework.web.servlet.DispatcherServlet;
8
9 import com.tc.test.TestConfigObject;
10 import com.tctest.spring.bean.ISimpleBean;
11 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
12 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
13 import com.tctest.spring.integrationtests.framework.ProxyBuilder;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import junit.extensions.TestSetup;
19 import junit.framework.Test;
20
21
22 /**
23  * Test clustering session scoped bean.
24  */

25 public class SessionScopedBeanTest extends AbstractTwoServerDeploymentTest {
26
27   private static final String JavaDoc SERVICE_FOR_SHARED_SESSION_SCOPED_BEAN = "SimpleBeanSvc";
28 // private static final String SERVICE_FOR_LOCAL_SESSION_SCOPED_BEAN = "LocalSimpleBeanSvc";
29
private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-sessionscope.xml";
30   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/sessionscoped-tc-config.xml";
31
32
33   private static ISimpleBean beanN1S1; // node1 session1
34
private static ISimpleBean beanN1S2; // node1 session2
35

36   private static ISimpleBean beanN2S1; // node2 session1
37
private static ISimpleBean beanN2S2; // node2 session2
38

39 // private static ISimpleBean beanN1S1Local; // node1 session1
40
// private static ISimpleBean beanN2S1Local; // node2 session1
41

42   public SessionScopedBeanTest() {
43     this.disableVariant(TestConfigObject.SPRING_VARIANT, "128");
44   }
45   
46 // public void testLocalSessionScopedBeans() throws Exception {
47
// logger.debug("testing local beans");
48
//
49
// beanN1S1Local.setField("newLocalVal1");
50
//
51
// Thread.sleep(4000);
52
//
53
// assertEquals("Unexcpected value: ", "newLocalVal1", beanN1S1Local.getField());
54
// assertEquals("Unexcpected sharing: ", "local-v1", beanN2S1Local.getField());
55
//
56
// logger.debug("!!!! Asserts passed !!!");
57
// }
58

59   public void testSharedFields() throws Exception JavaDoc {
60     beanN1S1.setField("newVal1");
61     beanN2S2.setField("newVal2");
62     
63     Thread.sleep(4000);
64     
65     assertEquals("Failed to shared: ", "newVal1", beanN2S1.getField());
66     assertEquals("Failed to shared: ", "newVal2", beanN1S2.getField());
67   }
68
69   public void testTransparentFields() throws Exception JavaDoc {
70     assertEquals("Failed to initialize/virtualize the transient field.", "transient-val", beanN1S1.getTransientField());
71     assertEquals("Failed to initialize/virtualize the transient field.", "transient-val", beanN1S2.getTransientField());
72     assertEquals("Failed to initialize/virtualize the transient field.", "transient-val", beanN2S1.getTransientField());
73     assertEquals("Failed to initialize/virtualize the transient field.", "transient-val", beanN2S2.getTransientField());
74
75     beanN1S1.setTransientField("newVal11");
76     beanN1S2.setTransientField("newVal12");
77     beanN2S1.setTransientField("newVal21");
78     beanN2S2.setTransientField("newVal22");
79     
80     assertEquals("Unexpected sharing: ", "newVal11", beanN1S1.getTransientField());
81     assertEquals("Unexpected sharing: ", "newVal12", beanN1S2.getTransientField());
82     assertEquals("Unexpected sharing: ", "newVal21", beanN2S1.getTransientField());
83     assertEquals("Unexpected sharing: ", "newVal22", beanN2S2.getTransientField());
84   }
85
86   private static class InnerTestSetup extends TwoSvrSetup {
87     private static final String JavaDoc APP_NAME = "test-sessionscope";
88
89     private InnerTestSetup() {
90       super(SessionScopedBeanTest.class, CONFIG_FILE_FOR_TEST, APP_NAME);
91     }
92
93     protected void setUp() throws Exception JavaDoc {
94       try {
95         super.setUp();
96         
97         Map JavaDoc initCtx = new HashMap JavaDoc();
98         initCtx.put(ProxyBuilder.EXPORTER_TYPE_KEY, HttpInvokerServiceExporter.class);
99         
100         beanN1S1 = (ISimpleBean) server1.getProxy(ISimpleBean.class, APP_NAME + "/http/" + SERVICE_FOR_SHARED_SESSION_SCOPED_BEAN, initCtx);
101 // beanN1S1Local = (ISimpleBean) server1.getProxy(ISimpleBean.class, APP_NAME + "/http/" + SERVICE_FOR_LOCAL_SESSION_SCOPED_BEAN);
102
beanN2S1 = (ISimpleBean) server2.getProxy(ISimpleBean.class, APP_NAME + "/http/" + SERVICE_FOR_SHARED_SESSION_SCOPED_BEAN, initCtx);
103 // beanN2S1Local = (ISimpleBean) server2.getProxy(ISimpleBean.class, APP_NAME + "/http/" + SERVICE_FOR_LOCAL_SESSION_SCOPED_BEAN);
104

105         initCtx.remove(ProxyBuilder.HTTP_CLIENT_KEY); // this resets the internal client
106

107         beanN1S2 = (ISimpleBean) server1.getProxy(ISimpleBean.class, APP_NAME + "/http/" + SERVICE_FOR_SHARED_SESSION_SCOPED_BEAN, initCtx);
108         beanN2S2 = (ISimpleBean) server2.getProxy(ISimpleBean.class, APP_NAME + "/http/" + SERVICE_FOR_SHARED_SESSION_SCOPED_BEAN, initCtx);
109       } catch (Exception JavaDoc e) {
110         e.printStackTrace(); throw e;
111       }
112     }
113
114     protected void configureWar(DeploymentBuilder builder) {
115       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
116       builder.addRemoteService(HttpInvokerServiceExporter.class,SERVICE_FOR_SHARED_SESSION_SCOPED_BEAN, "simplebean", ISimpleBean.class);
117 // builder.addRemoteService(HttpInvokerServiceExporter.class,SERVICE_FOR_LOCAL_SESSION_SCOPED_BEAN, "localsimplebean", ISimpleBean.class);
118
builder.setDispatcherServlet("httpinvoker", "/http/*", DispatcherServlet.class, null, true);
119       builder.addDirectoryOrJARContainingClass(net.sf.cglib.core.Constants.class);
120     }
121
122   }
123
124   /**
125    * JUnit test loader entry point
126    */

127   public static Test suite() {
128     TestSetup setup = new InnerTestSetup();
129     return setup;
130   }
131
132 }
133
Popular Tags