1 20 package org.apache.maven.cactus.sample; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 26 import javax.servlet.FilterChain ; 27 import javax.servlet.FilterConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.ServletRequest ; 30 import javax.servlet.ServletResponse ; 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 43 public class TestSampleFilter extends FilterTestCase 44 { 45 50 public TestSampleFilter(String theName) 51 { 52 super(theName); 53 } 54 55 60 public static void main(String [] theArgs) 61 { 62 junit.swingui.TestRunner.main( 63 new String [] {TestSampleFilter.class.getName()}); 64 } 65 66 70 public static Test suite() 71 { 72 return new TestSuite(TestSampleFilter.class); 74 } 75 76 78 85 public void testAddHeaderParamOK() throws ServletException , IOException 86 { 87 SampleFilter filter = new SampleFilter(); 88 89 config.setInitParameter("header", "<h1>header</h1>"); 90 filter.init(config); 91 92 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 93 94 filter.addHeader(baos); 95 96 assertEquals("<h1>header</h1>", baos.toString()); 97 } 98 99 101 108 public void testAddHeaderParamNotDefined() throws ServletException , 109 IOException 110 { 111 SampleFilter filter = new SampleFilter(); 112 113 filter.init(config); 114 115 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 116 117 filter.addHeader(baos); 118 119 assertEquals("", baos.toString()); 120 } 121 122 124 131 public void testAddFooterParamOK() throws ServletException , IOException 132 { 133 SampleFilter filter = new SampleFilter(); 134 135 config.setInitParameter("footer", "<h1>footer</h1>"); 136 filter.init(config); 137 138 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 139 140 filter.addFooter(baos); 141 142 assertEquals("<h1>footer</h1>", baos.toString()); 143 } 144 145 147 154 public void testAddFooterParamNotDefined() throws ServletException , 155 IOException 156 { 157 SampleFilter filter = new SampleFilter(); 158 159 filter.init(config); 160 161 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 162 163 filter.addFooter(baos); 164 165 assertEquals("", baos.toString()); 166 } 167 168 170 177 public void testDoFilterOK() throws ServletException , IOException 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 mockFilterChain = new FilterChain () 186 { 187 public void doFilter(ServletRequest theRequest, 188 ServletResponse theResponse) throws IOException , 189 ServletException 190 { 191 PrintWriter writer = theResponse.getWriter(); 192 193 writer.print("<p>some content</p>"); 194 writer.close(); 195 } 196 197 public void init(FilterConfig theConfig) 198 { 199 } 200 201 public void destroy() 202 { 203 } 204 }; 205 206 filter.doFilter(request, response, mockFilterChain); 207 } 208 209 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 |