001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import java.io.InputStream;
008: import java.io.PrintWriter;
009: import java.io.StringWriter;
010:
011: import com.opensymphony.util.ClassLoaderUtil;
012: import com.opensymphony.webwork.WebWorkStatics;
013: import com.opensymphony.webwork.views.jsp.AbstractUITagTest;
014: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletResponse;
015: import com.opensymphony.webwork.views.jsp.WebWorkMockServletContext;
016: import com.opensymphony.xwork.ActionContext;
017: import com.opensymphony.xwork.mock.MockActionInvocation;
018: import com.opensymphony.xwork.util.OgnlValueStack;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * Test case for PlainTextResult.
024: *
025: * @author tm_jee
026: * @version $Date: 2006-03-11 16:38:08 +0100 (Sat, 11 Mar 2006) $ $Id: PlainTextResultTest.java 2369 2006-03-11 15:38:08Z tmjee $
027: */
028: public class PlainTextResultTest extends TestCase {
029:
030: OgnlValueStack stack;
031: MockActionInvocation invocation;
032: ActionContext context;
033: WebWorkMockHttpServletResponse response;
034: PrintWriter writer;
035: StringWriter stringWriter;
036: WebWorkMockServletContext servletContext;
037:
038: public void testPlainText() throws Exception {
039: PlainTextResult result = new PlainTextResult();
040: result.setLocation("/someJspFile.jsp");
041:
042: response.setExpectedContentType("text/plain");
043: response.setExpectedHeader("Content-Disposition", "inline");
044: InputStream jspResourceInputStream = ClassLoaderUtil
045: .getResourceAsStream(
046: "com/opensymphony/webwork/dispatcher/someJspFile.jsp",
047: PlainTextResultTest.class);
048:
049: try {
050: servletContext.setResourceAsStream(jspResourceInputStream);
051: result.execute(invocation);
052:
053: String r = AbstractUITagTest.normalize(stringWriter
054: .getBuffer().toString(), true);
055: String e = AbstractUITagTest
056: .normalize(
057: readAsString("com/opensymphony/webwork/dispatcher/someJspFile.jsp"),
058: true);
059: assertEquals(r, e);
060: } finally {
061: jspResourceInputStream.close();
062: }
063: }
064:
065: public void testPlainTextWithEncoding() throws Exception {
066: PlainTextResult result = new PlainTextResult();
067: result.setLocation("/someJspFile.jsp");
068: result.setCharSet("UTF-8");
069:
070: response.setExpectedContentType("text/plain; charset=UTF-8");
071: response.setExpectedHeader("Content-Disposition", "inline");
072: InputStream jspResourceInputStream = ClassLoaderUtil
073: .getResourceAsStream(
074: "com/opensymphony/webwork/dispatcher/someJspFile.jsp",
075: PlainTextResultTest.class);
076:
077: try {
078: servletContext.setResourceAsStream(jspResourceInputStream);
079: result.execute(invocation);
080:
081: String r = AbstractUITagTest.normalize(stringWriter
082: .getBuffer().toString(), true);
083: String e = AbstractUITagTest
084: .normalize(
085: readAsString("com/opensymphony/webwork/dispatcher/someJspFile.jsp"),
086: true);
087: assertEquals(r, e);
088: } finally {
089: jspResourceInputStream.close();
090: }
091: }
092:
093: protected String readAsString(String resource) throws Exception {
094: InputStream is = null;
095: try {
096: is = ClassLoaderUtil.getResourceAsStream(resource,
097: PlainTextResultTest.class);
098: int sizeRead = 0;
099: byte[] buffer = new byte[1024];
100: StringBuffer stringBuffer = new StringBuffer();
101: while ((sizeRead = is.read(buffer)) != -1) {
102: stringBuffer.append(new String(buffer, 0, sizeRead));
103: }
104: return stringBuffer.toString();
105: } finally {
106: if (is != null)
107: is.close();
108: }
109:
110: }
111:
112: protected void setUp() throws Exception {
113: super .setUp();
114:
115: stringWriter = new StringWriter();
116: writer = new PrintWriter(stringWriter);
117: response = new WebWorkMockHttpServletResponse();
118: response.setWriter(writer);
119: servletContext = new WebWorkMockServletContext();
120: stack = new OgnlValueStack();
121: context = new ActionContext(stack.getContext());
122: context.put(WebWorkStatics.HTTP_RESPONSE, response);
123: context.put(WebWorkStatics.SERVLET_CONTEXT, servletContext);
124: invocation = new MockActionInvocation();
125: invocation.setStack(stack);
126: invocation.setInvocationContext(context);
127: }
128:
129: protected void tearDown() throws Exception {
130: stack = null;
131: invocation = null;
132: context = null;
133: response = null;
134: writer = null;
135: stringWriter = null;
136: servletContext = null;
137:
138: super.tearDown();
139: }
140: }
|