mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4292 Finishes refactoring Dispatcher to avoid passing ServletContext
as it is available via field reference
This commit is contained in:
@@ -21,17 +21,15 @@
|
||||
|
||||
package org.apache.struts2.components;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsException;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
@@ -43,15 +41,14 @@ import org.apache.struts2.views.annotations.StrutsTag;
|
||||
import org.apache.struts2.views.annotations.StrutsTagAttribute;
|
||||
import org.apache.struts2.views.jsp.TagUtils;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: javadoc -->
|
||||
@@ -188,7 +185,6 @@ public class ActionComponent extends ContextBean {
|
||||
Map newParams = createParametersForContext();
|
||||
|
||||
ActionContext ctx = new ActionContext(stack.getContext());
|
||||
ServletContext servletContext = (ServletContext) ctx.get(ServletActionContext.SERVLET_CONTEXT);
|
||||
PageContext pageContext = (PageContext) ctx.get(ServletActionContext.PAGE_CONTEXT);
|
||||
Map session = ctx.getSession();
|
||||
Map application = ctx.getApplication();
|
||||
@@ -199,8 +195,7 @@ public class ActionComponent extends ContextBean {
|
||||
session,
|
||||
application,
|
||||
req,
|
||||
res,
|
||||
servletContext);
|
||||
res);
|
||||
|
||||
ValueStack newStack = valueStackFactory.createValueStack(stack);
|
||||
extraContext.put(ActionContext.VALUE_STACK, newStack);
|
||||
|
||||
@@ -186,11 +186,14 @@ public class Dispatcher {
|
||||
dispatcherListeners.remove(listener);
|
||||
}
|
||||
|
||||
private ServletContext servletContext;
|
||||
private Map<String, String> initParams;
|
||||
|
||||
private ValueStackFactory valueStackFactory;
|
||||
|
||||
/**
|
||||
* Keeps current reference to external world and must be protected to support class inheritance
|
||||
*/
|
||||
protected ServletContext servletContext;
|
||||
protected Map<String, String> initParams;
|
||||
|
||||
/**
|
||||
* Create the Dispatcher instance for a given ServletContext and set of initialization parameters.
|
||||
*
|
||||
@@ -479,9 +482,7 @@ public class Dispatcher {
|
||||
l.dispatcherInitialized(this);
|
||||
}
|
||||
}
|
||||
//if (servletContext != null) {
|
||||
errorHandler.init(servletContext);
|
||||
//}
|
||||
errorHandler.init(servletContext);
|
||||
|
||||
} catch (Exception ex) {
|
||||
if (LOG.isErrorEnabled())
|
||||
@@ -494,6 +495,16 @@ public class Dispatcher {
|
||||
return new ConfigurationManager(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use version without ServletContext param
|
||||
*/
|
||||
@Deprecated
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
|
||||
ActionMapping mapping) throws ServletException {
|
||||
|
||||
serviceAction(request, response, mapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Action class for mapping and invoke the appropriate Action method, or go directly to the Result.
|
||||
* <p/>
|
||||
@@ -509,12 +520,13 @@ public class Dispatcher {
|
||||
* @param mapping the action mapping object
|
||||
* @throws ServletException when an unknown error occurs (not a 404, but typically something that
|
||||
* would end up as a 5xx by the servlet container)
|
||||
* @param context Our ServletContext object
|
||||
*
|
||||
* @since 2.3.17
|
||||
*/
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
|
||||
ActionMapping mapping) throws ServletException {
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
|
||||
throws ServletException {
|
||||
|
||||
Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
|
||||
Map<String, Object> extraContext = createContextMap(request, response, mapping);
|
||||
|
||||
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
|
||||
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
|
||||
@@ -586,17 +598,28 @@ public class Dispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use version without servletContext param
|
||||
*/
|
||||
@Deprecated
|
||||
public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
|
||||
ActionMapping mapping, ServletContext context) {
|
||||
|
||||
return createContextMap(request, response, mapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a context map containing all the wrapped request objects
|
||||
*
|
||||
* @param request The servlet request
|
||||
* @param response The servlet response
|
||||
* @param mapping The action mapping
|
||||
* @param context The servlet context
|
||||
* @return A map of context objects
|
||||
*
|
||||
* @since 2.3.17
|
||||
*/
|
||||
public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
|
||||
ActionMapping mapping, ServletContext context) {
|
||||
ActionMapping mapping) {
|
||||
|
||||
// request map wrapping the http request objects
|
||||
Map requestMap = new RequestMap(request);
|
||||
@@ -608,9 +631,9 @@ public class Dispatcher {
|
||||
Map session = new SessionMap(request);
|
||||
|
||||
// application map wrapping the ServletContext
|
||||
Map application = new ApplicationMap(context);
|
||||
Map application = new ApplicationMap(servletContext);
|
||||
|
||||
Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);
|
||||
Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response);
|
||||
|
||||
if (mapping != null) {
|
||||
extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
|
||||
@@ -618,6 +641,21 @@ public class Dispatcher {
|
||||
return extraContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use version without ServletContext param
|
||||
*/
|
||||
@Deprecated
|
||||
public HashMap<String,Object> createContextMap(Map requestMap,
|
||||
Map parameterMap,
|
||||
Map sessionMap,
|
||||
Map applicationMap,
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
ServletContext servletContext) {
|
||||
|
||||
return createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge all application and servlet attributes into a single <tt>HashMap</tt> to represent the entire
|
||||
* <tt>Action</tt> context.
|
||||
@@ -628,16 +666,16 @@ public class Dispatcher {
|
||||
* @param applicationMap a Map of all servlet context attributes.
|
||||
* @param request the HttpServletRequest object.
|
||||
* @param response the HttpServletResponse object.
|
||||
* @param servletContext the ServletContextmapping object.
|
||||
* @return a HashMap representing the <tt>Action</tt> context.
|
||||
*
|
||||
* @since 2.3.17
|
||||
*/
|
||||
public HashMap<String,Object> createContextMap(Map requestMap,
|
||||
Map parameterMap,
|
||||
Map sessionMap,
|
||||
Map applicationMap,
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
ServletContext servletContext) {
|
||||
HttpServletResponse response) {
|
||||
HashMap<String,Object> extraContext = new HashMap<String,Object>();
|
||||
extraContext.put(ActionContext.PARAMETERS, new HashMap(parameterMap));
|
||||
extraContext.put(ActionContext.SESSION, sessionMap);
|
||||
@@ -672,9 +710,8 @@ public class Dispatcher {
|
||||
* Return the path to save uploaded files to (this is configurable).
|
||||
*
|
||||
* @return the path to save uploaded files to
|
||||
* @param servletContext Our ServletContext
|
||||
*/
|
||||
private String getSaveDir(ServletContext servletContext) {
|
||||
private String getSaveDir() {
|
||||
String saveDir = multipartSaveDir.trim();
|
||||
|
||||
if (saveDir.equals("")) {
|
||||
@@ -762,6 +799,14 @@ public class Dispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use version without ServletContext param
|
||||
*/
|
||||
@Deprecated
|
||||
public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
|
||||
return wrapRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap and return the given request or return the original request object.
|
||||
* </p>
|
||||
@@ -771,12 +816,13 @@ public class Dispatcher {
|
||||
* flexible - look first to that object before overriding this method to handle multipart data.
|
||||
*
|
||||
* @param request the HttpServletRequest object.
|
||||
* @param servletContext Our ServletContext object
|
||||
* @return a wrapped request or original request.
|
||||
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper
|
||||
* @throws java.io.IOException on any error.
|
||||
*
|
||||
* @since 2.3.17
|
||||
*/
|
||||
public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
|
||||
public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException {
|
||||
// don't wrap more than once
|
||||
if (request instanceof StrutsRequestWrapper) {
|
||||
return request;
|
||||
@@ -786,7 +832,7 @@ public class Dispatcher {
|
||||
if (content_type != null && content_type.contains("multipart/form-data")) {
|
||||
MultiPartRequest mpr = getMultiPartRequest();
|
||||
LocaleProvider provider = getContainer().getInstance(LocaleProvider.class);
|
||||
request = new MultiPartRequestWrapper(mpr, request, getSaveDir(servletContext), provider);
|
||||
request = new MultiPartRequestWrapper(mpr, request, getSaveDir(), provider);
|
||||
} else {
|
||||
request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup);
|
||||
}
|
||||
|
||||
@@ -361,7 +361,7 @@ public class FilterDispatcher implements StrutsStatics, Filter {
|
||||
try {
|
||||
// Wrap request first, just in case it is multipart/form-data
|
||||
// parameters might not be accessible through before encoding (ww-1278)
|
||||
request = dispatcher.wrapRequest(request, getServletContext());
|
||||
request = dispatcher.wrapRequest(request);
|
||||
} catch (IOException e) {
|
||||
String message = "Could not wrap servlet request with MultipartRequestWrapper!";
|
||||
log.error(message, e);
|
||||
@@ -431,7 +431,7 @@ public class FilterDispatcher implements StrutsStatics, Filter {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatcher.serviceAction(request, response, servletContext, mapping);
|
||||
dispatcher.serviceAction(request, response, mapping);
|
||||
|
||||
} finally {
|
||||
dispatcher.cleanUpRequest(request);
|
||||
|
||||
@@ -35,12 +35,16 @@ import java.io.IOException;
|
||||
* Contains execution operations for filters
|
||||
*/
|
||||
public class ExecuteOperations {
|
||||
private ServletContext servletContext;
|
||||
|
||||
private Dispatcher dispatcher;
|
||||
|
||||
@Deprecated
|
||||
public ExecuteOperations(ServletContext servletContext, Dispatcher dispatcher) {
|
||||
this.dispatcher = dispatcher;
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
public ExecuteOperations(Dispatcher dispatcher) {
|
||||
this.dispatcher = dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,6 +78,6 @@ public class ExecuteOperations {
|
||||
* @throws ServletException
|
||||
*/
|
||||
public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
|
||||
dispatcher.serviceAction(request, response, servletContext, mapping);
|
||||
dispatcher.serviceAction(request, response, mapping);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,15 +47,18 @@ public class PrepareOperations {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PrepareOperations.class);
|
||||
|
||||
private ServletContext servletContext;
|
||||
private Dispatcher dispatcher;
|
||||
private static final String STRUTS_ACTION_MAPPING_KEY = "struts.actionMapping";
|
||||
public static final String CLEANUP_RECURSION_COUNTER = "__cleanup_recursion_counter";
|
||||
private Logger log = LoggerFactory.getLogger(PrepareOperations.class);
|
||||
|
||||
@Deprecated
|
||||
public PrepareOperations(ServletContext servletContext, Dispatcher dispatcher) {
|
||||
this.dispatcher = dispatcher;
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
public PrepareOperations(Dispatcher dispatcher) {
|
||||
this.dispatcher = dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,7 +78,7 @@ public class PrepareOperations {
|
||||
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
|
||||
} else {
|
||||
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
|
||||
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
|
||||
stack.getContext().putAll(dispatcher.createContextMap(request, response, null));
|
||||
ctx = new ActionContext(stack.getContext());
|
||||
}
|
||||
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
|
||||
@@ -131,7 +134,7 @@ public class PrepareOperations {
|
||||
try {
|
||||
// Wrap request first, just in case it is multipart/form-data
|
||||
// parameters might not be accessible through before encoding (ww-1278)
|
||||
request = dispatcher.wrapRequest(request, servletContext);
|
||||
request = dispatcher.wrapRequest(request);
|
||||
} catch (IOException e) {
|
||||
throw new ServletException("Could not wrap servlet request with MultipartRequestWrapper!", e);
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ public class StrutsExecuteFilter implements StrutsStatics, Filter {
|
||||
Dispatcher dispatcher = init.findDispatcherOnThread();
|
||||
init.initStaticContentLoader(new FilterHostConfig(filterConfig), dispatcher);
|
||||
|
||||
prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
|
||||
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
|
||||
prepare = new PrepareOperations(dispatcher);
|
||||
execute = new ExecuteOperations(dispatcher);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -57,8 +57,8 @@ public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
|
||||
dispatcher = init.initDispatcher(config);
|
||||
init.initStaticContentLoader(config, dispatcher);
|
||||
|
||||
prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
|
||||
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
|
||||
prepare = new PrepareOperations(dispatcher);
|
||||
execute = new ExecuteOperations(dispatcher);
|
||||
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
|
||||
|
||||
postInit(dispatcher, filterConfig);
|
||||
|
||||
@@ -55,7 +55,7 @@ public class StrutsPrepareFilter implements StrutsStatics, Filter {
|
||||
init.initLogging(config);
|
||||
dispatcher = init.initDispatcher(config);
|
||||
|
||||
prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
|
||||
prepare = new PrepareOperations(dispatcher);
|
||||
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
|
||||
|
||||
postInit(dispatcher, filterConfig);
|
||||
|
||||
@@ -46,7 +46,7 @@ public class StrutsListener implements ServletContextListener {
|
||||
dispatcher = init.initDispatcher(config);
|
||||
init.initStaticContentLoader(config, dispatcher);
|
||||
|
||||
prepare = new PrepareOperations(config.getServletContext(), dispatcher);
|
||||
prepare = new PrepareOperations(dispatcher);
|
||||
sce.getServletContext().setAttribute(StrutsStatics.SERVLET_DISPATCHER, dispatcher);
|
||||
} finally {
|
||||
if (dispatcher != null) {
|
||||
|
||||
@@ -54,8 +54,8 @@ public class StrutsServlet extends HttpServlet {
|
||||
dispatcher = init.initDispatcher(config);
|
||||
init.initStaticContentLoader(config, dispatcher);
|
||||
|
||||
prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
|
||||
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
|
||||
prepare = new PrepareOperations(dispatcher);
|
||||
execute = new ExecuteOperations(dispatcher);
|
||||
} finally {
|
||||
if (dispatcher != null) {
|
||||
dispatcher.cleanUpAfterInit();
|
||||
|
||||
@@ -21,12 +21,11 @@
|
||||
|
||||
package org.apache.struts2.views.jsp;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import org.apache.struts2.RequestUtils;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.dispatcher.ApplicationMap;
|
||||
@@ -37,11 +36,10 @@ import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.apache.struts2.util.AttributeMap;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
@@ -67,8 +65,7 @@ public class TagUtils {
|
||||
new SessionMap(req),
|
||||
new ApplicationMap(pageContext.getServletContext()),
|
||||
req,
|
||||
res,
|
||||
pageContext.getServletContext());
|
||||
res);
|
||||
extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
|
||||
stack.getContext().putAll(extraContext);
|
||||
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
|
||||
|
||||
@@ -122,7 +122,7 @@ public class FilterDispatcherTest extends StrutsInternalTestCase {
|
||||
return request;
|
||||
}
|
||||
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
|
||||
serviceRequest = true;
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ public class FilterDispatcherTest extends StrutsInternalTestCase {
|
||||
return request;
|
||||
}
|
||||
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
|
||||
serviceRequest = true;
|
||||
// if we set the chracter encoding AFTER we do wrap request, we will get
|
||||
// a failing test
|
||||
|
||||
@@ -313,10 +313,10 @@ public class FilterTest extends StrutsInternalTestCase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
|
||||
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
|
||||
service = true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
cleanUp = true;
|
||||
|
||||
@@ -43,12 +43,14 @@ import javax.servlet.ServletContext;
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.apache.struts2.views.jsp.AbstractUITagTest.normalize;
|
||||
|
||||
/**
|
||||
* Test case for FreeMarkerResult.
|
||||
*
|
||||
*/
|
||||
public class FreeMarkerResultTest extends StrutsInternalTestCase {
|
||||
|
||||
@@ -62,54 +64,6 @@ public class FreeMarkerResultTest extends StrutsInternalTestCase {
|
||||
private FreemarkerManager mgr;
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
public void testActionThatThrowsExceptionTag() throws Exception {
|
||||
//get fm config to use it in mock servlet context
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("callActionFreeMarker2.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/callActionFreeMarker.ftl")).andReturn(file.getAbsolutePath());
|
||||
file = new File(FreeMarkerResultTest.class.getResource("nested.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/nested.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
|
||||
request.setRequestURI("/tutorial/test2.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, servletContext, mapping);
|
||||
assertEquals("beforenestedafter", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testActionThatSucceedsTag() throws Exception {
|
||||
//get fm config to use it in mock servlet context
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("callActionFreeMarker2.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/callActionFreeMarker2.ftl")).andReturn(file.getAbsolutePath());
|
||||
file = new File(FreeMarkerResultTest.class.getResource("nested.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/nested.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
|
||||
request.setRequestURI("/tutorial/test5.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, servletContext, mapping);
|
||||
assertEquals("beforenestedafter", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testWriteIfCompleted() throws Exception {
|
||||
FreemarkerResult result = new FreemarkerResult();
|
||||
result.setLocation("someFreeMarkerFile.ftl");
|
||||
@@ -138,21 +92,21 @@ public class FreeMarkerResultTest extends StrutsInternalTestCase {
|
||||
}
|
||||
|
||||
public void testContentTypeIsNotOverwritten() throws Exception {
|
||||
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
|
||||
"nested.ftl").toURI()).toURL().getFile());
|
||||
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
|
||||
"nested.ftl").toURI()).toURL().getFile());
|
||||
|
||||
FreemarkerResult result = new FreemarkerResult();
|
||||
result.setLocation("nested.ftl");
|
||||
result.setFreemarkerManager(mgr);
|
||||
|
||||
response.setContentType("contenttype");
|
||||
|
||||
response.setContentType("contenttype");
|
||||
result.execute(invocation);
|
||||
assertEquals("contenttype", response.getContentType());
|
||||
}
|
||||
|
||||
public void testDefaultContentType() throws Exception {
|
||||
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
|
||||
"nested.ftl").toURI()).toURL().getFile());
|
||||
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
|
||||
"nested.ftl").toURI()).toURL().getFile());
|
||||
|
||||
FreemarkerResult result = new FreemarkerResult();
|
||||
result.setLocation("nested.ftl");
|
||||
@@ -164,8 +118,8 @@ public class FreeMarkerResultTest extends StrutsInternalTestCase {
|
||||
}
|
||||
|
||||
public void testContentTypeFromTemplate() throws Exception {
|
||||
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
|
||||
"something.ftl").toURI()).toURL().getFile());
|
||||
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
|
||||
"something.ftl").toURI()).toURL().getFile());
|
||||
|
||||
FreemarkerResult result = new FreemarkerResult();
|
||||
result.setLocation("something.ftl");
|
||||
@@ -176,140 +130,6 @@ public class FreeMarkerResultTest extends StrutsInternalTestCase {
|
||||
assertEquals("text/xml", response.getContentType());
|
||||
}
|
||||
|
||||
public void testDynamicAttributesSupport() throws Exception {
|
||||
//get fm config to use it in mock servlet context
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("dynaAttributes.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/dynaAttributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/text.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/text.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/css.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/scripting-events.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/common-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/dynamic-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
|
||||
request.setRequestURI("/tutorial/test6.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, servletContext, mapping);
|
||||
|
||||
// TODO lukaszlenart: remove expectedJDK15 and if() after switching to Java 1.6
|
||||
String expectedJDK15 =
|
||||
"<input type=\"text\" name=\"test\" value=\"\" id=\"test\" foo=\"bar\" placeholder=\"input\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" foo=\"bar\" placeholder=\"input\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" break=\"true\"/>";
|
||||
String expectedJDK16 =
|
||||
"<input type=\"text\" name=\"test\" value=\"\" id=\"test\" placeholder=\"input\" foo=\"bar\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" placeholder=\"input\" foo=\"bar\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" break=\"true\"/>"
|
||||
+ "<input type=\"text\" name=\"required\" value=\"\" id=\"required\" required=\"true\"/>";
|
||||
|
||||
String result = stringWriter.toString();
|
||||
|
||||
if (result.contains("foo=\"bar\" placeholder=\"input\"")) {
|
||||
assertEquals(expectedJDK15, result);
|
||||
} else {
|
||||
assertEquals(expectedJDK16, result);
|
||||
}
|
||||
}
|
||||
|
||||
public void testManualListInTemplate() throws Exception {
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("manual-list.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/manual-list.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/radiomap.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/radiomap.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/css.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/scripting-events.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/common-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/dynamic-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
|
||||
request.setRequestURI("/tutorial/test7.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, servletContext, mapping);
|
||||
String expected = "<input type=\"radio\" name=\"client\" id=\"client_foo\" value=\"foo\"/><label for=\"client_foo\">foo</label>\n"
|
||||
+ "<input type=\"radio\" name=\"client\" id=\"client_bar\" value=\"bar\"/><label for=\"client_bar\">bar</label>\n"
|
||||
+ "\n"
|
||||
+ "<input type=\"radio\" name=\"car\" id=\"carford\" value=\"ford\"/><label for=\"carford\">Ford Motor Co</label>\n"
|
||||
+ "<input type=\"radio\" name=\"car\" id=\"cartoyota\" value=\"toyota\"/><label for=\"cartoyota\">Toyota</label>\n";
|
||||
assertEquals(normalize(expected), normalize(stringWriter.toString()));
|
||||
}
|
||||
|
||||
public void testDynamicAttributesInTheme() throws Exception {
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("customTextField.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/customTextField.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/test/text.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/test/text.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
request.setRequestURI("/tutorial/test8.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, servletContext, mapping);
|
||||
String expected = "<input type=\"text\"autofocus=\"autofocus\"/>";
|
||||
assertEquals(expected, stringWriter.toString());
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mgr = new FreemarkerManager();
|
||||
@@ -345,7 +165,7 @@ public class FreeMarkerResultTest extends StrutsInternalTestCase {
|
||||
invocation.setStack(stack);
|
||||
invocation.setInvocationContext(context);
|
||||
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
|
||||
"someFreeMarkerFile.ftl").toURI()).toURL().getFile());
|
||||
"someFreeMarkerFile.ftl").toURI()).toURL().getFile());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
|
||||
+262
@@ -0,0 +1,262 @@
|
||||
package org.apache.struts2.views.freemarker;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.mock.MockActionInvocation;
|
||||
import com.opensymphony.xwork2.util.ClassLoaderUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
|
||||
import org.easymock.EasyMock;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.apache.struts2.views.jsp.AbstractUITagTest.normalize;
|
||||
|
||||
public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
|
||||
|
||||
ValueStack stack;
|
||||
MockActionInvocation invocation;
|
||||
ActionContext context;
|
||||
StrutsMockHttpServletResponse response;
|
||||
PrintWriter writer;
|
||||
StringWriter stringWriter;
|
||||
ServletContext servletContext;
|
||||
FreemarkerManager mgr;
|
||||
MockHttpServletRequest request;
|
||||
|
||||
public void testActionThatThrowsExceptionTag() throws Exception {
|
||||
//get fm config to use it in mock servlet context
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("callActionFreeMarker2.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/callActionFreeMarker.ftl")).andReturn(file.getAbsolutePath());
|
||||
file = new File(FreeMarkerResultTest.class.getResource("nested.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/nested.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
init();
|
||||
|
||||
request.setRequestURI("/tutorial/test2.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, mapping);
|
||||
assertEquals("beforenestedafter", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testActionThatSucceedsTag() throws Exception {
|
||||
//get fm config to use it in mock servlet context
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("callActionFreeMarker2.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/callActionFreeMarker2.ftl")).andReturn(file.getAbsolutePath());
|
||||
file = new File(FreeMarkerResultTest.class.getResource("nested.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/nested.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
init();
|
||||
|
||||
request.setRequestURI("/tutorial/test5.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, mapping);
|
||||
assertEquals("beforenestedafter", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testDynamicAttributesSupport() throws Exception {
|
||||
//get fm config to use it in mock servlet context
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("dynaAttributes.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/dynaAttributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/text.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/text.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/css.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/scripting-events.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/common-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/dynamic-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
init();
|
||||
|
||||
request.setRequestURI("/tutorial/test6.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, mapping);
|
||||
|
||||
// TODO lukaszlenart: remove expectedJDK15 and if() after switching to Java 1.6
|
||||
String expectedJDK15 =
|
||||
"<input type=\"text\" name=\"test\" value=\"\" id=\"test\" foo=\"bar\" placeholder=\"input\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" foo=\"bar\" placeholder=\"input\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" break=\"true\"/>";
|
||||
String expectedJDK16 =
|
||||
"<input type=\"text\" name=\"test\" value=\"\" id=\"test\" placeholder=\"input\" foo=\"bar\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" placeholder=\"input\" foo=\"bar\"/>"
|
||||
+ "<input type=\"text\" name=\"test\" value=\"\" id=\"test\" break=\"true\"/>"
|
||||
+ "<input type=\"text\" name=\"required\" value=\"\" id=\"required\" required=\"true\"/>";
|
||||
|
||||
String result = stringWriter.toString();
|
||||
|
||||
if (result.contains("foo=\"bar\" placeholder=\"input\"")) {
|
||||
assertEquals(expectedJDK15, result);
|
||||
} else {
|
||||
assertEquals(expectedJDK16, result);
|
||||
}
|
||||
}
|
||||
|
||||
public void testManualListInTemplate() throws Exception {
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("manual-list.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/manual-list.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/radiomap.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/radiomap.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/css.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/css.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/scripting-events.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/scripting-events.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/common-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/common-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/simple/dynamic-attributes.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/~~~simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
init();
|
||||
|
||||
request.setRequestURI("/tutorial/test7.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, mapping);
|
||||
String expected = "<input type=\"radio\" name=\"client\" id=\"client_foo\" value=\"foo\"/><label for=\"client_foo\">foo</label>\n"
|
||||
+ "<input type=\"radio\" name=\"client\" id=\"client_bar\" value=\"bar\"/><label for=\"client_bar\">bar</label>\n"
|
||||
+ "\n"
|
||||
+ "<input type=\"radio\" name=\"car\" id=\"carford\" value=\"ford\"/><label for=\"carford\">Ford Motor Co</label>\n"
|
||||
+ "<input type=\"radio\" name=\"car\" id=\"cartoyota\" value=\"toyota\"/><label for=\"cartoyota\">Toyota</label>\n";
|
||||
assertEquals(normalize(expected), normalize(stringWriter.toString()));
|
||||
}
|
||||
|
||||
public void testDynamicAttributesInTheme() throws Exception {
|
||||
FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
|
||||
Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
|
||||
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
servletContext = EasyMock.createNiceMock(ServletContext.class);
|
||||
|
||||
File file = new File(FreeMarkerResultTest.class.getResource("customTextField.ftl").toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/customTextField.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
file = new File(ClassLoaderUtil.getResource("template/test/text.ftl", getClass()).toURI());
|
||||
EasyMock.expect(servletContext.getRealPath("/template/test/text.ftl")).andReturn(file.getAbsolutePath());
|
||||
|
||||
EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
|
||||
EasyMock.replay(servletContext);
|
||||
|
||||
freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
|
||||
init();
|
||||
|
||||
request.setRequestURI("/tutorial/test8.action");
|
||||
ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
dispatcher.serviceAction(request, response, mapping);
|
||||
String expected = "<input type=\"text\"autofocus=\"autofocus\"/>";
|
||||
assertEquals(expected, stringWriter.toString());
|
||||
}
|
||||
|
||||
private void init() throws MalformedURLException, URISyntaxException {
|
||||
mgr = new FreemarkerManager();
|
||||
mgr.setEncoding("UTF-8");
|
||||
|
||||
DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
|
||||
container.inject(factory);
|
||||
mgr.setFileManagerFactory(factory);
|
||||
|
||||
FreemarkerThemeTemplateLoader themeLoader = new FreemarkerThemeTemplateLoader();
|
||||
container.inject(themeLoader);
|
||||
mgr.setThemeTemplateLoader(themeLoader);
|
||||
|
||||
stringWriter = new StringWriter();
|
||||
writer = new PrintWriter(stringWriter);
|
||||
response = new StrutsMockHttpServletResponse();
|
||||
response.setWriter(writer);
|
||||
request = new MockHttpServletRequest();
|
||||
stack = ActionContext.getContext().getValueStack();
|
||||
|
||||
context = new ActionContext(stack.getContext());
|
||||
context.put(StrutsStatics.HTTP_RESPONSE, response);
|
||||
context.put(StrutsStatics.HTTP_REQUEST, request);
|
||||
context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
|
||||
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
ServletActionContext.setRequest(request);
|
||||
ServletActionContext.setResponse(response);
|
||||
servletContext.setAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY, null);
|
||||
|
||||
invocation = new MockActionInvocation();
|
||||
invocation.setStack(stack);
|
||||
invocation.setInvocationContext(context);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -120,8 +120,7 @@ public abstract class AbstractTagTest extends StrutsInternalTestCase {
|
||||
session,
|
||||
new ApplicationMap(pageContext.getServletContext()),
|
||||
request,
|
||||
response,
|
||||
pageContext.getServletContext());
|
||||
response);
|
||||
// let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
|
||||
// ... but generally we want to just use no locale (let it stay system default)
|
||||
extraContext.remove(ActionContext.LOCALE);
|
||||
|
||||
@@ -555,8 +555,7 @@ public class URLTagTest extends AbstractUITagTest {
|
||||
session,
|
||||
new ApplicationMap(pageContext.getServletContext()),
|
||||
request,
|
||||
response,
|
||||
pageContext.getServletContext());
|
||||
response);
|
||||
// let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
|
||||
// ... but generally we want to just use no locale (let it stay system default)
|
||||
extraContext.remove(ActionContext.LOCALE);
|
||||
|
||||
+1
-2
@@ -119,8 +119,7 @@ public abstract class AbstractTagTest extends StrutsTestCase {
|
||||
session,
|
||||
new ApplicationMap(pageContext.getServletContext()),
|
||||
request,
|
||||
response,
|
||||
pageContext.getServletContext());
|
||||
response);
|
||||
// let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
|
||||
// ... but generally we want to just use no locale (let it stay system default)
|
||||
extraContext.remove(ActionContext.LOCALE);
|
||||
|
||||
@@ -32,22 +32,16 @@ import org.apache.struts2.dispatcher.ApplicationMap;
|
||||
import org.apache.struts2.dispatcher.Dispatcher;
|
||||
import org.apache.struts2.dispatcher.RequestMap;
|
||||
import org.apache.struts2.dispatcher.SessionMap;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
|
||||
import uk.ltd.getahead.dwr.WebContextFactory;
|
||||
|
||||
import com.opensymphony.xwork2.Action;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.DefaultActionInvocation;
|
||||
import com.opensymphony.xwork2.ValidationAware;
|
||||
import com.opensymphony.xwork2.ValidationAwareSupport;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.config.entities.PackageConfig;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
|
||||
@@ -69,8 +63,9 @@ import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
* </pre>
|
||||
*/
|
||||
public class DWRValidator {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DWRValidator.class);
|
||||
|
||||
|
||||
public ValidationAwareSupport doPost(String namespace, String actionName, Map params) throws Exception {
|
||||
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
|
||||
ServletContext servletContext = WebContextFactory.get().getServletContext();
|
||||
@@ -91,8 +86,7 @@ public class DWRValidator {
|
||||
session,
|
||||
application,
|
||||
req,
|
||||
res,
|
||||
servletContext);
|
||||
res);
|
||||
|
||||
try {
|
||||
ActionProxyFactory actionProxyFactory = du.getContainer().getInstance(ActionProxyFactory.class);
|
||||
|
||||
@@ -137,7 +137,7 @@ public abstract class StrutsJUnit4TestCase<T> extends XWorkJUnit4TestCase {
|
||||
ActionMapping mapping = getActionMapping(request);
|
||||
|
||||
assertNotNull(mapping);
|
||||
Dispatcher.getInstance().serviceAction(request, response, servletContext, mapping);
|
||||
Dispatcher.getInstance().serviceAction(request, response, mapping);
|
||||
|
||||
if (response.getStatus() != HttpServletResponse.SC_OK)
|
||||
throw new ServletException("Error code [" + response.getStatus() + "], Error: ["
|
||||
|
||||
@@ -115,7 +115,7 @@ public abstract class StrutsTestCase extends XWorkTestCase {
|
||||
ActionMapping mapping = getActionMapping(request);
|
||||
|
||||
assertNotNull(mapping);
|
||||
Dispatcher.getInstance().serviceAction(request, response, servletContext, mapping);
|
||||
Dispatcher.getInstance().serviceAction(request, response, mapping);
|
||||
|
||||
if (response.getStatus() != HttpServletResponse.SC_OK) {
|
||||
throw new ServletException("Error code [" + response.getStatus() + "], Error: [" + response.getErrorMessage() + "]");
|
||||
|
||||
+4
-3
@@ -184,6 +184,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
|
||||
private Dispatcher dispatcherUtils;
|
||||
private ActionMapper actionMapper;
|
||||
private Container container;
|
||||
private ServletContext servletContext;
|
||||
|
||||
/**
|
||||
* Initialize the portlet with the init parameters from <tt>portlet.xml</tt>
|
||||
@@ -201,7 +202,8 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
|
||||
params.put(name, value);
|
||||
}
|
||||
|
||||
dispatcherUtils = new Dispatcher(new PortletServletContext(cfg.getPortletContext()), params);
|
||||
servletContext = new PortletServletContext(cfg.getPortletContext());
|
||||
dispatcherUtils = new Dispatcher(servletContext, params);
|
||||
dispatcherUtils.init();
|
||||
|
||||
// For testability
|
||||
@@ -428,11 +430,10 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
|
||||
String actionName = null;
|
||||
String namespace;
|
||||
try {
|
||||
ServletContext servletContext = new PortletServletContext(getPortletContext());
|
||||
HttpServletRequest servletRequest = new PortletServletRequest(request, getPortletContext());
|
||||
HttpServletResponse servletResponse = createPortletServletResponse(response);
|
||||
if (phase.isAction()) {
|
||||
servletRequest = dispatcherUtils.wrapRequest(servletRequest, servletContext);
|
||||
servletRequest = dispatcherUtils.wrapRequest(servletRequest);
|
||||
if (servletRequest instanceof MultiPartRequestWrapper) {
|
||||
// Multipart request. Request parameters are encoded in the multipart data,
|
||||
// so we need to manually add them to the parameter map.
|
||||
|
||||
+1
-1
@@ -97,7 +97,7 @@ public abstract class OldDecorator2NewStrutsDecorator extends BaseWebAppDecorato
|
||||
if (ctx == null) {
|
||||
// ok, one isn't associated with the request, so let's create one using the current Dispatcher
|
||||
ValueStack vs = Dispatcher.getInstance().getContainer().getInstance(ValueStackFactory.class).createValueStack();
|
||||
vs.getContext().putAll(Dispatcher.getInstance().createContextMap(request, response, null, servletContext));
|
||||
vs.getContext().putAll(Dispatcher.getInstance().createContextMap(request, response, null));
|
||||
ctx = new ActionContext(vs.getContext());
|
||||
if (ctx.getActionInvocation() == null) {
|
||||
// put in a dummy ActionSupport so basic functionality still works
|
||||
|
||||
Reference in New Issue
Block a user