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
RevLine 
[1369]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;
[1417]23import java.util.LinkedList;
[1369]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
[1591]31import org.apache.commons.lang.SerializationUtils;
[1369]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;
[1417]41import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
[1561]42import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory;
43import de.ugoe.cs.autoquest.plugin.http.logdata.Session;
[1369]44import de.ugoe.cs.util.console.TextConsole;
45
46/**
47 * Test for the new HTMLLogParser
[1623]48 *
[1369]49 * @author Fabian Glaser
[1623]50 *
[1369]51 */
52public class HTTPLogParserTest {
[1623]53
[1369]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    }
[1623]74
[1369]75    /**
76     * Tests the parseFile method with a given trace file.
[1623]77     *
[1369]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
[1623]85            Session session = HTTPTestUtils.createRandomSession(100);
[1369]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
[1623]120                    HTTPTestUtils.assertExchangeEquals(session.getHttpExchange().get(j),
121                                                       ((HTTPEventType) sequence.get(j).getType())
122                                                           .getExchange());
[1369]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    }
[1623]133
[1417]134    /**
135     * Tests the parseFile method with a given trace file.
[1623]136     *
[1417]137     * @throws Exception
138     */
139    @Test
140    public void testParseFile_2() throws Exception {
141        HTTPLogParser parser = new HTTPLogParser();
[1623]142        parser.parseFile(new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml")
143            .getFile()));
[1417]144        Collection<List<Event>> events = parser.getSequences();
[1369]145
[1417]146        assertNotNull(events);
147        assertEquals(1, events.size());
[1623]148
[1417]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(",");
[1623]162
[1417]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");
[1623]172
[1417]173        assertEquals(870, soapEvents.size());
174    }
[1623]175
[1369]176    /**
[1623]177     * Tests the parseFile method with a given trace file.
178     *
[1591]179     * @throws Exception
180     */
181    @Test
[1623]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
[1591]225    public void testSerializability_1() throws Exception {
226        HTTPLogParser parser = new HTTPLogParser();
[1623]227        parser.parseFile(new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml")
228            .getFile()));
[1591]229        Collection<List<Event>> events = parser.getSequences();
230
231        assertNotNull(events);
232        assertEquals(1, events.size());
[1623]233
[1591]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    }
[1623]246
[1591]247    /**
[1369]248     *
249     */
250    private void writeToFile(Session session, File logFile) throws Exception {
251        System.out.println("writing session to " + logFile);
[1623]252
[1369]253        logFile.getParentFile().mkdirs();
[1623]254
[1369]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);
[1623]259
[1369]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            }
[1623]273
[1369]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.