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