001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import com.opensymphony.util.ClassLoaderUtil;
008: import com.opensymphony.webwork.ServletActionContext;
009: import com.opensymphony.xwork.Action;
010: import com.opensymphony.xwork.ActionContext;
011: import com.opensymphony.xwork.mock.MockActionInvocation;
012: import com.opensymphony.xwork.util.OgnlValueStack;
013: import junit.framework.TestCase;
014: import org.springframework.mock.web.MockHttpServletResponse;
015:
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.InputStream;
019: import java.net.URI;
020: import java.net.URL;
021:
022: /**
023: * Unit test for {@link StreamResult}.
024: *
025: * @author Claus Ibsen
026: * @author tm_jee
027: */
028: public class StreamResultTest extends TestCase {
029:
030: private StreamResult result;
031: private MockHttpServletResponse response;
032:
033: private MockActionInvocation mai;
034: private OgnlValueStack stack;
035: private int contentLength = 0;
036:
037: public void testStreamResultNoInputName() throws Exception {
038: result.setParse(false);
039: result.setInputName(null);
040:
041: try {
042: result.doExecute("helloworld", mai);
043: fail("Should have thrown an IllegalArgumentException");
044: } catch (IllegalArgumentException e) {
045: // success
046: }
047: }
048:
049: public void testStreamResultParseNoInputName() throws Exception {
050: result.setParse(true);
051: result.setInputName("${top}");
052:
053: try {
054: result.doExecute("helloworld", mai);
055: fail("Should have thrown an IllegalArgumentException");
056: } catch (IllegalArgumentException e) {
057: // success
058: }
059: }
060:
061: public void testStreamResultDefault() throws Exception {
062: result.setInputName("streamForImage");
063:
064: result.doExecute("helloworld", mai);
065:
066: assertEquals(null, result.getContentLength());
067: assertEquals("text/plain", result.getContentType());
068: assertEquals("streamForImage", result.getInputName());
069: assertEquals(1024, result.getBufferSize()); // 1024 is default
070: assertEquals("inline", result.getContentDisposition());
071:
072: assertEquals("text/plain", response.getContentType());
073: assertEquals(0, response.getContentLength());
074: assertEquals("inline", response
075: .getHeader("Content-disposition"));
076: }
077:
078: public void testStreamResultNoDefault() throws Exception {
079: // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
080: result.setParse(false);
081: result.setInputName("streamForImage");
082: result.setBufferSize(128);
083: result.setContentLength(String.valueOf(contentLength));
084: result.setContentDisposition("filename=\"logo.png\"");
085: result.setContentType("image/jpeg");
086:
087: result.doExecute("helloworld", mai);
088:
089: assertEquals(String.valueOf(contentLength), result
090: .getContentLength());
091: assertEquals("image/jpeg", result.getContentType());
092: assertEquals("streamForImage", result.getInputName());
093: assertEquals(128, result.getBufferSize());
094: assertEquals("filename=\"logo.png\"", result
095: .getContentDisposition());
096:
097: assertEquals("image/jpeg", response.getContentType());
098: assertEquals(contentLength, response.getContentLength());
099: assertEquals("filename=\"logo.png\"", response
100: .getHeader("Content-disposition"));
101: }
102:
103: public void testStreamResultParse1() throws Exception {
104: ///////////////////
105: result.setParse(true);
106: // ${...} conditionalParse of Result, returns String,
107: // which gets evaluated to the stack, that's how it works.
108: // We use ${streamForImageAsString} that returns "streamForImage"
109: // which is a property that returns an InputStream object.
110: result.setInputName("${streamForImageAsString}");
111: result.setBufferSize(128);
112: result.setContentLength(String.valueOf(contentLength));
113: result.setContentDisposition("filename=\"logo.png\"");
114: result.setContentType("image/jpeg");
115:
116: result.doExecute("helloworld", mai);
117:
118: assertEquals(String.valueOf(contentLength), result
119: .getContentLength());
120: assertEquals("image/jpeg", result.getContentType());
121: assertEquals("${streamForImageAsString}", result.getInputName());
122: assertEquals(128, result.getBufferSize());
123: assertEquals("filename=\"logo.png\"", result
124: .getContentDisposition());
125:
126: assertEquals("image/jpeg", response.getContentType());
127: assertEquals(contentLength, response.getContentLength());
128: assertEquals("filename=\"logo.png\"", response
129: .getHeader("Content-disposition"));
130: }
131:
132: public void testStreamResultParse2() throws Exception {
133: ///////////////////
134: result.setParse(true);
135: // This time we dun use ${...}, so streamForImage will
136: // be evaluated to the stack, which should reaturn an
137: // InputStream object, cause there's such a property in
138: // the action object itself.
139: result.setInputName("streamForImage");
140: result.setBufferSize(128);
141: result.setContentLength(String.valueOf(contentLength));
142: result.setContentDisposition("filename=\"logo.png\"");
143: result.setContentType("image/jpeg");
144:
145: result.doExecute("helloworld", mai);
146:
147: assertEquals(String.valueOf(contentLength), result
148: .getContentLength());
149: assertEquals("image/jpeg", result.getContentType());
150: assertEquals("streamForImage", result.getInputName());
151: assertEquals(128, result.getBufferSize());
152: assertEquals("filename=\"logo.png\"", result
153: .getContentDisposition());
154:
155: assertEquals("image/jpeg", response.getContentType());
156: assertEquals(contentLength, response.getContentLength());
157: assertEquals("filename=\"logo.png\"", response
158: .getHeader("Content-disposition"));
159: }
160:
161: protected void setUp() throws Exception {
162: response = new MockHttpServletResponse();
163:
164: result = new StreamResult();
165: stack = new OgnlValueStack();
166: ActionContext.getContext().setValueStack(stack);
167:
168: MyImageAction action = new MyImageAction();
169: contentLength = (int) action.getContentLength();
170:
171: mai = new com.opensymphony.xwork.mock.MockActionInvocation();
172: mai.setAction(action);
173: mai.setStack(stack);
174: mai.setInvocationContext(ActionContext.getContext());
175: stack.push(action);
176:
177: ActionContext.getContext().put(
178: ServletActionContext.HTTP_RESPONSE, response);
179: }
180:
181: protected void tearDown() {
182: response = null;
183: result = null;
184: stack = null;
185: contentLength = 0;
186: mai = null;
187: }
188:
189: public class MyImageAction implements Action {
190:
191: public InputStream getStreamForImage() throws Exception {
192: // just use src/test/log4j.properties as test file
193: URL url = ClassLoaderUtil.getResource("log4j.properties",
194: StreamResultTest.class);
195: File file = new File(new URI(url.toString()));
196: FileInputStream fis = new FileInputStream(file);
197: return fis;
198: }
199:
200: public String execute() throws Exception {
201: return SUCCESS;
202: }
203:
204: public long getContentLength() throws Exception {
205: URL url = ClassLoaderUtil.getResource("log4j.properties",
206: StreamResultTest.class);
207: File file = new File(new URI(url.toString()));
208: return file.length();
209: }
210:
211: public String getStreamForImageAsString() {
212: return "streamForImage";
213: }
214: }
215:
216: }
|