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 1629bf3ab..45131e87c 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 c0b33d934..87c32de87 100644
--- a/core/src/main/java/org/apache/struts2/components/Debug.java
+++ b/core/src/main/java/org/apache/struts2/components/Debug.java
@@ -34,13 +34,12 @@ 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")
+ description="Prints debugging information (Only if 'struts.devMode' enabled or disable tag is set 'false')")
public class Debug extends UIBean {
public static final String TEMPLATE = "debug";
-
+
protected ReflectionProvider reflectionProvider;
-
public Debug(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
super(stack, request, response);
@@ -50,7 +49,7 @@ public class Debug extends UIBean {
public void setReflectionProvider(ReflectionProvider prov) {
this.reflectionProvider = prov;
}
-
+
protected String getDefaultTemplate() {
return TEMPLATE;
}
@@ -58,25 +57,40 @@ public class Debug extends UIBean {
public boolean start(Writer writer) {
boolean result = super.start(writer);
- ValueStack stack = getStack();
- Iterator iter = stack.getRoot().iterator();
- List stackValues = new ArrayList(stack.getRoot().size());
- while (iter.hasNext()) {
- Object o = iter.next();
- Map values;
- try {
- values = reflectionProvider.getBeanMap(o);
- } catch (Exception e) {
- throw new StrutsException("Caught an exception while getting the property values of " + o, e);
+ if (showDebug()) {
+ ValueStack stack = getStack();
+ Iterator iter = stack.getRoot().iterator();
+ List stackValues = new ArrayList(stack.getRoot().size());
+ while (iter.hasNext()) {
+ Object o = iter.next();
+ Map values;
+ try {
+ values = reflectionProvider.getBeanMap(o);
+ } catch (Exception e) {
+ throw new StrutsException("Caught an exception while getting the property values of " + o, e);
+ }
+ stackValues.add(new DebugMapEntry(o.getClass().getName(), values));
}
- stackValues.add(new DebugMapEntry(o.getClass().getName(), values));
+
+ addParameter("stackValues", stackValues);
}
-
- addParameter("stackValues", stackValues);
-
return result;
}
+ @Override
+ public boolean end(Writer writer, String body) {
+ if (showDebug()) {
+ return super.end(writer, body);
+ } else {
+ popComponentStack();
+ return false;
+ }
+ }
+
+ protected boolean showDebug() {
+ return (devMode || "false".equalsIgnoreCase(disabled));
+ }
+
private static class DebugMapEntry implements Map.Entry {
private Object key;
private Object value;
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
new file mode 100644
index 000000000..4fc1de30f
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DebugTagTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.views.jsp.ui;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.config.ConfigurationException;
+import com.opensymphony.xwork2.inject.ContainerBuilder;
+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.views.jsp.AbstractUITagTest;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Test case for {@link org.apache.struts2.components.Debug}.
+ */
+public class DebugTagTest extends AbstractUITagTest {
+
+ private DebugTag tag;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ tag = new DebugTag();
+ tag.setPageContext(pageContext);
+ context.put("checkStackProperty", "Hello World");
+ }
+
+ public void testDevModeEnabled() throws Exception {
+ setDevMode(true);
+
+ tag.doStartTag();
+ tag.doEndTag();
+ String result = writer.toString();
+ assertTrue(StringUtils.isNotEmpty(result));
+ assertTrue("Property 'checkStackProperty' should be in Debug Tag output", StringUtils.contains(result, "checkStackProperty | "));
+ }
+
+ public void testDevModeDisabled() throws Exception {
+ setDevMode(false);
+
+ tag.doStartTag();
+ tag.doEndTag();
+ assertTrue("nothing to see here, devMode=false", StringUtils.isEmpty(writer.toString()));
+ }
+
+ public void testTagAttributeDisableEmpty() 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");
+ tag.doStartTag();
+ tag.doEndTag();
+ String result = writer.toString();
+ assertTrue("devMode=false but Tag is set 'false'", StringUtils.isNotEmpty(result));
+ assertTrue("Property 'checkStackProperty' should be in Debug Tag output", StringUtils.contains(result, "checkStackProperty | "));
+ }
+
+ public void testTagAttributeDisableTrue() throws Exception {
+ setDevMode(false);
+
+ tag.setDisabled("true");
+ tag.doStartTag();
+ tag.doEndTag();
+ assertTrue("nothing to see here, devMode=false and Tag is set 'true", 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));
+ }});
+ }
+
+ /**
+ * Overwrite the Struts Constant and reload container
+ */
+ private void setStrutsConstant(final Map overwritePropeties) {
+ configurationManager.addContainerProvider(new StubConfigurationProvider() {
+ @Override
+ public boolean needsReload() {
+ return true;
+ }
+
+ @Override
+ public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
+ for (Map.Entry stringStringEntry : overwritePropeties.entrySet()) {
+ props.setProperty(stringStringEntry.getKey(), stringStringEntry.getValue(), null);
+ }
+ }
+ });
+
+ configurationManager.reload();
+ container = configurationManager.getConfiguration().getContainer();
+ stack.getContext().put(ActionContext.CONTAINER, container);
+ }
+}
\ No newline at end of file