/src/test/com/opensymphony/webwork/dispatcher/StreamResultTest.java

https://bitbucket.org/rainboyan/webwork · Java · 203 lines · 142 code · 37 blank · 24 comment · 0 complexity · 77f551b6ce8c572f6f60e5fb30f8ceca MD5 · raw file

  1. /*
  2. * Copyright (c) 2002-2006 by OpenSymphony
  3. * All rights reserved.
  4. */
  5. package com.opensymphony.webwork.dispatcher;
  6. import com.opensymphony.util.ClassLoaderUtil;
  7. import com.opensymphony.webwork.ServletActionContext;
  8. import com.opensymphony.xwork.Action;
  9. import com.opensymphony.xwork.ActionContext;
  10. import com.opensymphony.xwork.mock.MockActionInvocation;
  11. import com.opensymphony.xwork.util.OgnlValueStack;
  12. import junit.framework.TestCase;
  13. import org.springframework.mock.web.MockHttpServletResponse;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.InputStream;
  17. import java.net.URI;
  18. import java.net.URL;
  19. /**
  20. * Unit test for {@link StreamResult}.
  21. *
  22. * @author Claus Ibsen
  23. * @author tm_jee
  24. */
  25. public class StreamResultTest extends TestCase {
  26. private StreamResult result;
  27. private MockHttpServletResponse response;
  28. private MockActionInvocation mai;
  29. private OgnlValueStack stack;
  30. private int contentLength = 0;
  31. public void testStreamResultNoInputName() throws Exception {
  32. result.setParse(false);
  33. result.setInputName(null);
  34. try {
  35. result.doExecute("helloworld", mai);
  36. fail("Should have thrown an IllegalArgumentException");
  37. } catch (IllegalArgumentException e) {
  38. // success
  39. }
  40. }
  41. public void testStreamResultParseNoInputName() throws Exception {
  42. result.setParse(true);
  43. result.setInputName("${top}");
  44. try {
  45. result.doExecute("helloworld", mai);
  46. fail("Should have thrown an IllegalArgumentException");
  47. } catch (IllegalArgumentException e) {
  48. // success
  49. }
  50. }
  51. public void testStreamResultDefault() throws Exception {
  52. result.setInputName("streamForImage");
  53. result.doExecute("helloworld", mai);
  54. assertEquals(null, result.getContentLength());
  55. assertEquals("text/plain", result.getContentType());
  56. assertEquals("streamForImage", result.getInputName());
  57. assertEquals(1024, result.getBufferSize()); // 1024 is default
  58. assertEquals("inline", result.getContentDisposition());
  59. assertEquals("text/plain", response.getContentType());
  60. assertEquals(0, response.getContentLength());
  61. assertEquals("inline", response.getHeader("Content-disposition"));
  62. }
  63. public void testStreamResultNoDefault() throws Exception {
  64. // it's not easy to test using easymock as we use getOutputStream on HttpServletResponse.
  65. result.setParse(false);
  66. result.setInputName("streamForImage");
  67. result.setBufferSize(128);
  68. result.setContentLength(String.valueOf(contentLength));
  69. result.setContentDisposition("filename=\"logo.png\"");
  70. result.setContentType("image/jpeg");
  71. result.doExecute("helloworld", mai);
  72. assertEquals(String.valueOf(contentLength), result.getContentLength());
  73. assertEquals("image/jpeg", result.getContentType());
  74. assertEquals("streamForImage", result.getInputName());
  75. assertEquals(128, result.getBufferSize());
  76. assertEquals("filename=\"logo.png\"", result.getContentDisposition());
  77. assertEquals("image/jpeg", response.getContentType());
  78. assertEquals(contentLength, response.getContentLength());
  79. assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
  80. }
  81. public void testStreamResultParse1() throws Exception {
  82. ///////////////////
  83. result.setParse(true);
  84. // ${...} conditionalParse of Result, returns String,
  85. // which gets evaluated to the stack, that's how it works.
  86. // We use ${streamForImageAsString} that returns "streamForImage"
  87. // which is a property that returns an InputStream object.
  88. result.setInputName("${streamForImageAsString}");
  89. result.setBufferSize(128);
  90. result.setContentLength(String.valueOf(contentLength));
  91. result.setContentDisposition("filename=\"logo.png\"");
  92. result.setContentType("image/jpeg");
  93. result.doExecute("helloworld", mai);
  94. assertEquals(String.valueOf(contentLength), result.getContentLength());
  95. assertEquals("image/jpeg", result.getContentType());
  96. assertEquals("${streamForImageAsString}", result.getInputName());
  97. assertEquals(128, result.getBufferSize());
  98. assertEquals("filename=\"logo.png\"", result.getContentDisposition());
  99. assertEquals("image/jpeg", response.getContentType());
  100. assertEquals(contentLength, response.getContentLength());
  101. assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
  102. }
  103. public void testStreamResultParse2() throws Exception {
  104. ///////////////////
  105. result.setParse(true);
  106. // This time we dun use ${...}, so streamForImage will
  107. // be evaluated to the stack, which should reaturn an
  108. // InputStream object, cause there's such a property in
  109. // the action object itself.
  110. result.setInputName("streamForImage");
  111. result.setBufferSize(128);
  112. result.setContentLength(String.valueOf(contentLength));
  113. result.setContentDisposition("filename=\"logo.png\"");
  114. result.setContentType("image/jpeg");
  115. result.doExecute("helloworld", mai);
  116. assertEquals(String.valueOf(contentLength), result.getContentLength());
  117. assertEquals("image/jpeg", result.getContentType());
  118. assertEquals("streamForImage", result.getInputName());
  119. assertEquals(128, result.getBufferSize());
  120. assertEquals("filename=\"logo.png\"", result.getContentDisposition());
  121. assertEquals("image/jpeg", response.getContentType());
  122. assertEquals(contentLength, response.getContentLength());
  123. assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
  124. }
  125. protected void setUp() throws Exception {
  126. response = new MockHttpServletResponse();
  127. result = new StreamResult();
  128. stack = new OgnlValueStack();
  129. ActionContext.getContext().setValueStack(stack);
  130. MyImageAction action = new MyImageAction();
  131. contentLength = (int) action.getContentLength();
  132. mai = new com.opensymphony.xwork.mock.MockActionInvocation();
  133. mai.setAction(action);
  134. mai.setStack(stack);
  135. mai.setInvocationContext(ActionContext.getContext());
  136. stack.push(action);
  137. ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
  138. }
  139. protected void tearDown() {
  140. response = null;
  141. result = null;
  142. stack = null;
  143. contentLength = 0;
  144. mai = null;
  145. }
  146. public class MyImageAction implements Action {
  147. public InputStream getStreamForImage() throws Exception {
  148. // just use src/test/log4j.properties as test file
  149. URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
  150. File file = new File(new URI(url.toString()));
  151. FileInputStream fis = new FileInputStream(file);
  152. return fis;
  153. }
  154. public String execute() throws Exception {
  155. return SUCCESS;
  156. }
  157. public long getContentLength() throws Exception {
  158. URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
  159. File file = new File(new URI(url.toString()));
  160. return file.length();
  161. }
  162. public String getStreamForImageAsString() {
  163. return "streamForImage";
  164. }
  165. }
  166. }