[1376] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.httpmonitor; |
---|
| 16 | |
---|
[1392] | 17 | import static org.junit.Assert.assertEquals; |
---|
| 18 | import static org.junit.Assert.assertNotNull; |
---|
| 19 | import static org.junit.Assert.assertTrue; |
---|
[1614] | 20 | import static org.junit.Assert.fail; |
---|
[1392] | 21 | |
---|
[1376] | 22 | import java.io.BufferedReader; |
---|
| 23 | import java.io.File; |
---|
| 24 | import java.io.IOException; |
---|
| 25 | import java.io.InputStream; |
---|
| 26 | import java.io.InputStreamReader; |
---|
| 27 | import java.util.logging.Level; |
---|
| 28 | |
---|
[1390] | 29 | import org.apache.catalina.Context; |
---|
| 30 | import org.apache.catalina.startup.Tomcat; |
---|
[1376] | 31 | import org.apache.http.HttpEntity; |
---|
| 32 | import org.apache.http.HttpResponse; |
---|
| 33 | import org.apache.http.client.methods.HttpGet; |
---|
| 34 | import org.apache.http.client.methods.HttpPost; |
---|
| 35 | import org.apache.http.client.methods.HttpRequestBase; |
---|
| 36 | import org.apache.http.entity.ContentType; |
---|
| 37 | import org.apache.http.entity.StringEntity; |
---|
| 38 | import org.apache.http.impl.client.DefaultHttpClient; |
---|
| 39 | import org.junit.After; |
---|
| 40 | import org.junit.Before; |
---|
| 41 | |
---|
[1392] | 42 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 43 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; |
---|
| 44 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; |
---|
[1561] | 45 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
| 46 | import de.ugoe.cs.autoquest.plugin.http.logdata.Method; |
---|
[1376] | 47 | import de.ugoe.cs.util.console.TextConsole; |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * |
---|
| 51 | */ |
---|
| 52 | public abstract class AbstractTC { |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * |
---|
| 56 | */ |
---|
| 57 | protected static final TextConsole CONSOLE = new TextConsole(Level.FINEST); |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * |
---|
| 61 | */ |
---|
| 62 | protected final static String LOG_FILE_DIR = "target/tmp/logfiles/"; |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * |
---|
| 66 | */ |
---|
[1390] | 67 | protected static final int DUMMY_SERVER_PORT = 19098; |
---|
[1376] | 68 | |
---|
| 69 | /** |
---|
[1390] | 70 | * |
---|
[1376] | 71 | */ |
---|
[1390] | 72 | protected static final int PROXY_PORT = 19099; |
---|
[1376] | 73 | |
---|
| 74 | /** |
---|
[1390] | 75 | * |
---|
| 76 | */ |
---|
| 77 | protected static final int MONITOR_PORT = 19100; |
---|
| 78 | |
---|
| 79 | /** |
---|
[1376] | 80 | * the servlet of the dummy web server responding to requests |
---|
| 81 | */ |
---|
| 82 | private DummyServlet dummyServlet; |
---|
[1390] | 83 | |
---|
| 84 | /** */ |
---|
| 85 | private static Tomcat tomcat = new Tomcat(); |
---|
[1567] | 86 | |
---|
| 87 | /** |
---|
| 88 | * |
---|
| 89 | */ |
---|
| 90 | static String readStreamContentToString(InputStream stream) throws IOException { |
---|
| 91 | return readToString(new BufferedReader(new InputStreamReader(stream))); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * |
---|
| 96 | */ |
---|
| 97 | static String readToString(BufferedReader reader) throws IOException { |
---|
| 98 | StringBuffer message = new StringBuffer(); |
---|
| 99 | |
---|
| 100 | String line = reader.readLine(); |
---|
| 101 | while (line != null) { |
---|
| 102 | System.err.println(line); |
---|
| 103 | if (message.length() > 0) { |
---|
| 104 | message.append('\n'); |
---|
| 105 | } |
---|
| 106 | message.append(line); |
---|
| 107 | line = reader.readLine(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | return message.toString(); |
---|
| 111 | } |
---|
[1376] | 112 | |
---|
| 113 | /** |
---|
| 114 | * |
---|
| 115 | */ |
---|
| 116 | @Before |
---|
| 117 | public void setUp() throws Exception { |
---|
| 118 | // setup a simple HTTP server |
---|
[1390] | 119 | dummyServlet = new DummyServlet(); |
---|
[1376] | 120 | |
---|
[1390] | 121 | /*dummyServer = new Server(DUMMY_SERVER_PORT); |
---|
| 122 | |
---|
[1376] | 123 | ServletContextHandler root = |
---|
[1390] | 124 | new ServletContextHandler(dummyServer, "/dummyServer", ServletContextHandler.SESSIONS); |
---|
[1376] | 125 | |
---|
[1390] | 126 | |
---|
[1376] | 127 | root.addServlet(new ServletHolder(dummyServlet), "/*"); |
---|
[1390] | 128 | |
---|
| 129 | dummyServer.start();*/ |
---|
[1376] | 130 | |
---|
[1390] | 131 | File tomcatDir = new File("target/test/tomcat"); |
---|
| 132 | File webappRootDir = new File(tomcatDir, "webapp"); |
---|
[1376] | 133 | |
---|
[1390] | 134 | tomcat = new Tomcat(); |
---|
| 135 | tomcat.setPort(DUMMY_SERVER_PORT); |
---|
| 136 | tomcat.setBaseDir(tomcatDir.getAbsolutePath()); |
---|
| 137 | |
---|
| 138 | File warFile = new File(webappRootDir, "dummyService.war"); |
---|
| 139 | tomcat.addWebapp("/dummyWebapp", warFile.getAbsolutePath()); |
---|
| 140 | System.out.println("configuring Dummy Service from " + warFile.getAbsolutePath()); |
---|
| 141 | |
---|
| 142 | File servletRootDir = new File(tomcatDir, "servlet"); |
---|
| 143 | servletRootDir.mkdirs(); |
---|
| 144 | Context ctx = tomcat.addContext("/", servletRootDir.getAbsolutePath()); |
---|
| 145 | Tomcat.addServlet(ctx, "dummyServlet", dummyServlet); |
---|
| 146 | ctx.addServletMapping("/dummyServlet", "dummyServlet"); |
---|
| 147 | |
---|
| 148 | tomcat.start(); |
---|
| 149 | |
---|
[1376] | 150 | setUpHook(); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * |
---|
| 155 | */ |
---|
| 156 | protected abstract void setUpHook() throws Exception; |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * |
---|
| 160 | */ |
---|
| 161 | @After |
---|
| 162 | public void tearDown() throws Exception { |
---|
| 163 | // give all started servers a second to finalize their internal work after the test |
---|
| 164 | // execution. This prevents some unnecessary exceptions due to requests already processed |
---|
| 165 | // by the test case but not finalized on server side. |
---|
| 166 | Thread.sleep(1000); |
---|
| 167 | tearDownHook(); |
---|
| 168 | |
---|
[1390] | 169 | if (tomcat != null) { |
---|
[1376] | 170 | try { |
---|
[1390] | 171 | tomcat.stop(); |
---|
| 172 | tomcat.getServer().await(); |
---|
| 173 | tomcat.destroy(); |
---|
[1376] | 174 | } |
---|
| 175 | finally { |
---|
[1390] | 176 | tomcat = null; |
---|
[1376] | 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | deleteFiles(new File(LOG_FILE_DIR)); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * |
---|
| 185 | */ |
---|
| 186 | protected abstract void tearDownHook() throws Exception; |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * |
---|
| 190 | */ |
---|
[1614] | 191 | protected String sendDummyMessage(String type, String query, String message, String respMsg) |
---|
[1376] | 192 | throws Exception |
---|
| 193 | { |
---|
| 194 | dummyServlet.setResponse(respMsg); |
---|
| 195 | |
---|
| 196 | System.err.println("sending message: " + message); |
---|
| 197 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
---|
| 198 | HttpRequestBase httpRequest = null; |
---|
| 199 | |
---|
[1614] | 200 | String uri = "http://localhost:" + PROXY_PORT + "/dummyServlet"; |
---|
| 201 | |
---|
| 202 | if (query != null) { |
---|
| 203 | uri += "?" + query; |
---|
| 204 | } |
---|
| 205 | |
---|
[1376] | 206 | if ("POST".equals(type)) { |
---|
[1614] | 207 | httpRequest = new HttpPost(uri); |
---|
[1376] | 208 | HttpEntity entity = new StringEntity(message, ContentType.TEXT_PLAIN); |
---|
| 209 | ((HttpPost) httpRequest).setEntity(entity); |
---|
| 210 | } |
---|
| 211 | else if ("GET".equals(type)) { |
---|
[1614] | 212 | httpRequest = new HttpGet(uri); |
---|
[1376] | 213 | } |
---|
| 214 | |
---|
| 215 | try { |
---|
| 216 | HttpResponse response = httpclient.execute(httpRequest); |
---|
[1567] | 217 | |
---|
| 218 | assertEquals(message, dummyServlet.getRequest()); |
---|
[1614] | 219 | assertEquals(query, dummyServlet.getQuery()); |
---|
[1390] | 220 | System.err.println(response.getStatusLine()); |
---|
[1376] | 221 | String responseStr = readStreamContentToString(response.getEntity().getContent()); |
---|
[1390] | 222 | System.err.println("received response: " + responseStr); |
---|
[1376] | 223 | return responseStr; |
---|
| 224 | } |
---|
| 225 | finally { |
---|
| 226 | httpRequest.releaseConnection(); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /** |
---|
[1392] | 231 | * |
---|
| 232 | */ |
---|
[1614] | 233 | protected void assertEvent(Event event, |
---|
| 234 | String method, |
---|
| 235 | String query, |
---|
| 236 | String message, |
---|
| 237 | String response) |
---|
| 238 | { |
---|
[1392] | 239 | assertNotNull(event); |
---|
| 240 | assertNotNull(event.getType()); |
---|
| 241 | assertNotNull(event.getTarget()); |
---|
| 242 | |
---|
| 243 | assertTrue(event.getType() instanceof HTTPEventType); |
---|
| 244 | assertTrue(event.getTarget() instanceof HTTPTarget); |
---|
| 245 | |
---|
[1614] | 246 | assertExchange(((HTTPEventType) event.getType()).getExchange(), |
---|
| 247 | method, query, message, response); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * |
---|
| 252 | */ |
---|
| 253 | protected void assertExchange(HttpExchange exchange, |
---|
| 254 | String method, |
---|
| 255 | String query, |
---|
| 256 | String message, |
---|
| 257 | String response) |
---|
| 258 | { |
---|
[1392] | 259 | assertEquals(Method.fromValue(method), exchange.getRequest().getMethod()); |
---|
| 260 | |
---|
[1614] | 261 | if (query != null) { |
---|
| 262 | assertEquals(query, exchange.getRequest().getQuery()); |
---|
| 263 | } |
---|
| 264 | else if (exchange.getRequest().getQuery() != null) { |
---|
| 265 | System.err.println(exchange.getRequest().getQuery()); |
---|
| 266 | fail("unexpected query"); |
---|
| 267 | } |
---|
| 268 | |
---|
[1392] | 269 | if (message != null) { |
---|
| 270 | assertEquals(message, exchange.getRequest().getContent().getData()); |
---|
| 271 | } |
---|
| 272 | else if (exchange.getRequest().getContent() != null) { |
---|
| 273 | System.err.println(exchange.getRequest().getContent().getData()); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | if (response != null) { |
---|
| 277 | assertEquals(response, exchange.getResponse().getContent().getData()); |
---|
| 278 | } |
---|
| 279 | else if (exchange.getResponse().getContent() != null) { |
---|
| 280 | System.err.println(exchange.getResponse().getContent().getData()); |
---|
| 281 | } |
---|
| 282 | } |
---|
[1376] | 283 | |
---|
| 284 | /** |
---|
| 285 | * |
---|
| 286 | */ |
---|
| 287 | protected void deleteFiles(File file) { |
---|
| 288 | if (file.exists()) { |
---|
| 289 | if (file.isDirectory()) { |
---|
| 290 | for (File child : file.listFiles()) { |
---|
| 291 | deleteFiles(child); |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | try { |
---|
| 296 | file.delete(); |
---|
| 297 | } |
---|
| 298 | catch (Exception e) { |
---|
| 299 | // ignore and delete as much as possible |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | } |
---|