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

Last change on this file since 1591 was 1591, checked in by sherbold, 10 years ago
  • made the soapRequest and soapResponse fields of the SOAPEventType transient. It is as of now unclear whether we need them as part of Serialization and my be changed later. This should, at least, not affect the equals and hashCode operations, as both ignore the soapRequest and soapResponse fields.
File size: 7.6 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.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 * @author Fabian Glaser
49 *
50 */
51public class HTTPLogParserTest {
52   
53    /**
54     *
55     */
56    private final static String LOG_FILE_DIR = "target/tmp/logfiles/";
57
58    /**
59     *
60     */
61    @Before
62    public void setUp() {
63        new TextConsole(Level.FINEST);
64    }
65
66    /**
67     *
68     */
69    @After
70    public void tearDown() throws Exception {
71        deleteFiles(new File(LOG_FILE_DIR));
72    }
73   
74    /**
75     * Tests the parseFile method with a given trace file.
76     * @throws Exception
77     */
78    @Test
79    public void testParseFile_1() throws Exception {
80        for (int i = 0; i < 20; i++) {
81            File logFile = new File(LOG_FILE_DIR, "session" + i + ".xml");
82
83            Session session = HTTPTestUtils.createRandomSession(100);
84            writeToFile(session, logFile);
85
86            System.out.println("calling parser");
87            HTTPLogParser parser = new HTTPLogParser();
88            parser.parseFile(logFile);
89
90            System.out.println("evaluating events");
91            Collection<List<Event>> events = parser.getSequences();
92
93            assertNotNull(events);
94
95            if (session.getHttpExchange().size() > 0) {
96                assertEquals(1, events.size());
97
98                Iterator<List<Event>> iterator = events.iterator();
99                assertNotNull(iterator);
100                List<Event> sequence = iterator.next();
101                assertEquals(session.getHttpExchange().size(), sequence.size());
102                assertFalse(iterator.hasNext());
103
104                System.out.println("{");
105                System.out.println("  {");
106                for (int j = 0; j < sequence.size(); j++) {
107                    System.out.print("    ");
108                    System.out.print(sequence.get(j));
109                    System.out.println(",");
110
111                    assertNotNull(sequence.get(j));
112                    assertNotNull(sequence.get(j).getType());
113                    assertTrue(sequence.get(j).getType() instanceof HTTPEventType);
114
115                    assertNotNull(sequence.get(j).getTarget());
116                    assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget);
117
118                    HTTPTestUtils.assertExchangeEquals
119                        (session.getHttpExchange().get(j),
120                         ((HTTPEventType) sequence.get(j).getType()).getExchange());
121
122                    assertEquals(HTTPUtils.toString(session.getHttpExchange().get(j).getReceiver()),
123                                 ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier());
124                }
125                System.out.println("  }");
126                System.out.println("}");
127                System.out.println("\n\n");
128            }
129        }
130    }
131   
132    /**
133     * Tests the parseFile method with a given trace file.
134     * @throws Exception
135     */
136    @Test
137    public void testParseFile_2() throws Exception {
138        HTTPLogParser parser = new HTTPLogParser();
139        parser.parseFile
140            (new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml").getFile()));
141        Collection<List<Event>> events = parser.getSequences();
142
143        assertNotNull(events);
144        assertEquals(1, events.size());
145       
146        Iterator<List<Event>> iterator = events.iterator();
147        assertNotNull(iterator);
148        assertEquals(876, iterator.next().size());
149        assertFalse(iterator.hasNext());
150
151        List<Event> soapEvents = new LinkedList<Event>();
152        System.out.println("{");
153        for (List<Event> session : events) {
154            System.out.println("  {");
155            for (Event event : session) {
156                System.out.print("    ");
157                System.out.print(event);
158                System.out.println(",");
159               
160                if (event.getType() instanceof SOAPEventType) {
161                    assertNotNull(((SOAPEventType) event.getType()).getCalledMethod());
162                    soapEvents.add(event);
163                }
164            }
165            System.out.println("  }");
166        }
167        System.out.println("}");
168        System.out.println("\n\n");
169       
170        assertEquals(870, soapEvents.size());
171    }
172   
173    /**
174     * @throws Exception
175     */
176    @Test
177    public void testSerializability_1() throws Exception {
178        HTTPLogParser parser = new HTTPLogParser();
179        parser.parseFile
180            (new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml").getFile()));
181        Collection<List<Event>> events = parser.getSequences();
182
183        assertNotNull(events);
184        assertEquals(1, events.size());
185       
186        Iterator<List<Event>> iterator = events.iterator();
187        assertNotNull(iterator);
188        assertEquals(876, iterator.next().size());
189        assertFalse(iterator.hasNext());
190
191        for (List<Event> session : events) {
192            for (Event event : session) {
193                byte[] serializedEvent = SerializationUtils.serialize(event);
194                assertEquals(event, SerializationUtils.deserialize(serializedEvent));
195            }
196        }
197    }
198   
199    /**
200     *
201     */
202    private void writeToFile(Session session, File logFile) throws Exception {
203        System.out.println("writing session to " + logFile);
204       
205        logFile.getParentFile().mkdirs();
206       
207        JAXBContext jaxbContext = JAXBContext.newInstance(Session.class.getPackage().getName());
208        Marshaller marshaller = jaxbContext.createMarshaller();
209        StreamResult result = new StreamResult(new FileOutputStream(logFile));
210        marshaller.marshal(new ObjectFactory().createSession(session), result);
211       
212        result.getOutputStream().close();
213    }
214
215    /**
216     *
217     */
218    private void deleteFiles(File file) {
219        if (file.exists()) {
220            if (file.isDirectory()) {
221                for (File child : file.listFiles()) {
222                    deleteFiles(child);
223                }
224            }
225           
226            try {
227                file.delete();
228            }
229            catch (Exception e) {
230                // ignore and delete as much as possible
231            }
232        }
233    }
234}
Note: See TracBrowser for help on using the repository browser.