001: /*
002: * $Id: StreamResultTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.InputStream;
026: import java.net.URI;
027: import java.net.URL;
028:
029: import junit.framework.TestCase;
030:
031: import org.apache.struts2.ServletActionContext;
032: import org.springframework.mock.web.MockHttpServletResponse;
033:
034: import com.opensymphony.xwork2.util.ClassLoaderUtil;
035: import com.opensymphony.xwork2.util.ValueStackFactory;
036: import com.opensymphony.xwork2.Action;
037: import com.opensymphony.xwork2.ActionContext;
038: import com.opensymphony.xwork2.mock.MockActionInvocation;
039: import com.opensymphony.xwork2.util.ValueStack;
040:
041: /**
042: * Unit test for {@link StreamResult}.
043: *
044: */
045: public class StreamResultTest extends TestCase {
046:
047: private StreamResult result;
048: private MockHttpServletResponse response;
049:
050: private MockActionInvocation mai;
051: private ValueStack stack;
052: private int contentLength = 0;
053:
054: public void testStreamResultNoInputName() throws Exception {
055: result.setParse(false);
056: result.setInputName(null);
057:
058: try {
059: result.doExecute("helloworld", mai);
060: fail("Should have thrown an IllegalArgumentException");
061: } catch (IllegalArgumentException e) {
062: // success
063: }
064: }
065:
066: public void testStreamResultParseNoInputName() throws Exception {
067: result.setParse(true);
068: result.setInputName("${top}");
069:
070: try {
071: result.doExecute("helloworld", mai);
072: fail("Should have thrown an IllegalArgumentException");
073: } catch (IllegalArgumentException e) {
074: // success
075: }
076: }
077:
078: public void testStreamResultDefault() throws Exception {
079: result.setInputName("streamForImage");
080:
081: result.doExecute("helloworld", mai);
082:
083: assertEquals(null, result.getContentLength());
084: assertEquals("text/plain", result.getContentType());
085: assertEquals("streamForImage", result.getInputName());
086: assertEquals(1024, result.getBufferSize()); // 1024 is default
087: assertEquals("inline", result.getContentDisposition());
088:
089: assertEquals("text/plain", response.getContentType());
090: assertEquals(0, response.getContentLength());
091: assertEquals("inline", response
092: .getHeader("Content-disposition"));
093: }
094:
095: public void testStreamResultNoDefault() throws Exception {
096: // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
097: result.setParse(false);
098: result.setInputName("streamForImage");
099: result.setBufferSize(128);
100: result.setContentLength(String.valueOf(contentLength));
101: result.setContentDisposition("filename=\"logo.png\"");
102: result.setContentType("image/jpeg");
103:
104: result.doExecute("helloworld", mai);
105:
106: assertEquals(String.valueOf(contentLength), result
107: .getContentLength());
108: assertEquals("image/jpeg", result.getContentType());
109: assertEquals("streamForImage", result.getInputName());
110: assertEquals(128, result.getBufferSize());
111: assertEquals("filename=\"logo.png\"", result
112: .getContentDisposition());
113:
114: assertEquals("image/jpeg", response.getContentType());
115: assertEquals(contentLength, response.getContentLength());
116: assertEquals("filename=\"logo.png\"", response
117: .getHeader("Content-disposition"));
118: }
119:
120: public void testStreamResultParse1() throws Exception {
121: ///////////////////
122: result.setParse(true);
123: // ${...} conditionalParse of Result, returns String,
124: // which gets evaluated to the stack, that's how it works.
125: // We use ${streamForImageAsString} that returns "streamForImage"
126: // which is a property that returns an InputStream object.
127: result.setInputName("${streamForImageAsString}");
128: result.setBufferSize(128);
129: result.setContentLength(String.valueOf(contentLength));
130: result.setContentDisposition("filename=\"logo.png\"");
131: result.setContentType("image/jpeg");
132:
133: result.doExecute("helloworld", mai);
134:
135: assertEquals(String.valueOf(contentLength), result
136: .getContentLength());
137: assertEquals("image/jpeg", result.getContentType());
138: assertEquals("${streamForImageAsString}", result.getInputName());
139: assertEquals(128, result.getBufferSize());
140: assertEquals("filename=\"logo.png\"", result
141: .getContentDisposition());
142:
143: assertEquals("image/jpeg", response.getContentType());
144: assertEquals(contentLength, response.getContentLength());
145: assertEquals("filename=\"logo.png\"", response
146: .getHeader("Content-disposition"));
147: }
148:
149: public void testStreamResultParse2() throws Exception {
150: ///////////////////
151: result.setParse(true);
152: // This time we dun use ${...}, so streamForImage will
153: // be evaluated to the stack, which should reaturn an
154: // InputStream object, cause there's such a property in
155: // the action object itself.
156: result.setInputName("streamForImage");
157: result.setBufferSize(128);
158: result.setContentLength(String.valueOf(contentLength));
159: result.setContentDisposition("filename=\"logo.png\"");
160: result.setContentType("image/jpeg");
161:
162: result.doExecute("helloworld", mai);
163:
164: assertEquals(String.valueOf(contentLength), result
165: .getContentLength());
166: assertEquals("image/jpeg", result.getContentType());
167: assertEquals("streamForImage", result.getInputName());
168: assertEquals(128, result.getBufferSize());
169: assertEquals("filename=\"logo.png\"", result
170: .getContentDisposition());
171:
172: assertEquals("image/jpeg", response.getContentType());
173: assertEquals(contentLength, response.getContentLength());
174: assertEquals("filename=\"logo.png\"", response
175: .getHeader("Content-disposition"));
176: }
177:
178: protected void setUp() throws Exception {
179: response = new MockHttpServletResponse();
180:
181: result = new StreamResult();
182: stack = ValueStackFactory.getFactory().createValueStack();
183: ActionContext.getContext().setValueStack(stack);
184:
185: MyImageAction action = new MyImageAction();
186: contentLength = (int) action.getContentLength();
187:
188: mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
189: mai.setAction(action);
190: mai.setStack(stack);
191: mai.setInvocationContext(ActionContext.getContext());
192: stack.push(action);
193:
194: ActionContext.getContext().put(
195: ServletActionContext.HTTP_RESPONSE, response);
196: }
197:
198: protected void tearDown() {
199: response = null;
200: result = null;
201: stack = null;
202: contentLength = 0;
203: mai = null;
204: }
205:
206: public class MyImageAction implements Action {
207:
208: public InputStream getStreamForImage() throws Exception {
209: // just use src/test/log4j.properties as test file
210: URL url = ClassLoaderUtil.getResource("log4j.properties",
211: StreamResultTest.class);
212: File file = new File(new URI(url.toString()));
213: FileInputStream fis = new FileInputStream(file);
214: return fis;
215: }
216:
217: public String execute() throws Exception {
218: return SUCCESS;
219: }
220:
221: public long getContentLength() throws Exception {
222: URL url = ClassLoaderUtil.getResource("log4j.properties",
223: StreamResultTest.class);
224: File file = new File(new URI(url.toString()));
225: return file.length();
226: }
227:
228: public String getStreamForImageAsString() {
229: return "streamForImage";
230: }
231: }
232:
233: }
|