Cleanup and testing for SWF-1092
This commit is contained in:
@@ -16,8 +16,12 @@
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.core/2.5.6.A/org.springframework.core-2.5.6.A.jar" sourcepath="IVY_CACHE/org.springframework/org.springframework.core/2.5.6.A/org.springframework.core-sources-2.5.6.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.beans/2.5.6.A/org.springframework.beans-2.5.6.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.beans/2.5.6.A/org.springframework.beans-sources-2.5.6.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.context/2.5.6.A/org.springframework.context-2.5.6.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.context/2.5.6.A/org.springframework.context-sources-2.5.6.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.test/2.5.6.A/org.springframework.test-2.5.6.A.jar" sourcepath="IVY_CACHE/org.springframework/org.springframework.test/2.5.6.A/org.springframework.test-soruces-2.5.6.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.test/2.5.6.A/org.springframework.test-2.5.6.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.test/2.5.6.A/org.springframework.test-sources-2.5.6.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.web/2.5.6.A/org.springframework.web-2.5.6.A.jar" sourcepath="IVY_CACHE/org.springframework/org.springframework.web/2.5.6.A/org.springframework.web-sources-2.5.6.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.web.servlet/2.5.6.A/org.springframework.web.servlet-2.5.6.A.jar" sourcepath="IVY_CACHE/org.springframework/org.springframework.web.servlet/2.5.6.A/org.springframework.web.servlet-sources-2.5.6.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.servlet/com.springsource.javax.servlet.jsp.jstl/1.2.0/com.springsource.javax.servlet.jsp.jstl-1.2.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.digester/1.8.0/com.springsource.org.apache.commons.digester-1.8.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.collections/3.2.0/com.springsource.org.apache.commons.collections-3.2.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.beanutils/1.7.0/com.springsource.org.apache.commons.beanutils-1.7.0.jar" sourcepath="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.beanutils/1.7.0/com.springsource.org.apache.commons.beanutils-sources-1.7.0.jar"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->runtime" />
|
||||
<dependency org="org.junit" name="com.springsource.junit" rev="3.8.2" conf="test->runtime" />
|
||||
<dependency org="org.springframework" name="org.springframework.test" rev="2.5.6.A" conf="test->runtime" />
|
||||
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.digester" rev="1.8.0" conf="test->runtime"/>
|
||||
<dependency org="javax.servlet" name="com.springsource.javax.servlet.jsp.jstl" rev="1.2.0" conf="test->runtime"/>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.apache.tiles.Definition;
|
||||
import org.apache.tiles.Attribute.AttributeType;
|
||||
import org.apache.tiles.access.TilesAccess;
|
||||
import org.apache.tiles.context.TilesRequestContext;
|
||||
import org.apache.tiles.definition.DefinitionsFactoryException;
|
||||
import org.apache.tiles.impl.BasicTilesContainer;
|
||||
import org.springframework.js.ajax.AjaxHandler;
|
||||
import org.springframework.js.ajax.SpringJavascriptAjaxHandler;
|
||||
@@ -73,8 +74,10 @@ public class AjaxTilesView extends TilesView {
|
||||
String[] attrNames = getRenderFragments(model, request, response);
|
||||
if (attrNames.length == 0) {
|
||||
logger.warn("An Ajax request was detected, but no fragments were specified to be re-rendered. "
|
||||
+ "Falling back to full page render.");
|
||||
+ "Falling back to full page render. This can cause unpredictable results when processing "
|
||||
+ "the ajax response on the client.");
|
||||
super.renderMergedOutputModel(model, request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
BasicTilesContainer container = (BasicTilesContainer) TilesAccess.getContainer(servletContext);
|
||||
@@ -127,13 +130,17 @@ public class AjaxTilesView extends TilesView {
|
||||
while (i.hasNext()) {
|
||||
Object key = i.next();
|
||||
Attribute attr = (Attribute) compositeDefinition.getAttributes().get(key);
|
||||
if (AttributeType.DEFINITION.equals(attr.getType()) || AttributeType.TEMPLATE.equals(attr.getType())
|
||||
|| attr.getType() == null) {
|
||||
Definition nestedDefinition = container.getDefinitionsFactory().getDefinition(
|
||||
attr.getValue().toString(), requestContext);
|
||||
AttributeType attrType = attr.getType() != null ? attr.getType() : detectType(container,
|
||||
requestContext, attr);
|
||||
if (AttributeType.DEFINITION.equals(attrType) || AttributeType.TEMPLATE.equals(attrType)) {
|
||||
resultMap.put(key, attr);
|
||||
if (nestedDefinition != null && nestedDefinition != compositeDefinition) {
|
||||
flattenAttributeMap(container, requestContext, resultMap, nestedDefinition, request, response);
|
||||
if (AttributeType.DEFINITION.equals(attrType)) {
|
||||
Definition nestedDefinition = container.getDefinitionsFactory().getDefinition(
|
||||
attr.getValue().toString(), requestContext);
|
||||
if (nestedDefinition != null && nestedDefinition != compositeDefinition) {
|
||||
flattenAttributeMap(container, requestContext, resultMap, nestedDefinition, request,
|
||||
response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,4 +155,18 @@ public class AjaxTilesView extends TilesView {
|
||||
resultMap.put(key, attr);
|
||||
}
|
||||
}
|
||||
|
||||
private AttributeType detectType(BasicTilesContainer container, TilesRequestContext requestContext, Attribute attr)
|
||||
throws DefinitionsFactoryException {
|
||||
if (attr.getValue() instanceof String) {
|
||||
if (container.getDefinitionsFactory().getDefinition(attr.getValue().toString(), requestContext) != null) {
|
||||
return AttributeType.DEFINITION;
|
||||
} else if (attr.getValue().toString().startsWith("/")) {
|
||||
return AttributeType.TEMPLATE;
|
||||
} else {
|
||||
return AttributeType.STRING;
|
||||
}
|
||||
}
|
||||
return AttributeType.OBJECT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package org.springframework.js.ajax.tiles2;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.tiles.Attribute;
|
||||
import org.apache.tiles.Definition;
|
||||
import org.apache.tiles.access.TilesAccess;
|
||||
import org.apache.tiles.context.TilesRequestContext;
|
||||
import org.apache.tiles.impl.BasicTilesContainer;
|
||||
import org.springframework.js.ajax.SpringJavascriptAjaxHandler;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
|
||||
|
||||
public class AjaxTilesViewTests extends TestCase {
|
||||
|
||||
private AjaxTilesView ajaxTilesView;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletResponse response;
|
||||
private MockServletContext servletContext;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
|
||||
servletContext = new MockServletContext("/org/springframework/js/ajax/tiles2/");
|
||||
request = new MockHttpServletRequest(servletContext);
|
||||
response = new MockHttpServletResponse();
|
||||
|
||||
TilesConfigurer tc = new TilesConfigurer();
|
||||
tc.setDefinitions(new String[] { "tiles-definitions.xml" });
|
||||
tc.setValidateDefinitions(true);
|
||||
tc.setServletContext(servletContext);
|
||||
tc.afterPropertiesSet();
|
||||
|
||||
ajaxTilesView = new AjaxTilesView();
|
||||
}
|
||||
|
||||
private void setupStaticWebApplicationContext() {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(servletContext);
|
||||
wac.refresh();
|
||||
request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
ajaxTilesView.setApplicationContext(wac);
|
||||
}
|
||||
|
||||
public void testFullPageRendering() throws Exception {
|
||||
setupStaticWebApplicationContext();
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
assertEquals("/WEB-INF/layout.jsp", response.getForwardedUrl());
|
||||
}
|
||||
|
||||
public void testAjaxRequestNoFragments() throws Exception {
|
||||
setupStaticWebApplicationContext();
|
||||
request.addHeader("Accept", SpringJavascriptAjaxHandler.AJAX_ACCEPT_CONTENT_TYPE);
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
assertEquals("/WEB-INF/layout.jsp", response.getForwardedUrl());
|
||||
}
|
||||
|
||||
public void testRenderFragment_Template() throws Exception {
|
||||
setupStaticWebApplicationContext();
|
||||
request.addHeader("Accept", SpringJavascriptAjaxHandler.AJAX_ACCEPT_CONTENT_TYPE);
|
||||
request.addParameter("fragments", "searchResults");
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
assertEquals("/WEB-INF/searchResults.jsp", response.getIncludedUrl());
|
||||
}
|
||||
|
||||
public void testRenderFragment_Definition() throws Exception {
|
||||
setupStaticWebApplicationContext();
|
||||
request.addHeader("Accept", SpringJavascriptAjaxHandler.AJAX_ACCEPT_CONTENT_TYPE);
|
||||
request.addParameter("fragments", "body");
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
assertEquals("/WEB-INF/search.jsp", response.getIncludedUrl());
|
||||
}
|
||||
|
||||
public void testFlattenAttributeMap() throws Exception {
|
||||
BasicTilesContainer container = (BasicTilesContainer) TilesAccess.getContainer(servletContext);
|
||||
TilesRequestContext tilesRequestContext = container.getContextFactory().createRequestContext(
|
||||
container.getApplicationContext(), new Object[] { request, response });
|
||||
Definition compositeDefinition = container.getDefinitionsFactory().getDefinition("search", tilesRequestContext);
|
||||
Map resultMap = new HashMap();
|
||||
ajaxTilesView.flattenAttributeMap(container, tilesRequestContext, resultMap, compositeDefinition, request,
|
||||
response);
|
||||
assertNotNull(resultMap.get("body"));
|
||||
assertNotNull(resultMap.get("searchForm"));
|
||||
assertEquals("/WEB-INF/searchForm.jsp", ((Attribute) resultMap.get("searchForm")).getValue());
|
||||
assertNotNull(resultMap.get("searchResults"));
|
||||
}
|
||||
|
||||
public void testGetRenderFragments() throws Exception {
|
||||
Map model = new HashMap();
|
||||
request.setParameter("fragments", "f1,f2, f3");
|
||||
String[] fragments = ajaxTilesView.getRenderFragments(model, request, response);
|
||||
assertEquals("f1", fragments[0]);
|
||||
assertEquals("f2", fragments[1]);
|
||||
assertEquals("f3", fragments[2]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE tiles-definitions PUBLIC
|
||||
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
|
||||
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
|
||||
|
||||
<tiles-definitions>
|
||||
|
||||
<definition name="layout" template="/WEB-INF/layout.jsp"/>
|
||||
|
||||
<definition name="search" extends="layout">
|
||||
<put-attribute name="searchForm" value="Substituted Value" type="string"/>
|
||||
<put-attribute name="body" value="search.body" />
|
||||
</definition>
|
||||
|
||||
<definition name="search.body" template="/WEB-INF/search.jsp">
|
||||
<put-attribute name="searchForm" value="/WEB-INF/searchForm.jsp" />
|
||||
<put-attribute name="searchResults" value="/WEB-INF/searchResults.jsp" />
|
||||
</definition>
|
||||
|
||||
</tiles-definitions>
|
||||
Reference in New Issue
Block a user