View Javadoc

1   /*
2    * $Id: StreamResultTest.java 722124 2008-12-01 16:47:29Z musachy $
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 testAllowCacheDefault() throws Exception {
97          result.setInputName("streamForImage");
98  
99          result.doExecute("helloworld", mai);
100 
101         //check that that headers are not set by default        
102         assertNull(response.getHeader("Pragma"));
103         assertNull(response.getHeader("Cache-Control"));
104     }
105 
106      public void testAllowCacheFalse() throws Exception {
107         result.setInputName("streamForImage");
108         result.setAllowCaching(false);
109         result.doExecute("helloworld", mai);
110 
111         //check that that headers are not set by default
112         assertEquals("no-cache", response.getHeader("Pragma"));
113         assertEquals("no-cache", response.getHeader("Cache-Control"));
114     }
115 
116     public void testStreamResultNoDefault() throws Exception {
117         // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
118         result.setParse(false);
119         result.setInputName("streamForImage");
120         result.setBufferSize(128);
121         result.setContentLength(String.valueOf(contentLength));
122         result.setContentDisposition("filename=\"logo.png\"");
123         result.setContentType("image/jpeg");
124 
125         result.doExecute("helloworld", mai);
126 
127         assertEquals(String.valueOf(contentLength), result.getContentLength());
128         assertEquals("image/jpeg", result.getContentType());
129         assertEquals("streamForImage", result.getInputName());
130         assertEquals(128, result.getBufferSize());
131         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
132 
133         assertEquals("image/jpeg", response.getContentType());
134         assertEquals(contentLength, response.getContentLength());
135         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
136     }
137 
138     public void testStreamResultParse1() throws Exception {
139         ///////////////////
140         result.setParse(true);
141         // ${...} conditionalParse of Result, returns String,
142         // which gets evaluated to the stack, that's how it works.
143         // We use ${streamForImageAsString} that returns "streamForImage"
144         // which is a property that returns an InputStream object.
145         result.setInputName("${streamForImageAsString}");
146         result.setBufferSize(128);
147         result.setContentLength(String.valueOf(contentLength));
148         result.setContentDisposition("filename=\"logo.png\"");
149         result.setContentType("image/jpeg");
150 
151         result.doExecute("helloworld", mai);
152 
153         assertEquals(String.valueOf(contentLength), result.getContentLength());
154         assertEquals("image/jpeg", result.getContentType());
155         assertEquals("${streamForImageAsString}", result.getInputName());
156         assertEquals(128, result.getBufferSize());
157         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
158 
159         assertEquals("image/jpeg", response.getContentType());
160         assertEquals(contentLength, response.getContentLength());
161         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
162     }
163 
164     public void testStreamResultParse2() throws Exception {
165         ///////////////////
166         result.setParse(true);
167         // This time we dun use ${...}, so streamForImage will
168         // be evaluated to the stack, which should reaturn an
169         // InputStream object, cause there's such a property in
170         // the action object itself.
171         result.setInputName("streamForImage");
172         result.setBufferSize(128);
173         result.setContentLength(String.valueOf(contentLength));
174         result.setContentDisposition("filename=\"logo.png\"");
175         result.setContentType("image/jpeg");
176 
177         result.doExecute("helloworld", mai);
178 
179         assertEquals(String.valueOf(contentLength), result.getContentLength());
180         assertEquals("image/jpeg", result.getContentType());
181         assertEquals("streamForImage", result.getInputName());
182         assertEquals(128, result.getBufferSize());
183         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
184 
185         assertEquals("image/jpeg", response.getContentType());
186         assertEquals(contentLength, response.getContentLength());
187         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
188     }
189 
190     protected void setUp() throws Exception {
191         super.setUp();
192         response = new MockHttpServletResponse();
193 
194         result = new StreamResult();
195         stack = ActionContext.getContext().getValueStack();
196 
197         MyImageAction action = new MyImageAction();
198         contentLength = (int) action.getContentLength();
199 
200         mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
201         mai.setAction(action);
202         mai.setStack(stack);
203         mai.setInvocationContext(ActionContext.getContext());
204         stack.push(action);
205 
206         ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
207     }
208 
209 
210 
211     protected void tearDown() throws Exception {
212         super.tearDown();
213         response = null;
214         result = null;
215         stack = null;
216         contentLength = 0;
217         mai = null;
218     }
219 
220     public class MyImageAction implements Action {
221 
222         public InputStream getStreamForImage() throws Exception {
223             // just use src/test/log4j.properties as test file
224             URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
225             File file = new File(new URI(url.toString()));
226             FileInputStream fis = new FileInputStream(file);
227             return fis;
228         }
229 
230         public String execute() throws Exception {
231             return SUCCESS;
232         }
233 
234         public long getContentLength() throws Exception {
235             URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
236             File file = new File(new URI(url.toString()));
237             return file.length();
238         }
239 
240         public String getStreamForImageAsString() {
241             return "streamForImage";
242         }
243     }
244 
245 }