KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > cactus > sample > TestSampleFilter


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 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  * ========================================================================
19  */

20 package org.apache.maven.cactus.sample;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 import javax.servlet.FilterChain JavaDoc;
27 import javax.servlet.FilterConfig JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletRequest JavaDoc;
30 import javax.servlet.ServletResponse JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 import org.apache.cactus.FilterTestCase;
36 import org.apache.cactus.WebResponse;
37
38 /**
39  * Tests of the <code>SampleFilter</code> filter class.
40  *
41  * @version $Id: TestSampleFilter.java,v 1.3 2004/02/29 16:34:44 vmassol Exp $
42  */

43 public class TestSampleFilter extends FilterTestCase
44 {
45     /**
46      * Defines the testcase name for JUnit.
47      *
48      * @param theName the testcase's name.
49      */

50     public TestSampleFilter(String JavaDoc theName)
51     {
52         super(theName);
53     }
54
55     /**
56      * Start the tests.
57      *
58      * @param theArgs the arguments. Not used
59      */

60     public static void main(String JavaDoc[] theArgs)
61     {
62         junit.swingui.TestRunner.main(
63             new String JavaDoc[] {TestSampleFilter.class.getName()});
64     }
65
66     /**
67      * @return a test suite (<code>TestSuite</code>) that includes all methods
68      * starting with "test"
69      */

70     public static Test suite()
71     {
72         // All methods starting with "test" will be executed in the test suite.
73
return new TestSuite(TestSampleFilter.class);
74     }
75
76     //-------------------------------------------------------------------------
77

78     /**
79      * Test that adding a header to the output stream is working fine when
80      * a header parameter is defined.
81      *
82      * @exception ServletException on test failure
83      * @exception IOException on test failure
84      */

85     public void testAddHeaderParamOK() throws ServletException JavaDoc, IOException JavaDoc
86     {
87         SampleFilter filter = new SampleFilter();
88
89         config.setInitParameter("header", "<h1>header</h1>");
90         filter.init(config);
91
92         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
93
94         filter.addHeader(baos);
95
96         assertEquals("<h1>header</h1>", baos.toString());
97     }
98
99     //-------------------------------------------------------------------------
100

101     /**
102      * Test that adding a header to the output stream is working fine
103      * (i.e. nothing gets written) when no header parameter is defined.
104      *
105      * @exception ServletException on test failure
106      * @exception IOException on test failure
107      */

108     public void testAddHeaderParamNotDefined() throws ServletException JavaDoc,
109         IOException JavaDoc
110     {
111         SampleFilter filter = new SampleFilter();
112
113         filter.init(config);
114
115         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
116
117         filter.addHeader(baos);
118
119         assertEquals("", baos.toString());
120     }
121
122     //-------------------------------------------------------------------------
123

124     /**
125      * Test that adding a footer to the output stream is working fine when
126      * a footer parameter is defined.
127      *
128      * @exception ServletException on test failure
129      * @exception IOException on test failure
130      */

131     public void testAddFooterParamOK() throws ServletException JavaDoc, IOException JavaDoc
132     {
133         SampleFilter filter = new SampleFilter();
134
135         config.setInitParameter("footer", "<h1>footer</h1>");
136         filter.init(config);
137
138         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
139
140         filter.addFooter(baos);
141
142         assertEquals("<h1>footer</h1>", baos.toString());
143     }
144
145     //-------------------------------------------------------------------------
146

147     /**
148      * Test that adding a footer to the output stream is working fine
149      * (i.e. nothing gets written) when no footer parameter is defined.
150      *
151      * @exception ServletException on test failure
152      * @exception IOException on test failure
153      */

154     public void testAddFooterParamNotDefined() throws ServletException JavaDoc,
155         IOException JavaDoc
156     {
157         SampleFilter filter = new SampleFilter();
158
159         filter.init(config);
160
161         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
162
163         filter.addFooter(baos);
164
165         assertEquals("", baos.toString());
166     }
167
168     //-------------------------------------------------------------------------
169

170     /**
171      * Test that the filter does correctly add a header and footer to
172      * any requets it is serving.
173      *
174      * @exception ServletException on test failure
175      * @exception IOException on test failure
176      */

177     public void testDoFilterOK() throws ServletException JavaDoc, IOException JavaDoc
178     {
179         SampleFilter filter = new SampleFilter();
180
181         config.setInitParameter("header", "<h1>header</h1>");
182         config.setInitParameter("footer", "<h1>footer</h1>");
183         filter.init(config);
184
185         FilterChain JavaDoc mockFilterChain = new FilterChain JavaDoc()
186         {
187             public void doFilter(ServletRequest JavaDoc theRequest,
188                 ServletResponse JavaDoc theResponse) throws IOException JavaDoc,
189                 ServletException JavaDoc
190             {
191                 PrintWriter JavaDoc writer = theResponse.getWriter();
192
193                 writer.print("<p>some content</p>");
194                 writer.close();
195             }
196
197             public void init(FilterConfig JavaDoc theConfig)
198             {
199             }
200
201             public void destroy()
202             {
203             }
204         };
205
206         filter.doFilter(request, response, mockFilterChain);
207     }
208
209     /**
210      * Test that the filter does correctly add a header and footer to
211      * any requets it is serving.
212      *
213      * @param theResponse the response from the server side.
214      */

215     public void endDoFilterOK(WebResponse theResponse)
216     {
217         assertEquals("<h1>header</h1><p>some content</p><h1>footer</h1>",
218             theResponse.getText());
219     }
220 }
Popular Tags