001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.spring;
006:
007: import org.jmock.Mock;
008: import org.jmock.core.Invocation;
009: import org.jmock.core.constraint.IsEqual;
010: import org.jmock.core.matcher.MethodNameMatcher;
011: import org.jmock.core.stub.CustomStub;
012: import org.jmock.core.stub.ReturnStub;
013: import org.springframework.web.context.ContextLoaderListener;
014: import org.springframework.web.context.WebApplicationContext;
015: import org.springframework.web.context.support.WebApplicationContextUtils;
016:
017: import com.tc.object.config.ConfigLockLevel;
018: import com.tc.object.config.ConfigVisitor;
019: import com.tc.object.config.DSOClientConfigHelper;
020: import com.tc.object.config.DSOSpringConfigHelper;
021: import com.tc.object.config.Root;
022: import com.tc.object.config.StandardDSOSpringConfigHelper;
023: import com.tc.simulator.app.ApplicationConfig;
024: import com.tc.simulator.listener.ListenerProvider;
025: import com.tc.util.runtime.Vm;
026: import com.tctest.TransparentTestBase;
027: import com.tctest.runner.AbstractTransparentApp;
028: import com.tctest.spring.bean.Singleton;
029:
030: import java.net.URL;
031: import java.util.ArrayList;
032: import java.util.Date;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: import javax.servlet.ServletContext;
039: import javax.servlet.ServletContextEvent;
040:
041: /**
042: * Test case for Spring web application context
043: *
044: * @author Eugene Kuleshov
045: */
046: public class WebAppContext_Test extends TransparentTestBase {
047: private static final int LOOP_ITERATIONS = 1;
048: private static final int EXECUTION_COUNT = 1;
049: private static final int NODE_COUNT = 4;
050:
051: public WebAppContext_Test() {
052: if (Vm.isIBM()) {
053: disableAllUntil(new Date(Long.MAX_VALUE));
054: }
055: }
056:
057: protected void setUp() throws Exception {
058: super .setUp();
059: getTransparentAppConfig().setClientCount(NODE_COUNT)
060: .setApplicationInstancePerClientCount(EXECUTION_COUNT)
061: .setIntensity(LOOP_ITERATIONS);
062: initializeTestRunner();
063: }
064:
065: protected Class getApplicationClass() {
066: return WebAppContextApp.class;
067: }
068:
069: public static class WebAppContextApp extends AbstractTransparentApp {
070:
071: private List sharedSingletons = new ArrayList();
072:
073: public WebAppContextApp(String appId, ApplicationConfig cfg,
074: ListenerProvider listenerProvider) {
075: super (appId, cfg, listenerProvider);
076: }
077:
078: public void run() {
079: try {
080: final Map contextAttributes = new HashMap();
081:
082: final String contextName = "beanfactory.xml";
083: URL contextUrl = getClass().getResource(contextName);
084: assertNotNull("Unable to load context " + contextName,
085: contextUrl);
086:
087: Mock servletContextMock = new Mock(ServletContext.class);
088:
089: servletContextMock.expects(
090: new MethodNameMatcher(new IsEqual("log")))
091: .withAnyArguments().isVoid();
092:
093: servletContextMock.expects(
094: new MethodNameMatcher(new IsEqual(
095: "getInitParameter"))).with(
096: new IsEqual("locatorFactorySelector")).will(
097: new ReturnStub(null));
098:
099: servletContextMock.expects(
100: new MethodNameMatcher(new IsEqual(
101: "getInitParameter"))).with(
102: new IsEqual("parentContextKey")).will(
103: new ReturnStub(null));
104:
105: servletContextMock.expects(
106: new MethodNameMatcher(new IsEqual(
107: "getInitParameter"))).with(
108: new IsEqual("contextClass")).will(
109: new ReturnStub(null));
110:
111: servletContextMock.expects(
112: new MethodNameMatcher(new IsEqual(
113: "getInitParameter"))).with(
114: new IsEqual("contextConfigLocation")).will(
115: new ReturnStub(contextName));
116:
117: servletContextMock.expects(
118: new MethodNameMatcher(
119: new IsEqual("getResource"))).with(
120: new IsEqual("/" + contextName)).will(
121: new ReturnStub(contextUrl));
122:
123: servletContextMock.expects(
124: new MethodNameMatcher(new IsEqual(
125: "getResourceAsStream"))).with(
126: new IsEqual("/" + contextName)).will(
127: new CustomStub("getResourceAsStream") {
128: public Object invoke(Invocation arg)
129: throws Throwable {
130: return getClass().getResourceAsStream(
131: contextName);
132: }
133: });
134:
135: servletContextMock.expects(new MethodNameMatcher(
136: new IsEqual("setAttribute")) {
137: public void invoked(Invocation invocation) {
138: List params = invocation.parameterValues;
139: contextAttributes.put(params.get(0), params
140: .get(1));
141: }
142: });
143:
144: servletContextMock.expects(
145: new MethodNameMatcher(new IsEqual(
146: "getAttribute"))).will(
147: new CustomStub("getAttribute") {
148: public Object invoke(Invocation invocation)
149: throws Throwable {
150: return contextAttributes
151: .get(invocation.parameterValues
152: .get(0));
153: }
154: });
155:
156: ServletContext servletContext = (ServletContext) servletContextMock
157: .proxy();
158: ContextLoaderListener listener = new ContextLoaderListener();
159: listener.contextInitialized(new ServletContextEvent(
160: servletContext));
161:
162: WebApplicationContext ctx = WebApplicationContextUtils
163: .getWebApplicationContext(servletContext);
164:
165: moveToStageAndWait(1);
166:
167: Singleton singleton = (Singleton) ctx
168: .getBean("singleton");
169: singleton.incrementCounter();
170:
171: String applicationId = getApplicationId();
172: singleton.setTransientValue(applicationId);
173:
174: synchronized (sharedSingletons) {
175: sharedSingletons.add(singleton);
176: }
177:
178: moveToStageAndWait(2);
179:
180: assertTrue(
181: "Expected more then one object in the collection",
182: sharedSingletons.size() > 1);
183:
184: for (Iterator it = sharedSingletons.iterator(); it
185: .hasNext();) {
186: Singleton o = (Singleton) it.next();
187: assertTrue("Found non-singleton object",
188: o == singleton);
189: assertEquals("Invalid value in shared field " + o,
190: NODE_COUNT, o.getCounter());
191: assertEquals("Invalid transient value " + o,
192: applicationId, o.getTransientValue());
193: }
194:
195: } catch (Throwable e) {
196: notifyError(e);
197:
198: }
199: }
200:
201: public static void visitL1DSOConfig(ConfigVisitor visitor,
202: DSOClientConfigHelper config) {
203: config
204: .addRoot(
205: new Root(
206: "com.tctest.spring.WebAppContext_Test$WebAppContextApp",
207: "sharedSingletons",
208: "sharedSingletons"), false);
209: config
210: .addAutolock(
211: "* com.tctest.spring.WebAppContext_Test$WebAppContextApp.run()",
212: ConfigLockLevel.WRITE);
213:
214: config.addIncludePattern("org.jmock.core.Invocation"); // needed to make mock definitions happy
215:
216: DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
217: springConfig
218: .addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing
219: // framework
220: springConfig.addConfigPattern("*/beanfactory.xml");
221: springConfig.addBean("singleton");
222: config.addDSOSpringConfig(springConfig);
223: }
224:
225: }
226:
227: }
|