View Javadoc

1   /*
2    * $Id: PlainTextResultTest.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import java.io.InputStream;
21  import java.io.PrintWriter;
22  import java.io.StringWriter;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.struts2.StrutsStatics;
27  import org.apache.struts2.views.jsp.AbstractUITagTest;
28  import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
29  import org.apache.struts2.views.jsp.StrutsMockServletContext;
30  
31  import com.opensymphony.xwork2.util.ClassLoaderUtil;
32  import com.opensymphony.xwork2.util.ValueStackFactory;
33  import com.opensymphony.xwork2.ActionContext;
34  import com.opensymphony.xwork2.mock.MockActionInvocation;
35  import com.opensymphony.xwork2.util.ValueStack;
36  
37  /***
38   * Test case for PlainTextResult.
39   * 
40   */
41  public class PlainTextResultTest extends TestCase {
42  	
43  	ValueStack stack;
44  	MockActionInvocation invocation;
45  	ActionContext context;
46  	StrutsMockHttpServletResponse response;
47  	PrintWriter writer;
48  	StringWriter stringWriter;
49  	StrutsMockServletContext servletContext;
50  	
51  
52  	public void testPlainText() throws Exception {
53  		PlainTextResult result = new PlainTextResult();
54  		result.setLocation("/someJspFile.jsp");
55  		
56  		response.setExpectedContentType("text/plain");
57  		response.setExpectedHeader("Content-Disposition", "inline");
58  		InputStream jspResourceInputStream = 
59  			ClassLoaderUtil.getResourceAsStream(
60  				"org/apache/struts2/dispatcher/someJspFile.jsp",
61  				PlainTextResultTest.class);
62  		
63  		
64  		try {
65  			servletContext.setResourceAsStream(jspResourceInputStream);
66  			result.execute(invocation);
67  			
68  			String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
69  			String e = AbstractUITagTest.normalize(
70  					readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
71  			assertEquals(r, e);
72  		}
73  		finally {
74  			jspResourceInputStream.close();
75  		}
76  	}
77  	
78  	public void testPlainTextWithEncoding() throws Exception {
79  		PlainTextResult result = new PlainTextResult();
80  		result.setLocation("/someJspFile.jsp");
81  		result.setCharSet("UTF-8");
82  		
83  		response.setExpectedContentType("text/plain; charset=UTF-8");
84  		response.setExpectedHeader("Content-Disposition", "inline");
85  		InputStream jspResourceInputStream = 
86  			ClassLoaderUtil.getResourceAsStream(
87  				"org/apache/struts2/dispatcher/someJspFile.jsp",
88  				PlainTextResultTest.class);
89  		
90  		
91  		try {
92  			servletContext.setResourceAsStream(jspResourceInputStream);
93  			result.execute(invocation);
94  			
95  			String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
96  			String e = AbstractUITagTest.normalize(
97  					readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
98  			assertEquals(r, e);
99  		}
100 		finally {
101 			jspResourceInputStream.close();
102 		}
103 	}
104 	
105 	protected String readAsString(String resource) throws Exception {
106 		InputStream is = null;
107 		try {
108 			is = ClassLoaderUtil.getResourceAsStream(resource, PlainTextResultTest.class);
109 			int sizeRead = 0;
110 			byte[] buffer = new byte[1024];
111 			StringBuffer stringBuffer = new StringBuffer();
112 			while((sizeRead = is.read(buffer)) != -1) {
113 				stringBuffer.append(new String(buffer, 0, sizeRead));
114 			}
115 			return stringBuffer.toString();
116 		}
117 		finally {
118 			if (is != null) 
119 				is.close();
120 		}
121 	
122 	}
123 	
124 	
125 	protected void setUp() throws Exception {
126 		super.setUp();
127 		
128 		stringWriter = new StringWriter();
129 		writer = new PrintWriter(stringWriter);
130 		response = new StrutsMockHttpServletResponse();
131 		response.setWriter(writer);
132 		servletContext = new StrutsMockServletContext();
133 		stack = ValueStackFactory.getFactory().createValueStack();
134 		context = new ActionContext(stack.getContext());
135 		context.put(StrutsStatics.HTTP_RESPONSE, response);
136 		context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
137 		invocation = new MockActionInvocation();
138 		invocation.setStack(stack);
139 		invocation.setInvocationContext(context);
140 	}
141 	
142 	
143 	protected void tearDown() throws Exception {
144 		stack = null;
145 		invocation = null;
146 		context = null;
147 		response = null;
148 		writer = null;
149 		stringWriter = null;
150 		servletContext = null;
151 		
152 		super.tearDown();
153 	}
154 }