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.File;
25 import java.io.FileInputStream;
26 import java.io.InputStream;
27 import java.net.URI;
28 import java.net.URL;
29
30 import junit.framework.TestCase;
31
32 import org.apache.struts2.ServletActionContext;
33 import org.apache.struts2.StrutsTestCase;
34 import org.springframework.mock.web.MockHttpServletResponse;
35
36 import com.opensymphony.xwork2.util.ClassLoaderUtil;
37 import com.opensymphony.xwork2.util.ValueStackFactory;
38 import com.opensymphony.xwork2.Action;
39 import com.opensymphony.xwork2.ActionContext;
40 import com.opensymphony.xwork2.mock.MockActionInvocation;
41 import com.opensymphony.xwork2.util.ValueStack;
42
43 /***
44 * Unit test for {@link StreamResult}.
45 *
46 */
47 public class StreamResultTest extends StrutsTestCase {
48
49 private StreamResult result;
50 private MockHttpServletResponse response;
51
52 private MockActionInvocation mai;
53 private ValueStack stack;
54 private int contentLength = 0;
55
56 public void testStreamResultNoInputName() throws Exception {
57 result.setParse(false);
58 result.setInputName(null);
59
60 try {
61 result.doExecute("helloworld", mai);
62 fail("Should have thrown an IllegalArgumentException");
63 } catch (IllegalArgumentException e) {
64
65 }
66 }
67
68 public void testStreamResultParseNoInputName() throws Exception {
69 result.setParse(true);
70 result.setInputName("${top}");
71
72 try {
73 result.doExecute("helloworld", mai);
74 fail("Should have thrown an IllegalArgumentException");
75 } catch (IllegalArgumentException e) {
76
77 }
78 }
79
80 public void testStreamResultDefault() throws Exception {
81 result.setInputName("streamForImage");
82
83 result.doExecute("helloworld", mai);
84
85 assertEquals(String.valueOf(contentLength), result.getContentLength());
86 assertEquals("text/plain", result.getContentType());
87 assertEquals("streamForImage", result.getInputName());
88 assertEquals(1024, result.getBufferSize());
89 assertEquals("inline", result.getContentDisposition());
90
91 assertEquals("text/plain", response.getContentType());
92 assertEquals(contentLength, response.getContentLength());
93 assertEquals("inline", response.getHeader("Content-disposition"));
94 }
95
96 public void testStreamResultNoDefault() throws Exception {
97
98 result.setParse(false);
99 result.setInputName("streamForImage");
100 result.setBufferSize(128);
101 result.setContentLength(String.valueOf(contentLength));
102 result.setContentDisposition("filename=\"logo.png\"");
103 result.setContentType("image/jpeg");
104
105 result.doExecute("helloworld", mai);
106
107 assertEquals(String.valueOf(contentLength), result.getContentLength());
108 assertEquals("image/jpeg", result.getContentType());
109 assertEquals("streamForImage", result.getInputName());
110 assertEquals(128, result.getBufferSize());
111 assertEquals("filename=\"logo.png\"", result.getContentDisposition());
112
113 assertEquals("image/jpeg", response.getContentType());
114 assertEquals(contentLength, response.getContentLength());
115 assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
116 }
117
118 public void testStreamResultParse1() throws Exception {
119
120 result.setParse(true);
121
122
123
124
125 result.setInputName("${streamForImageAsString}");
126 result.setBufferSize(128);
127 result.setContentLength(String.valueOf(contentLength));
128 result.setContentDisposition("filename=\"logo.png\"");
129 result.setContentType("image/jpeg");
130
131 result.doExecute("helloworld", mai);
132
133 assertEquals(String.valueOf(contentLength), result.getContentLength());
134 assertEquals("image/jpeg", result.getContentType());
135 assertEquals("${streamForImageAsString}", result.getInputName());
136 assertEquals(128, result.getBufferSize());
137 assertEquals("filename=\"logo.png\"", result.getContentDisposition());
138
139 assertEquals("image/jpeg", response.getContentType());
140 assertEquals(contentLength, response.getContentLength());
141 assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
142 }
143
144 public void testStreamResultParse2() throws Exception {
145
146 result.setParse(true);
147
148
149
150
151 result.setInputName("streamForImage");
152 result.setBufferSize(128);
153 result.setContentLength(String.valueOf(contentLength));
154 result.setContentDisposition("filename=\"logo.png\"");
155 result.setContentType("image/jpeg");
156
157 result.doExecute("helloworld", mai);
158
159 assertEquals(String.valueOf(contentLength), result.getContentLength());
160 assertEquals("image/jpeg", result.getContentType());
161 assertEquals("streamForImage", result.getInputName());
162 assertEquals(128, result.getBufferSize());
163 assertEquals("filename=\"logo.png\"", result.getContentDisposition());
164
165 assertEquals("image/jpeg", response.getContentType());
166 assertEquals(contentLength, response.getContentLength());
167 assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
168 }
169
170 protected void setUp() throws Exception {
171 super.setUp();
172 response = new MockHttpServletResponse();
173
174 result = new StreamResult();
175 stack = ActionContext.getContext().getValueStack();
176
177 MyImageAction action = new MyImageAction();
178 contentLength = (int) action.getContentLength();
179
180 mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
181 mai.setAction(action);
182 mai.setStack(stack);
183 mai.setInvocationContext(ActionContext.getContext());
184 stack.push(action);
185
186 ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
187 }
188
189
190
191 protected void tearDown() throws Exception {
192 super.tearDown();
193 response = null;
194 result = null;
195 stack = null;
196 contentLength = 0;
197 mai = null;
198 }
199
200 public class MyImageAction implements Action {
201
202 public InputStream getStreamForImage() throws Exception {
203
204 URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
205 File file = new File(new URI(url.toString()));
206 FileInputStream fis = new FileInputStream(file);
207 return fis;
208 }
209
210 public String execute() throws Exception {
211 return SUCCESS;
212 }
213
214 public long getContentLength() throws Exception {
215 URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
216 File file = new File(new URI(url.toString()));
217 return file.length();
218 }
219
220 public String getStreamForImageAsString() {
221 return "streamForImage";
222 }
223 }
224
225 }