KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > 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.cactus.sample.servlet;
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 org.apache.cactus.FilterTestCase;
33 import org.apache.cactus.WebResponse;
34
35 /**
36  * Tests of the <code>SampleFilter</code> filter class.
37  *
38  * @version $Id: TestSampleFilter.java,v 1.3 2004/02/29 16:36:44 vmassol Exp $
39  */

40 public class TestSampleFilter extends FilterTestCase
41 {
42     /**
43      * Test that adding a header to the output stream is working fine when
44      * a header parameter is defined.
45      *
46      * @exception ServletException on test failure
47      * @exception IOException on test failure
48      */

49     public void testAddHeaderParamOK() throws ServletException JavaDoc, IOException JavaDoc
50     {
51         SampleFilter filter = new SampleFilter();
52
53         config.setInitParameter("header", "<h1>header</h1>");
54         filter.init(config);
55
56         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
57
58         filter.addHeader(baos);
59
60         assertEquals("<h1>header</h1>", baos.toString());
61     }
62
63     //-------------------------------------------------------------------------
64

65     /**
66      * Test that adding a header to the output stream is working fine
67      * (i.e. nothing gets written) when no header parameter is defined.
68      *
69      * @exception ServletException on test failure
70      * @exception IOException on test failure
71      */

72     public void testAddHeaderParamNotDefined() throws ServletException JavaDoc,
73         IOException JavaDoc
74     {
75         SampleFilter filter = new SampleFilter();
76
77         filter.init(config);
78
79         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
80
81         filter.addHeader(baos);
82
83         assertEquals("", baos.toString());
84     }
85
86     //-------------------------------------------------------------------------
87

88     /**
89      * Test that adding a footer to the output stream is working fine when
90      * a footer parameter is defined.
91      *
92      * @exception ServletException on test failure
93      * @exception IOException on test failure
94      */

95     public void testAddFooterParamOK() throws ServletException JavaDoc, IOException JavaDoc
96     {
97         SampleFilter filter = new SampleFilter();
98
99         config.setInitParameter("footer", "<h1>footer</h1>");
100         filter.init(config);
101
102         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
103
104         filter.addFooter(baos);
105
106         assertEquals("<h1>footer</h1>", baos.toString());
107     }
108
109     //-------------------------------------------------------------------------
110

111     /**
112      * Test that adding a footer to the output stream is working fine
113      * (i.e. nothing gets written) when no footer parameter is defined.
114      *
115      * @exception ServletException on test failure
116      * @exception IOException on test failure
117      */

118     public void testAddFooterParamNotDefined() throws ServletException JavaDoc,
119         IOException JavaDoc
120     {
121         SampleFilter filter = new SampleFilter();
122
123         filter.init(config);
124
125         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
126
127         filter.addFooter(baos);
128
129         assertEquals("", baos.toString());
130     }
131
132     //-------------------------------------------------------------------------
133

134     /**
135      * Test that the filter does correctly add a header and footer to
136      * any requets it is serving.
137      *
138      * @exception ServletException on test failure
139      * @exception IOException on test failure
140      */

141     public void testDoFilterOK() throws ServletException JavaDoc, IOException JavaDoc
142     {
143         SampleFilter filter = new SampleFilter();
144
145         config.setInitParameter("header", "<h1>header</h1>");
146         config.setInitParameter("footer", "<h1>footer</h1>");
147         filter.init(config);
148
149         FilterChain JavaDoc mockFilterChain = new FilterChain JavaDoc()
150         {
151             public void doFilter(ServletRequest JavaDoc theRequest,
152                 ServletResponse JavaDoc theResponse) throws IOException JavaDoc,
153                 ServletException JavaDoc
154             {
155                 PrintWriter JavaDoc writer = theResponse.getWriter();
156
157                 writer.print("<p>some content</p>");
158                 writer.close();
159             }
160
161             public void init(FilterConfig JavaDoc theConfig)
162             {
163             }
164
165             public void destroy()
166             {
167             }
168         };
169
170         filter.doFilter(request, response, mockFilterChain);
171     }
172
173     /**
174      * Test that the filter does correctly add a header and footer to
175      * any requets it is serving.
176      *
177      * @param theResponse the response from the server side.
178      */

179     public void endDoFilterOK(WebResponse theResponse)
180     {
181         assertEquals("<h1>header</h1><p>some content</p><h1>footer</h1>",
182             theResponse.getText());
183     }
184 }
185
Popular Tags