001: package com.mockrunner.test.web;
002:
003: import java.io.IOException;
004:
005: import javax.servlet.ServletException;
006: import javax.servlet.http.HttpServlet;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009: import javax.servlet.jsp.JspException;
010: import javax.servlet.jsp.tagext.TagSupport;
011:
012: import junit.framework.TestCase;
013:
014: import org.apache.struts.action.Action;
015: import org.apache.struts.action.ActionForm;
016: import org.apache.struts.action.ActionForward;
017: import org.apache.struts.action.ActionMapping;
018: import org.jdom.Element;
019:
020: import com.mockrunner.base.HTMLOutputModule;
021: import com.mockrunner.base.VerifyFailedException;
022: import com.mockrunner.base.WebTestModule;
023: import com.mockrunner.mock.web.ActionMockObjectFactory;
024: import com.mockrunner.mock.web.MockActionForward;
025: import com.mockrunner.servlet.ServletTestModule;
026: import com.mockrunner.struts.ActionTestModule;
027: import com.mockrunner.tag.TagTestModule;
028: import com.mockrunner.util.common.StreamUtil;
029:
030: public class HTMLOutputModuleTest extends TestCase {
031: private final static String testHTML = "<html><body><tag></body></html>";
032: private ActionMockObjectFactory actionWebFactory;
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036: actionWebFactory = new ActionMockObjectFactory();
037: }
038:
039: public void testActionTestModuleOutput() {
040: ActionTestModule module = new ActionTestModule(actionWebFactory);
041: module.actionPerform(TestOutputAction.class);
042: doTestOutputAsString(module);
043: doTestOutputAsBufferedReader(module);
044: doTestOutputAsJDOMDocument(module);
045: doTestVerifyOutput(module);
046: doTestVerifyOutputContains(module);
047: doTestVerifyOutputRegularExpression(module);
048: }
049:
050: public void testActionTestModuleAttributes() {
051: ActionTestModule module = new ActionTestModule(actionWebFactory);
052: doTestAttributes(module);
053: }
054:
055: public void testServletTestModuleOutput() {
056: ServletTestModule module = new ServletTestModule(
057: actionWebFactory);
058: module.createServlet(TestOutputServlet.class);
059: module.doGet();
060: doTestOutputAsString(module);
061: doTestOutputAsBufferedReader(module);
062: doTestOutputAsJDOMDocument(module);
063: doTestVerifyOutput(module);
064: doTestVerifyOutputContains(module);
065: doTestVerifyOutputRegularExpression(module);
066: }
067:
068: public void testServletTestModuleAttributes() {
069: ServletTestModule module = new ServletTestModule(
070: actionWebFactory);
071: doTestAttributes(module);
072: }
073:
074: public void testTagTestModuleOutput() {
075: TagTestModule module = new TagTestModule(actionWebFactory);
076: module.createTag(TestOutputTag.class);
077: module.doStartTag();
078: doTestOutputAsString(module);
079: doTestOutputAsBufferedReader(module);
080: doTestOutputAsJDOMDocument(module);
081: doTestVerifyOutput(module);
082: doTestVerifyOutputContains(module);
083: doTestVerifyOutputRegularExpression(module);
084: }
085:
086: public void testTagTestModuleAttributes() {
087: TagTestModule module = new TagTestModule(actionWebFactory);
088: doTestAttributes(module);
089: }
090:
091: private void doTestOutputAsString(HTMLOutputModule module) {
092: assertEquals(testHTML, module.getOutput());
093: }
094:
095: private void doTestOutputAsBufferedReader(HTMLOutputModule module) {
096: assertEquals(testHTML, StreamUtil.getReaderAsString(module
097: .getOutputAsBufferedReader()));
098: }
099:
100: private void doTestOutputAsJDOMDocument(HTMLOutputModule module) {
101: Element root = module.getOutputAsJDOMDocument()
102: .getRootElement();
103: assertEquals("html", root.getName());
104: Element body = root.getChild("body");
105: assertNotNull(body);
106: Element tag = body.getChild("tag");
107: assertNotNull(tag);
108: }
109:
110: private void doTestVerifyOutput(HTMLOutputModule module) {
111: module.setCaseSensitive(true);
112: module.verifyOutput("<html><body><tag></body></html>");
113: try {
114: module.verifyOutput("<HTml><body><tAg></body></html>");
115: fail();
116: } catch (VerifyFailedException exc) {
117: //should throw exception
118: }
119: module.setCaseSensitive(false);
120: module.verifyOutput("<HTml><body><tAg></body></html>");
121: try {
122: module.verifyOutput("xyz");
123: fail();
124: } catch (VerifyFailedException exc) {
125: //should throw exception
126: }
127: }
128:
129: private void doTestVerifyOutputContains(HTMLOutputModule module) {
130: module.setCaseSensitive(true);
131: module.verifyOutputContains("<body>");
132: try {
133: module.verifyOutputContains("<BODY>");
134: fail();
135: } catch (VerifyFailedException exc) {
136: //should throw exception
137: }
138: module.setCaseSensitive(false);
139: module.verifyOutputContains("<BODY>");
140: try {
141: module.verifyOutputContains("boddy");
142: fail();
143: } catch (VerifyFailedException exc) {
144: //should throw exception
145: }
146: }
147:
148: private void doTestVerifyOutputRegularExpression(
149: HTMLOutputModule module) {
150: module.setCaseSensitive(true);
151: module.verifyOutputRegularExpression(".*<body.*");
152: try {
153: module.verifyOutputRegularExpression(".*<BO.*");
154: fail();
155: } catch (VerifyFailedException exc) {
156: //should throw exception
157: }
158: module.setCaseSensitive(false);
159: module.verifyOutputRegularExpression(".*<BO.*");
160: try {
161: module.verifyOutputRegularExpression(".*<BOG.*");
162: fail();
163: } catch (VerifyFailedException exc) {
164: //should throw exception
165: }
166: }
167:
168: private void doTestAttributes(WebTestModule module) {
169: module.setSessionAttribute("sessionatt", new Integer(3));
170: module.addRequestParameter("requestparam");
171: module.setRequestAttribute("requestatt", "xyz");
172: assertEquals(new Integer(3), actionWebFactory.getMockSession()
173: .getAttribute("sessionatt"));
174: assertEquals("", actionWebFactory.getMockRequest()
175: .getParameter("requestparam"));
176: assertEquals("xyz", actionWebFactory.getMockRequest()
177: .getAttribute("requestatt"));
178: }
179:
180: public static class TestOutputAction extends Action {
181:
182: public ActionForward execute(ActionMapping mspping,
183: ActionForm form, HttpServletRequest request,
184: HttpServletResponse response) throws Exception {
185: response.getWriter().write(testHTML);
186: return new MockActionForward();
187: }
188: }
189:
190: public static class TestOutputServlet extends HttpServlet {
191: protected void doGet(HttpServletRequest request,
192: HttpServletResponse response) throws ServletException,
193: IOException {
194: response.getWriter().write(testHTML);
195: }
196: }
197:
198: public static class TestOutputTag extends TagSupport {
199: public int doStartTag() throws JspException {
200: try {
201: pageContext.getOut().print(testHTML);
202: } catch (IOException exc) {
203: throw new RuntimeException(exc.getMessage());
204: }
205: return TagSupport.SKIP_BODY;
206: }
207: }
208: }
|