1 20 package org.apache.cactus.sample.servlet; 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 org.apache.cactus.FilterTestCase; 33 import org.apache.cactus.WebResponse; 34 35 40 public class TestSampleFilter extends FilterTestCase 41 { 42 49 public void testAddHeaderParamOK() throws ServletException , IOException 50 { 51 SampleFilter filter = new SampleFilter(); 52 53 config.setInitParameter("header", "<h1>header</h1>"); 54 filter.init(config); 55 56 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 57 58 filter.addHeader(baos); 59 60 assertEquals("<h1>header</h1>", baos.toString()); 61 } 62 63 65 72 public void testAddHeaderParamNotDefined() throws ServletException , 73 IOException 74 { 75 SampleFilter filter = new SampleFilter(); 76 77 filter.init(config); 78 79 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 80 81 filter.addHeader(baos); 82 83 assertEquals("", baos.toString()); 84 } 85 86 88 95 public void testAddFooterParamOK() throws ServletException , IOException 96 { 97 SampleFilter filter = new SampleFilter(); 98 99 config.setInitParameter("footer", "<h1>footer</h1>"); 100 filter.init(config); 101 102 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 103 104 filter.addFooter(baos); 105 106 assertEquals("<h1>footer</h1>", baos.toString()); 107 } 108 109 111 118 public void testAddFooterParamNotDefined() throws ServletException , 119 IOException 120 { 121 SampleFilter filter = new SampleFilter(); 122 123 filter.init(config); 124 125 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 126 127 filter.addFooter(baos); 128 129 assertEquals("", baos.toString()); 130 } 131 132 134 141 public void testDoFilterOK() throws ServletException , IOException 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 mockFilterChain = new FilterChain () 150 { 151 public void doFilter(ServletRequest theRequest, 152 ServletResponse theResponse) throws IOException , 153 ServletException 154 { 155 PrintWriter writer = theResponse.getWriter(); 156 157 writer.print("<p>some content</p>"); 158 writer.close(); 159 } 160 161 public void init(FilterConfig theConfig) 162 { 163 } 164 165 public void destroy() 166 { 167 } 168 }; 169 170 filter.doFilter(request, response, mockFilterChain); 171 } 172 173 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 |