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

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