1 20 package org.apache.cactus.internal.client.connector.http; 21 22 import java.io.IOException ; 23 import java.net.HttpURLConnection ; 24 25 import junit.framework.Test; 26 27 import org.apache.cactus.Request; 28 import org.apache.cactus.WebRequest; 29 import org.apache.cactus.internal.RequestDirectives; 30 import org.apache.cactus.internal.WebRequestImpl; 31 import org.apache.cactus.internal.client.WebResponseObjectFactory; 32 import org.apache.cactus.internal.configuration.WebConfiguration; 33 import org.apache.cactus.internal.util.JUnitVersionHelper; 34 import org.apache.cactus.spi.client.ResponseObjectFactory; 35 import org.apache.cactus.spi.client.connector.ProtocolHandler; 36 import org.apache.cactus.spi.client.connector.ProtocolState; 37 38 45 public class HttpProtocolHandler implements ProtocolHandler 46 { 47 51 private WebConfiguration configuration; 52 53 56 public HttpProtocolHandler(WebConfiguration theConfiguration) 57 { 58 this.configuration = theConfiguration; 59 } 60 61 63 66 public Request createRequest() 67 { 68 return new WebRequestImpl(getConfiguration()); 69 } 70 71 74 public ProtocolState runTest(Test theDelegatedTest, Test theWrappedTest, 75 Request theRequest) throws Throwable 76 { 77 WebRequest request = (WebRequest) theRequest; 78 79 HttpURLConnection connection = runWebTest(theDelegatedTest, 81 theWrappedTest, request); 82 83 HttpProtocolState state = new HttpProtocolState(); 84 state.setConnection(connection); 85 return state; 86 } 87 88 91 public ResponseObjectFactory createResponseObjectFactory( 92 ProtocolState theState) 93 { 94 HttpProtocolState state = (HttpProtocolState) theState; 95 return new WebResponseObjectFactory(state.getConnection()); 96 } 97 98 101 public void afterTest(ProtocolState theState) throws IOException 102 { 103 HttpProtocolState state = (HttpProtocolState) theState; 104 105 state.getConnection().getInputStream().close(); 108 } 109 110 112 115 private WebConfiguration getConfiguration() 116 { 117 return this.configuration; 118 } 119 120 133 private HttpURLConnection runWebTest(Test theDelegatedTest, 134 Test theWrappedTest, WebRequest theRequest) throws Throwable 135 { 136 RequestDirectives directives = new RequestDirectives(theRequest); 139 directives.setClassName(theDelegatedTest.getClass().getName()); 140 directives.setMethodName(getCurrentTestName(theDelegatedTest)); 141 directives.setAutoSession( 142 theRequest.getAutomaticSession() ? "true" : "false"); 143 144 if (theWrappedTest != null) 146 { 147 directives.setWrappedTestName(theWrappedTest.getClass().getName()); 148 } 149 150 if (theRequest.getURL() != null) 152 { 153 theRequest.getURL().saveToRequest(theRequest); 154 } 155 156 DefaultHttpClient client = new DefaultHttpClient(getConfiguration()); 159 HttpURLConnection connection = client.doTest(theRequest); 160 161 return connection; 162 } 163 164 170 private String getCurrentTestName(Test theDelegatedTest) 171 { 172 return JUnitVersionHelper.getTestCaseName(theDelegatedTest); 173 } 174 } 175 | Popular Tags |