KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > WebAppContext_Test


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;
5
6 import org.jmock.Mock;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.constraint.IsEqual;
9 import org.jmock.core.matcher.MethodNameMatcher;
10 import org.jmock.core.stub.CustomStub;
11 import org.jmock.core.stub.ReturnStub;
12 import org.springframework.web.context.ContextLoaderListener;
13 import org.springframework.web.context.WebApplicationContext;
14 import org.springframework.web.context.support.WebApplicationContextUtils;
15
16 import com.tc.object.config.ConfigLockLevel;
17 import com.tc.object.config.ConfigVisitor;
18 import com.tc.object.config.DSOClientConfigHelper;
19 import com.tc.object.config.DSOSpringConfigHelper;
20 import com.tc.object.config.StandardDSOSpringConfigHelper;
21 import com.tc.simulator.app.ApplicationConfig;
22 import com.tc.simulator.listener.ListenerProvider;
23 import com.tctest.TransparentTestBase;
24 import com.tctest.runner.AbstractTransparentApp;
25 import com.tctest.spring.bean.Singleton;
26
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.ServletContext JavaDoc;
35 import javax.servlet.ServletContextEvent JavaDoc;
36
37 /**
38  * Test case for Spring web application context
39  *
40  * @author Eugene Kuleshov
41  */

42 public class WebAppContext_Test extends TransparentTestBase {
43   private static final int LOOP_ITERATIONS = 1;
44   private static final int EXECUTION_COUNT = 1;
45   private static final int NODE_COUNT = 4;
46
47   public WebAppContext_Test() {
48     disableAllUntil("2008-01-01");
49   }
50
51   protected void setUp() throws Exception JavaDoc {
52     super.setUp();
53     getTransparentAppConfig().setClientCount(NODE_COUNT).setApplicationInstancePerClientCount(EXECUTION_COUNT)
54         .setIntensity(LOOP_ITERATIONS);
55     initializeTestRunner();
56   }
57
58   protected Class JavaDoc getApplicationClass() {
59     return WebAppContextApp.class;
60   }
61   
62
63   public static class WebAppContextApp extends AbstractTransparentApp {
64
65     private List JavaDoc sharedSingletons = new ArrayList JavaDoc();
66     
67     
68     public WebAppContextApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
69       super(appId, cfg, listenerProvider);
70     }
71
72     public void run() {
73         try {
74           final Map JavaDoc contextAttributes = new HashMap JavaDoc();
75
76           final String JavaDoc contextName = "beanfactory.xml";
77           URL JavaDoc contextUrl = getClass().getResource(contextName);
78           assertNotNull("Unable to load context "+contextName, contextUrl);
79
80           Mock servletContextMock = new Mock(ServletContext JavaDoc.class);
81           
82           servletContextMock.expects(new MethodNameMatcher(new IsEqual("log"))).withAnyArguments().isVoid();
83           
84           servletContextMock.expects(new MethodNameMatcher(new IsEqual("getInitParameter")))
85               .with(new IsEqual("locatorFactorySelector")).will(new ReturnStub(null));
86           
87           servletContextMock.expects(new MethodNameMatcher(new IsEqual("getInitParameter")))
88               .with(new IsEqual("parentContextKey")).will(new ReturnStub(null));
89
90           servletContextMock.expects(new MethodNameMatcher(new IsEqual("getInitParameter")))
91               .with(new IsEqual("contextClass")).will(new ReturnStub(null));
92           
93           servletContextMock.expects(new MethodNameMatcher(new IsEqual("getInitParameter")))
94             .with(new IsEqual("contextConfigLocation")).will(new ReturnStub(contextName));
95       
96           servletContextMock.expects(new MethodNameMatcher(new IsEqual("getResource")))
97             .with(new IsEqual("/"+contextName)).will(new ReturnStub(contextUrl));
98       
99           servletContextMock.expects(new MethodNameMatcher(new IsEqual("getResourceAsStream")))
100             .with(new IsEqual("/"+contextName))
101             .will(new CustomStub("getResourceAsStream") {
102                 public Object JavaDoc invoke(Invocation arg) throws Throwable JavaDoc {
103                   return getClass().getResourceAsStream(contextName);
104                 }
105               });
106           
107           servletContextMock.expects(new MethodNameMatcher(new IsEqual("setAttribute")) {
108               public void invoked(Invocation invocation) {
109                 List JavaDoc params = invocation.parameterValues;
110                 contextAttributes.put(params.get(0), params.get(1));
111               }
112             });
113         
114           servletContextMock.expects(new MethodNameMatcher(new IsEqual("getAttribute")))
115             .will(new CustomStub("getAttribute") {
116                 public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
117                   return contextAttributes.get(invocation.parameterValues.get(0));
118                 }
119               });
120         
121           ServletContext JavaDoc servletContext = (ServletContext JavaDoc) servletContextMock.proxy();
122           ContextLoaderListener listener = new ContextLoaderListener();
123           listener.contextInitialized(new ServletContextEvent JavaDoc(servletContext));
124
125           WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
126
127           moveToStageAndWait(1);
128           
129           Singleton singleton = (Singleton) ctx.getBean("singleton");
130           singleton.incrementCounter();
131
132           String JavaDoc applicationId = getApplicationId();
133           singleton.setTransientValue(applicationId);
134           
135           synchronized (sharedSingletons) {
136             sharedSingletons.add(singleton);
137           }
138           
139           moveToStageAndWait(2);
140           
141           assertTrue("Expected more then one object in the collection", sharedSingletons.size()>1);
142           
143           for (Iterator JavaDoc it = sharedSingletons.iterator(); it.hasNext();) {
144             Singleton o = (Singleton) it.next();
145             assertTrue("Found non-singleton object", o==singleton);
146             assertEquals("Invalid value in shared field "+o, NODE_COUNT, o.getCounter());
147             assertEquals("Invalid transient value "+o, applicationId, o.getTransientValue());
148           }
149           
150         } catch (Throwable JavaDoc e) {
151           notifyError(e);
152            
153         }
154     }
155
156     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
157       config.addRoot("com.tctest.spring.WebAppContext_Test$WebAppContextApp", "sharedSingletons", "sharedSingletons", false);
158       config.addAutolock("* com.tctest.spring.WebAppContext_Test$WebAppContextApp.run()", ConfigLockLevel.WRITE);
159       
160       config.addIncludePattern("org.jmock.core.Invocation"); // needed to make mock definitions happy
161

162       DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
163       springConfig.addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing framework
164
springConfig.addConfigPattern("*/beanfactory.xml");
165       springConfig.addBean("singleton");
166       config.addDSOSpringConfig(springConfig);
167     }
168     
169   }
170
171 }
172
173
Popular Tags