[927] | 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 | |
---|
[864] | 15 | package de.ugoe.cs.autoquest.htmlmonitor; |
---|
| 16 | |
---|
| 17 | import static org.junit.Assert.*; |
---|
| 18 | |
---|
| 19 | import java.io.File; |
---|
[1069] | 20 | import java.util.Collection; |
---|
[1342] | 21 | import java.util.HashMap; |
---|
[1069] | 22 | import java.util.Iterator; |
---|
[864] | 23 | import java.util.List; |
---|
| 24 | |
---|
| 25 | import org.apache.http.HttpEntity; |
---|
| 26 | import org.apache.http.HttpResponse; |
---|
| 27 | import org.apache.http.client.methods.HttpPost; |
---|
| 28 | import org.apache.http.entity.ContentType; |
---|
| 29 | import org.apache.http.entity.StringEntity; |
---|
| 30 | import org.apache.http.impl.client.DefaultHttpClient; |
---|
| 31 | import org.junit.After; |
---|
| 32 | import org.junit.Before; |
---|
| 33 | import org.junit.Test; |
---|
| 34 | |
---|
[1069] | 35 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 36 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
| 37 | import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange; |
---|
| 38 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction; |
---|
| 39 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick; |
---|
| 40 | import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick; |
---|
| 41 | import de.ugoe.cs.autoquest.eventcore.gui.Scroll; |
---|
| 42 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
| 43 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
| 44 | import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser; |
---|
| 45 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLDocument; |
---|
| 46 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElement; |
---|
| 47 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer; |
---|
[864] | 48 | import de.ugoe.cs.util.console.TextConsole; |
---|
| 49 | |
---|
| 50 | /** |
---|
[1078] | 51 | * |
---|
[864] | 52 | * @author Patrick Harms |
---|
| 53 | */ |
---|
| 54 | public class HtmlMonitorTest { |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * |
---|
| 58 | */ |
---|
| 59 | public static final TextConsole CONSOLE = new TextConsole(); |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * |
---|
| 63 | */ |
---|
| 64 | private final static String LOG_FILE_DIR = "target/tmp/logfiles/"; |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * |
---|
| 68 | */ |
---|
| 69 | private static final int PORT = 19098; |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * |
---|
| 73 | */ |
---|
| 74 | private HtmlMonitor htmlMonitor; |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * |
---|
| 78 | */ |
---|
| 79 | @Before |
---|
| 80 | public void setUp() throws Exception { |
---|
| 81 | htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) }); |
---|
| 82 | htmlMonitor.init(); |
---|
| 83 | htmlMonitor.start(); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * |
---|
| 88 | */ |
---|
| 89 | @After |
---|
| 90 | public void tearDown() throws Exception { |
---|
| 91 | if (htmlMonitor != null) { |
---|
| 92 | try { |
---|
| 93 | htmlMonitor.stop(); |
---|
| 94 | } |
---|
| 95 | finally { |
---|
| 96 | htmlMonitor = null; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | deleteFiles(new File(LOG_FILE_DIR)); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * |
---|
| 105 | */ |
---|
| 106 | @Test |
---|
| 107 | public void testOneSimpleMessage() throws Exception { |
---|
| 108 | String clientId = "123"; |
---|
| 109 | |
---|
| 110 | String message = |
---|
| 111 | "{" + |
---|
| 112 | " \"message\": {" + |
---|
| 113 | " \"clientInfos\": {" + |
---|
| 114 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 115 | " \"userAgent\":\"Agent\"," + |
---|
| 116 | " \"title\":\"Title\"," + |
---|
| 117 | " \"url\":\"http://host/path\"" + |
---|
| 118 | " }," + |
---|
[1069] | 119 | " \"guiModel\": {" + |
---|
| 120 | " \"tagName\":\"html\"," + |
---|
| 121 | " \"index\":\"0\"," + |
---|
| 122 | " \"children\":" + |
---|
| 123 | " [ {" + |
---|
| 124 | " \"tagName\":\"head\"," + |
---|
| 125 | " \"index\":\"0\"," + |
---|
| 126 | " }," + |
---|
| 127 | " {" + |
---|
| 128 | " \"tagName\":\"body\"," + |
---|
| 129 | " \"htmlId\":\"gsr\"," + |
---|
| 130 | " }" + |
---|
| 131 | " ]" + |
---|
| 132 | " }," + |
---|
[864] | 133 | " \"events\":" + |
---|
| 134 | " [ {" + |
---|
| 135 | " \"time\":\"12345\"," + |
---|
[1069] | 136 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 137 | " \"eventType\":\"onclick\"" + |
---|
| 138 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
[864] | 139 | " }" + |
---|
| 140 | " ]" + |
---|
| 141 | " }" + |
---|
| 142 | "}"; |
---|
| 143 | |
---|
| 144 | sendMessageAndAssertResponse(message); |
---|
| 145 | |
---|
[1069] | 146 | htmlMonitor.stop(); |
---|
| 147 | htmlMonitor = null; |
---|
| 148 | |
---|
[1229] | 149 | File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 150 | clientId + File.separator + "htmlmonitor_" + clientId + "_000.log"); |
---|
[864] | 151 | |
---|
| 152 | assertTrue(logFile.exists()); |
---|
| 153 | |
---|
[1342] | 154 | HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>()); |
---|
[864] | 155 | |
---|
[1069] | 156 | parser.parseFile(logFile); |
---|
| 157 | |
---|
| 158 | // check the GUI model |
---|
| 159 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 160 | assertNotNull(guiModel); |
---|
| 161 | |
---|
| 162 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 163 | assertNotNull(nodes); |
---|
| 164 | assertEquals(1, nodes.size()); |
---|
| 165 | |
---|
| 166 | // get server node |
---|
| 167 | IGUIElement node = nodes.get(0); |
---|
| 168 | assertNotNull(node); |
---|
| 169 | assertTrue(node instanceof HTMLServer); |
---|
| 170 | assertEquals("HTML", node.getPlatform()); |
---|
| 171 | assertFalse(node.isUsed()); |
---|
| 172 | |
---|
| 173 | nodes = guiModel.getChildren(node); |
---|
| 174 | assertNotNull(nodes); |
---|
| 175 | assertEquals(1, nodes.size()); |
---|
| 176 | |
---|
| 177 | // get document node |
---|
| 178 | node = nodes.get(0); |
---|
| 179 | assertNotNull(node); |
---|
| 180 | assertTrue(node instanceof HTMLDocument); |
---|
| 181 | assertEquals("HTML", node.getPlatform()); |
---|
| 182 | assertFalse(node.isUsed()); |
---|
| 183 | |
---|
| 184 | nodes = guiModel.getChildren(node); |
---|
| 185 | assertNotNull(nodes); |
---|
| 186 | assertEquals(1, nodes.size()); |
---|
| 187 | |
---|
| 188 | // get html node |
---|
| 189 | node = nodes.get(0); |
---|
| 190 | assertNotNull(node); |
---|
| 191 | assertTrue(node instanceof HTMLPageElement); |
---|
| 192 | assertEquals("HTML", node.getPlatform()); |
---|
| 193 | assertFalse(node.isUsed()); |
---|
| 194 | |
---|
| 195 | nodes = guiModel.getChildren(node); |
---|
| 196 | assertNotNull(nodes); |
---|
| 197 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 198 | |
---|
| 199 | // get body node |
---|
| 200 | node = nodes.get(0); |
---|
| 201 | assertNotNull(node); |
---|
| 202 | assertTrue(node instanceof HTMLPageElement); |
---|
| 203 | assertEquals("HTML", node.getPlatform()); |
---|
| 204 | assertTrue(node.isUsed()); |
---|
| 205 | |
---|
| 206 | nodes = guiModel.getChildren(node); |
---|
| 207 | assertNotNull(nodes); |
---|
| 208 | assertEquals(0, nodes.size()); |
---|
| 209 | |
---|
| 210 | // check the sequences |
---|
| 211 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 212 | |
---|
| 213 | assertNotNull(sequences); |
---|
| 214 | |
---|
| 215 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 216 | assertTrue(iterator.hasNext()); |
---|
| 217 | |
---|
| 218 | List<Event> sequence = iterator.next(); |
---|
| 219 | assertFalse(iterator.hasNext()); |
---|
| 220 | |
---|
| 221 | assertNotNull(sequence); |
---|
| 222 | assertEquals(1, sequence.size()); |
---|
| 223 | |
---|
| 224 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
[864] | 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * |
---|
| 229 | */ |
---|
| 230 | @Test |
---|
| 231 | public void testSeveralMessagesInOneSession() throws Exception { |
---|
| 232 | String clientId = "123"; |
---|
| 233 | |
---|
| 234 | String message = |
---|
| 235 | "{" + |
---|
| 236 | " \"message\": {" + |
---|
| 237 | " \"clientInfos\": {" + |
---|
| 238 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 239 | " \"userAgent\":\"Agent\"," + |
---|
| 240 | " \"title\":\"Title\"," + |
---|
| 241 | " \"url\":\"http://host/path\"" + |
---|
| 242 | " }," + |
---|
[1069] | 243 | " \"guiModel\": {" + |
---|
| 244 | " \"tagName\":\"html\"," + |
---|
| 245 | " \"index\":\"0\"," + |
---|
| 246 | " \"children\":" + |
---|
| 247 | " [ {" + |
---|
| 248 | " \"tagName\":\"head\"," + |
---|
| 249 | " \"index\":\"0\"," + |
---|
| 250 | " }," + |
---|
| 251 | " {" + |
---|
| 252 | " \"tagName\":\"body\"," + |
---|
| 253 | " \"htmlId\":\"gsr\"," + |
---|
| 254 | " \"children\":" + |
---|
| 255 | " [ {" + |
---|
| 256 | " \"tagName\":\"input_button\"," + |
---|
| 257 | " \"htmlId\":\"input1\"," + |
---|
| 258 | " }," + |
---|
| 259 | " {" + |
---|
| 260 | " \"tagName\":\"input_button\"," + |
---|
| 261 | " \"htmlId\":\"input2\"," + |
---|
| 262 | " }," + |
---|
| 263 | " {" + |
---|
| 264 | " \"tagName\":\"input_button\"," + |
---|
| 265 | " \"htmlId\":\"input3\"," + |
---|
| 266 | " }," + |
---|
| 267 | " {" + |
---|
| 268 | " \"tagName\":\"input_button\"," + |
---|
| 269 | " \"htmlId\":\"input4\"," + |
---|
| 270 | " }," + |
---|
| 271 | " {" + |
---|
| 272 | " \"tagName\":\"input_button\"," + |
---|
| 273 | " \"htmlId\":\"input5\"," + |
---|
| 274 | " }," + |
---|
| 275 | " {" + |
---|
| 276 | " \"tagName\":\"input_button\"," + |
---|
| 277 | " \"htmlId\":\"input6\"," + |
---|
| 278 | " }," + |
---|
| 279 | " {" + |
---|
| 280 | " \"tagName\":\"input_button\"," + |
---|
| 281 | " \"htmlId\":\"input7\"," + |
---|
| 282 | " }," + |
---|
| 283 | " {" + |
---|
| 284 | " \"tagName\":\"input_button\"," + |
---|
| 285 | " \"htmlId\":\"input8\"," + |
---|
| 286 | " }," + |
---|
| 287 | " {" + |
---|
| 288 | " \"tagName\":\"input_button\"," + |
---|
| 289 | " \"htmlId\":\"input9\"," + |
---|
| 290 | " }," + |
---|
| 291 | " {" + |
---|
| 292 | " \"tagName\":\"input_button\"," + |
---|
| 293 | " \"htmlId\":\"input10\"," + |
---|
| 294 | " }," + |
---|
| 295 | " ]" + |
---|
| 296 | " }" + |
---|
| 297 | " ]" + |
---|
| 298 | " }," + |
---|
[864] | 299 | " \"events\":" + |
---|
[1069] | 300 | " [ {" + |
---|
[864] | 301 | " \"time\":\"1\"," + |
---|
[1069] | 302 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input1)\"," + |
---|
[864] | 303 | " \"eventType\":\"onclick\"," + |
---|
| 304 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 305 | " }," + |
---|
| 306 | " {" + |
---|
| 307 | " \"time\":\"2\"," + |
---|
[1069] | 308 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input2)\"," + |
---|
[864] | 309 | " \"eventType\":\"ondblclick\"," + |
---|
| 310 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 311 | " }," + |
---|
| 312 | " {" + |
---|
| 313 | " \"time\":\"3\"," + |
---|
[1069] | 314 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input3)\"," + |
---|
[864] | 315 | " \"eventType\":\"onfocus\"" + |
---|
| 316 | " }," + |
---|
| 317 | " {" + |
---|
| 318 | " \"time\":\"4\"," + |
---|
[1069] | 319 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input4)\"," + |
---|
[864] | 320 | " \"eventType\":\"onclick\"," + |
---|
| 321 | " \"coordinates\": [\"125\", \"14\"]" + |
---|
| 322 | " }," + |
---|
| 323 | " {" + |
---|
| 324 | " \"time\":\"5\"," + |
---|
[1069] | 325 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input5)\"," + |
---|
[864] | 326 | " \"eventType\":\"onfocus\"" + |
---|
| 327 | " }," + |
---|
| 328 | " {" + |
---|
| 329 | " \"time\":\"6\"," + |
---|
[1069] | 330 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input6)\"," + |
---|
[864] | 331 | " \"eventType\":\"onfocus\"" + |
---|
| 332 | " }," + |
---|
| 333 | " {" + |
---|
| 334 | " \"time\":\"7\"," + |
---|
[1069] | 335 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input7)\"," + |
---|
[864] | 336 | " \"eventType\":\"onfocus\"" + |
---|
| 337 | " }," + |
---|
| 338 | " {" + |
---|
| 339 | " \"time\":\"8\"," + |
---|
[1069] | 340 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input8)\"," + |
---|
[864] | 341 | " \"eventType\":\"onclick\"," + |
---|
| 342 | " \"coordinates\": [\"255\", \"4\"]" + |
---|
| 343 | " }," + |
---|
| 344 | " {" + |
---|
| 345 | " \"time\":\"9\"," + |
---|
[1069] | 346 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
[864] | 347 | " \"eventType\":\"onscroll\"," + |
---|
[943] | 348 | " \"scrollPosition\": [\"23\", \"567\"]" + |
---|
[864] | 349 | " }," + |
---|
| 350 | " {" + |
---|
| 351 | " \"time\":\"10\"," + |
---|
[1069] | 352 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input10)\"," + |
---|
[864] | 353 | " \"eventType\":\"onclick\"," + |
---|
| 354 | " \"coordinates\": [\"516\", \"154\"]" + |
---|
| 355 | " }" + |
---|
| 356 | " ]" + |
---|
| 357 | " }" + |
---|
| 358 | "}"; |
---|
| 359 | |
---|
| 360 | sendMessageAndAssertResponse(message); |
---|
| 361 | |
---|
[1069] | 362 | htmlMonitor.stop(); |
---|
| 363 | htmlMonitor = null; |
---|
| 364 | |
---|
[1229] | 365 | File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 366 | clientId + File.separator + "htmlmonitor_" + clientId + "_000.log"); |
---|
[864] | 367 | |
---|
| 368 | assertTrue(logFile.exists()); |
---|
| 369 | |
---|
[1342] | 370 | HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>()); |
---|
[864] | 371 | |
---|
[1069] | 372 | parser.parseFile(logFile); |
---|
| 373 | |
---|
| 374 | // check the GUI model |
---|
| 375 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 376 | assertNotNull(guiModel); |
---|
| 377 | |
---|
| 378 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 379 | assertNotNull(nodes); |
---|
| 380 | assertEquals(1, nodes.size()); |
---|
| 381 | |
---|
| 382 | // get server node |
---|
| 383 | IGUIElement node = nodes.get(0); |
---|
| 384 | assertNotNull(node); |
---|
| 385 | assertTrue(node instanceof HTMLServer); |
---|
| 386 | assertEquals("HTML", node.getPlatform()); |
---|
| 387 | assertFalse(node.isUsed()); |
---|
| 388 | |
---|
| 389 | nodes = guiModel.getChildren(node); |
---|
| 390 | assertNotNull(nodes); |
---|
| 391 | assertEquals(1, nodes.size()); |
---|
| 392 | |
---|
| 393 | // get document node |
---|
| 394 | node = nodes.get(0); |
---|
| 395 | assertNotNull(node); |
---|
| 396 | assertTrue(node instanceof HTMLDocument); |
---|
| 397 | assertEquals("HTML", node.getPlatform()); |
---|
| 398 | assertFalse(node.isUsed()); |
---|
| 399 | |
---|
| 400 | nodes = guiModel.getChildren(node); |
---|
| 401 | assertNotNull(nodes); |
---|
| 402 | assertEquals(1, nodes.size()); |
---|
| 403 | |
---|
| 404 | // get html node |
---|
| 405 | node = nodes.get(0); |
---|
| 406 | assertNotNull(node); |
---|
| 407 | assertTrue(node instanceof HTMLPageElement); |
---|
| 408 | assertEquals("HTML", node.getPlatform()); |
---|
| 409 | assertFalse(node.isUsed()); |
---|
| 410 | |
---|
| 411 | nodes = guiModel.getChildren(node); |
---|
| 412 | assertNotNull(nodes); |
---|
| 413 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 414 | |
---|
| 415 | // get body node |
---|
| 416 | IGUIElement body = nodes.get(0); |
---|
| 417 | assertNotNull(body); |
---|
| 418 | assertTrue(body instanceof HTMLPageElement); |
---|
| 419 | assertEquals("HTML", body.getPlatform()); |
---|
| 420 | assertTrue(body.isUsed()); |
---|
| 421 | |
---|
| 422 | nodes = guiModel.getChildren(body); |
---|
| 423 | assertNotNull(nodes); |
---|
| 424 | |
---|
[1315] | 425 | // wait for only 9 nodes. The directly used GUI elements are 10, but one (the body) is the |
---|
| 426 | // parent of the others. input9 is unused and must, therefore, not be logged. |
---|
| 427 | assertEquals(9, nodes.size()); |
---|
| 428 | |
---|
[1069] | 429 | // get input nodes |
---|
| 430 | for (int i = 0; i < nodes.size(); i++) { |
---|
| 431 | node = nodes.get(i); |
---|
| 432 | assertNotNull(node); |
---|
| 433 | assertTrue(node instanceof HTMLPageElement); |
---|
| 434 | assertEquals("HTML", node.getPlatform()); |
---|
[1315] | 435 | assertTrue(node.isUsed()); |
---|
[1069] | 436 | |
---|
| 437 | assertNotNull(guiModel.getChildren(node)); |
---|
| 438 | assertEquals(0, guiModel.getChildren(node).size()); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | // check the sequences |
---|
| 442 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 443 | |
---|
| 444 | assertNotNull(sequences); |
---|
| 445 | |
---|
| 446 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 447 | assertTrue(iterator.hasNext()); |
---|
| 448 | |
---|
| 449 | List<Event> sequence = iterator.next(); |
---|
| 450 | assertFalse(iterator.hasNext()); |
---|
| 451 | |
---|
| 452 | assertNotNull(sequence); |
---|
| 453 | assertEquals(10, sequence.size()); |
---|
| 454 | |
---|
| 455 | assertEvent(sequence.get(0), 1, MouseClick.class, nodes.get(0), 194, 7); |
---|
| 456 | assertEvent(sequence.get(1), 2, MouseDoubleClick.class, nodes.get(1), 194, 7); |
---|
| 457 | assertEvent(sequence.get(2), 3, KeyboardFocusChange.class, nodes.get(2), 0, 0); |
---|
| 458 | assertEvent(sequence.get(3), 4, MouseClick.class, nodes.get(3), 125, 14); |
---|
| 459 | assertEvent(sequence.get(4), 5, KeyboardFocusChange.class, nodes.get(4), 0, 0); |
---|
| 460 | assertEvent(sequence.get(5), 6, KeyboardFocusChange.class, nodes.get(5), 0, 0); |
---|
| 461 | assertEvent(sequence.get(6), 7, KeyboardFocusChange.class, nodes.get(6), 0, 0); |
---|
| 462 | assertEvent(sequence.get(7), 8, MouseClick.class, nodes.get(7), 255, 4); |
---|
| 463 | assertEvent(sequence.get(8), 9, Scroll.class, body, 0, 0); |
---|
[1315] | 464 | assertEvent(sequence.get(9), 10, MouseClick.class, nodes.get(8), 516, 154); |
---|
[1069] | 465 | |
---|
[864] | 466 | } |
---|
| 467 | |
---|
| 468 | /** |
---|
| 469 | * |
---|
| 470 | */ |
---|
| 471 | @Test |
---|
| 472 | public void testSeveralSessions() throws Exception { |
---|
| 473 | String clientId = "123"; |
---|
| 474 | |
---|
| 475 | String message = |
---|
| 476 | "{" + |
---|
| 477 | " \"message\": {" + |
---|
| 478 | " \"clientInfos\": {" + |
---|
| 479 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 480 | " \"userAgent\":\"Agent\"," + |
---|
| 481 | " \"title\":\"Title\"," + |
---|
| 482 | " \"url\":\"http://host/path\"" + |
---|
| 483 | " }," + |
---|
[1069] | 484 | " \"guiModel\": {" + |
---|
| 485 | " \"tagName\":\"html\"," + |
---|
| 486 | " \"index\":\"0\"," + |
---|
| 487 | " \"children\":" + |
---|
| 488 | " [ {" + |
---|
| 489 | " \"tagName\":\"head\"," + |
---|
| 490 | " \"index\":\"0\"," + |
---|
| 491 | " }," + |
---|
| 492 | " {" + |
---|
| 493 | " \"tagName\":\"body\"," + |
---|
| 494 | " \"htmlId\":\"gsr\"," + |
---|
| 495 | " }" + |
---|
| 496 | " ]" + |
---|
| 497 | " }," + |
---|
[864] | 498 | " \"events\":" + |
---|
| 499 | " [ {" + |
---|
| 500 | " \"time\":\"12345\"," + |
---|
[1069] | 501 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 502 | " \"eventType\":\"onclick\"" + |
---|
| 503 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
[864] | 504 | " }" + |
---|
| 505 | " ]" + |
---|
| 506 | " }" + |
---|
| 507 | "}"; |
---|
| 508 | |
---|
| 509 | sendMessageAndAssertResponse(message); |
---|
| 510 | |
---|
| 511 | int numberOfSessions = 10; |
---|
| 512 | for (int i = 0; i < numberOfSessions; i++) { |
---|
| 513 | htmlMonitor.stop(); |
---|
| 514 | htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) }); |
---|
| 515 | htmlMonitor.init(); |
---|
| 516 | htmlMonitor.start(); |
---|
| 517 | sendMessageAndAssertResponse(message); |
---|
| 518 | } |
---|
| 519 | |
---|
[1069] | 520 | htmlMonitor.stop(); |
---|
| 521 | htmlMonitor = null; |
---|
| 522 | |
---|
[1342] | 523 | HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>()); |
---|
[1069] | 524 | |
---|
[864] | 525 | // assert 9 already rotated log files |
---|
[1069] | 526 | for (int i = 0; i < numberOfSessions; i++) { |
---|
[1229] | 527 | File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 528 | clientId + File.separator + "htmlmonitor_" + clientId + "_00" + |
---|
| 529 | i + ".log"); |
---|
[864] | 530 | |
---|
| 531 | assertTrue(logFile.exists()); |
---|
| 532 | |
---|
[1069] | 533 | parser.parseFile(logFile); |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | // check the GUI model |
---|
| 537 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 538 | assertNotNull(guiModel); |
---|
| 539 | |
---|
| 540 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 541 | assertNotNull(nodes); |
---|
| 542 | assertEquals(1, nodes.size()); |
---|
| 543 | |
---|
| 544 | // get server node |
---|
| 545 | IGUIElement node = nodes.get(0); |
---|
| 546 | assertNotNull(node); |
---|
| 547 | assertTrue(node instanceof HTMLServer); |
---|
| 548 | assertEquals("HTML", node.getPlatform()); |
---|
| 549 | assertFalse(node.isUsed()); |
---|
| 550 | |
---|
| 551 | nodes = guiModel.getChildren(node); |
---|
| 552 | assertNotNull(nodes); |
---|
| 553 | assertEquals(1, nodes.size()); |
---|
| 554 | |
---|
| 555 | // get document node |
---|
| 556 | node = nodes.get(0); |
---|
| 557 | assertNotNull(node); |
---|
| 558 | assertTrue(node instanceof HTMLDocument); |
---|
| 559 | assertEquals("HTML", node.getPlatform()); |
---|
| 560 | assertFalse(node.isUsed()); |
---|
| 561 | |
---|
| 562 | nodes = guiModel.getChildren(node); |
---|
| 563 | assertNotNull(nodes); |
---|
| 564 | assertEquals(1, nodes.size()); |
---|
| 565 | |
---|
| 566 | // get html node |
---|
| 567 | node = nodes.get(0); |
---|
| 568 | assertNotNull(node); |
---|
| 569 | assertTrue(node instanceof HTMLPageElement); |
---|
| 570 | assertEquals("HTML", node.getPlatform()); |
---|
| 571 | assertFalse(node.isUsed()); |
---|
| 572 | |
---|
| 573 | nodes = guiModel.getChildren(node); |
---|
| 574 | assertNotNull(nodes); |
---|
| 575 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 576 | |
---|
| 577 | // get body node |
---|
| 578 | node = nodes.get(0); |
---|
| 579 | assertNotNull(node); |
---|
| 580 | assertTrue(node instanceof HTMLPageElement); |
---|
| 581 | assertEquals("HTML", node.getPlatform()); |
---|
| 582 | assertTrue(node.isUsed()); |
---|
| 583 | |
---|
| 584 | nodes = guiModel.getChildren(node); |
---|
| 585 | assertNotNull(nodes); |
---|
| 586 | assertEquals(0, nodes.size()); |
---|
| 587 | |
---|
| 588 | // check the sequences |
---|
| 589 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 590 | |
---|
| 591 | assertNotNull(sequences); |
---|
| 592 | assertEquals(numberOfSessions, sequences.size()); |
---|
| 593 | |
---|
| 594 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 595 | |
---|
| 596 | while (iterator.hasNext()) { |
---|
| 597 | List<Event> sequence = iterator.next(); |
---|
| 598 | |
---|
| 599 | assertNotNull(sequence); |
---|
| 600 | assertEquals(1, sequence.size()); |
---|
[864] | 601 | |
---|
[1069] | 602 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
[864] | 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | /** |
---|
| 607 | * |
---|
| 608 | */ |
---|
[1229] | 609 | @Test |
---|
| 610 | public void testRevertOfOldFiles() throws Exception { |
---|
| 611 | String clientId = "123"; |
---|
| 612 | |
---|
| 613 | String message = |
---|
| 614 | "{" + |
---|
| 615 | " \"message\": {" + |
---|
| 616 | " \"clientInfos\": {" + |
---|
| 617 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 618 | " \"userAgent\":\"Agent\"," + |
---|
| 619 | " \"title\":\"Title\"," + |
---|
| 620 | " \"url\":\"http://host/path\"" + |
---|
| 621 | " }," + |
---|
| 622 | " \"guiModel\": {" + |
---|
| 623 | " \"tagName\":\"html\"," + |
---|
| 624 | " \"index\":\"0\"," + |
---|
| 625 | " \"children\":" + |
---|
| 626 | " [ {" + |
---|
| 627 | " \"tagName\":\"head\"," + |
---|
| 628 | " \"index\":\"0\"," + |
---|
| 629 | " }," + |
---|
| 630 | " {" + |
---|
| 631 | " \"tagName\":\"body\"," + |
---|
| 632 | " \"htmlId\":\"gsr\"," + |
---|
| 633 | " }" + |
---|
| 634 | " ]" + |
---|
| 635 | " }," + |
---|
| 636 | " \"events\":" + |
---|
| 637 | " [ {" + |
---|
| 638 | " \"time\":\"12345\"," + |
---|
| 639 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 640 | " \"eventType\":\"onclick\"" + |
---|
| 641 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 642 | " }" + |
---|
| 643 | " ]" + |
---|
| 644 | " }" + |
---|
| 645 | "}"; |
---|
| 646 | |
---|
| 647 | sendMessageAndAssertResponse(message); |
---|
| 648 | |
---|
| 649 | htmlMonitor.stop(); |
---|
| 650 | htmlMonitor = null; |
---|
| 651 | |
---|
| 652 | File logFile1 = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 653 | clientId + File.separator + "htmlmonitor_" + clientId + "_000.log"); |
---|
| 654 | |
---|
| 655 | assertTrue(logFile1.exists()); |
---|
| 656 | |
---|
| 657 | // Move file to a the directory in which it would resist in the old structure and then |
---|
| 658 | // restart the server, resend the message and ensure two separate log files in the new |
---|
| 659 | // structure |
---|
| 660 | File oldLogFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator + |
---|
| 661 | "htmlmonitor_" + clientId + "_000.log"); |
---|
| 662 | |
---|
| 663 | assertTrue(oldLogFile.getParentFile().mkdirs()); |
---|
| 664 | assertTrue(logFile1.renameTo(oldLogFile)); |
---|
| 665 | |
---|
| 666 | htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) }); |
---|
| 667 | htmlMonitor.init(); |
---|
| 668 | htmlMonitor.start(); |
---|
| 669 | |
---|
| 670 | sendMessageAndAssertResponse(message); |
---|
| 671 | |
---|
| 672 | htmlMonitor.stop(); |
---|
| 673 | htmlMonitor = null; |
---|
| 674 | |
---|
| 675 | assertTrue(logFile1.exists()); |
---|
| 676 | |
---|
| 677 | File logFile2 = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 678 | clientId + File.separator + "htmlmonitor_" + clientId + "_001.log"); |
---|
| 679 | |
---|
| 680 | assertTrue(logFile2.exists()); |
---|
| 681 | |
---|
[1342] | 682 | HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>()); |
---|
[1229] | 683 | |
---|
| 684 | parser.parseFile(logFile1); |
---|
| 685 | parser.parseFile(logFile2); |
---|
| 686 | |
---|
| 687 | // check the GUI model |
---|
| 688 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 689 | assertNotNull(guiModel); |
---|
| 690 | |
---|
| 691 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 692 | assertNotNull(nodes); |
---|
| 693 | assertEquals(1, nodes.size()); |
---|
| 694 | |
---|
| 695 | // get server node |
---|
| 696 | IGUIElement node = nodes.get(0); |
---|
| 697 | assertNotNull(node); |
---|
| 698 | assertTrue(node instanceof HTMLServer); |
---|
| 699 | assertEquals("HTML", node.getPlatform()); |
---|
| 700 | assertFalse(node.isUsed()); |
---|
| 701 | |
---|
| 702 | nodes = guiModel.getChildren(node); |
---|
| 703 | assertNotNull(nodes); |
---|
| 704 | assertEquals(1, nodes.size()); |
---|
| 705 | |
---|
| 706 | // get document node |
---|
| 707 | node = nodes.get(0); |
---|
| 708 | assertNotNull(node); |
---|
| 709 | assertTrue(node instanceof HTMLDocument); |
---|
| 710 | assertEquals("HTML", node.getPlatform()); |
---|
| 711 | assertFalse(node.isUsed()); |
---|
| 712 | |
---|
| 713 | nodes = guiModel.getChildren(node); |
---|
| 714 | assertNotNull(nodes); |
---|
| 715 | assertEquals(1, nodes.size()); |
---|
| 716 | |
---|
| 717 | // get html node |
---|
| 718 | node = nodes.get(0); |
---|
| 719 | assertNotNull(node); |
---|
| 720 | assertTrue(node instanceof HTMLPageElement); |
---|
| 721 | assertEquals("HTML", node.getPlatform()); |
---|
| 722 | assertFalse(node.isUsed()); |
---|
| 723 | |
---|
| 724 | nodes = guiModel.getChildren(node); |
---|
| 725 | assertNotNull(nodes); |
---|
| 726 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 727 | |
---|
| 728 | // get body node |
---|
| 729 | node = nodes.get(0); |
---|
| 730 | assertNotNull(node); |
---|
| 731 | assertTrue(node instanceof HTMLPageElement); |
---|
| 732 | assertEquals("HTML", node.getPlatform()); |
---|
| 733 | assertTrue(node.isUsed()); |
---|
| 734 | |
---|
| 735 | nodes = guiModel.getChildren(node); |
---|
| 736 | assertNotNull(nodes); |
---|
| 737 | assertEquals(0, nodes.size()); |
---|
| 738 | |
---|
| 739 | // check the sequences |
---|
| 740 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 741 | |
---|
| 742 | assertNotNull(sequences); |
---|
| 743 | |
---|
| 744 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 745 | assertTrue(iterator.hasNext()); |
---|
| 746 | |
---|
| 747 | List<Event> sequence = iterator.next(); |
---|
| 748 | |
---|
| 749 | assertNotNull(sequence); |
---|
| 750 | assertEquals(1, sequence.size()); |
---|
| 751 | |
---|
| 752 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
| 753 | |
---|
| 754 | assertTrue(iterator.hasNext()); |
---|
| 755 | |
---|
| 756 | sequence = iterator.next(); |
---|
| 757 | assertFalse(iterator.hasNext()); |
---|
| 758 | |
---|
| 759 | assertNotNull(sequence); |
---|
| 760 | assertEquals(1, sequence.size()); |
---|
| 761 | |
---|
| 762 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | /** |
---|
| 766 | * |
---|
| 767 | */ |
---|
[864] | 768 | private void sendMessageAndAssertResponse(String message) throws Exception { |
---|
| 769 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
---|
| 770 | HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/"); |
---|
| 771 | HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON); |
---|
| 772 | httpPost.setEntity(entity); |
---|
| 773 | |
---|
| 774 | try { |
---|
| 775 | HttpResponse response = httpclient.execute(httpPost); |
---|
| 776 | |
---|
| 777 | // the monitor always returns 200 without any additional information. The client must |
---|
| 778 | // never get more or less information. This is especially important for preventing |
---|
| 779 | // hackers from finding out more |
---|
| 780 | assertEquals(200, response.getStatusLine().getStatusCode()); |
---|
| 781 | assertTrue |
---|
[1245] | 782 | ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0) || |
---|
| 783 | ((response.getEntity().getContentLength() == 1) && |
---|
| 784 | (response.getEntity().getContent().read() == ' '))); |
---|
[864] | 785 | } |
---|
| 786 | finally { |
---|
| 787 | httpPost.releaseConnection(); |
---|
| 788 | } |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | /** |
---|
| 792 | * |
---|
| 793 | */ |
---|
[1069] | 794 | private void assertEvent(Event event, |
---|
| 795 | int timestamp, |
---|
| 796 | Class<? extends IEventType> eventType, |
---|
| 797 | IGUIElement eventTarget, |
---|
| 798 | int xCoordinate, |
---|
| 799 | int yCoordinate) |
---|
[864] | 800 | { |
---|
[1069] | 801 | assertEquals(timestamp, event.getTimestamp()); |
---|
| 802 | assertTrue(eventType.isInstance(event.getType())); |
---|
| 803 | assertEquals(eventTarget, event.getTarget()); |
---|
[864] | 804 | |
---|
[1069] | 805 | if (event.getType() instanceof MouseButtonInteraction) { |
---|
| 806 | assertEquals(xCoordinate, ((MouseButtonInteraction) event.getType()).getX()); |
---|
| 807 | assertEquals(yCoordinate, ((MouseButtonInteraction) event.getType()).getY()); |
---|
[864] | 808 | } |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | /** |
---|
| 812 | * |
---|
| 813 | */ |
---|
| 814 | private void deleteFiles(File file) { |
---|
| 815 | if (file.exists()) { |
---|
| 816 | if (file.isDirectory()) { |
---|
| 817 | for (File child : file.listFiles()) { |
---|
| 818 | deleteFiles(child); |
---|
| 819 | } |
---|
| 820 | } |
---|
| 821 | |
---|
| 822 | try { |
---|
| 823 | file.delete(); |
---|
| 824 | } |
---|
| 825 | catch (Exception e) { |
---|
| 826 | // ignore and delete as much as possible |
---|
| 827 | } |
---|
| 828 | } |
---|
| 829 | } |
---|
| 830 | |
---|
| 831 | } |
---|