View Javadoc

1   /*
2    * $Id: StreamResultTest.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.InputStream;
23  import java.net.URI;
24  import java.net.URL;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.struts2.ServletActionContext;
29  import org.springframework.mock.web.MockHttpServletResponse;
30  
31  import com.opensymphony.xwork2.util.ClassLoaderUtil;
32  import com.opensymphony.xwork2.util.ValueStackFactory;
33  import com.opensymphony.xwork2.Action;
34  import com.opensymphony.xwork2.ActionContext;
35  import com.opensymphony.xwork2.mock.MockActionInvocation;
36  import com.opensymphony.xwork2.util.ValueStack;
37  
38  /***
39   * Unit test for {@link StreamResult}.
40   *
41   */
42  public class StreamResultTest extends TestCase {
43  
44      private StreamResult result;
45      private MockHttpServletResponse response;
46  
47      private MockActionInvocation mai;
48      private ValueStack stack;
49      private int contentLength = 0;
50  
51      public void testStreamResultNoInputName() throws Exception {
52          result.setParse(false);
53          result.setInputName(null);
54  
55          try {
56              result.doExecute("helloworld", mai);
57              fail("Should have thrown an IllegalArgumentException");
58          } catch (IllegalArgumentException e) {
59              // success
60          }
61      }
62  
63      public void testStreamResultParseNoInputName() throws Exception {
64          result.setParse(true);
65          result.setInputName("${top}");
66  
67          try {
68              result.doExecute("helloworld", mai);
69              fail("Should have thrown an IllegalArgumentException");
70          } catch (IllegalArgumentException e) {
71              // success
72          }
73      }
74  
75      public void testStreamResultDefault() throws Exception {
76          result.setInputName("streamForImage");
77  
78          result.doExecute("helloworld", mai);
79  
80          assertEquals(null, result.getContentLength());
81          assertEquals("text/plain", result.getContentType());
82          assertEquals("streamForImage", result.getInputName());
83          assertEquals(1024, result.getBufferSize()); // 1024 is default
84          assertEquals("inline", result.getContentDisposition());
85  
86          assertEquals("text/plain", response.getContentType());
87          assertEquals(0, response.getContentLength());
88          assertEquals("inline", response.getHeader("Content-disposition"));
89      }
90  
91      public void testStreamResultNoDefault() throws Exception {
92          // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
93          result.setParse(false);
94          result.setInputName("streamForImage");
95          result.setBufferSize(128);
96          result.setContentLength(String.valueOf(contentLength));
97          result.setContentDisposition("filename=\"logo.png\"");
98          result.setContentType("image/jpeg");
99  
100         result.doExecute("helloworld", mai);
101 
102         assertEquals(String.valueOf(contentLength), result.getContentLength());
103         assertEquals("image/jpeg", result.getContentType());
104         assertEquals("streamForImage", result.getInputName());
105         assertEquals(128, result.getBufferSize());
106         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
107 
108         assertEquals("image/jpeg", response.getContentType());
109         assertEquals(contentLength, response.getContentLength());
110         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
111     }
112 
113     public void testStreamResultParse1() throws Exception {
114     	///////////////////
115         result.setParse(true);
116         // ${...} conditionalParse of Result, returns String, 
117         // which gets evaluated to the stack, that's how it works.
118         // We use ${streamForImageAsString} that returns "streamForImage"
119         // which is a property that returns an InputStream object.
120         result.setInputName("${streamForImageAsString}");
121         result.setBufferSize(128);
122         result.setContentLength(String.valueOf(contentLength));
123         result.setContentDisposition("filename=\"logo.png\"");
124         result.setContentType("image/jpeg");
125 
126         result.doExecute("helloworld", mai);
127 
128         assertEquals(String.valueOf(contentLength), result.getContentLength());
129         assertEquals("image/jpeg", result.getContentType());
130         assertEquals("${streamForImageAsString}", result.getInputName());
131         assertEquals(128, result.getBufferSize());
132         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
133 
134         assertEquals("image/jpeg", response.getContentType());
135         assertEquals(contentLength, response.getContentLength());
136         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
137     }
138     
139     public void testStreamResultParse2() throws Exception {
140     	///////////////////
141         result.setParse(true);
142         // This time we dun use ${...}, so streamForImage will
143         // be evaluated to the stack, which should reaturn an
144         // InputStream object, cause there's such a property in 
145         // the action object itself.
146         result.setInputName("streamForImage");
147         result.setBufferSize(128);
148         result.setContentLength(String.valueOf(contentLength));
149         result.setContentDisposition("filename=\"logo.png\"");
150         result.setContentType("image/jpeg");
151 
152         result.doExecute("helloworld", mai);
153 
154         assertEquals(String.valueOf(contentLength), result.getContentLength());
155         assertEquals("image/jpeg", result.getContentType());
156         assertEquals("streamForImage", result.getInputName());
157         assertEquals(128, result.getBufferSize());
158         assertEquals("filename=\"logo.png\"", result.getContentDisposition());
159 
160         assertEquals("image/jpeg", response.getContentType());
161         assertEquals(contentLength, response.getContentLength());
162         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
163     }
164 
165     protected void setUp() throws Exception {
166         response = new MockHttpServletResponse();
167 
168         result = new StreamResult();
169         stack = ValueStackFactory.getFactory().createValueStack();
170         ActionContext.getContext().setValueStack(stack);
171 
172         MyImageAction action = new MyImageAction();
173         contentLength = (int) action.getContentLength();
174 
175         mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
176         mai.setAction(action);
177         mai.setStack(stack);
178         mai.setInvocationContext(ActionContext.getContext());
179         stack.push(action);
180 
181         ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
182     }
183     
184     
185 
186     protected void tearDown() {
187         response = null;
188         result = null;
189         stack = null;
190         contentLength = 0;
191         mai = null;
192     }
193 
194     public class MyImageAction implements Action {
195 
196         public InputStream getStreamForImage() throws Exception {
197             // just use src/test/log4j.properties as test file 
198             URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
199             File file = new File(new URI(url.toString()));
200             FileInputStream fis = new FileInputStream(file);
201             return fis;
202         }
203 
204         public String execute() throws Exception {
205             return SUCCESS;
206         }
207 
208         public long getContentLength() throws Exception {
209             URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
210             File file = new File(new URI(url.toString()));
211             return file.length();
212         }
213         
214         public String getStreamForImageAsString() {
215         	return "streamForImage";
216         }
217     }
218 
219 }