View Javadoc

    1   /*
    2    * $Id: PlainTextResultTest.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.InputStream;
    24  import java.io.PrintWriter;
    25  import java.io.StringWriter;
    26
    27  import junit.framework.TestCase;
    28
    29  import org.apache.struts2.StrutsStatics;
    30  import org.apache.struts2.views.jsp.AbstractUITagTest;
    31  import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
    32  import org.apache.struts2.views.jsp.StrutsMockServletContext;
    33
    34  import com.opensymphony.xwork2.util.ClassLoaderUtil;
    35  import com.opensymphony.xwork2.util.ValueStackFactory;
    36  import com.opensymphony.xwork2.ActionContext;
    37  import com.opensymphony.xwork2.mock.MockActionInvocation;
    38  import com.opensymphony.xwork2.util.ValueStack;
    39
    40  /***
    41   * Test case for PlainTextResult.
    42   *
    43   */
    44  public class PlainTextResultTest extends TestCase {
    45
    46      ValueStack stack;
    47      MockActionInvocation invocation;
    48      ActionContext context;
    49      StrutsMockHttpServletResponse response;
    50      PrintWriter writer;
    51      StringWriter stringWriter;
    52      StrutsMockServletContext servletContext;
    53
    54
    55      public void testPlainText() throws Exception {
    56          PlainTextResult result = new PlainTextResult();
    57          result.setLocation("/someJspFile.jsp");
    58
    59          response.setExpectedContentType("text/plain");
    60          response.setExpectedHeader("Content-Disposition", "inline");
    61          InputStream jspResourceInputStream =
    62              ClassLoaderUtil.getResourceAsStream(
    63                  "org/apache/struts2/dispatcher/someJspFile.jsp",
    64                  PlainTextResultTest.class);
    65
    66
    67          try {
    68              servletContext.setResourceAsStream(jspResourceInputStream);
    69              result.execute(invocation);
    70
    71              String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
    72              String e = AbstractUITagTest.normalize(
    73                      readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
    74              assertEquals(r, e);
    75          }
    76          finally {
    77              jspResourceInputStream.close();
    78          }
    79      }
    80
    81      public void testPlainTextWithEncoding() throws Exception {
    82          PlainTextResult result = new PlainTextResult();
    83          result.setLocation("/someJspFile.jsp");
    84          result.setCharSet("UTF-8");
    85
    86          response.setExpectedContentType("text/plain; charset=UTF-8");
    87          response.setExpectedHeader("Content-Disposition", "inline");
    88          InputStream jspResourceInputStream =
    89              ClassLoaderUtil.getResourceAsStream(
    90                  "org/apache/struts2/dispatcher/someJspFile.jsp",
    91                  PlainTextResultTest.class);
    92
    93
    94          try {
    95              servletContext.setResourceAsStream(jspResourceInputStream);
    96              result.execute(invocation);
    97
    98              String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
    99              String e = AbstractUITagTest.normalize(
    100                     readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
    101             assertEquals(r, e);
    102         }
    103         finally {
    104             jspResourceInputStream.close();
    105         }
    106     }
    107
    108     protected String readAsString(String resource) throws Exception {
    109         InputStream is = null;
    110         try {
    111             is = ClassLoaderUtil.getResourceAsStream(resource, PlainTextResultTest.class);
    112             int sizeRead = 0;
    113             byte[] buffer = new byte[1024];
    114             StringBuffer stringBuffer = new StringBuffer();
    115             while((sizeRead = is.read(buffer)) != -1) {
    116                 stringBuffer.append(new String(buffer, 0, sizeRead));
    117             }
    118             return stringBuffer.toString();
    119         }
    120         finally {
    121             if (is != null)
    122                 is.close();
    123         }
    124
    125     }
    126
    127
    128     protected void setUp() throws Exception {
    129         super.setUp();
    130
    131         stringWriter = new StringWriter();
    132         writer = new PrintWriter(stringWriter);
    133         response = new StrutsMockHttpServletResponse();
    134         response.setWriter(writer);
    135         servletContext = new StrutsMockServletContext();
    136         stack = ValueStackFactory.getFactory().createValueStack();
    137         context = new ActionContext(stack.getContext());
    138         context.put(StrutsStatics.HTTP_RESPONSE, response);
    139         context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
    140         invocation = new MockActionInvocation();
    141         invocation.setStack(stack);
    142         invocation.setInvocationContext(context);
    143     }
    144
    145
    146     protected void tearDown() throws Exception {
    147         stack = null;
    148         invocation = null;
    149         context = null;
    150         response = null;
    151         writer = null;
    152         stringWriter = null;
    153         servletContext = null;
    154
    155         super.tearDown();
    156     }
    157 }