001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import com.opensymphony.webwork.ServletActionContext;
008: import com.opensymphony.webwork.TestAction;
009: import com.opensymphony.webwork.WebWorkTestCase;
010: import com.opensymphony.webwork.config.Configuration;
011: import com.opensymphony.webwork.dispatcher.ApplicationMap;
012: import com.opensymphony.webwork.dispatcher.DispatcherUtils;
013: import com.opensymphony.webwork.dispatcher.RequestMap;
014: import com.opensymphony.webwork.dispatcher.SessionMap;
015: import com.opensymphony.xwork.Action;
016: import com.opensymphony.xwork.ActionContext;
017: import com.opensymphony.xwork.util.OgnlValueStack;
018:
019: import javax.servlet.http.HttpServletResponse;
020: import javax.servlet.jsp.JspWriter;
021: import java.io.File;
022: import java.io.StringWriter;
023: import java.util.Map;
024:
025: /**
026: * Base class to extend for unit testing UI Tags.
027: *
028: * @author jcarreira
029: */
030: public abstract class AbstractTagTest extends WebWorkTestCase {
031: protected Action action;
032: protected Map context;
033: protected Map session;
034: protected OgnlValueStack stack;
035:
036: /**
037: * contains the buffer that our unit test will write to. we can later verify this buffer for correctness.
038: */
039: protected StringWriter writer;
040: protected WebWorkMockHttpServletRequest request;
041: protected WebWorkMockPageContext pageContext;
042: protected HttpServletResponse response;
043: protected WebWorkMockServletContext servletContext;
044:
045: /**
046: * Constructs the action that we're going to test against. For most UI tests, this default action should be enough.
047: * However, simply override getAction to return a custom Action if you need something more sophisticated.
048: *
049: * @return the Action to be added to the OgnlValueStack as part of the unit test
050: */
051: public Action getAction() {
052: return new TestAction();
053: }
054:
055: protected void setUp() throws Exception {
056: super .setUp();
057:
058: /**
059: * create our standard mock objects
060: */
061: action = this .getAction();
062: stack = new OgnlValueStack();
063: context = stack.getContext();
064: stack.push(action);
065:
066: request = new WebWorkMockHttpServletRequest();
067: request.setAttribute(
068: ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
069: response = new WebWorkMockHttpServletResponse();
070: request.setSession(new WebWorkMockHttpSession());
071: request.setupGetServletPath("/");
072:
073: writer = new StringWriter();
074:
075: JspWriter jspWriter = new WebWorkMockJspWriter(writer);
076:
077: servletContext = new WebWorkMockServletContext();
078: servletContext.setRealPath(new File("nosuchfile.properties")
079: .getAbsolutePath());
080: servletContext.setServletInfo("Resin");
081:
082: pageContext = new WebWorkMockPageContext();
083: pageContext.setRequest(request);
084: pageContext.setResponse(response);
085: pageContext.setJspWriter(jspWriter);
086: pageContext.setServletContext(servletContext);
087:
088: DispatcherUtils.initialize(pageContext.getServletContext());
089: DispatcherUtils du = DispatcherUtils.getInstance();
090: session = new SessionMap(request);
091: Map extraContext = du.createContextMap(new RequestMap(request),
092: request.getParameterMap(), session, new ApplicationMap(
093: pageContext.getServletContext()), request,
094: response, pageContext.getServletContext());
095: // let's not set the locale -- there is a test that checks if DispatcherUtils actually picks this up...
096: // ... but generally we want to just use no locale (let it stay system default)
097: extraContext.remove(ActionContext.LOCALE);
098: stack.getContext().putAll(extraContext);
099:
100: context.put(ServletActionContext.HTTP_REQUEST, request);
101: context.put(ServletActionContext.HTTP_RESPONSE, response);
102: context.put(ServletActionContext.SERVLET_CONTEXT,
103: servletContext);
104:
105: ActionContext.setContext(new ActionContext(context));
106:
107: Configuration.setConfiguration(null);
108: }
109:
110: protected void tearDown() throws Exception {
111: pageContext.verify();
112: request.verify();
113: }
114: }
|