View Javadoc

1   /*
2    * $Id: StreamResultTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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 testStreamResultNoDefault() throws Exception {
97          // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
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         // ${...} conditionalParse of Result, returns String,
122         // which gets evaluated to the stack, that's how it works.
123         // We use ${streamForImageAsString} that returns "streamForImage"
124         // which is a property that returns an InputStream object.
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         // This time we dun use ${...}, so streamForImage will
148         // be evaluated to the stack, which should reaturn an
149         // InputStream object, cause there's such a property in
150         // the action object itself.
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             // just use src/test/log4j.properties as test file
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 }