View Javadoc

1   /*
2    * $Id: StreamResultTest.java 817363 2009-09-21 18:32:16Z wesw $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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              // success
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              // success
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()); // 1024 is default
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 testStreamResultWithCharSet() throws Exception {
97          result.setInputName("streamForImage");
98          result.setContentCharSet("ISO-8859-1");
99          result.doExecute("helloworld", mai);
100 
101         assertEquals(String.valueOf(contentLength), result.getContentLength());
102         assertEquals("text/plain", result.getContentType());
103         assertEquals("streamForImage", result.getInputName());
104         assertEquals(1024, result.getBufferSize()); // 1024 is default
105         assertEquals("inline", result.getContentDisposition());
106         assertEquals("text/plain;charset=ISO-8859-1", response.getContentType());
107         assertEquals(contentLength, response.getContentLength());
108         assertEquals("inline", response.getHeader("Content-disposition"));
109     }
110 
111     public void testStreamResultWithCharSet2() throws Exception {
112         result.setParse(true);
113         result.setInputName("streamForImage");
114         result.setContentCharSet("${contentCharSetMethod}");
115 
116         result.doExecute("helloworld", mai);
117 
118         assertEquals(String.valueOf(contentLength), result.getContentLength());
119         assertEquals("text/plain", result.getContentType());
120         assertEquals("streamForImage", result.getInputName());
121         assertEquals(1024, result.getBufferSize()); // 1024 is default
122         assertEquals("inline", result.getContentDisposition());
123         assertEquals("text/plain;charset=UTF-8", response.getContentType());
124         assertEquals(contentLength, response.getContentLength());
125         assertEquals("inline", response.getHeader("Content-disposition"));
126     }
127 
128     public void testAllowCacheDefault() throws Exception {
129         result.setInputName("streamForImage");
130 
131         result.doExecute("helloworld", mai);
132 
133         //check that that headers are not set by default        
134         assertNull(response.getHeader("Pragma"));
135         assertNull(response.getHeader("Cache-Control"));
136     }
137 
138      public void testAllowCacheFalse() throws Exception {
139         result.setInputName("streamForImage");
140         result.setAllowCaching(false);
141         result.doExecute("helloworld", mai);
142 
143         //check that that headers are not set by default
144         assertEquals("no-cache", response.getHeader("Pragma"));
145         assertEquals("no-cache", response.getHeader("Cache-Control"));
146     }
147 
148     public void testStreamResultNoDefault() throws Exception {
149         // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
150         result.setParse(false);
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     public void testStreamResultParse1() throws Exception {
171         ///////////////////
172         result.setParse(true);
173         // ${...} conditionalParse of Result, returns String,
174         // which gets evaluated to the stack, that's how it works.
175         // We use ${streamForImageAsString} that returns "streamForImage"
176         // which is a property that returns an InputStream object.
177         result.setInputName("${streamForImageAsString}");
178         result.setBufferSize(128);
179         result.setContentLength(String.valueOf(contentLength));
180         result.setContentDisposition("filename=\"logo.png\"");
181         result.setContentType("image/jpeg");
182 
183         result.doExecute("helloworld", mai);
184 
185         assertEquals(String.valueOf(contentLength), result.getContentLength());
186         assertEquals("image/jpeg", result.getContentType());
187         assertEquals("${streamForImageAsString}", result.getInputName());
188         assertEquals(128, result.getBufferSize());
189         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
190 
191         assertEquals("image/jpeg", response.getContentType());
192         assertEquals(contentLength, response.getContentLength());
193         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
194     }
195 
196     public void testStreamResultParse2() throws Exception {
197         ///////////////////
198         result.setParse(true);
199         // This time we dun use ${...}, so streamForImage will
200         // be evaluated to the stack, which should reaturn an
201         // InputStream object, cause there's such a property in
202         // the action object itself.
203         result.setInputName("streamForImage");
204         result.setBufferSize(128);
205         result.setContentLength(String.valueOf(contentLength));
206         result.setContentDisposition("filename=\"logo.png\"");
207         result.setContentType("image/jpeg");
208 
209         result.doExecute("helloworld", mai);
210 
211         assertEquals(String.valueOf(contentLength), result.getContentLength());
212         assertEquals("image/jpeg", result.getContentType());
213         assertEquals("streamForImage", result.getInputName());
214         assertEquals(128, result.getBufferSize());
215         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
216 
217         assertEquals("image/jpeg", response.getContentType());
218         assertEquals(contentLength, response.getContentLength());
219         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
220     }
221 
222     protected void setUp() throws Exception {
223         super.setUp();
224         response = new MockHttpServletResponse();
225 
226         result = new StreamResult();
227         stack = ActionContext.getContext().getValueStack();
228 
229         MyImageAction action = new MyImageAction();
230         contentLength = (int) action.getContentLength();
231 
232         mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
233         mai.setAction(action);
234         mai.setStack(stack);
235         mai.setInvocationContext(ActionContext.getContext());
236         stack.push(action);
237 
238         ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
239     }
240 
241 
242 
243     protected void tearDown() throws Exception {
244         super.tearDown();
245         response = null;
246         result = null;
247         stack = null;
248         contentLength = 0;
249         mai = null;
250     }
251 
252     public class MyImageAction implements Action {
253 
254         public InputStream getStreamForImage() throws Exception {
255             // just use src/test/log4j.properties as test file
256             URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
257             File file = new File(new URI(url.toString()));
258             FileInputStream fis = new FileInputStream(file);
259             return fis;
260         }
261 
262         public String execute() throws Exception {
263             return SUCCESS;
264         }
265 
266         public long getContentLength() throws Exception {
267             URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
268             File file = new File(new URI(url.toString()));
269             return file.length();
270         }
271 
272         public String getStreamForImageAsString() {
273             return "streamForImage";
274         }
275 
276         public String getContentCharSetMethod() {
277             return "UTF-8";
278         }
279     }
280 
281 }