From 17b598433db28681344b65d10436ccac5f1fff54 Mon Sep 17 00:00:00 2001 From: Hedju Hor Date: Thu, 18 Jan 2018 23:10:25 +0100 Subject: [PATCH] WW-4891 PrepareOperations overrideDevMode for explicitly switch on for this action only --- .../tag/nonui/debugtag/DebugTagAction.java | 30 ++++++++++++++++++ .../src/main/resources/struts-tags-non-ui.xml | 2 +- .../main/webapp/WEB-INF/tags/non-ui/debug.jsp | 2 +- .../org/apache/struts2/components/Debug.java | 5 +-- .../struts2/views/jsp/ui/DebugTagTest.java | 31 +++++-------------- 5 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 apps/showcase/src/main/java/org/apache/struts2/showcase/tag/nonui/debugtag/DebugTagAction.java diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/tag/nonui/debugtag/DebugTagAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/tag/nonui/debugtag/DebugTagAction.java new file mode 100644 index 000000000..e3c268150 --- /dev/null +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/tag/nonui/debugtag/DebugTagAction.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.struts2.showcase.tag.nonui.debugtag; + +import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.dispatcher.PrepareOperations; + +public class DebugTagAction extends ActionSupport { + + public String execute() throws Exception { + PrepareOperations.overrideDevMode(true); // Just for Showcase, explicitly switch on for this action only + return SUCCESS; + } +} \ No newline at end of file diff --git a/apps/showcase/src/main/resources/struts-tags-non-ui.xml b/apps/showcase/src/main/resources/struts-tags-non-ui.xml index 484341976..42d0506ae 100644 --- a/apps/showcase/src/main/resources/struts-tags-non-ui.xml +++ b/apps/showcase/src/main/resources/struts-tags-non-ui.xml @@ -151,7 +151,7 @@ /WEB-INF/tags/non-ui/date.jsp - + /WEB-INF/tags/non-ui/debug.jsp diff --git a/apps/showcase/src/main/webapp/WEB-INF/tags/non-ui/debug.jsp b/apps/showcase/src/main/webapp/WEB-INF/tags/non-ui/debug.jsp index 45131e87c..1629bf3ab 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/tags/non-ui/debug.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/tags/non-ui/debug.jsp @@ -42,7 +42,7 @@

Just click on the Debug label to see the Struts ValueStack Debug information.

- + diff --git a/core/src/main/java/org/apache/struts2/components/Debug.java b/core/src/main/java/org/apache/struts2/components/Debug.java index 87c32de87..e1c72fb28 100644 --- a/core/src/main/java/org/apache/struts2/components/Debug.java +++ b/core/src/main/java/org/apache/struts2/components/Debug.java @@ -30,11 +30,12 @@ import java.util.Map; import java.util.ArrayList; import java.util.List; +import org.apache.struts2.dispatcher.PrepareOperations; import org.apache.struts2.views.annotations.StrutsTag; import org.apache.struts2.StrutsException; @StrutsTag(name="debug", tldTagClass="org.apache.struts2.views.jsp.ui.DebugTag", - description="Prints debugging information (Only if 'struts.devMode' enabled or disable tag is set 'false')") + description="Prints debugging information (Only if 'struts.devMode' is enabled)") public class Debug extends UIBean { public static final String TEMPLATE = "debug"; @@ -88,7 +89,7 @@ public class Debug extends UIBean { } protected boolean showDebug() { - return (devMode || "false".equalsIgnoreCase(disabled)); + return (devMode || Boolean.TRUE == PrepareOperations.getDevModeOverride()); } private static class DebugMapEntry implements Map.Entry { diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java index 4fc1de30f..0a4e50d97 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java @@ -25,6 +25,7 @@ import com.opensymphony.xwork2.test.StubConfigurationProvider; import com.opensymphony.xwork2.util.location.LocatableProperties; import org.apache.commons.lang3.StringUtils; import org.apache.struts2.StrutsConstants; +import org.apache.struts2.dispatcher.PrepareOperations; import org.apache.struts2.views.jsp.AbstractUITagTest; import java.util.HashMap; @@ -63,44 +64,26 @@ public class DebugTagTest extends AbstractUITagTest { assertTrue("nothing to see here, devMode=false", StringUtils.isEmpty(writer.toString())); } - public void testTagAttributeDisableEmpty() throws Exception { + public void testTagAttributeOverrideDevModeTrue() throws Exception { setDevMode(false); - tag.doStartTag(); - tag.doEndTag(); - assertTrue("nothing to see here, devMode=false and Tag not set", StringUtils.isEmpty(writer.toString())); - } - - public void testTagAttributeDisableFalse() throws Exception { - setDevMode(false); - - tag.setDisabled("false"); + PrepareOperations.overrideDevMode(true); tag.doStartTag(); tag.doEndTag(); String result = writer.toString(); - assertTrue("devMode=false but Tag is set 'false'", StringUtils.isNotEmpty(result)); + assertTrue(StringUtils.isNotEmpty(result)); assertTrue("Property 'checkStackProperty' should be in Debug Tag output", StringUtils.contains(result, "checkStackProperty")); } - public void testTagAttributeDisableTrue() throws Exception { + public void testTagAttributeOverrideDevModeFalse() throws Exception { setDevMode(false); - tag.setDisabled("true"); + PrepareOperations.overrideDevMode(false); tag.doStartTag(); tag.doEndTag(); - assertTrue("nothing to see here, devMode=false and Tag is set 'true", StringUtils.isEmpty(writer.toString())); + assertTrue("nothing to see here, devMode=false and overrideDevMode=false", StringUtils.isEmpty(writer.toString())); } - public void testTagAttributeDisableAny() throws Exception { - setDevMode(false); - - tag.setDisabled("rubbish"); - tag.doStartTag(); - tag.doEndTag(); - assertTrue("nothing to see here, devMode=false and Tag is set 'rubbish", StringUtils.isEmpty(writer.toString())); - } - - private void setDevMode(final boolean devMode) { setStrutsConstant(new HashMap() {{ put(StrutsConstants.STRUTS_DEVMODE, Boolean.toString(devMode));