source: trunk/autoquest-htmlmonitor-test/src/test/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorTest.java @ 1429

Last change on this file since 1429 was 1429, checked in by pharms, 10 years ago
  • adapted logging of GUI elements to ensure that all GUI elements on the same page are logged at least once
File size: 28.6 KB
Line 
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
15package de.ugoe.cs.autoquest.htmlmonitor;
16
17import static org.junit.Assert.*;
18
19import java.io.File;
20import java.util.Collection;
21import java.util.HashMap;
22import java.util.Iterator;
23import java.util.List;
24
25import org.apache.http.HttpEntity;
26import org.apache.http.HttpResponse;
27import org.apache.http.client.methods.HttpPost;
28import org.apache.http.entity.ContentType;
29import org.apache.http.entity.StringEntity;
30import org.apache.http.impl.client.DefaultHttpClient;
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34
35import de.ugoe.cs.autoquest.eventcore.Event;
36import de.ugoe.cs.autoquest.eventcore.IEventType;
37import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange;
38import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction;
39import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
40import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick;
41import de.ugoe.cs.autoquest.eventcore.gui.Scroll;
42import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
43import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
44import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
45import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLDocument;
46import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElement;
47import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer;
48import de.ugoe.cs.util.console.TextConsole;
49
50/**
51 * 
52 * @author Patrick Harms
53 */
54public 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            "    }," +
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            "    }," +
133            "    \"events\":" +
134            "    [ {" +
135            "        \"time\":\"12345\"," +
136            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
137            "        \"eventType\":\"onclick\"" +
138            "        \"coordinates\": [\"194\", \"7\"]" +
139            "      }" +
140            "    ]" +
141            "  }" +
142            "}";
143
144        sendMessageAndAssertResponse(message);
145       
146        htmlMonitor.stop();
147        htmlMonitor = null;
148
149        File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator +
150                                clientId + File.separator + "htmlmonitor_" + clientId + "_000.log");
151       
152        assertTrue(logFile.exists());
153       
154        HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>());
155       
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);
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            "    }," +
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            "    }," +
299            "    \"events\":" +
300            "    [ {" +
301            "        \"time\":\"1\"," +
302            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input1)\"," +
303            "        \"eventType\":\"onclick\"," +
304            "        \"coordinates\": [\"194\", \"7\"]" +
305            "      }," +
306            "      {" +
307            "        \"time\":\"2\"," +
308            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input2)\"," +
309            "        \"eventType\":\"ondblclick\"," +
310            "        \"coordinates\": [\"194\", \"7\"]" +
311            "      }," +
312            "      {" +
313            "        \"time\":\"3\"," +
314            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input3)\"," +
315            "        \"eventType\":\"onfocus\"" +
316            "      }," +
317            "      {" +
318            "        \"time\":\"4\"," +
319            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input4)\"," +
320            "        \"eventType\":\"onclick\"," +
321            "        \"coordinates\": [\"125\", \"14\"]" +
322            "      }," +
323            "      {" +
324            "        \"time\":\"5\"," +
325            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input5)\"," +
326            "        \"eventType\":\"onfocus\"" +
327            "      }," +
328            "      {" +
329            "        \"time\":\"6\"," +
330            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input6)\"," +
331            "        \"eventType\":\"onfocus\"" +
332            "      }," +
333            "      {" +
334            "        \"time\":\"7\"," +
335            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input7)\"," +
336            "        \"eventType\":\"onfocus\"" +
337            "      }," +
338            "      {" +
339            "        \"time\":\"8\"," +
340            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input8)\"," +
341            "        \"eventType\":\"onclick\"," +
342            "        \"coordinates\": [\"255\", \"4\"]" +
343            "      }," +
344            "      {" +
345            "        \"time\":\"9\"," +
346            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
347            "        \"eventType\":\"onscroll\"," +
348            "        \"scrollPosition\": [\"23\", \"567\"]" +
349            "      }," +
350            "      {" +
351            "        \"time\":\"10\"," +
352            "        \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input10)\"," +
353            "        \"eventType\":\"onclick\"," +
354            "        \"coordinates\": [\"516\", \"154\"]" +
355            "      }" +
356            "    ]" +
357            "  }" +
358            "}";
359 
360        sendMessageAndAssertResponse(message);
361       
362        htmlMonitor.stop();
363        htmlMonitor = null;
364
365        File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator +
366                                clientId + File.separator + "htmlmonitor_" + clientId + "_000.log");
367       
368        assertTrue(logFile.exists());
369       
370        HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>());
371       
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       
425        // wait for all 10 GUI elements on the same page to be logged although only 9 are used
426        assertEquals(10, nodes.size());
427       
428        // get input nodes
429        for (int i = 0; i < nodes.size(); i++) {
430            node = nodes.get(i);
431            assertNotNull(node);
432            assertTrue(node instanceof HTMLPageElement);
433            assertEquals("HTML", node.getPlatform());
434           
435            if (!"input9".equals(((HTMLPageElement) node).getHtmlId())) {
436                assertTrue(node.isUsed());
437            }
438            else {
439                assertFalse(node.isUsed());
440            }
441
442            assertNotNull(guiModel.getChildren(node));
443            assertEquals(0, guiModel.getChildren(node).size());
444        }
445       
446        // check the sequences
447        Collection<List<Event>> sequences = parser.getSequences();
448       
449        assertNotNull(sequences);
450       
451        Iterator<List<Event>> iterator = sequences.iterator();
452        assertTrue(iterator.hasNext());
453       
454        List<Event> sequence = iterator.next();
455        assertFalse(iterator.hasNext());
456       
457        assertNotNull(sequence);
458        assertEquals(10, sequence.size());
459       
460        assertEvent(sequence.get(0), 1, MouseClick.class, nodes.get(0), 194, 7);
461        assertEvent(sequence.get(1), 2, MouseDoubleClick.class, nodes.get(1), 194, 7);
462        assertEvent(sequence.get(2), 3, KeyboardFocusChange.class, nodes.get(2), 0, 0);
463        assertEvent(sequence.get(3), 4, MouseClick.class, nodes.get(3), 125, 14);
464        assertEvent(sequence.get(4), 5, KeyboardFocusChange.class, nodes.get(4), 0, 0);
465        assertEvent(sequence.get(5), 6, KeyboardFocusChange.class, nodes.get(5), 0, 0);
466        assertEvent(sequence.get(6), 7, KeyboardFocusChange.class, nodes.get(6), 0, 0);
467        assertEvent(sequence.get(7), 8, MouseClick.class, nodes.get(7), 255, 4);
468        assertEvent(sequence.get(8), 9, Scroll.class, body, 0, 0);
469        assertEvent(sequence.get(9), 10, MouseClick.class, nodes.get(9), 516, 154);
470
471    }
472
473    /**
474     *
475     */
476    @Test
477    public void testSeveralSessions() throws Exception {
478        String clientId = "123";
479       
480        String message =
481            "{" +
482            "  \"message\": {" +
483            "    \"clientInfos\": {" +
484            "      \"clientId\":\"" + clientId + "\"," +
485            "      \"userAgent\":\"Agent\"," +
486            "      \"title\":\"Title\"," +
487            "      \"url\":\"http://host/path\"" +
488            "    }," +
489            "    \"guiModel\": {" +
490            "      \"tagName\":\"html\"," +
491            "      \"index\":\"0\"," +
492            "      \"children\":" +
493            "      [ {" +
494            "          \"tagName\":\"head\"," +
495            "          \"index\":\"0\"," +
496            "        }," +
497            "        {" +
498            "          \"tagName\":\"body\"," +
499            "          \"htmlId\":\"gsr\"," +
500            "        }" +
501            "      ]" +
502            "    }," +
503            "    \"events\":" +
504            "    [ {" +
505            "        \"time\":\"12345\"," +
506            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
507            "        \"eventType\":\"onclick\"" +
508            "        \"coordinates\": [\"194\", \"7\"]" +
509            "      }" +
510            "    ]" +
511            "  }" +
512            "}";
513
514        sendMessageAndAssertResponse(message);
515       
516        int numberOfSessions = 10;
517        for (int i = 0; i < numberOfSessions; i++) {
518            htmlMonitor.stop();
519            htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) });
520            htmlMonitor.init();
521            htmlMonitor.start();
522            sendMessageAndAssertResponse(message);
523        }
524       
525        htmlMonitor.stop();
526        htmlMonitor = null;
527       
528        HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>());
529       
530        // assert 9 already rotated log files
531        for (int i = 0; i < numberOfSessions; i++) {
532            File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator +
533                                    clientId + File.separator + "htmlmonitor_" + clientId + "_00" +
534                                    i + ".log");
535       
536            assertTrue(logFile.exists());
537       
538            parser.parseFile(logFile);
539        }
540
541        // check the GUI model
542        GUIModel guiModel = parser.getGuiModel();
543        assertNotNull(guiModel);
544       
545        List<IGUIElement> nodes = guiModel.getRootElements();
546        assertNotNull(nodes);
547        assertEquals(1, nodes.size());
548       
549        // get server node
550        IGUIElement node = nodes.get(0);
551        assertNotNull(node);
552        assertTrue(node instanceof HTMLServer);
553        assertEquals("HTML", node.getPlatform());
554        assertFalse(node.isUsed());
555       
556        nodes = guiModel.getChildren(node);
557        assertNotNull(nodes);
558        assertEquals(1, nodes.size());
559       
560        // get document node
561        node = nodes.get(0);
562        assertNotNull(node);
563        assertTrue(node instanceof HTMLDocument);
564        assertEquals("HTML", node.getPlatform());
565        assertFalse(node.isUsed());
566       
567        nodes = guiModel.getChildren(node);
568        assertNotNull(nodes);
569        assertEquals(1, nodes.size());
570       
571        // get html node
572        node = nodes.get(0);
573        assertNotNull(node);
574        assertTrue(node instanceof HTMLPageElement);
575        assertEquals("HTML", node.getPlatform());
576        assertFalse(node.isUsed());
577       
578        nodes = guiModel.getChildren(node);
579        assertNotNull(nodes);
580        assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored
581       
582        // get body node
583        node = nodes.get(0);
584        assertNotNull(node);
585        assertTrue(node instanceof HTMLPageElement);
586        assertEquals("HTML", node.getPlatform());
587        assertTrue(node.isUsed());
588
589        nodes = guiModel.getChildren(node);
590        assertNotNull(nodes);
591        assertEquals(0, nodes.size());
592       
593        // check the sequences
594        Collection<List<Event>> sequences = parser.getSequences();
595       
596        assertNotNull(sequences);
597        assertEquals(numberOfSessions, sequences.size());
598       
599        Iterator<List<Event>> iterator = sequences.iterator();
600       
601        while (iterator.hasNext()) {
602            List<Event> sequence = iterator.next();
603       
604            assertNotNull(sequence);
605            assertEquals(1, sequence.size());
606       
607            assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7);
608        }
609    }
610
611    /**
612     *
613     */
614    @Test
615    public void testRevertOfOldFiles() throws Exception {
616        String clientId = "123";
617       
618        String message =
619            "{" +
620            "  \"message\": {" +
621            "    \"clientInfos\": {" +
622            "      \"clientId\":\"" + clientId + "\"," +
623            "      \"userAgent\":\"Agent\"," +
624            "      \"title\":\"Title\"," +
625            "      \"url\":\"http://host/path\"" +
626            "    }," +
627            "    \"guiModel\": {" +
628            "      \"tagName\":\"html\"," +
629            "      \"index\":\"0\"," +
630            "      \"children\":" +
631            "      [ {" +
632            "          \"tagName\":\"head\"," +
633            "          \"index\":\"0\"," +
634            "        }," +
635            "        {" +
636            "          \"tagName\":\"body\"," +
637            "          \"htmlId\":\"gsr\"," +
638            "        }" +
639            "      ]" +
640            "    }," +
641            "    \"events\":" +
642            "    [ {" +
643            "        \"time\":\"12345\"," +
644            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
645            "        \"eventType\":\"onclick\"" +
646            "        \"coordinates\": [\"194\", \"7\"]" +
647            "      }" +
648            "    ]" +
649            "  }" +
650            "}";
651
652        sendMessageAndAssertResponse(message);
653       
654        htmlMonitor.stop();
655        htmlMonitor = null;
656
657        File logFile1 = new File(LOG_FILE_DIR + File.separator + "host" + File.separator +
658                                 clientId + File.separator + "htmlmonitor_" + clientId + "_000.log");
659       
660        assertTrue(logFile1.exists());
661       
662        // Move file to a the directory in which it would resist in the old structure and then
663        // restart the server, resend the message and ensure two separate log files in the new
664        // structure
665        File oldLogFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator +
666                                   "htmlmonitor_" + clientId + "_000.log");
667       
668        assertTrue(oldLogFile.getParentFile().mkdirs());
669        assertTrue(logFile1.renameTo(oldLogFile));
670
671        htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) });
672        htmlMonitor.init();
673        htmlMonitor.start();
674
675        sendMessageAndAssertResponse(message);
676       
677        htmlMonitor.stop();
678        htmlMonitor = null;
679
680        assertTrue(logFile1.exists());
681
682        File logFile2 = new File(LOG_FILE_DIR + File.separator + "host" + File.separator +
683                                 clientId + File.separator + "htmlmonitor_" + clientId + "_001.log");
684         
685        assertTrue(logFile2.exists());
686         
687        HTMLLogParser parser = new HTMLLogParser(new HashMap<String, List<String>>());
688       
689        parser.parseFile(logFile1);
690        parser.parseFile(logFile2);
691       
692        // check the GUI model
693        GUIModel guiModel = parser.getGuiModel();
694        assertNotNull(guiModel);
695       
696        List<IGUIElement> nodes = guiModel.getRootElements();
697        assertNotNull(nodes);
698        assertEquals(1, nodes.size());
699       
700        // get server node
701        IGUIElement node = nodes.get(0);
702        assertNotNull(node);
703        assertTrue(node instanceof HTMLServer);
704        assertEquals("HTML", node.getPlatform());
705        assertFalse(node.isUsed());
706       
707        nodes = guiModel.getChildren(node);
708        assertNotNull(nodes);
709        assertEquals(1, nodes.size());
710       
711        // get document node
712        node = nodes.get(0);
713        assertNotNull(node);
714        assertTrue(node instanceof HTMLDocument);
715        assertEquals("HTML", node.getPlatform());
716        assertFalse(node.isUsed());
717       
718        nodes = guiModel.getChildren(node);
719        assertNotNull(nodes);
720        assertEquals(1, nodes.size());
721       
722        // get html node
723        node = nodes.get(0);
724        assertNotNull(node);
725        assertTrue(node instanceof HTMLPageElement);
726        assertEquals("HTML", node.getPlatform());
727        assertFalse(node.isUsed());
728       
729        nodes = guiModel.getChildren(node);
730        assertNotNull(nodes);
731        assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored
732       
733        // get body node
734        node = nodes.get(0);
735        assertNotNull(node);
736        assertTrue(node instanceof HTMLPageElement);
737        assertEquals("HTML", node.getPlatform());
738        assertTrue(node.isUsed());
739
740        nodes = guiModel.getChildren(node);
741        assertNotNull(nodes);
742        assertEquals(0, nodes.size());
743       
744        // check the sequences
745        Collection<List<Event>> sequences = parser.getSequences();
746       
747        assertNotNull(sequences);
748       
749        Iterator<List<Event>> iterator = sequences.iterator();
750        assertTrue(iterator.hasNext());
751       
752        List<Event> sequence = iterator.next();
753       
754        assertNotNull(sequence);
755        assertEquals(1, sequence.size());
756       
757        assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7);
758
759        assertTrue(iterator.hasNext());
760       
761        sequence = iterator.next();
762        assertFalse(iterator.hasNext());
763       
764        assertNotNull(sequence);
765        assertEquals(1, sequence.size());
766       
767        assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7);
768    }
769
770    /**
771     *
772     */
773    private void sendMessageAndAssertResponse(String message) throws Exception {
774        DefaultHttpClient httpclient = new DefaultHttpClient();
775        HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/");
776        HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON);
777        httpPost.setEntity(entity);
778       
779        try {
780            HttpResponse response = httpclient.execute(httpPost);
781           
782            // the monitor always returns 200 without any additional information. The client must
783            // never get more or less information. This is especially important for preventing
784            // hackers from finding out more
785            assertEquals(200, response.getStatusLine().getStatusCode());
786            assertTrue
787                ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0) ||
788                 ((response.getEntity().getContentLength() == 1) &&
789                  (response.getEntity().getContent().read() == ' ')));
790        }
791        finally {
792            httpPost.releaseConnection();
793        }
794    }
795
796    /**
797     *
798     */
799    private void assertEvent(Event                       event,
800                             int                         timestamp,
801                             Class<? extends IEventType> eventType,
802                             IGUIElement                 eventTarget,
803                             int                         xCoordinate,
804                             int                         yCoordinate)
805    {
806        assertEquals(timestamp, event.getTimestamp());
807        assertTrue(eventType.isInstance(event.getType()));
808        assertEquals(eventTarget, event.getTarget());
809       
810        if (event.getType() instanceof MouseButtonInteraction) {
811            assertEquals(xCoordinate, ((MouseButtonInteraction) event.getType()).getX());
812            assertEquals(yCoordinate, ((MouseButtonInteraction) event.getType()).getY());
813        }
814    }
815
816    /**
817     *
818     */
819    private void deleteFiles(File file) {
820        if (file.exists()) {
821            if (file.isDirectory()) {
822                for (File child : file.listFiles()) {
823                    deleteFiles(child);
824                }
825            }
826           
827            try {
828                file.delete();
829            }
830            catch (Exception e) {
831                // ignore and delete as much as possible
832            }
833        }
834    }
835
836}
Note: See TracBrowser for help on using the repository browser.