View Javadoc

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