FileDocCategorySizeDatePackage
Jsr168DispatcherTest.javaAPI DocExample14208Mon Jul 23 13:26:12 BST 2007org.apache.struts2.portlet.dispatcher

Jsr168DispatcherTest

public class Jsr168DispatcherTest extends org.jmock.cglib.MockObjectTestCase implements org.apache.struts2.portlet.PortletActionConstants
Jsr168DispatcherTest. Insert description.

Fields Summary
Jsr168Dispatcher
dispatcher
org.jmock.Mock
mockConfig
org.jmock.Mock
mockCtx
org.jmock.Mock
mockRequest
org.jmock.Mock
mockSession
org.jmock.Mock
mockActionFactory
org.jmock.Mock
mockActionProxy
org.jmock.Mock
mockAction
org.jmock.Mock
mockInvocation
Constructors Summary
Methods Summary
private voidinitPortletConfig(java.util.Map initParams, java.util.Map attributes)

        mockConfig = mock(PortletConfig.class);
        mockCtx = mock(PortletContext.class);
        mockConfig.stubs().method(ANYTHING);
        setupStub(initParams, mockConfig, "getInitParameter");
        mockCtx.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(attributes.keySet())));
        setupStub(attributes, mockCtx, "getAttribute");
        mockConfig.stubs().method("getPortletContext").will(returnValue(mockCtx.proxy()));
        mockCtx.stubs().method("getInitParameterNames").will(returnValue(Collections.enumeration(initParams.keySet())));
        setupStub(initParams, mockCtx, "getInitParameter");
        mockConfig.stubs().method("getInitParameterNames").will(returnValue(Collections.enumeration(initParams.keySet())));
        setupStub(initParams, mockConfig, "getInitParameter");

        mockConfig.stubs().method("getResourceBundle").will(returnValue(new ListResourceBundle() {
            protected Object[][] getContents() {
                return new String[][]{{"javax.portlet.title", "MyTitle"}};
            }
        }));
    
private voidinitRequest(java.util.Map requestParams, java.util.Map requestAttributes, java.util.Map sessionParams, java.util.Map renderParams, javax.portlet.PortletMode mode, javax.portlet.WindowState state, boolean isEvent, java.util.Locale locale)
Initialize the mock request (and as a result, the mock session)

param
requestParams The request parameters
param
requestAttributes The request attributes
param
sessionParams The session attributes
param
renderParams The render parameters. Will only be set if isEvent is true
param
mode The portlet mode
param
state The portlet window state
param
isEvent true when the request is an ActionRequest.
param
locale The locale. If null, the request will return Locale.getDefault()

        mockRequest = isEvent ? mock(ActionRequest.class) : mock(RenderRequest.class);
        mockSession = mock(PortletSession.class);
        mockSession.stubs().method(ANYTHING);
        mockRequest.stubs().method(ANYTHING);
        setupStub(sessionParams, mockSession, "getAttribute");
        mockSession.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(sessionParams.keySet())));
        setupParamStub(requestParams, mockRequest, "getParameter");
        setupStub(requestAttributes, mockRequest, "getAttribute");
        mockRequest.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(requestAttributes.keySet())));
        mockRequest.stubs().method("getParameterMap").will(returnValue(requestParams));
        mockRequest.stubs().method("getParameterNames").will(returnValue(Collections.enumeration(requestParams.keySet())));
        mockRequest.stubs().method("getPortletSession").will(returnValue(mockSession.proxy()));
        if(locale != null) {
            mockRequest.stubs().method("getLocale").will(returnValue(locale));
        }
        else {
            mockRequest.stubs().method("getLocale").will(returnValue(Locale.getDefault()));
        }
        mockRequest.stubs().method("getPortletMode").will(returnValue(mode));
        mockRequest.stubs().method("getWindowState").will(returnValue(state));
    
public static voidmain(java.lang.String[] args)

        TestRunner.run(Jsr168DispatcherTest.class);
    
public voidsetUp()


       
        dispatcher = new Jsr168Dispatcher();
    
private voidsetupActionFactory(java.lang.String namespace, java.lang.String actionName, java.lang.String result, com.opensymphony.xwork2.util.ValueStack stack)

        if(mockActionFactory == null) {
            mockActionFactory = mock(ActionProxyFactory.class);
        }
        mockAction = mock(Action.class);
        mockActionProxy = mock(ActionProxy.class);
        mockInvocation = mock(ActionInvocation.class);

        mockActionFactory.expects(once()).method("createActionProxy").with(new Constraint[]{eq(namespace), eq(actionName), isA(Map.class)}).will(returnValue(mockActionProxy.proxy()));
        mockActionProxy.stubs().method("getAction").will(returnValue(mockAction.proxy()));
        mockActionProxy.expects(once()).method("execute").will(returnValue(result));
        mockActionProxy.expects(once()).method("getInvocation").will(returnValue(mockInvocation.proxy()));
        mockActionProxy.expects(once()).method("setMethod");
        mockInvocation.stubs().method("getStack").will(returnValue(stack));

    
