001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.tests.framework.router;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019: import junit.framework.TestCase;
020: import org.araneaframework.core.BaseService;
021: import org.araneaframework.core.NoSuchServiceException;
022: import org.araneaframework.framework.ThreadContext;
023: import org.araneaframework.framework.router.StandardThreadServiceRouterService;
024: import org.araneaframework.http.core.StandardServletInputData;
025: import org.araneaframework.http.core.StandardServletOutputData;
026: import org.araneaframework.mock.MockUtil;
027: import org.araneaframework.mock.core.MockEventfulStandardService;
028: import org.springframework.mock.web.MockHttpServletRequest;
029: import org.springframework.mock.web.MockHttpServletResponse;
030:
031: /**
032: * @author "Toomas Römer" <toomas@webmedia.ee>
033: */
034: public class StandardThreadServiceRouterServiceTests extends TestCase {
035: private StandardThreadServiceRouterService service;
036: private MockEventfulStandardService child1;
037: private MockEventfulStandardService child2;
038:
039: private StandardServletInputData input;
040: private StandardServletOutputData output;
041:
042: private MockHttpServletRequest req;
043: private MockHttpServletResponse res;
044:
045: private Map map;
046:
047: public void setUp() throws Exception {
048: service = new StandardThreadServiceRouterService();
049: map = new HashMap();
050:
051: child1 = new MockEventfulStandardService();
052: child2 = new MockEventfulStandardService();
053:
054: req = new MockHttpServletRequest();
055: res = new MockHttpServletResponse();
056:
057: input = new StandardServletInputData(req);
058: output = new StandardServletOutputData(req, res);
059:
060: map.put("child1", child1);
061: map.put("child2", child2);
062:
063: service.setServiceMap(map);
064: service._getComponent().init(null, MockUtil.getEnv());
065:
066: service.setDefaultServiceId("child1");
067: }
068:
069: public void testCloseRemoves() throws Exception, Throwable {
070: service._getService().action(MockUtil.getPath(), input, output);
071: ThreadContext sess = (ThreadContext) child1.getTheEnvironment()
072: .getEntry(ThreadContext.class);
073: assertNotNull(sess.getService("child1"));
074: sess.close("child1");
075: assertTrue(child1.getDestroyCalled());
076: assertNull(sess.getService("child1"));
077: }
078:
079: public void testServiceExpiration() throws Exception {
080: ThreadContext ctx = (ThreadContext) child1.getTheEnvironment()
081: .getEntry(ThreadContext.class);
082: ctx.addService("newService", new BaseService() {
083: }, new Long(1000));
084: Thread.sleep(1200);
085: assertNotNull("Action is not yet called.", ctx
086: .getService("newService"));
087: service._getService().action(MockUtil.getPath(), input, output);
088: assertNull(
089: "Action is called when service should already be expired.",
090: ctx.getService("newService"));
091:
092: // make sure that in addition to killing expired services, their lifetimes are updated in action()
093: ctx.addService("nextService", new BaseService() {
094: }, new Long(2000));
095: MockHttpServletRequest req = new MockHttpServletRequest();
096: req.addParameter(ThreadContext.THREAD_SERVICE_KEY,
097: "nextService");
098: input = new StandardServletInputData(req);
099:
100: Thread.sleep(1000);
101: service._getService().action(MockUtil.getPath(), input, output);
102: Thread.sleep(1500);
103: service._getService().action(MockUtil.getPath(), input, output);
104:
105: assertNotNull("Should still be alive", ctx
106: .getService("nextService"));
107:
108: Thread.sleep(2200);
109:
110: try {
111: service._getService().action(MockUtil.getPath(), input,
112: output);
113: fail("Routing to 'nextService' should have failed.");
114: } catch (NoSuchServiceException e) {
115: //
116: }
117:
118: assertNull("Should be dead now.", ctx.getService("nextService"));
119: }
120: }
|