1 20 package org.apache.cactus.internal; 21 22 import java.io.ByteArrayInputStream ; 23 24 import java.net.URL ; 25 26 import junit.framework.AssertionFailedError; 27 import junit.framework.TestCase; 28 29 import org.apache.cactus.WebRequest; 30 import org.apache.cactus.internal.client.ClientException; 31 import org.apache.cactus.internal.client.ClientTestCaseCaller; 32 import org.apache.cactus.internal.client.WebResponseObjectFactory; 33 import org.apache.cactus.internal.client.connector.http.HttpProtocolHandler; 34 import org.apache.cactus.internal.configuration.DefaultServletConfiguration; 35 import org.apache.cactus.internal.util.JUnitVersionHelper; 36 import org.apache.cactus.mock.MockHttpURLConnection; 37 38 45 public abstract class AbstractTestAbstractCactusTestCase extends TestCase 46 { 47 55 public void runBare() throws Throwable 56 { 57 runTest(); 58 } 59 60 67 protected void runTest() throws Throwable 68 { 69 ClientTestCaseCaller delegator = new ClientTestCaseCaller( 70 this, this, 71 new HttpProtocolHandler(new DefaultServletConfiguration())); 72 73 try 74 { 75 WebRequest request = 77 new WebRequestImpl(new DefaultServletConfiguration()); 78 79 delegator.callBeginMethod(request); 80 81 MockHttpURLConnection connection = new MockHttpURLConnection( 84 new URL ("http://something")); 85 86 connection.setExpectedGetHeaderField("HTTP/1.1 200 OK"); 90 connection.setExpectedGetInputStream( 91 new ByteArrayInputStream ("".getBytes())); 92 93 delegator.callEndMethod(request, 95 new WebResponseObjectFactory(connection)); 96 } 97 catch (AssertionFailedError e) 98 { 99 if (!verifyBeginMethodsOk(e.getMessage()) 101 && !verifyEndMethodsOk(e.getMessage())) 102 { 103 throw e; 104 } 105 } 106 catch (ClientException e) 107 { 108 if (!verifyBeginMethodsOk(e.getMessage()) 110 && !verifyEndMethodsOk(e.getMessage())) 111 { 112 throw e; 113 } 114 } 115 } 116 117 122 private boolean checkName(String theTestName) 123 { 124 return JUnitVersionHelper.getTestCaseName(this).equals( 125 theTestName); 126 } 127 128 134 private boolean verifyBeginMethodsOk(String theMessage) 135 { 136 if (checkName("testBeginMethodBadReturnType")) 140 { 141 assertEquals("The method " 142 + "[beginBeginMethodBadReturnType] should return void and " 143 + "not [java.lang.String]", theMessage); 144 145 return true; 146 } 147 148 if (checkName("testBeginMethodNotPublic")) 151 { 152 assertEquals("Method [beginBeginMethodNotPublic] should be " 153 + "declared public", theMessage); 154 155 return true; 156 } 157 158 if (checkName("testBeginMethodBadParamType")) 162 { 163 assertEquals("The method " 164 + "[beginBeginMethodBadParamType] must accept " 165 + "[org.apache.cactus.Request] as 1st parameter, but " 166 + "found a [java.lang.String] parameter instead", theMessage); 167 168 return true; 169 } 170 171 if (checkName("testBeginMethodBadParamNumber")) 175 { 176 assertEquals("The method " 177 + "[beginBeginMethodBadParamNumber] must have " 178 + "1 parameter(s), but 2 parameter(s) were found", 179 theMessage); 180 181 return true; 182 } 183 184 if (checkName("testBeginMethodOK")) 187 { 188 assertEquals("beginBeginMethodOK", theMessage); 189 190 return true; 191 } 192 193 return false; 194 } 195 196 202 private boolean verifyEndMethodsOk(String theMessage) 203 { 204 if (checkName("testEndMethodBadReturnType")) 208 { 209 assertEquals("The method " 210 + "[endEndMethodBadReturnType] should return void and " 211 + "not [java.lang.String]", theMessage); 212 213 return true; 214 } 215 216 if (checkName("testEndMethodNotPublic")) 219 { 220 assertEquals("Method [endEndMethodNotPublic] should be " 221 + "declared public", theMessage); 222 223 return true; 224 } 225 226 if (checkName("testEndMethodBadParamType")) 230 { 231 assertEquals("The method [endEndMethodBadParamType] " 232 + "has a bad parameter of type [java.lang.String]", 233 theMessage); 234 235 return true; 236 } 237 238 if (checkName("testEndMethodBadParamNumber")) 242 { 243 assertEquals("The method [endEndMethodBadParamNumber] " 244 + "must have 1 parameter(s), but 2 parameter(s) were found", 245 theMessage); 246 247 return true; 248 } 249 250 if (checkName("testEndMethodOK1")) 254 { 255 assertEquals("endEndMethodOK1", theMessage); 256 257 return true; 258 } 259 260 if (checkName("testEndMethodOK2")) 264 { 265 assertEquals("endEndMethodOK2", theMessage); 266 267 return true; 268 } 269 270 if (checkName("testEndMethodOK3")) 274 { 275 assertEquals("endEndMethodOK3", theMessage); 276 277 return true; 278 } 279 280 return false; 281 } 282 283 } 284 | Popular Tags |