1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.dispatcher;
23
24 import java.io.InputStream;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27
28 import junit.framework.TestCase;
29
30 import org.apache.struts2.StrutsStatics;
31 import org.apache.struts2.StrutsTestCase;
32 import org.apache.struts2.views.jsp.AbstractUITagTest;
33 import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
34 import org.apache.struts2.views.jsp.StrutsMockServletContext;
35
36 import com.opensymphony.xwork2.util.ClassLoaderUtil;
37 import com.opensymphony.xwork2.util.ValueStackFactory;
38 import com.opensymphony.xwork2.ActionContext;
39 import com.opensymphony.xwork2.mock.MockActionInvocation;
40 import com.opensymphony.xwork2.util.ValueStack;
41
42 /***
43 * Test case for PlainTextResult.
44 *
45 */
46 public class PlainTextResultTest extends StrutsTestCase {
47
48 ValueStack stack;
49 MockActionInvocation invocation;
50 ActionContext context;
51 StrutsMockHttpServletResponse response;
52 PrintWriter writer;
53 StringWriter stringWriter;
54 StrutsMockServletContext servletContext;
55
56
57 public void testPlainText() throws Exception {
58 PlainTextResult result = new PlainTextResult();
59 result.setLocation("/someJspFile.jsp");
60
61 response.setExpectedContentType("text/plain");
62 response.setExpectedHeader("Content-Disposition", "inline");
63 InputStream jspResourceInputStream =
64 ClassLoaderUtil.getResourceAsStream(
65 "org/apache/struts2/dispatcher/someJspFile.jsp",
66 PlainTextResultTest.class);
67
68
69 try {
70 servletContext.setResourceAsStream(jspResourceInputStream);
71 result.execute(invocation);
72
73 String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
74 String e = AbstractUITagTest.normalize(
75 readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
76 assertEquals(r, e);
77 }
78 finally {
79 jspResourceInputStream.close();
80 }
81 }
82
83 public void testPlainTextWithEncoding() throws Exception {
84 PlainTextResult result = new PlainTextResult();
85 result.setLocation("/someJspFile.jsp");
86 result.setCharSet("UTF-8");
87
88 response.setExpectedContentType("text/plain; charset=UTF-8");
89 response.setExpectedHeader("Content-Disposition", "inline");
90 InputStream jspResourceInputStream =
91 ClassLoaderUtil.getResourceAsStream(
92 "org/apache/struts2/dispatcher/someJspFile.jsp",
93 PlainTextResultTest.class);
94
95
96 try {
97 servletContext.setResourceAsStream(jspResourceInputStream);
98 result.execute(invocation);
99
100 String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
101 String e = AbstractUITagTest.normalize(
102 readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
103 assertEquals(r, e);
104 }
105 finally {
106 jspResourceInputStream.close();
107 }
108 }
109
110 protected String readAsString(String resource) throws Exception {
111 InputStream is = null;
112 try {
113 is = ClassLoaderUtil.getResourceAsStream(resource, 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 }
122 finally {
123 if (is != null)
124 is.close();
125 }
126
127 }
128
129
130 protected void setUp() throws Exception {
131 super.setUp();
132
133 stringWriter = new StringWriter();
134 writer = new PrintWriter(stringWriter);
135 response = new StrutsMockHttpServletResponse();
136 response.setWriter(writer);
137 servletContext = new StrutsMockServletContext();
138 stack = ActionContext.getContext().getValueStack();
139 context = new ActionContext(stack.getContext());
140 context.put(StrutsStatics.HTTP_RESPONSE, response);
141 context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
142 invocation = new MockActionInvocation();
143 invocation.setStack(stack);
144 invocation.setInvocationContext(context);
145 }
146
147
148 protected void tearDown() throws Exception {
149 super.tearDown();
150 stack = null;
151 invocation = null;
152 context = null;
153 response = null;
154 writer = null;
155 stringWriter = null;
156 servletContext = null;
157
158 super.tearDown();
159 }
160 }