FileDocCategorySizeDatePackage
PortletUrlTagTest.javaAPI DocExample14292Mon Jul 23 13:26:16 BST 2007org.apache.struts2.views.jsp

PortletUrlTagTest

public class PortletUrlTagTest extends org.jmock.cglib.MockObjectTestCase

Fields Summary
URLTag
tag
org.jmock.Mock
mockHttpReq
org.jmock.Mock
mockHttpRes
org.jmock.Mock
mockPortletReq
org.jmock.Mock
mockPortletRes
org.jmock.Mock
mockPageCtx
org.jmock.Mock
mockPortletUrl
com.mockobjects.servlet.MockJspWriter
mockJspWriter
com.opensymphony.xwork2.util.ValueStack
stack
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


         
        TestRunner.run(PortletUrlTagTest.class);
    
private voidmockPortletApiAvailable()

        try {
            Field field = Dispatcher.class.getDeclaredField("portletSupportActive");
            field.setAccessible(true);
            field.set(null, Boolean.TRUE);
        }
        catch(Exception e) {

        }

    
public voidsetUp()

        super.setUp();

        Dispatcher du = new Dispatcher(null, new HashMap());
        du.init();
        Dispatcher.setInstance(du);

        mockPortletApiAvailable();

        stack = ValueStackFactory.getFactory().createValueStack();


        mockHttpReq = mock(HttpServletRequest.class);
        mockHttpRes = mock(HttpServletResponse.class);
        mockPortletReq = mock(RenderRequest.class);
        mockPortletRes = mock(RenderResponse.class);
        mockPageCtx = mock(PageContext.class);
        mockPortletUrl = mock(PortletURL.class);
        mockJspWriter = new MockJspWriter();

        mockPageCtx.stubs().method("getRequest").will(
                returnValue((HttpServletRequest) mockHttpReq.proxy()));
        mockPageCtx.stubs().method("getResponse").will(
                returnValue((HttpServletResponse) mockHttpRes.proxy()));
        mockPageCtx.stubs().method("getOut").will(returnValue(mockJspWriter));

        mockHttpReq.stubs().method("getScheme").will(returnValue("http"));
        mockHttpReq.stubs().method("getAttribute").with(
                eq("struts.valueStack")).will(returnValue(stack));
        mockHttpReq.stubs().method("getAttribute").with(
                eq("javax.portlet.response")).will(
                returnValue((PortletResponse) mockPortletRes.proxy()));
        mockHttpReq.stubs().method("getAttribute").with(
                eq("javax.portlet.request")).will(
                returnValue((PortletRequest) mockPortletReq.proxy()));

        mockPortletReq.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
        mockPortletReq.stubs().method("getWindowState").will(returnValue(WindowState.NORMAL));
        mockPortletReq.stubs().method("getContextPath").will(returnValue("/contextPath"));

        tag.setPageContext((PageContext) mockPageCtx.proxy());

        Map modeMap = new HashMap();
        modeMap.put(PortletMode.VIEW, "/view");
        modeMap.put(PortletMode.HELP, "/help");
        modeMap.put(PortletMode.EDIT, "/edit");
        Map sessionMap = new HashMap();
        Map contextMap = new HashMap();
        contextMap.put(ActionContext.SESSION, sessionMap);
        contextMap.put(PortletActionConstants.REQUEST, mockPortletReq.proxy());
        contextMap.put(PortletActionConstants.RESPONSE, mockPortletRes.proxy());
        contextMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
        contextMap.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
        ActionContext ctx = new ActionContext(contextMap);
        ctx.setValueStack(stack);
        ActionContext.setContext(ctx);
    
public voidtestActionUrl()


        PortletMode mode = PortletMode.VIEW;

        mockHttpReq.stubs().method("getQueryString").will(returnValue(""));

        mockPortletRes.expects(once()).method("createActionURL").will(
                returnValue((PortletURL) mockPortletUrl.proxy()));

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

        mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
        mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
        mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));

        tag.setAction("testAction");
        tag.setPortletUrlType("action");
        tag.doStartTag();
        tag.doEndTag();
    
