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

AbstractTagTest

public abstract class AbstractTagTest extends org.apache.struts2.StrutsTestCase
Base class to extend for unit testing UI Tags.

Fields Summary
protected com.opensymphony.xwork2.Action
action
protected Map
context
protected Map
session
protected com.opensymphony.xwork2.util.ValueStack
stack
protected StringWriter
writer
contains the buffer that our unit test will write to. we can later verify this buffer for correctness.
protected StrutsMockHttpServletRequest
request
protected StrutsMockPageContext
pageContext
protected HttpServletResponse
response
protected StrutsMockServletContext
servletContext
protected com.mockobjects.dynamic.Mock
mockContainer
Constructors Summary
Methods Summary
public com.opensymphony.xwork2.ActiongetAction()
Constructs the action that we're going to test against. For most UI tests, this default action should be enough. However, simply override getAction to return a custom Action if you need something more sophisticated.

return
the Action to be added to the ValueStack as part of the unit test

        return new TestAction();
    
protected voidsetUp()

        super.setUp();

        /**
         * create our standard mock objects
         */
        action = this.getAction();
        stack = ValueStackFactory.getFactory().createValueStack();
        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();

        JspWriter jspWriter = new StrutsMockJspWriter(writer);

        servletContext = new StrutsMockServletContext();
        servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
        servletContext.setServletInfo("Resin");

        pageContext = new StrutsMockPageContext();
        pageContext.setRequest(request);
        pageContext.setResponse(response);
        pageContext.setJspWriter(jspWriter);
        pageContext.setServletContext(servletContext);

        mockContainer = new Mock(Container.class);
        Dispatcher du = new Dispatcher(pageContext.getServletContext(), new HashMap());
        du.init();
        Dispatcher.setInstance(du);
        du.setConfigurationManager(configurationManager);
        session = new SessionMap(request);
        Map extraContext = du.createContextMap(new RequestMap(request),
                request.getParameterMap(),
                session,
                new ApplicationMap(pageContext.getServletContext()),
                request,
                response,
                pageContext.getServletContext());
        // 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.setContext(new ActionContext(context));
    
protected voidtearDown()

        pageContext.verify();
        request.verify();