private voidsetupParamStub(java.util.Map requestParams, org.jmock.Mock mockRequest, java.lang.String method)

param
requestParams
param
mockRequest2
param
string

        Map newMap = new HashMap();
        Iterator it = requestParams.keySet().iterator();
        while(it.hasNext()) {
            Object key = it.next();
            String[] val = (String[])requestParams.get(key);
            newMap.put(key, val[0]);
        }
        setupStub(newMap, mockRequest, method);

    
private voidsetupStub(java.util.Map map, org.jmock.Mock mock, java.lang.String method)
Set up stubs for the mock.

param
map The map containing the key and values. The key is the expected parameter to method, and value is the value that should be returned from the stub.
param
mock The mock to initialize.
param
method The name of the method to stub.

        Iterator it = map.keySet().iterator();
        while(it.hasNext()) {
            Object key = it.next();
            Object val = map.get(key);
            mock.stubs().method(method).with(eq(key)).will(returnValue(val));
        }
    
public voidtestModeChangeUsingPortletWidgets()

        final Mock mockResponse = mock(RenderResponse.class);
        mockResponse.stubs().method(ANYTHING);
        PortletMode mode = PortletMode.EDIT;

        Map requestParams = new HashMap();
        requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
        requestParams.put(EVENT_ACTION, new String[]{"false"});
        requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{PortletMode.VIEW.toString()});

        Map sessionMap = new HashMap();

        Map initParams = new HashMap();
        initParams.put("viewNamespace", "/view");
        initParams.put("editNamespace", "/edit");

        initPortletConfig(initParams, new HashMap());
        initRequest(requestParams, new HashMap(), sessionMap, new HashMap(), mode, WindowState.NORMAL, false, null);
        setupActionFactory("/edit", "default", "success", ValueStackFactory.getFactory().createValueStack());

        mockInvocation.expects(once()).method("getStack").will(
                returnValue(null));
        //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
        try {
            dispatcher
                    .setActionProxyFactory((ActionProxyFactory) mockActionFactory
                            .proxy());
            dispatcher.init((PortletConfig) mockConfig.proxy());
            dispatcher.render((RenderRequest) mockRequest.proxy(),
                    (RenderResponse) mockResponse.proxy());
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error occured");
        }
    
public voidtestProcessAction_ok()

        final Mock mockResponse = mock(ActionResponse.class);

        PortletMode mode = PortletMode.VIEW;
        Map initParams = new HashMap();
        initParams.put("viewNamespace", "/view");

        Map requestParams = new HashMap();
        requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
        requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});

        initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");
        initPortletConfig(initParams, new HashMap());
        initRequest(requestParams, new HashMap(), new HashMap(), new HashMap(), PortletMode.VIEW, WindowState.NORMAL, true, null);
        setupActionFactory("/view", "testAction", "success", ValueStackFactory.getFactory().createValueStack());
        Constraint[] paramConstraints = new Constraint[] {
                eq(PortletActionConstants.EVENT_ACTION), same(mockActionProxy.proxy()) };

        mockSession.expects(once()).method("setAttribute").with(
                paramConstraints);

        mockResponse.expects(once()).method("setRenderParameter").with(
                new Constraint[] { eq(PortletActionConstants.EVENT_ACTION),
                        eq("true") });

        //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
        try {
            dispatcher
                    .setActionProxyFactory((ActionProxyFactory) mockActionFactory
                            .proxy());
            dispatcher.init((PortletConfig) mockConfig.proxy());
            dispatcher.processAction((ActionRequest) mockRequest.proxy(),
                    (ActionResponse) mockResponse.proxy());
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error occured");
        }
    
public voidtestRender_ok()

        final Mock mockResponse = mock(RenderResponse.class);
        mockResponse.stubs().method(ANYTHING);
        final Mock servletContext = mock(ServletContext.class);
        servletContext.stubs().method(ANYTHING);
        ServletContextEvent event = new ServletContextEvent((ServletContext)servletContext.proxy());
        new ServletContextHolderListener().contextInitialized(event);
        PortletMode mode = PortletMode.VIEW;

        Map requestParams = new HashMap();
        requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
        requestParams.put(EVENT_ACTION, new String[]{"true"});
        requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});

        Map sessionMap = new HashMap();



        Map initParams = new HashMap();
        initParams.put("viewNamespace", "/view");
        initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");

        initPortletConfig(initParams, new HashMap());
        initRequest(requestParams, new HashMap(), sessionMap, new HashMap(), PortletMode.VIEW, WindowState.NORMAL, false, null);
        setupActionFactory("/view", "testAction", "success", ValueStackFactory.getFactory().createValueStack());

        mockInvocation.expects(once()).method("getStack").will(
                returnValue(null));
        //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
        try {
            dispatcher
                    .setActionProxyFactory((ActionProxyFactory) mockActionFactory
                            .proxy());
            dispatcher.init((PortletConfig) mockConfig.proxy());
            dispatcher.render((RenderRequest) mockRequest.proxy(),
                    (RenderResponse) mockResponse.proxy());
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error occured");
        }