use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class URLTagTest method testEmptyActionCustomMapper.
public void testEmptyActionCustomMapper() throws Exception {
Map<String, String> props = new HashMap<>();
props.put("config", "struts-default.xml,struts-plugin.xml,struts.xml,org/apache/struts2/views/jsp/WW3090-struts.xml");
this.tearDown();
Dispatcher du = this.initDispatcher(props);
/**
* create our standard mock objects
*/
action = this.getAction();
stack = ActionContext.getContext().getValueStack();
context = stack.getContext();
stack.push(action);
request = new StrutsMockHttpServletRequest();
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
response = new StrutsMockHttpServletResponse();
request.setSession(new StrutsMockHttpSession());
request.setupGetServletPath("/");
writer = new StringWriter();
servletContext = new StrutsMockServletContext();
servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
servletContext.setServletInfo("Resin");
pageContext = new StrutsMockPageContext();
pageContext.setRequest(request);
pageContext.setResponse(response);
pageContext.setServletContext(servletContext);
mockContainer = new Mock(Container.class);
session = new SessionMap(request);
Map<String, Object> extraContext = du.createContextMap(new RequestMap(request), HttpParameters.create(request.getParameterMap()).build(), session, new ApplicationMap(pageContext.getServletContext()), request, 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);
stack.getContext().putAll(extraContext);
context.put(ServletActionContext.HTTP_REQUEST, request);
context.put(ServletActionContext.HTTP_RESPONSE, response);
context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
ActionContext actionContext = ActionContext.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
// Make sure we have an action invocation available
ActionContext.getContext().withActionInvocation(new DefaultActionInvocation(null, true));
DefaultActionProxyFactory apFactory = new DefaultActionProxyFactory();
apFactory.setContainer(container);
ActionProxy ap = apFactory.createActionProxy("/", "hello", null, null);
ActionContext.getContext().getActionInvocation().init(ap);
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(80);
tag = new URLTag();
tag.setPageContext(pageContext);
JspWriter jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
request.setRequestURI("/context/someAction.action");
tag.setAction(null);
tag.setValue(null);
tag.doStartTag();
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
URLTag freshTag = new URLTag();
freshTag.setPageContext(pageContext);
// URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
tag.doEndTag();
assertEquals("/hello.action-blue", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
// URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
// URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class URLTagTest method testEmptyActionCustomMapper_clearTagStateSet.
public void testEmptyActionCustomMapper_clearTagStateSet() throws Exception {
Map<String, String> props = new HashMap<String, String>();
props.put("config", "struts-default.xml,struts-plugin.xml,struts.xml,org/apache/struts2/views/jsp/WW3090-struts.xml");
this.tearDown();
Dispatcher du = this.initDispatcher(props);
action = this.getAction();
stack = ActionContext.getContext().getValueStack();
context = stack.getContext();
stack.push(action);
request = new StrutsMockHttpServletRequest();
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
response = new StrutsMockHttpServletResponse();
request.setSession(new StrutsMockHttpSession());
request.setupGetServletPath("/");
writer = new StringWriter();
servletContext = new StrutsMockServletContext();
servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
servletContext.setServletInfo("Resin");
pageContext = new StrutsMockPageContext();
pageContext.setRequest(request);
pageContext.setResponse(response);
pageContext.setServletContext(servletContext);
mockContainer = new Mock(Container.class);
session = new SessionMap<>(request);
Map<String, Object> extraContext = du.createContextMap(new RequestMap(request), HttpParameters.create(request.getParameterMap()).build(), session, new ApplicationMap(pageContext.getServletContext()), request, 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 = ActionContext.of(extraContext).withLocale(null).getContextMap();
stack.getContext().putAll(extraContext);
ActionContext actionContext = ActionContext.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
// Make sure we have an action invocation available
ActionContext.getContext().withActionInvocation(new DefaultActionInvocation(null, true));
DefaultActionProxyFactory apFactory = new DefaultActionProxyFactory();
apFactory.setContainer(container);
ActionProxy ap = apFactory.createActionProxy("/", "hello", null, null);
ActionContext.getContext().getActionInvocation().init(ap);
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(80);
tag = new URLTag();
// Explicitly request tag state clearing.
tag.setPerformClearTagStateForTagPoolingServers(true);
tag.setPageContext(pageContext);
JspWriter jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
request.setRequestURI("/context/someAction.action");
tag.setAction(null);
tag.setValue(null);
tag.doStartTag();
// Ensure component tag state clearing is set true (to match tag).
setComponentTagClearTagState(tag, true);
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
URLTag freshTag = new URLTag();
freshTag.setPerformClearTagStateForTagPoolingServers(true);
freshTag.setPageContext(pageContext);
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
// Ensure component tag state clearing is set true (to match tag).
setComponentTagClearTagState(tag, true);
tag.doEndTag();
assertEquals("/hello.action-blue", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
// Ensure component tag state clearing is set true (to match tag).
setComponentTagClearTagState(tag, true);
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class DefaultActionInvocationTester method testUnknownHandlerManagerThatThrowsException.
public void testUnknownHandlerManagerThatThrowsException() throws Exception {
// given
DefaultActionInvocation dai = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), false);
container.inject(dai);
UnknownHandlerManager uhm = new DefaultUnknownHandlerManager() {
@Override
public boolean hasUnknownHandlers() {
return true;
}
@Override
public Object handleUnknownMethod(Object action, String methodName) throws NoSuchMethodException {
throw new NoSuchMethodException();
}
};
MockActionProxy proxy = new MockActionProxy();
proxy.setMethod("notExists");
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
dai.proxy = proxy;
dai.ognlUtil = new OgnlUtil();
dai.unknownHandlerManager = uhm;
// when
// when
Throwable actual = null;
try {
dai.invokeAction(new SimpleAction(), null);
} catch (Exception e) {
actual = e;
}
// then
assertNotNull(actual);
assertTrue(actual instanceof NoSuchMethodException);
}
use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class DefaultActionInvocationTester method testInvokingExistingMethodThatThrowsException.
public void testInvokingExistingMethodThatThrowsException() throws Exception {
// given
DefaultActionInvocation dai = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), false);
container.inject(dai);
SimpleAction action = new SimpleAction() {
@Override
public String execute() throws Exception {
throw new IllegalArgumentException();
}
};
MockActionProxy proxy = new MockActionProxy();
proxy.setMethod("execute");
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
dai.proxy = proxy;
dai.ognlUtil = new OgnlUtil();
// when
Throwable actual = null;
try {
dai.invokeAction(action, null);
} catch (Exception e) {
actual = e;
}
// then
assertNotNull(actual);
assertTrue(actual instanceof IllegalArgumentException);
}
use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class DefaultActionInvocationTester method testInvokeWithLazyParams.
public void testInvokeWithLazyParams() throws Exception {
HashMap<String, Object> params = new HashMap<>();
params.put("blah", "this is blah");
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
DefaultActionInvocation defaultActionInvocation = new DefaultActionInvocation(extraContext, true);
container.inject(defaultActionInvocation);
ActionProxy actionProxy = actionProxyFactory.createActionProxy("", "LazyFoo", null, extraContext);
defaultActionInvocation.init(actionProxy);
defaultActionInvocation.invoke();
SimpleAction action = (SimpleAction) defaultActionInvocation.getAction();
assertEquals("this is blah", action.getBlah());
assertEquals("this is blah", action.getName());
}
Aggregations