SPR-7943 Add test case for inserting extra hidden fields

This commit is contained in:
Rossen Stoyanchev 2011-10-06 21:19:54 +00:00
parent 4199d772cc
commit facb40e5a6
4 changed files with 99 additions and 5 deletions

View File

@ -458,7 +458,7 @@ public class FormTag extends AbstractHtmlElementTag {
if (hiddenFields != null) {
for (String name : hiddenFields.keySet()) {
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");
}
}

View File

@ -16,11 +16,14 @@
package org.springframework.web.servlet.tags.form;
import static org.easymock.EasyMock.createMock;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
@ -28,8 +31,11 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockPageContext;
import org.springframework.validation.BindingResult;
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.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.RequestContextAwareTag;
@ -59,10 +65,12 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
protected MockPageContext createAndPopulatePageContext() throws JspException {
MockPageContext pageContext = createPageContext();
MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
RequestContext requestContext = new JspAwareRequestContext(pageContext);
pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.getWebApplicationContext(request);
wac.registerSingleton("requestDataValueProcessor", DelegatingRequestDataValueProcessor.class);
extendRequest(request);
extendPageContext(pageContext);
RequestContext requestContext = new JspAwareRequestContext(pageContext);
pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
return pageContext;
}
@ -91,6 +99,13 @@ public abstract class AbstractHtmlElementTagTests extends AbstractTagTests {
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) {
// wrap errors in a Model

View File

@ -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;
}
}

View File

@ -16,10 +16,20 @@
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.tagext.Tag;
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
@ -49,13 +59,13 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
this.tag.setPageContext(getPageContext());
}
@Override
protected void extendRequest(MockHttpServletRequest request) {
request.setRequestURI(REQUEST_URI);
request.setQueryString(QUERY_STRING);
this.request = request;
}
public void testWriteForm() throws Exception {
String commandName = "myCommand";
String name = "formName";
@ -269,6 +279,26 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
this.tag.doFinally();
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) {
int inputStart = output.indexOf("<", 1);
@ -290,5 +320,5 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
private static void assertFormTagClosed(String output) {
assertTrue(output.endsWith("</form>"));
}
}