[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 | |
---|
| 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; |
---|
[1417] | 23 | import java.util.LinkedList; |
---|
[1369] | 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 | |
---|
[1591] | 31 | import org.apache.commons.lang.SerializationUtils; |
---|
[1369] | 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; |
---|
[1417] | 41 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; |
---|
[1561] | 42 | import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory; |
---|
| 43 | import de.ugoe.cs.autoquest.plugin.http.logdata.Session; |
---|
[1369] | 44 | import de.ugoe.cs.util.console.TextConsole; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Test for the new HTMLLogParser |
---|
| 48 | * @author Fabian Glaser |
---|
| 49 | * |
---|
| 50 | */ |
---|
| 51 | public 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 | } |
---|
[1417] | 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(); |
---|
[1369] | 142 | |
---|
[1417] | 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 | |
---|
[1369] | 173 | /** |
---|
[1591] | 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 | /** |
---|
[1369] | 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 | } |
---|