public voidtestEnsureParamsAreStringArrays()

        Map params = new HashMap();
        params.put("param1", "Test1");
        params.put("param2", new String[] { "Test2" });

        Map result = PortletUrlHelper.ensureParamsAreStringArrays(params);
        assertEquals(2, result.size());
        assertTrue(result.get("param1") instanceof String[]);
    
public voidtestResourceUrl()

        mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
        mockPortletRes.expects(once()).method("encodeURL").will(returnValue("/contextPath/image.gif"));
        mockJspWriter.setExpectedData("/contextPath/image.gif");
        tag.setValue("image.gif");
        tag.doStartTag();
        tag.doEndTag();
        mockJspWriter.verify();
    
public voidtestResourceUrlWithNestedParam()

        mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
        mockPortletRes.expects(once()).method("encodeURL").with(eq("/contextPath/image.gif?testParam1=testValue1")).will(returnValue("/contextPath/image.gif?testParam1=testValue1"));
        mockJspWriter.setExpectedData("/contextPath/image.gif?testParam1=testValue1");

        ParamTag paramTag = new ParamTag();
        paramTag.setPageContext((PageContext)mockPageCtx.proxy());
        paramTag.setParent(tag);
        paramTag.setName("testParam1");
        paramTag.setValue("'testValue1'");
        tag.setValue("image.gif");
        tag.doStartTag();
        paramTag.doStartTag();
        paramTag.doEndTag();
        tag.doEndTag();
        mockJspWriter.verify();
    
public voidtestResourceUrlWithTwoNestedParam()

        mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
        mockPortletRes.expects(once()).method("encodeURL").with(eq("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2")).will(returnValue("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2"));
        mockJspWriter.setExpectedData("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2");

        ParamTag paramTag = new ParamTag();
        paramTag.setPageContext((PageContext)mockPageCtx.proxy());
        paramTag.setParent(tag);
        paramTag.setName("testParam1");
        paramTag.setValue("'testValue1'");
        ParamTag paramTag2 = new ParamTag();
        paramTag2.setPageContext((PageContext)mockPageCtx.proxy());
        paramTag2.setParent(tag);
        paramTag2.setName("testParam2");
        paramTag2.setValue("'testValue2'");
        tag.setValue("image.gif");
        tag.doStartTag();
        paramTag.doStartTag();
        paramTag.doEndTag();
        paramTag2.doStartTag();
        paramTag2.doEndTag();
        tag.doEndTag();
        mockJspWriter.verify();
    
public voidtestSetPortletMode()


        PortletMode mode = PortletMode.HELP;

        mockHttpReq.stubs().method("getQueryString").will(returnValue(""));

        mockPortletRes.expects(once()).method("createRenderURL").will(
                returnValue((PortletURL) mockPortletUrl.proxy()));

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

        mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
        mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.HELP));
        mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));

        tag.setAction("testAction");
        tag.setPortletMode("help");
        tag.doStartTag();
        tag.doEndTag();
    
public voidtestSetWindowState()


        PortletMode mode = PortletMode.VIEW;

        mockHttpReq.stubs().method("getQueryString").will(returnValue(""));

        mockPortletRes.expects(once()).method("createRenderURL").will(
                returnValue((PortletURL) mockPortletUrl.proxy()));

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

        mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
        mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.MAXIMIZED));
        mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));

        tag.setAction("testAction");
        tag.setWindowState("maximized");
        tag.doStartTag();
        tag.doEndTag();

    
public voidtestUrlWithQueryParams()


        PortletMode mode = PortletMode.VIEW;

        mockHttpReq.stubs().method("getQueryString").will(returnValue(""));

        mockPortletRes.expects(once()).method("createRenderURL").will(
                returnValue((PortletURL) mockPortletUrl.proxy()));

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

        mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
        mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
        mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));

        tag.setAction("testAction?testParam1=testValue1");
        tag.doStartTag();
        tag.doEndTag();