source: trunk/autoquest-plugin-http-test/src/test/java/de/ugoe/cs/autoquest/http/HTTPLogParserTest.java

Last change on this file was 2234, checked in by pharms, 7 years ago
File size: 9.2 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.http;
16
17import static org.junit.Assert.*;
18
19import java.io.File;
20import java.io.FileOutputStream;
21import java.util.Collection;
22import java.util.Iterator;
23import java.util.LinkedList;
24import java.util.List;
25import java.util.logging.Level;
26
27import javax.xml.bind.JAXBContext;
28import javax.xml.bind.Marshaller;
29import javax.xml.transform.stream.StreamResult;
30
31import org.apache.commons.lang.SerializationUtils;
32import org.junit.After;
33import org.junit.Before;
34import org.junit.Test;
35
36import de.ugoe.cs.autoquest.eventcore.Event;
37import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
38import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
39import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType;
40import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget;
41import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
42import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory;
43import de.ugoe.cs.autoquest.plugin.http.logdata.Session;
44import de.ugoe.cs.util.console.TextConsole;
45
46/**
47 * Test for the new HTMLLogParser
48 *
49 * @author Fabian Glaser
50 *
51 */
52public class HTTPLogParserTest {
53
54    /**
55     *
56     */
57    private final static String LOG_FILE_DIR = "target/tmp/logfiles/";
58
59    /**
60     *
61     */
62    @Before
63    public void setUp() {
64        new TextConsole(Level.FINEST);
65    }
66
67    /**
68     *
69     */
70    @After
71    public void tearDown() throws Exception {
72        deleteFiles(new File(LOG_FILE_DIR));
73    }
74
75    /**
76     * Tests the parseFile method with a given trace file.
77     *
78     */
79    @Test
80    public void testParseFile_1() throws Exception {
81        for (int i = 0; i < 20; i++) {
82            File logFile = new File(LOG_FILE_DIR, "session" + i + ".xml");
83
84            Session session = HTTPTestUtils.createRandomSession(100);
85            writeToFile(session, logFile);
86
87            System.out.println("calling parser");
88            HTTPLogParser parser = new HTTPLogParser();
89            parser.parseFile(logFile);
90
91            System.out.println("evaluating events");
92            Collection<List<Event>> events = parser.getSequences();
93
94            assertNotNull(events);
95
96            if (session.getHttpExchange().size() > 0) {
97                assertEquals(1, events.size());
98
99                Iterator<List<Event>> iterator = events.iterator();
100                assertNotNull(iterator);
101                List<Event> sequence = iterator.next();
102                assertEquals(session.getHttpExchange().size(), sequence.size());
103                assertFalse(iterator.hasNext());
104
105                System.out.println("{");
106                System.out.println("  {");
107                for (int j = 0; j < sequence.size(); j++) {
108                    System.out.print("    ");
109                    System.out.print(sequence.get(j));
110                    System.out.println(",");
111
112                    assertNotNull(sequence.get(j));
113                    assertNotNull(sequence.get(j).getType());
114                    assertTrue(sequence.get(j).getType() instanceof HTTPEventType);
115
116                    assertNotNull(sequence.get(j).getTarget());
117                    assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget);
118
119                    HTTPTestUtils.assertExchangeEquals(session.getHttpExchange().get(j),
120                                                       ((HTTPEventType) sequence.get(j).getType())
121                                                           .getExchange());
122
123                    assertEquals(HTTPUtils.toString(session.getHttpExchange().get(j).getReceiver()),
124                                 ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier());
125                }
126                System.out.println("  }");
127                System.out.println("}");
128                System.out.println("\n\n");
129            }
130        }
131    }
132
133    /**
134     * Tests the parseFile method with a given trace file.
135     *
136     */
137    @Test
138    public void testParseFile_2() throws Exception {
139        HTTPLogParser parser = new HTTPLogParser();
140        parser.parseFile(new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml")
141            .getFile()));
142        Collection<List<Event>> events = parser.getSequences();
143
144        assertNotNull(events);
145        assertEquals(1, events.size());
146
147        Iterator<List<Event>> iterator = events.iterator();
148        assertNotNull(iterator);
149        assertEquals(876, iterator.next().size());
150        assertFalse(iterator.hasNext());
151
152        List<Event> soapEvents = new LinkedList<Event>();
153        System.out.println("{");
154        for (List<Event> session : events) {
155            System.out.println("  {");
156            for (Event event : session) {
157                System.out.print("    ");
158                System.out.print(event);
159                System.out.println(",");
160
161                if (event.getType() instanceof SOAPEventType) {
162                    assertNotNull(((SOAPEventType) event.getType()).getCalledMethod());
163                    soapEvents.add(event);
164                }
165            }
166            System.out.println("  }");
167        }
168        System.out.println("}");
169        System.out.println("\n\n");
170
171        assertEquals(870, soapEvents.size());
172    }
173
174    /**
175     * Tests the parseFile method with a given trace file.
176     *
177     */
178    @Test
179    public void testParseFile_3() throws Exception {
180        // tests parsing with properties
181        HTTPLogParser parser =
182            new HTTPLogParser(new File(ClassLoader
183                .getSystemResource("testParseFile_3_properties.txt").getFile()));
184        parser.parseFile(new File(ClassLoader.getSystemResource("testParseFile_3_logfile.log")
185            .getFile()));
186        Collection<List<Event>> events = parser.getSequences();
187
188        assertNotNull(events);
189        assertEquals(1, events.size());
190
191        Iterator<List<Event>> iterator = events.iterator();
192        assertNotNull(iterator);
193        assertEquals(153, iterator.next().size());
194        assertFalse(iterator.hasNext());
195
196        List<Event> soapEvents = new LinkedList<Event>();
197        System.out.println("{");
198        for (List<Event> session : events) {
199            System.out.println("  {");
200            for (Event event : session) {
201                System.out.print("    ");
202                System.out.print(event);
203                System.out.println(",");
204
205                if (event.getType() instanceof SOAPEventType) {
206                    assertNotNull(((SOAPEventType) event.getType()).getCalledMethod());
207                    soapEvents.add(event);
208                }
209            }
210            System.out.println("  }");
211        }
212        System.out.println("}");
213        System.out.println("\n\n");
214
215        assertEquals(147, soapEvents.size());
216    }
217
218    /**
219     */
220    @Test
221    public void testSerializability_1() throws Exception {
222        HTTPLogParser parser = new HTTPLogParser();
223        parser.parseFile(new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml")
224            .getFile()));
225        Collection<List<Event>> events = parser.getSequences();
226
227        assertNotNull(events);
228        assertEquals(1, events.size());
229
230        Iterator<List<Event>> iterator = events.iterator();
231        assertNotNull(iterator);
232        assertEquals(876, iterator.next().size());
233        assertFalse(iterator.hasNext());
234
235        for (List<Event> session : events) {
236            for (Event event : session) {
237                byte[] serializedEvent = SerializationUtils.serialize(event);
238                assertEquals(event, SerializationUtils.deserialize(serializedEvent));
239            }
240        }
241    }
242
243    /**
244     *
245     */
246    private void writeToFile(Session session, File logFile) throws Exception {
247        System.out.println("writing session to " + logFile);
248
249        logFile.getParentFile().mkdirs();
250
251        JAXBContext jaxbContext = JAXBContext.newInstance(Session.class.getPackage().getName());
252        Marshaller marshaller = jaxbContext.createMarshaller();
253        StreamResult result = new StreamResult(new FileOutputStream(logFile));
254        marshaller.marshal(new ObjectFactory().createSession(session), result);
255
256        result.getOutputStream().close();
257    }
258
259    /**
260     *
261     */
262    private void deleteFiles(File file) {
263        if (file.exists()) {
264            if (file.isDirectory()) {
265                File[] children = file.listFiles();
266                if (children != null) {
267                    for (File child : children) {
268                        deleteFiles(child);
269                    }
270                }
271            }
272
273            try {
274                file.delete();
275            }
276            catch (Exception e) {
277                // ignore and delete as much as possible
278            }
279        }
280    }
281}
Note: See TracBrowser for help on using the repository browser.