- Implement PrepareOperations.clearDevModeOverride() method, to clear any
leftover ThreadLocal state, as suggested by Y. Zamani.
- Call PrepareOperations.clearDevModeOverride() in the
StrutsInternalTestCase.setup() to ensure consistent initial state for every
test thread before the tests are executed.
- Call PrepareOperations.clearDevModeOverride() at the end of DebugTagTest
tests that call PrepareOperations.overrideDevMode(), for better state
management in those tests.
This commit is contained in:
JCgH4164838Gh792C124B5
2021-09-26 18:00:21 -04:00
parent 25c5a2b4b9
commit 68713648fa
3 changed files with 25 additions and 0 deletions
@@ -251,4 +251,15 @@ public class PrepareOperations {
return devModeOverride.get();
}
/**
* Clear any override of the static devMode value being applied to the current thread.
*
* This can be useful for any situation where {@link #overrideDevMode(boolean)} might be called
* in a flow where {@link #cleanupRequest(javax.servlet.http.HttpServletRequest)} does not get called.
* May be very situational (such as some unit tests), but may have other utility as well.
*/
public static void clearDevModeOverride() {
devModeOverride.remove(); // Remove current thread's value, enxure next read returns it to initialValue (typically null).
}
}
@@ -21,6 +21,7 @@ package org.apache.struts2;
import com.opensymphony.xwork2.XWorkTestCase;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.PrepareOperations;
import org.apache.struts2.util.StrutsTestCaseHelper;
import org.apache.struts2.views.jsp.StrutsMockServletContext;
import java.util.HashMap;
@@ -37,9 +38,13 @@ public abstract class StrutsInternalTestCase extends XWorkTestCase {
/**
* Sets up the configuration settings, XWork configuration, and
* message resources
*
* @throws java.lang.Exception
*/
@Override
protected void setUp() throws Exception {
super.setUp();
PrepareOperations.clearDevModeOverride(); // Clear DevMode override every time (consistent ThreadLocal state for tests).
initDispatcher(null);
}
@@ -66,6 +71,7 @@ public abstract class StrutsInternalTestCase extends XWorkTestCase {
return initDispatcher(params);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
// maybe someone else already destroyed Dispatcher
@@ -138,6 +138,8 @@ public class DebugTagTest extends AbstractUITagTest {
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " +
"May indicate that clearTagStateForTagPoolingServers() calls are not working properly.",
strutsBodyTagsAreReflectionEqual(tag, freshTag));
PrepareOperations.clearDevModeOverride(); // Clear DevMode override. Avoid ThreadLocal side-effects if test thread re-used.
}
public void testTagAttributeOverrideDevModeTrue_clearTagStateSet() throws Exception {
@@ -159,6 +161,8 @@ public class DebugTagTest extends AbstractUITagTest {
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " +
"May indicate that clearTagStateForTagPoolingServers() calls are not working properly.",
strutsBodyTagsAreReflectionEqual(tag, freshTag));
PrepareOperations.clearDevModeOverride(); // Clear DevMode override. Avoid ThreadLocal side-effects if test thread re-used.
}
public void testTagAttributeOverrideDevModeFalse() throws Exception {
@@ -176,6 +180,8 @@ public class DebugTagTest extends AbstractUITagTest {
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " +
"May indicate that clearTagStateForTagPoolingServers() calls are not working properly.",
strutsBodyTagsAreReflectionEqual(tag, freshTag));
PrepareOperations.clearDevModeOverride(); // Clear DevMode override. Avoid ThreadLocal side-effects if test thread re-used.
}
public void testTagAttributeOverrideDevModeFalse_clearTagStateSet() throws Exception {
@@ -195,6 +201,8 @@ public class DebugTagTest extends AbstractUITagTest {
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " +
"May indicate that clearTagStateForTagPoolingServers() calls are not working properly.",
strutsBodyTagsAreReflectionEqual(tag, freshTag));
PrepareOperations.clearDevModeOverride(); // Clear DevMode override. Avoid ThreadLocal side-effects if test thread re-used.
}
private void setDevMode(final boolean devMode) {