001: /*
002: * $Id: PlainTextResultTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.io.InputStream;
024: import java.io.PrintWriter;
025: import java.io.StringWriter;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.struts2.StrutsStatics;
030: import org.apache.struts2.views.jsp.AbstractUITagTest;
031: import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
032: import org.apache.struts2.views.jsp.StrutsMockServletContext;
033:
034: import com.opensymphony.xwork2.util.ClassLoaderUtil;
035: import com.opensymphony.xwork2.util.ValueStackFactory;
036: import com.opensymphony.xwork2.ActionContext;
037: import com.opensymphony.xwork2.mock.MockActionInvocation;
038: import com.opensymphony.xwork2.util.ValueStack;
039:
040: /**
041: * Test case for PlainTextResult.
042: *
043: */
044: public class PlainTextResultTest extends TestCase {
045:
046: ValueStack stack;
047: MockActionInvocation invocation;
048: ActionContext context;
049: StrutsMockHttpServletResponse response;
050: PrintWriter writer;
051: StringWriter stringWriter;
052: StrutsMockServletContext servletContext;
053:
054: public void testPlainText() throws Exception {
055: PlainTextResult result = new PlainTextResult();
056: result.setLocation("/someJspFile.jsp");
057:
058: response.setExpectedContentType("text/plain");
059: response.setExpectedHeader("Content-Disposition", "inline");
060: InputStream jspResourceInputStream = ClassLoaderUtil
061: .getResourceAsStream(
062: "org/apache/struts2/dispatcher/someJspFile.jsp",
063: PlainTextResultTest.class);
064:
065: try {
066: servletContext.setResourceAsStream(jspResourceInputStream);
067: result.execute(invocation);
068:
069: String r = AbstractUITagTest.normalize(stringWriter
070: .getBuffer().toString(), true);
071: String e = AbstractUITagTest
072: .normalize(
073: readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"),
074: true);
075: assertEquals(r, e);
076: } finally {
077: jspResourceInputStream.close();
078: }
079: }
080:
081: public void testPlainTextWithEncoding() throws Exception {
082: PlainTextResult result = new PlainTextResult();
083: result.setLocation("/someJspFile.jsp");
084: result.setCharSet("UTF-8");
085:
086: response.setExpectedContentType("text/plain; charset=UTF-8");
087: response.setExpectedHeader("Content-Disposition", "inline");
088: InputStream jspResourceInputStream = ClassLoaderUtil
089: .getResourceAsStream(
090: "org/apache/struts2/dispatcher/someJspFile.jsp",
091: PlainTextResultTest.class);
092:
093: try {
094: servletContext.setResourceAsStream(jspResourceInputStream);
095: result.execute(invocation);
096:
097: String r = AbstractUITagTest.normalize(stringWriter
098: .getBuffer().toString(), true);
099: String e = AbstractUITagTest
100: .normalize(
101: readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"),
102: true);
103: assertEquals(r, e);
104: } finally {
105: jspResourceInputStream.close();
106: }
107: }
108:
109: protected String readAsString(String resource) throws Exception {
110: InputStream is = null;
111: try {
112: is = ClassLoaderUtil.getResourceAsStream(resource,
113: PlainTextResultTest.class);
114: int sizeRead = 0;
115: byte[] buffer = new byte[1024];
116: StringBuffer stringBuffer = new StringBuffer();
117: while ((sizeRead = is.read(buffer)) != -1) {
118: stringBuffer.append(new String(buffer, 0, sizeRead));
119: }
120: return stringBuffer.toString();
121: } finally {
122: if (is != null)
123: is.close();
124: }
125:
126: }
127:
128: protected void setUp() throws Exception {
129: super .setUp();
130:
131: stringWriter = new StringWriter();
132: writer = new PrintWriter(stringWriter);
133: response = new StrutsMockHttpServletResponse();
134: response.setWriter(writer);
135: servletContext = new StrutsMockServletContext();
136: stack = ValueStackFactory.getFactory().createValueStack();
137: context = new ActionContext(stack.getContext());
138: context.put(StrutsStatics.HTTP_RESPONSE, response);
139: context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
140: invocation = new MockActionInvocation();
141: invocation.setStack(stack);
142: invocation.setInvocationContext(context);
143: }
144:
145: protected void tearDown() throws Exception {
146: stack = null;
147: invocation = null;
148: context = null;
149: response = null;
150: writer = null;
151: stringWriter = null;
152: servletContext = null;
153:
154: super.tearDown();
155: }
156: }
|