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