Add <spring:argument> subtag for message/theme
Add a new <spring:argument> tag that cab be nested within <spring:message> and <spring:theme>. The tag is based on the <fmt:param> tag and uses conventions found throughout other Spring tags. Issue: SPR-9678
This commit is contained in:
committed by
Phillip Webb
parent
50bd70f13d
commit
f9b17a708f
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* 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.springframework.web.servlet.tags;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
import javax.servlet.jsp.tagext.TagSupport;
|
||||
|
||||
import org.springframework.mock.web.test.MockBodyContent;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
|
||||
/**
|
||||
* Unit tests for ArgumentTag
|
||||
*
|
||||
* @author Nicholas Williams
|
||||
*/
|
||||
public class ArgumentTagTests extends AbstractTagTests {
|
||||
|
||||
private ArgumentTag tag;
|
||||
|
||||
private MockArgumentSupportTag parent;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
PageContext context = createPageContext();
|
||||
parent = new MockArgumentSupportTag();
|
||||
tag = new ArgumentTag();
|
||||
tag.setPageContext(context);
|
||||
tag.setParent(parent);
|
||||
}
|
||||
|
||||
public void testArgumentWithStringValue() throws JspException {
|
||||
tag.setValue("value1");
|
||||
|
||||
int action = tag.doEndTag();
|
||||
|
||||
assertEquals(Tag.EVAL_PAGE, action);
|
||||
assertEquals("value1", parent.getArgument());
|
||||
}
|
||||
|
||||
public void testArgumentWithImplicitNullValue() throws JspException {
|
||||
int action = tag.doEndTag();
|
||||
|
||||
assertEquals(Tag.EVAL_PAGE, action);
|
||||
assertNull(parent.getArgument());
|
||||
}
|
||||
|
||||
public void testArgumentWithExplicitNullValue() throws JspException {
|
||||
tag.setValue(null);
|
||||
|
||||
int action = tag.doEndTag();
|
||||
|
||||
assertEquals(Tag.EVAL_PAGE, action);
|
||||
assertNull(parent.getArgument());
|
||||
}
|
||||
|
||||
public void testArgumentWithBodyValue() throws JspException {
|
||||
tag.setBodyContent(new MockBodyContent("value2",
|
||||
new MockHttpServletResponse()));
|
||||
|
||||
int action = tag.doEndTag();
|
||||
|
||||
assertEquals(Tag.EVAL_PAGE, action);
|
||||
assertEquals("value2", parent.getArgument());
|
||||
}
|
||||
|
||||
public void testArgumentWithValueThenReleaseThenBodyValue() throws JspException {
|
||||
tag.setValue("value3");
|
||||
|
||||
int action = tag.doEndTag();
|
||||
|
||||
assertEquals(Tag.EVAL_PAGE, action);
|
||||
assertEquals("value3", parent.getArgument());
|
||||
|
||||
tag.release();
|
||||
|
||||
parent = new MockArgumentSupportTag();
|
||||
tag.setPageContext(createPageContext());
|
||||
tag.setParent(parent);
|
||||
tag.setBodyContent(new MockBodyContent("value4",
|
||||
new MockHttpServletResponse()));
|
||||
|
||||
action = tag.doEndTag();
|
||||
|
||||
assertEquals(Tag.EVAL_PAGE, action);
|
||||
assertEquals("value4", parent.getArgument());
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private class MockArgumentSupportTag extends TagSupport implements ArgumentAware {
|
||||
|
||||
Object argument;
|
||||
|
||||
@Override
|
||||
public void addArgument(Object argument) {
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
private Object getArgument() {
|
||||
return argument;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,6 @@ package org.springframework.web.servlet.tags;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.jsp.JspException;
|
||||
@@ -27,14 +26,16 @@ import javax.servlet.jsp.tagext.Tag;
|
||||
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
|
||||
/**
|
||||
* Tests for {@link MessageTag}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Alef Arendsen
|
||||
* @author Nicholas Williams
|
||||
*/
|
||||
public class MessageTagTests extends AbstractTagTests {
|
||||
|
||||
@@ -51,6 +52,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setPageContext(pc);
|
||||
tag.setMessage(new DefaultMessageSourceResolvable("test"));
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test message", message.toString());
|
||||
}
|
||||
|
||||
@@ -67,6 +69,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode("test");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test message", message.toString());
|
||||
}
|
||||
|
||||
@@ -83,6 +86,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode(null);
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "null", message.toString());
|
||||
}
|
||||
|
||||
@@ -100,6 +104,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setCode("testArgs");
|
||||
tag.setArguments("arg1");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test arg1 message {1}", message.toString());
|
||||
}
|
||||
|
||||
@@ -117,6 +122,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setCode("testArgs");
|
||||
tag.setArguments("arg1,arg2");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test arg1 message arg2", message.toString());
|
||||
}
|
||||
|
||||
@@ -135,6 +141,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setArguments("arg1,1;arg2,2");
|
||||
tag.setArgumentSeparator(";");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test arg1,1 message arg2,2", message.toString());
|
||||
}
|
||||
|
||||
@@ -150,8 +157,9 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
};
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode("testArgs");
|
||||
tag.setArguments(new Object[] {"arg1", new Integer(5)});
|
||||
tag.setArguments(new Object[] {"arg1", 5});
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test arg1 message 5", message.toString());
|
||||
}
|
||||
|
||||
@@ -167,11 +175,68 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
};
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode("testArgs");
|
||||
tag.setArguments(new Integer(5));
|
||||
tag.setArguments(5);
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test 5 message {1}", message.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public void testMessageTagWithCodeAndArgumentAndNestedArgument() throws JspException {
|
||||
PageContext pc = createPageContext();
|
||||
final StringBuffer message = new StringBuffer();
|
||||
MessageTag tag = new MessageTag() {
|
||||
@Override
|
||||
protected void writeMessage(String msg) {
|
||||
message.append(msg);
|
||||
}
|
||||
};
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode("testArgs");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
tag.setArguments(5);
|
||||
tag.addArgument(7);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test 5 message 7", message.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public void testMessageTagWithCodeAndNestedArgument() throws JspException {
|
||||
PageContext pc = createPageContext();
|
||||
final StringBuffer message = new StringBuffer();
|
||||
MessageTag tag = new MessageTag() {
|
||||
@Override
|
||||
protected void writeMessage(String msg) {
|
||||
message.append(msg);
|
||||
}
|
||||
};
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode("testArgs");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
tag.addArgument(7);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test 7 message {1}", message.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public void testMessageTagWithCodeAndNestedArguments() throws JspException {
|
||||
PageContext pc = createPageContext();
|
||||
final StringBuffer message = new StringBuffer();
|
||||
MessageTag tag = new MessageTag() {
|
||||
@Override
|
||||
protected void writeMessage(String msg) {
|
||||
message.append(msg);
|
||||
}
|
||||
};
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode("testArgs");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
tag.addArgument("arg1");
|
||||
tag.addArgument(6);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test arg1 message 6", message.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public void testMessageTagWithCodeAndText() throws JspException {
|
||||
PageContext pc = createPageContext();
|
||||
@@ -186,6 +251,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setCode("test");
|
||||
tag.setText("testtext");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test message", (message.toString()));
|
||||
}
|
||||
|
||||
@@ -203,6 +269,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setText("test & text");
|
||||
tag.setHtmlEscape(true);
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test & text", message.toString());
|
||||
}
|
||||
|
||||
@@ -220,6 +287,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setText("' test & text \\");
|
||||
tag.setJavaScriptEscape(true);
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "\\' test & text \\\\", message.toString());
|
||||
}
|
||||
|
||||
@@ -238,6 +306,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setHtmlEscape(true);
|
||||
tag.setJavaScriptEscape(true);
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "' test & text \\\\", message.toString());
|
||||
}
|
||||
|
||||
@@ -249,6 +318,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setVar("testvar");
|
||||
tag.setScope("page");
|
||||
tag.doStartTag();
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("text & text", pc.getAttribute("testvar"));
|
||||
tag.release();
|
||||
|
||||
@@ -257,6 +327,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setCode("test");
|
||||
tag.setVar("testvar2");
|
||||
tag.doStartTag();
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test message", pc.getAttribute("testvar2"));
|
||||
tag.release();
|
||||
}
|
||||
@@ -268,6 +339,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setText("text & text");
|
||||
tag.setVar("testvar");
|
||||
tag.doStartTag();
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("text & text", pc.getAttribute("testvar"));
|
||||
tag.release();
|
||||
|
||||
@@ -277,6 +349,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setVar("testvar");
|
||||
|
||||
tag.doStartTag();
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("Correct message", "test message", pc.getAttribute("testvar"));
|
||||
}
|
||||
|
||||
@@ -291,6 +364,7 @@ public class MessageTagTests extends AbstractTagTests {
|
||||
tag.setCode("test");
|
||||
tag.setVar("testvar2");
|
||||
tag.doStartTag();
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
}
|
||||
|
||||
public void testRequestContext() throws ServletException {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -48,6 +48,7 @@ public class ThemeTagTests extends AbstractTagTests {
|
||||
tag.setPageContext(pc);
|
||||
tag.setCode("themetest");
|
||||
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
|
||||
assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag());
|
||||
assertEquals("theme test message", message.toString());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user