SPR-7943 Add test case for inserting extra hidden fields
This commit is contained in:
parent
4199d772cc
commit
facb40e5a6
|
@ -458,7 +458,7 @@ public class FormTag extends AbstractHtmlElementTag {
|
||||||
if (hiddenFields != null) {
|
if (hiddenFields != null) {
|
||||||
for (String name : hiddenFields.keySet()) {
|
for (String name : hiddenFields.keySet()) {
|
||||||
this.tagWriter.appendValue("<input type=\"hidden\" ");
|
this.tagWriter.appendValue("<input type=\"hidden\" ");
|
||||||
this.tagWriter.appendValue("name=\"" + name + "\" value=\"" + hiddenFields.get(name) + "\" ");
|
this.tagWriter.appendValue("name=\"" + name + "\" value=\"" + hiddenFields.get(name) + "\">");
|
||||||
this.tagWriter.appendValue("</input>\n");
|
this.tagWriter.appendValue("</input>\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.web.servlet.tags.form;
|
package org.springframework.web.servlet.tags.form;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.createMock;
|
||||||
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.jsp.JspException;
|
import javax.servlet.jsp.JspException;
|
||||||
|
|
||||||
|
@ -28,8 +31,11 @@ import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockPageContext;
|
import org.springframework.mock.web.MockPageContext;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
|
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||||
import org.springframework.web.servlet.support.JspAwareRequestContext;
|
import org.springframework.web.servlet.support.JspAwareRequestContext;
|
||||||
import org.springframework.web.servlet.support.RequestContext;
|
import org.springframework.web.servlet.support.RequestContext;
|
||||||
|
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||||
|
import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
||||||
import org.springframework.web.servlet.tags.AbstractTagTests;
|
import org.springframework.web.servlet.tags.AbstractTagTests;
|
||||||
import org.springframework.web.servlet.tags.RequestContextAwareTag;
|
import org.springframework.web.servlet.tags.RequestContextAwareTag;
|
||||||
|
|
||||||
|
@ -59,10 +65,12 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
|
||||||
protected MockPageContext createAndPopulatePageContext() throws JspException {
|
protected MockPageContext createAndPopulatePageContext() throws JspException {
|
||||||
MockPageContext pageContext = createPageContext();
|
MockPageContext pageContext = createPageContext();
|
||||||
MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
|
MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
|
||||||
RequestContext requestContext = new JspAwareRequestContext(pageContext);
|
StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.getWebApplicationContext(request);
|
||||||
pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
|
wac.registerSingleton("requestDataValueProcessor", DelegatingRequestDataValueProcessor.class);
|
||||||
extendRequest(request);
|
extendRequest(request);
|
||||||
extendPageContext(pageContext);
|
extendPageContext(pageContext);
|
||||||
|
RequestContext requestContext = new JspAwareRequestContext(pageContext);
|
||||||
|
pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
|
||||||
return pageContext;
|
return pageContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +99,13 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
|
||||||
return (RequestContext) getPageContext().getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE);
|
return (RequestContext) getPageContext().getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected RequestDataValueProcessor getMockRequestDataValueProcessor() {
|
||||||
|
RequestDataValueProcessor mockProcessor = createMock(RequestDataValueProcessor.class);
|
||||||
|
ServletRequest request = getPageContext().getRequest();
|
||||||
|
StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.getWebApplicationContext(request);
|
||||||
|
wac.getBean(DelegatingRequestDataValueProcessor.class).setRequestDataValueProcessor(mockProcessor);
|
||||||
|
return mockProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
protected void exposeBindingResult(Errors errors) {
|
protected void exposeBindingResult(Errors errors) {
|
||||||
// wrap errors in a Model
|
// wrap errors in a Model
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2011 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.form;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
||||||
|
|
||||||
|
class DelegatingRequestDataValueProcessor implements RequestDataValueProcessor {
|
||||||
|
|
||||||
|
private RequestDataValueProcessor processor;
|
||||||
|
|
||||||
|
public void setRequestDataValueProcessor(RequestDataValueProcessor processor) {
|
||||||
|
this.processor = processor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String processUrl(HttpServletRequest request, String url) {
|
||||||
|
return (this.processor != null) ? this.processor.processUrl(request, url) : url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String processFormFieldValue(HttpServletRequest request, String name, String value, String type) {
|
||||||
|
return (this.processor != null) ? this.processor.processFormFieldValue(request, name, value, type) : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String processAction(HttpServletRequest request, String action) {
|
||||||
|
return (this.processor != null) ? this.processor.processAction(request, action) : action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getExtraHiddenFields(HttpServletRequest request) {
|
||||||
|
return (this.processor != null) ? this.processor.getExtraHiddenFields(request) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,10 +16,20 @@
|
||||||
|
|
||||||
package org.springframework.web.servlet.tags.form;
|
package org.springframework.web.servlet.tags.form;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.createMock;
|
||||||
|
import static org.easymock.EasyMock.expect;
|
||||||
|
import static org.easymock.EasyMock.replay;
|
||||||
|
import static org.easymock.EasyMock.verify;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import javax.servlet.jsp.PageContext;
|
import javax.servlet.jsp.PageContext;
|
||||||
import javax.servlet.jsp.tagext.Tag;
|
import javax.servlet.jsp.tagext.Tag;
|
||||||
|
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
|
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||||
|
import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
|
@ -49,13 +59,13 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
|
||||||
this.tag.setPageContext(getPageContext());
|
this.tag.setPageContext(getPageContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected void extendRequest(MockHttpServletRequest request) {
|
protected void extendRequest(MockHttpServletRequest request) {
|
||||||
request.setRequestURI(REQUEST_URI);
|
request.setRequestURI(REQUEST_URI);
|
||||||
request.setQueryString(QUERY_STRING);
|
request.setQueryString(QUERY_STRING);
|
||||||
this.request = request;
|
this.request = request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testWriteForm() throws Exception {
|
public void testWriteForm() throws Exception {
|
||||||
String commandName = "myCommand";
|
String commandName = "myCommand";
|
||||||
String name = "formName";
|
String name = "formName";
|
||||||
|
@ -270,6 +280,26 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
|
||||||
assertNull(getPageContext().getAttribute(FormTag.MODEL_ATTRIBUTE_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
|
assertNull(getPageContext().getAttribute(FormTag.MODEL_ATTRIBUTE_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testRequestDataValueProcessorHooks() throws Exception {
|
||||||
|
String action = "/my/form?foo=bar";
|
||||||
|
RequestDataValueProcessor processor = getMockRequestDataValueProcessor();
|
||||||
|
expect(processor.processAction(this.request, action)).andReturn(action);
|
||||||
|
expect(processor.getExtraHiddenFields(this.request)).andReturn(Collections.singletonMap("key", "value"));
|
||||||
|
replay(processor);
|
||||||
|
|
||||||
|
this.tag.doStartTag();
|
||||||
|
this.tag.doEndTag();
|
||||||
|
this.tag.doFinally();
|
||||||
|
|
||||||
|
String output = getOutput();
|
||||||
|
|
||||||
|
assertEquals("<input type=\"hidden\" name=\"key\" value=\"value\"></input>", getInputTag(output));
|
||||||
|
assertFormTagOpened(output);
|
||||||
|
assertFormTagClosed(output);
|
||||||
|
|
||||||
|
verify(processor);
|
||||||
|
}
|
||||||
|
|
||||||
private String getFormTag(String output) {
|
private String getFormTag(String output) {
|
||||||
int inputStart = output.indexOf("<", 1);
|
int inputStart = output.indexOf("<", 1);
|
||||||
int inputEnd = output.lastIndexOf(">", output.length() - 2);
|
int inputEnd = output.lastIndexOf(">", output.length() - 2);
|
||||||
|
|
Loading…
Reference in New Issue