001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.maven.cactus.sample;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.PrintWriter;
025:
026: import javax.servlet.FilterChain;
027: import javax.servlet.FilterConfig;
028: import javax.servlet.ServletException;
029: import javax.servlet.ServletRequest;
030: import javax.servlet.ServletResponse;
031:
032: import junit.framework.Test;
033: import junit.framework.TestSuite;
034:
035: import org.apache.cactus.FilterTestCase;
036: import org.apache.cactus.WebResponse;
037:
038: /**
039: * Tests of the <code>SampleFilter</code> filter class.
040: *
041: * @version $Id: TestSampleFilter.java 238815 2004-02-29 16:34:44Z vmassol $
042: */
043: public class TestSampleFilter extends FilterTestCase {
044: /**
045: * Defines the testcase name for JUnit.
046: *
047: * @param theName the testcase's name.
048: */
049: public TestSampleFilter(String theName) {
050: super (theName);
051: }
052:
053: /**
054: * Start the tests.
055: *
056: * @param theArgs the arguments. Not used
057: */
058: public static void main(String[] theArgs) {
059: junit.swingui.TestRunner
060: .main(new String[] { TestSampleFilter.class.getName() });
061: }
062:
063: /**
064: * @return a test suite (<code>TestSuite</code>) that includes all methods
065: * starting with "test"
066: */
067: public static Test suite() {
068: // All methods starting with "test" will be executed in the test suite.
069: return new TestSuite(TestSampleFilter.class);
070: }
071:
072: //-------------------------------------------------------------------------
073:
074: /**
075: * Test that adding a header to the output stream is working fine when
076: * a header parameter is defined.
077: *
078: * @exception ServletException on test failure
079: * @exception IOException on test failure
080: */
081: public void testAddHeaderParamOK() throws ServletException,
082: IOException {
083: SampleFilter filter = new SampleFilter();
084:
085: config.setInitParameter("header", "<h1>header</h1>");
086: filter.init(config);
087:
088: ByteArrayOutputStream baos = new ByteArrayOutputStream();
089:
090: filter.addHeader(baos);
091:
092: assertEquals("<h1>header</h1>", baos.toString());
093: }
094:
095: //-------------------------------------------------------------------------
096:
097: /**
098: * Test that adding a header to the output stream is working fine
099: * (i.e. nothing gets written) when no header parameter is defined.
100: *
101: * @exception ServletException on test failure
102: * @exception IOException on test failure
103: */
104: public void testAddHeaderParamNotDefined() throws ServletException,
105: IOException {
106: SampleFilter filter = new SampleFilter();
107:
108: filter.init(config);
109:
110: ByteArrayOutputStream baos = new ByteArrayOutputStream();
111:
112: filter.addHeader(baos);
113:
114: assertEquals("", baos.toString());
115: }
116:
117: //-------------------------------------------------------------------------
118:
119: /**
120: * Test that adding a footer to the output stream is working fine when
121: * a footer parameter is defined.
122: *
123: * @exception ServletException on test failure
124: * @exception IOException on test failure
125: */
126: public void testAddFooterParamOK() throws ServletException,
127: IOException {
128: SampleFilter filter = new SampleFilter();
129:
130: config.setInitParameter("footer", "<h1>footer</h1>");
131: filter.init(config);
132:
133: ByteArrayOutputStream baos = new ByteArrayOutputStream();
134:
135: filter.addFooter(baos);
136:
137: assertEquals("<h1>footer</h1>", baos.toString());
138: }
139:
140: //-------------------------------------------------------------------------
141:
142: /**
143: * Test that adding a footer to the output stream is working fine
144: * (i.e. nothing gets written) when no footer parameter is defined.
145: *
146: * @exception ServletException on test failure
147: * @exception IOException on test failure
148: */
149: public void testAddFooterParamNotDefined() throws ServletException,
150: IOException {
151: SampleFilter filter = new SampleFilter();
152:
153: filter.init(config);
154:
155: ByteArrayOutputStream baos = new ByteArrayOutputStream();
156:
157: filter.addFooter(baos);
158:
159: assertEquals("", baos.toString());
160: }
161:
162: //-------------------------------------------------------------------------
163:
164: /**
165: * Test that the filter does correctly add a header and footer to
166: * any requets it is serving.
167: *
168: * @exception ServletException on test failure
169: * @exception IOException on test failure
170: */
171: public void testDoFilterOK() throws ServletException, IOException {
172: SampleFilter filter = new SampleFilter();
173:
174: config.setInitParameter("header", "<h1>header</h1>");
175: config.setInitParameter("footer", "<h1>footer</h1>");
176: filter.init(config);
177:
178: FilterChain mockFilterChain = new FilterChain() {
179: public void doFilter(ServletRequest theRequest,
180: ServletResponse theResponse) throws IOException,
181: ServletException {
182: PrintWriter writer = theResponse.getWriter();
183:
184: writer.print("<p>some content</p>");
185: writer.close();
186: }
187:
188: public void init(FilterConfig theConfig) {
189: }
190:
191: public void destroy() {
192: }
193: };
194:
195: filter.doFilter(request, response, mockFilterChain);
196: }
197:
198: /**
199: * Test that the filter does correctly add a header and footer to
200: * any requets it is serving.
201: *
202: * @param theResponse the response from the server side.
203: */
204: public void endDoFilterOK(WebResponse theResponse) {
205: assertEquals(
206: "<h1>header</h1><p>some content</p><h1>footer</h1>",
207: theResponse.getText());
208: }
209: }
|