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

Last change on this file since 1078 was 1078, checked in by pharms, 11 years ago
  • removed TODOs
File size: 22.8 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 + clientId + File.separator +
149                                "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 + clientId + File.separator +
365                                "htmlmonitor_" + clientId + "_000.log");
366       
367        assertTrue(logFile.exists());
368       
369        HTMLLogParser parser = new HTMLLogParser();
370       
371        parser.parseFile(logFile);
372       
373        // check the GUI model
374        GUIModel guiModel = parser.getGuiModel();
375        assertNotNull(guiModel);
376       
377        List<IGUIElement> nodes = guiModel.getRootElements();
378        assertNotNull(nodes);
379        assertEquals(1, nodes.size());
380       
381        // get server node
382        IGUIElement node = nodes.get(0);
383        assertNotNull(node);
384        assertTrue(node instanceof HTMLServer);
385        assertEquals("HTML", node.getPlatform());
386        assertFalse(node.isUsed());
387       
388        nodes = guiModel.getChildren(node);
389        assertNotNull(nodes);
390        assertEquals(1, nodes.size());
391       
392        // get document node
393        node = nodes.get(0);
394        assertNotNull(node);
395        assertTrue(node instanceof HTMLDocument);
396        assertEquals("HTML", node.getPlatform());
397        assertFalse(node.isUsed());
398       
399        nodes = guiModel.getChildren(node);
400        assertNotNull(nodes);
401        assertEquals(1, nodes.size());
402       
403        // get html node
404        node = nodes.get(0);
405        assertNotNull(node);
406        assertTrue(node instanceof HTMLPageElement);
407        assertEquals("HTML", node.getPlatform());
408        assertFalse(node.isUsed());
409       
410        nodes = guiModel.getChildren(node);
411        assertNotNull(nodes);
412        assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored
413       
414        // get body node
415        IGUIElement body = nodes.get(0);
416        assertNotNull(body);
417        assertTrue(body instanceof HTMLPageElement);
418        assertEquals("HTML", body.getPlatform());
419        assertTrue(body.isUsed());
420
421        nodes = guiModel.getChildren(body);
422        assertNotNull(nodes);
423        assertEquals(10, nodes.size());
424       
425        // get input nodes
426        for (int i = 0; i < nodes.size(); i++) {
427            node = nodes.get(i);
428            assertNotNull(node);
429            assertTrue(node instanceof HTMLPageElement);
430            assertEquals("HTML", node.getPlatform());
431           
432            if (i != 8) {
433                assertTrue(node.isUsed());
434            }
435            else {
436                assertFalse(node.isUsed());
437            }
438
439            assertNotNull(guiModel.getChildren(node));
440            assertEquals(0, guiModel.getChildren(node).size());
441        }
442       
443        // check the sequences
444        Collection<List<Event>> sequences = parser.getSequences();
445       
446        assertNotNull(sequences);
447       
448        Iterator<List<Event>> iterator = sequences.iterator();
449        assertTrue(iterator.hasNext());
450       
451        List<Event> sequence = iterator.next();
452        assertFalse(iterator.hasNext());
453       
454        assertNotNull(sequence);
455        assertEquals(10, sequence.size());
456       
457        assertEvent(sequence.get(0), 1, MouseClick.class, nodes.get(0), 194, 7);
458        assertEvent(sequence.get(1), 2, MouseDoubleClick.class, nodes.get(1), 194, 7);
459        assertEvent(sequence.get(2), 3, KeyboardFocusChange.class, nodes.get(2), 0, 0);
460        assertEvent(sequence.get(3), 4, MouseClick.class, nodes.get(3), 125, 14);
461        assertEvent(sequence.get(4), 5, KeyboardFocusChange.class, nodes.get(4), 0, 0);
462        assertEvent(sequence.get(5), 6, KeyboardFocusChange.class, nodes.get(5), 0, 0);
463        assertEvent(sequence.get(6), 7, KeyboardFocusChange.class, nodes.get(6), 0, 0);
464        assertEvent(sequence.get(7), 8, MouseClick.class, nodes.get(7), 255, 4);
465        assertEvent(sequence.get(8), 9, Scroll.class, body, 0, 0);
466        assertEvent(sequence.get(9), 10, MouseClick.class, nodes.get(9), 516, 154);
467
468    }
469
470    /**
471     *
472     */
473    @Test
474    public void testSeveralSessions() throws Exception {
475        String clientId = "123";
476       
477        String message =
478            "{" +
479            "  \"message\": {" +
480            "    \"clientInfos\": {" +
481            "      \"clientId\":\"" + clientId + "\"," +
482            "      \"userAgent\":\"Agent\"," +
483            "      \"title\":\"Title\"," +
484            "      \"url\":\"http://host/path\"" +
485            "    }," +
486            "    \"guiModel\": {" +
487            "      \"tagName\":\"html\"," +
488            "      \"index\":\"0\"," +
489            "      \"children\":" +
490            "      [ {" +
491            "          \"tagName\":\"head\"," +
492            "          \"index\":\"0\"," +
493            "        }," +
494            "        {" +
495            "          \"tagName\":\"body\"," +
496            "          \"htmlId\":\"gsr\"," +
497            "        }" +
498            "      ]" +
499            "    }," +
500            "    \"events\":" +
501            "    [ {" +
502            "        \"time\":\"12345\"," +
503            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
504            "        \"eventType\":\"onclick\"" +
505            "        \"coordinates\": [\"194\", \"7\"]" +
506            "      }" +
507            "    ]" +
508            "  }" +
509            "}";
510
511        sendMessageAndAssertResponse(message);
512       
513        int numberOfSessions = 10;
514        for (int i = 0; i < numberOfSessions; i++) {
515            htmlMonitor.stop();
516            htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) });
517            htmlMonitor.init();
518            htmlMonitor.start();
519            sendMessageAndAssertResponse(message);
520        }
521       
522        htmlMonitor.stop();
523        htmlMonitor = null;
524       
525        HTMLLogParser parser = new HTMLLogParser();
526       
527        // assert 9 already rotated log files
528        for (int i = 0; i < numberOfSessions; i++) {
529            File logFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator +
530                                    "htmlmonitor_" + clientId + "_00" + i + ".log");
531       
532            assertTrue(logFile.exists());
533       
534            parser.parseFile(logFile);
535        }
536
537        // check the GUI model
538        GUIModel guiModel = parser.getGuiModel();
539        assertNotNull(guiModel);
540       
541        List<IGUIElement> nodes = guiModel.getRootElements();
542        assertNotNull(nodes);
543        assertEquals(1, nodes.size());
544       
545        // get server node
546        IGUIElement node = nodes.get(0);
547        assertNotNull(node);
548        assertTrue(node instanceof HTMLServer);
549        assertEquals("HTML", node.getPlatform());
550        assertFalse(node.isUsed());
551       
552        nodes = guiModel.getChildren(node);
553        assertNotNull(nodes);
554        assertEquals(1, nodes.size());
555       
556        // get document node
557        node = nodes.get(0);
558        assertNotNull(node);
559        assertTrue(node instanceof HTMLDocument);
560        assertEquals("HTML", node.getPlatform());
561        assertFalse(node.isUsed());
562       
563        nodes = guiModel.getChildren(node);
564        assertNotNull(nodes);
565        assertEquals(1, nodes.size());
566       
567        // get html node
568        node = nodes.get(0);
569        assertNotNull(node);
570        assertTrue(node instanceof HTMLPageElement);
571        assertEquals("HTML", node.getPlatform());
572        assertFalse(node.isUsed());
573       
574        nodes = guiModel.getChildren(node);
575        assertNotNull(nodes);
576        assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored
577       
578        // get body node
579        node = nodes.get(0);
580        assertNotNull(node);
581        assertTrue(node instanceof HTMLPageElement);
582        assertEquals("HTML", node.getPlatform());
583        assertTrue(node.isUsed());
584
585        nodes = guiModel.getChildren(node);
586        assertNotNull(nodes);
587        assertEquals(0, nodes.size());
588       
589        // check the sequences
590        Collection<List<Event>> sequences = parser.getSequences();
591       
592        assertNotNull(sequences);
593        assertEquals(numberOfSessions, sequences.size());
594       
595        Iterator<List<Event>> iterator = sequences.iterator();
596       
597        while (iterator.hasNext()) {
598            List<Event> sequence = iterator.next();
599       
600            assertNotNull(sequence);
601            assertEquals(1, sequence.size());
602       
603            assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7);
604        }
605    }
606
607    /**
608     *
609     */
610    private void sendMessageAndAssertResponse(String message) throws Exception {
611        DefaultHttpClient httpclient = new DefaultHttpClient();
612        HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/");
613        HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON);
614        httpPost.setEntity(entity);
615       
616        try {
617            HttpResponse response = httpclient.execute(httpPost);
618           
619            // the monitor always returns 200 without any additional information. The client must
620            // never get more or less information. This is especially important for preventing
621            // hackers from finding out more
622            assertEquals(200, response.getStatusLine().getStatusCode());
623            assertTrue
624                ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0));
625        }
626        finally {
627            httpPost.releaseConnection();
628        }
629    }
630
631    /**
632     *
633     */
634    private void assertEvent(Event                       event,
635                             int                         timestamp,
636                             Class<? extends IEventType> eventType,
637                             IGUIElement                 eventTarget,
638                             int                         xCoordinate,
639                             int                         yCoordinate)
640    {
641        assertEquals(timestamp, event.getTimestamp());
642        assertTrue(eventType.isInstance(event.getType()));
643        assertEquals(eventTarget, event.getTarget());
644       
645        if (event.getType() instanceof MouseButtonInteraction) {
646            assertEquals(xCoordinate, ((MouseButtonInteraction) event.getType()).getX());
647            assertEquals(yCoordinate, ((MouseButtonInteraction) event.getType()).getY());
648        }
649    }
650
651    /**
652     *
653     */
654    private void deleteFiles(File file) {
655        if (file.exists()) {
656            if (file.isDirectory()) {
657                for (File child : file.listFiles()) {
658                    deleteFiles(child);
659                }
660            }
661           
662            try {
663                file.delete();
664            }
665            catch (Exception e) {
666                // ignore and delete as much as possible
667            }
668        }
669    }
670
671}
Note: See TracBrowser for help on using the repository browser.