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

Last change on this file since 1623 was 1623, checked in by sherbold, 10 years ago
  • added test cases for service name mapping
File size: 9.1 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     * @throws Exception
79     */
80    @Test
81    public void testParseFile_1() throws Exception {
82        for (int i = 0; i < 20; i++) {
83            File logFile = new File(LOG_FILE_DIR, "session" + i + ".xml");
84
85            Session session = HTTPTestUtils.createRandomSession(100);
86            writeToFile(session, logFile);
87
88            System.out.println("calling parser");
89            HTTPLogParser parser = new HTTPLogParser();
90            parser.parseFile(logFile);
91
92            System.out.println("evaluating events");
93            Collection<List<Event>> events = parser.getSequences();
94
95            assertNotNull(events);
96
97            if (session.getHttpExchange().size() > 0) {
98                assertEquals(1, events.size());
99
100                Iterator<List<Event>> iterator = events.iterator();
101                assertNotNull(iterator);
102                List<Event> sequence = iterator.next();
103                assertEquals(session.getHttpExchange().size(), sequence.size());
104                assertFalse(iterator.hasNext());
105
106                System.out.println("{");
107                System.out.println("  {");
108                for (int j = 0; j < sequence.size(); j++) {
109                    System.out.print("    ");
110                    System.out.print(sequence.get(j));
111                    System.out.println(",");
112
113                    assertNotNull(sequence.get(j));
114                    assertNotNull(sequence.get(j).getType());
115                    assertTrue(sequence.get(j).getType() instanceof HTTPEventType);
116
117                    assertNotNull(sequence.get(j).getTarget());
118                    assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget);
119
120                    HTTPTestUtils.assertExchangeEquals(session.getHttpExchange().get(j),
121                                                       ((HTTPEventType) sequence.get(j).getType())
122                                                           .getExchange());
123
124                    assertEquals(HTTPUtils.toString(session.getHttpExchange().get(j).getReceiver()),
125                                 ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier());
126                }
127                System.out.println("  }");
128                System.out.println("}");
129                System.out.println("\n\n");
130            }
131        }
132    }
133
134    /**
135     * Tests the parseFile method with a given trace file.
136     *
137     * @throws Exception
138     */
139    @Test
140    public void testParseFile_2() throws Exception {
141        HTTPLogParser parser = new HTTPLogParser();
142        parser.parseFile(new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml")
143            .getFile()));
144        Collection<List<Event>> events = parser.getSequences();
145
146        assertNotNull(events);
147        assertEquals(1, events.size());
148
149        Iterator<List<Event>> iterator = events.iterator();
150        assertNotNull(iterator);
151        assertEquals(876, iterator.next().size());
152        assertFalse(iterator.hasNext());
153
154        List<Event> soapEvents = new LinkedList<Event>();
155        System.out.println("{");
156        for (List<Event> session : events) {
157            System.out.println("  {");
158            for (Event event : session) {
159                System.out.print("    ");
160                System.out.print(event);
161                System.out.println(",");
162
163                if (event.getType() instanceof SOAPEventType) {
164                    assertNotNull(((SOAPEventType) event.getType()).getCalledMethod());
165                    soapEvents.add(event);
166                }
167            }
168            System.out.println("  }");
169        }
170        System.out.println("}");
171        System.out.println("\n\n");
172
173        assertEquals(870, soapEvents.size());
174    }
175
176    /**
177     * Tests the parseFile method with a given trace file.
178     *
179     * @throws Exception
180     */
181    @Test
182    public void testParseFile_3() throws Exception {
183        // tests parsing with properties
184        HTTPLogParser parser =
185            new HTTPLogParser(new File(ClassLoader
186                .getSystemResource("testParseFile_3_properties.txt").getFile()));
187        parser.parseFile(new File(ClassLoader.getSystemResource("testParseFile_3_logfile.log")
188            .getFile()));
189        Collection<List<Event>> events = parser.getSequences();
190
191        assertNotNull(events);
192        assertEquals(1, events.size());
193
194        Iterator<List<Event>> iterator = events.iterator();
195        assertNotNull(iterator);
196        assertEquals(153, iterator.next().size());
197        assertFalse(iterator.hasNext());
198
199        List<Event> soapEvents = new LinkedList<Event>();
200        System.out.println("{");
201        for (List<Event> session : events) {
202            System.out.println("  {");
203            for (Event event : session) {
204                System.out.print("    ");
205                System.out.print(event);
206                System.out.println(",");
207
208                if (event.getType() instanceof SOAPEventType) {
209                    assertNotNull(((SOAPEventType) event.getType()).getCalledMethod());
210                    soapEvents.add(event);
211                }
212            }
213            System.out.println("  }");
214        }
215        System.out.println("}");
216        System.out.println("\n\n");
217
218        assertEquals(147, soapEvents.size());
219    }
220
221    /**
222     * @throws Exception
223     */
224    @Test
225    public void testSerializability_1() throws Exception {
226        HTTPLogParser parser = new HTTPLogParser();
227        parser.parseFile(new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml")
228            .getFile()));
229        Collection<List<Event>> events = parser.getSequences();
230
231        assertNotNull(events);
232        assertEquals(1, events.size());
233
234        Iterator<List<Event>> iterator = events.iterator();
235        assertNotNull(iterator);
236        assertEquals(876, iterator.next().size());
237        assertFalse(iterator.hasNext());
238
239        for (List<Event> session : events) {
240            for (Event event : session) {
241                byte[] serializedEvent = SerializationUtils.serialize(event);
242                assertEquals(event, SerializationUtils.deserialize(serializedEvent));
243            }
244        }
245    }
246
247    /**
248     *
249     */
250    private void writeToFile(Session session, File logFile) throws Exception {
251        System.out.println("writing session to " + logFile);
252
253        logFile.getParentFile().mkdirs();
254
255        JAXBContext jaxbContext = JAXBContext.newInstance(Session.class.getPackage().getName());
256        Marshaller marshaller = jaxbContext.createMarshaller();
257        StreamResult result = new StreamResult(new FileOutputStream(logFile));
258        marshaller.marshal(new ObjectFactory().createSession(session), result);
259
260        result.getOutputStream().close();
261    }
262
263    /**
264     *
265     */
266    private void deleteFiles(File file) {
267        if (file.exists()) {
268            if (file.isDirectory()) {
269                for (File child : file.listFiles()) {
270                    deleteFiles(child);
271                }
272            }
273
274            try {
275                file.delete();
276            }
277            catch (Exception e) {
278                // ignore and delete as much as possible
279            }
280        }
281    }
282}
Note: See TracBrowser for help on using the repository browser.