Java Code Examples for org.apache.struts2.ServletActionContext#setContext()

The following examples show how to use org.apache.struts2.ServletActionContext#setContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageSettingsActionAspectTest.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    ActionContext actionContext = Mockito.mock(ActionContext.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    request.setSession(new MockHttpSession(servletContext));
    when(actionContext.getLocale()).thenReturn(Locale.ENGLISH);
    ValueStack valueStack = Mockito.mock(ValueStack.class);
    Map<String, Object> context = new HashMap<>();
    Container container = Mockito.mock(Container.class);
    XWorkConverter conv = Mockito.mock(XWorkConverter.class);
    when(container.getInstance(XWorkConverter.class)).thenReturn(conv);
    when(conv.convertValue(Mockito.any(Map.class), Mockito.any(Object.class), Mockito.any(Class.class))).thenAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return "VALUE";
        }
    });
    context.put(ActionContext.CONTAINER, container);
    when(valueStack.getContext()).thenReturn(context);
    when(actionContext.getValueStack()).thenReturn(valueStack);
    when(actionContext.get(ServletActionContext.HTTP_REQUEST)).thenReturn(request);
    ServletActionContext.setContext(actionContext);
    when(joinPoint.getTarget()).thenReturn(pageSettingsAction);
    when(textProvider.getText(ArgumentMatchers.anyString(), ArgumentMatchers.anyList())).thenReturn("text");
}
 
Example 2
Source File: ApsAdminBaseTestCase.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Created action class based on namespace and name
 *
 * @param namespace The namespace
 * @param name The name of the action
 * @throws java.lang.Exception In case of error
 */
protected void initAction(String namespace, String name) throws Exception {
    // create a proxy class which is just a wrapper around the action call.
    // The proxy is created by checking the namespace and name against the
    // struts.xml configuration
    ActionProxyFactory proxyFactory = (ActionProxyFactory) this.dispatcher.getContainer().getInstance(ActionProxyFactory.class);
    this.proxy = proxyFactory.createActionProxy(namespace, name, null, null, true, false);

    // set to true if you want to process Freemarker or JSP results
    this.proxy.setExecuteResult(false);
    // by default, don't pass in any request parameters

    // set the actions context to the one which the proxy is using
    this.proxy.getInvocation().getInvocationContext().setSession(new HashMap<>());
    ServletActionContext.setContext(this.proxy.getInvocation().getInvocationContext());
    ServletActionContext.setRequest(this.request);
    ServletActionContext.setResponse(response);
    ServletActionContext.setServletContext(servletContext);
    this.action = (ActionSupport) this.proxy.getAction();

    //reset previsious params
    List<String> paramNames = new ArrayList<String>(this.request.getParameterMap().keySet());
    for (int i = 0; i < paramNames.size(); i++) {
        String paramName = (String) paramNames.get(i);
        this.removeParameter(paramName);
    }
}