View Javadoc

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