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

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