hlship 2004/08/11 15:13:26
Modified: framework/src/java/org/apache/hivemind/servlet
ServletMessages.java ServletStrings.properties
HiveMindFilter.java
framework/src/java/org/apache/hivemind/test
HiveMindTestCase.java
Added: framework/src/test/org/apache/hivemind/servlet
TestHiveMindFilter.java
Removed: framework/src/test/hivemind/test/servlet
MockFilterChain.java MockRequest.java
MockFilterConfig.java MockServletContext.java
MockResponse.java TestHiveMindFilter.java
Log:
Change HiveMindFilter to not store the Registry in the context.
Update the tests for HiveMindFilter to use EasyMock.
Revision Changes Path
1.5 +2 -2
jakarta-hivemind/framework/src/java/org/apache/hivemind/servlet/ServletMessages.java
Index: ServletMessages.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/servlet/ServletMessages.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ServletMessages.java 25 Jun 2004 19:29:54 -0000 1.4
+++ ServletMessages.java 11 Aug 2004 22:13:26 -0000 1.5
@@ -37,9 +37,9 @@
return _formatter.getMessage("filter-init");
}
- public static String storedRegistry(Registry registry, String contextKey)
+ public static String constructedRegistry(Registry registry, long millis)
{
- return _formatter.format("stored-registry", registry, contextKey);
+ return _formatter.format("constructed-registry", registry, new
Long(millis));
}
}
1.3 +1 -1
jakarta-hivemind/framework/src/java/org/apache/hivemind/servlet/ServletStrings.properties
Index: ServletStrings.properties
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/servlet/ServletStrings.properties,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ServletStrings.properties 25 Jun 2004 19:29:54 -0000 1.2
+++ ServletStrings.properties 11 Aug 2004 22:13:26 -0000 1.3
@@ -15,4 +15,4 @@
filter-init=Constructing HiveMind Registry.
filter-cleanup-error=Unable to cleanup current thread: {0}
-stored-registry=Stored {0} as servlet context attribute ''{1}''.
+constructed-registry=Constructed {0} in {1} milliseconds.
1.9 +21 -15
jakarta-hivemind/framework/src/java/org/apache/hivemind/servlet/HiveMindFilter.java
Index: HiveMindFilter.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/servlet/HiveMindFilter.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- HiveMindFilter.java 12 Jul 2004 17:28:24 -0000 1.8
+++ HiveMindFilter.java 11 Aug 2004 22:13:26 -0000 1.9
@@ -46,33 +46,39 @@
private static final Log LOG = LogFactory.getLog(HiveMindFilter.class);
/**
- * ServletContext attribute key that stores the Registry.
- */
- public static final String CONTEXT_KEY =
"org.apache.hivemind.DefaultRegistry";
-
- /**
* Request attribute key that stores the Registry.
*/
- public static final String REQUEST_KEY =
"org.apache.hivemind.RequestRegistry";
+ static final String REQUEST_KEY = "org.apache.hivemind.RequestRegistry";
private Registry _registry;
/**
+ * Package private method used for testing.
+ */
+ Registry getRegistry()
+ {
+ return _registry;
+ }
+
+ /**
* Constructs a [EMAIL PROTECTED] Registry} and stores it into the
* <code>ServletContext</code>. Any exception throws is logged.
*/
public void init(FilterConfig config) throws ServletException
{
+ long startTime = System.currentTimeMillis();
+
LOG.info(ServletMessages.filterInit());
try
{
_registry = constructRegistry(config);
- config.getServletContext().setAttribute(CONTEXT_KEY, _registry);
-
- LOG.info(ServletMessages.storedRegistry(_registry, CONTEXT_KEY));
+ LOG.info(
+ ServletMessages.constructedRegistry(
+ _registry,
+ System.currentTimeMillis() - startTime));
}
catch (Exception ex)
{
@@ -80,12 +86,12 @@
}
}
- /**
- * Invoked from [EMAIL PROTECTED] #init(FilterConfig)} to actually
- * construct the Registry. Subclasses may override if
- * they have specific initialization needs, or have nonstandard
- * rules for finding HiveMind module deployment descriptors.
- */
+ /**
+ * Invoked from [EMAIL PROTECTED] #init(FilterConfig)} to actually
+ * construct the Registry. Subclasses may override if
+ * they have specific initialization needs, or have nonstandard
+ * rules for finding HiveMind module deployment descriptors.
+ */
protected Registry constructRegistry(FilterConfig config)
{
ClassResolver resolver = new DefaultClassResolver();
1.10 +3 -1
jakarta-hivemind/framework/src/java/org/apache/hivemind/test/HiveMindTestCase.java
Index: HiveMindTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/test/HiveMindTestCase.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- HiveMindTestCase.java 1 Aug 2004 17:40:36 -0000 1.9
+++ HiveMindTestCase.java 11 Aug 2004 22:13:26 -0000 1.10
@@ -406,7 +406,8 @@
}
/**
- * Invokes [EMAIL PROTECTED] org.easymock.MockControl#verify()} on all
+ * Invokes [EMAIL PROTECTED] org.easymock.MockControl#verify()} and
+ * [EMAIL PROTECTED] MockControl#reset()} on all
* controls created by [EMAIL PROTECTED] #newControl(Class)}.
*/
@@ -417,6 +418,7 @@
{
MockControl c = (MockControl) i.next();
c.verify();
+ c.reset();
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/servlet/TestHiveMindFilter.java
Index: TestHiveMindFilter.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.servlet;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.HiveMind;
import org.apache.hivemind.Registry;
import org.apache.hivemind.service.ThreadCleanupListener;
import org.apache.hivemind.service.ThreadEventNotifier;
import org.apache.hivemind.test.HiveMindTestCase;
import org.easymock.MockControl;
/**
* Tests for [EMAIL PROTECTED] org.apache.hivemind.servlet.HiveMindFilter}.
*
* @author Howard Lewis Ship
*/
public class TestHiveMindFilter extends HiveMindTestCase
{
private static class Listener implements ThreadCleanupListener
{
private boolean _cleanup;
public void threadDidCleanup()
{
_cleanup = true;
}
public boolean getCleanup()
{
return _cleanup;
}
}
private static class HiveMindFilterFixture extends HiveMindFilter
{
protected Registry constructRegistry(FilterConfig config)
{
throw new ApplicationRuntimeException("Forced failure.");
}
}
public void testBasic() throws Exception
{
FilterConfig filterConfig = (FilterConfig)
newMock(FilterConfig.class);
replayControls();
HiveMindFilter f = new HiveMindFilter();
f.init(filterConfig);
verifyControls();
Registry r = f.getRegistry();
assertNotNull(r);
ThreadEventNotifier t =
(ThreadEventNotifier) r.getService(
HiveMind.THREAD_EVENT_NOTIFIER_SERVICE,
ThreadEventNotifier.class);
Listener l = new Listener();
t.addThreadCleanupListener(l);
MockControl requestControl = newControl(HttpServletRequest.class);
HttpServletRequest request = (HttpServletRequest)
requestControl.getMock();
HttpServletResponse response = (HttpServletResponse)
newMock(HttpServletResponse.class);
FilterChain chain = (FilterChain) newMock(FilterChain.class);
request.setAttribute(HiveMindFilter.REQUEST_KEY, r);
request.getAttribute(HiveMindFilter.REQUEST_KEY);
requestControl.setReturnValue(r);
chain.doFilter(request, response);
replayControls();
f.doFilter(request, response, chain);
assertSame(r, HiveMindFilter.getRegistry(request));
assertEquals(true, l.getCleanup());
f.destroy();
try
{
t.addThreadCleanupListener(null);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertExceptionSubstring(ex, "The HiveMind Registry has been
shutdown.");
}
verifyControls();
}
public void testExceptionInInit() throws Exception
{
Filter f = new HiveMindFilterFixture();
interceptLogging(HiveMindFilter.class.getName());
f.init(null);
assertLoggedMessage("Forced failure");
HttpServletRequest request = (HttpServletRequest)
newMock(HttpServletRequest.class);
HttpServletResponse response = (HttpServletResponse)
newMock(HttpServletResponse.class);
FilterChain chain = (FilterChain) newMock(FilterChain.class);
request.setAttribute(HiveMindFilter.REQUEST_KEY, null);
chain.doFilter(request, response);
replayControls();
f.doFilter(request, response, chain);
verifyControls();
f.destroy();
}
public void testDestroyWithoutRepository()
{
Filter f = new HiveMindFilter();
f.destroy();
}
public void testFilterWithoutRepository() throws Exception
{
Filter f = new HiveMindFilter();
interceptLogging(HiveMindFilter.class.getName());
HttpServletRequest request = (HttpServletRequest)
newMock(HttpServletRequest.class);
HttpServletResponse response = (HttpServletResponse)
newMock(HttpServletResponse.class);
FilterChain chain = (FilterChain) newMock(FilterChain.class);
request.setAttribute(HiveMindFilter.REQUEST_KEY, null);
chain.doFilter(request, response);
replayControls();
f.doFilter(request, response, chain);
assertLoggedMessage("Unable to cleanup current thread");
verifyControls();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]