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 | |
---|
15 | package de.ugoe.cs.autoquest.http; |
---|
16 | |
---|
17 | import static org.junit.Assert.*; |
---|
18 | |
---|
19 | import java.io.File; |
---|
20 | import java.io.FileOutputStream; |
---|
21 | import java.util.Collection; |
---|
22 | import java.util.Iterator; |
---|
23 | import java.util.LinkedList; |
---|
24 | import java.util.List; |
---|
25 | import java.util.logging.Level; |
---|
26 | |
---|
27 | import javax.xml.bind.JAXBContext; |
---|
28 | import javax.xml.bind.Marshaller; |
---|
29 | import javax.xml.transform.stream.StreamResult; |
---|
30 | |
---|
31 | import org.apache.commons.lang.SerializationUtils; |
---|
32 | import org.junit.After; |
---|
33 | import org.junit.Before; |
---|
34 | import org.junit.Test; |
---|
35 | |
---|
36 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
37 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; |
---|
38 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
39 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; |
---|
40 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; |
---|
41 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; |
---|
42 | import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory; |
---|
43 | import de.ugoe.cs.autoquest.plugin.http.logdata.Session; |
---|
44 | import de.ugoe.cs.util.console.TextConsole; |
---|
45 | |
---|
46 | /** |
---|
47 | * Test for the new HTMLLogParser |
---|
48 | * |
---|
49 | * @author Fabian Glaser |
---|
50 | * |
---|
51 | */ |
---|
52 | public 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 : file.listFiles()) { |
---|
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 | } |
---|