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

Last change on this file since 1369 was 1369, checked in by pharms, 10 years ago

Initial import.

File size: 5.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.List;
24import java.util.logging.Level;
25
26import javax.xml.bind.JAXBContext;
27import javax.xml.bind.Marshaller;
28import javax.xml.transform.stream.StreamResult;
29
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.httpmonitor.exchange.ObjectFactory;
36import de.ugoe.cs.autoquest.httpmonitor.exchange.Session;
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.util.console.TextConsole;
42
43/**
44 * Test for the new HTMLLogParser
45 * @author Fabian Glaser
46 *
47 */
48public class HTTPLogParserTest {
49   
50    /**
51     *
52     */
53    private final static String LOG_FILE_DIR = "target/tmp/logfiles/";
54
55    /**
56     *
57     */
58    @Before
59    public void setUp() {
60        new TextConsole(Level.FINEST);
61    }
62
63    /**
64     *
65     */
66    @After
67    public void tearDown() throws Exception {
68        deleteFiles(new File(LOG_FILE_DIR));
69    }
70   
71    /**
72     * Tests the parseFile method with a given trace file.
73     * @throws Exception
74     */
75    @Test
76    public void testParseFile_1() throws Exception {
77        for (int i = 0; i < 20; i++) {
78            File logFile = new File(LOG_FILE_DIR, "session" + i + ".xml");
79
80            Session session = HTTPTestUtils.createRandomSession(100);
81            writeToFile(session, logFile);
82
83            System.out.println("calling parser");
84            HTTPLogParser parser = new HTTPLogParser();
85            parser.parseFile(logFile);
86
87            System.out.println("evaluating events");
88            Collection<List<Event>> events = parser.getSequences();
89
90            assertNotNull(events);
91
92            if (session.getHttpExchange().size() > 0) {
93                assertEquals(1, events.size());
94
95                Iterator<List<Event>> iterator = events.iterator();
96                assertNotNull(iterator);
97                List<Event> sequence = iterator.next();
98                assertEquals(session.getHttpExchange().size(), sequence.size());
99                assertFalse(iterator.hasNext());
100
101                System.out.println("{");
102                System.out.println("  {");
103                for (int j = 0; j < sequence.size(); j++) {
104                    System.out.print("    ");
105                    System.out.print(sequence.get(j));
106                    System.out.println(",");
107
108                    assertNotNull(sequence.get(j));
109                    assertNotNull(sequence.get(j).getType());
110                    assertTrue(sequence.get(j).getType() instanceof HTTPEventType);
111
112                    assertNotNull(sequence.get(j).getTarget());
113                    assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget);
114
115                    HTTPTestUtils.assertExchangeEquals
116                        (session.getHttpExchange().get(j),
117                         ((HTTPEventType) sequence.get(j).getType()).getExchange());
118
119                    assertEquals(HTTPUtils.toString(session.getHttpExchange().get(j).getReceiver()),
120                                 ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier());
121                }
122                System.out.println("  }");
123                System.out.println("}");
124                System.out.println("\n\n");
125            }
126        }
127    }
128
129    /**
130     *
131     */
132    private void writeToFile(Session session, File logFile) throws Exception {
133        System.out.println("writing session to " + logFile);
134       
135        logFile.getParentFile().mkdirs();
136       
137        JAXBContext jaxbContext = JAXBContext.newInstance(Session.class.getPackage().getName());
138        Marshaller marshaller = jaxbContext.createMarshaller();
139        StreamResult result = new StreamResult(new FileOutputStream(logFile));
140        marshaller.marshal(new ObjectFactory().createSession(session), result);
141       
142        result.getOutputStream().close();
143    }
144
145    /**
146     *
147     */
148    private void deleteFiles(File file) {
149        if (file.exists()) {
150            if (file.isDirectory()) {
151                for (File child : file.listFiles()) {
152                    deleteFiles(child);
153                }
154            }
155           
156            try {
157                file.delete();
158            }
159            catch (Exception e) {
160                // ignore and delete as much as possible
161            }
162        }
163    }
164}
Note: See TracBrowser for help on using the repository browser.