[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 | |
---|
| 31 | import org.junit.After; |
---|
| 32 | import org.junit.Before; |
---|
| 33 | import org.junit.Test; |
---|
| 34 | |
---|
| 35 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 36 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; |
---|
| 37 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
| 38 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; |
---|
| 39 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; |
---|
[1417] | 40 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; |
---|
[1561] | 41 | import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory; |
---|
| 42 | import de.ugoe.cs.autoquest.plugin.http.logdata.Session; |
---|
[1369] | 43 | import de.ugoe.cs.util.console.TextConsole; |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * Test for the new HTMLLogParser |
---|
| 47 | * @author Fabian Glaser |
---|
| 48 | * |
---|
| 49 | */ |
---|
| 50 | public class HTTPLogParserTest { |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * |
---|
| 54 | */ |
---|
| 55 | private final static String LOG_FILE_DIR = "target/tmp/logfiles/"; |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * |
---|
| 59 | */ |
---|
| 60 | @Before |
---|
| 61 | public void setUp() { |
---|
| 62 | new TextConsole(Level.FINEST); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * |
---|
| 67 | */ |
---|
| 68 | @After |
---|
| 69 | public void tearDown() throws Exception { |
---|
| 70 | deleteFiles(new File(LOG_FILE_DIR)); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Tests the parseFile method with a given trace file. |
---|
| 75 | * @throws Exception |
---|
| 76 | */ |
---|
| 77 | @Test |
---|
| 78 | public void testParseFile_1() throws Exception { |
---|
| 79 | for (int i = 0; i < 20; i++) { |
---|
| 80 | File logFile = new File(LOG_FILE_DIR, "session" + i + ".xml"); |
---|
| 81 | |
---|
| 82 | Session session = HTTPTestUtils.createRandomSession(100); |
---|
| 83 | writeToFile(session, logFile); |
---|
| 84 | |
---|
| 85 | System.out.println("calling parser"); |
---|
| 86 | HTTPLogParser parser = new HTTPLogParser(); |
---|
| 87 | parser.parseFile(logFile); |
---|
| 88 | |
---|
| 89 | System.out.println("evaluating events"); |
---|
| 90 | Collection<List<Event>> events = parser.getSequences(); |
---|
| 91 | |
---|
| 92 | assertNotNull(events); |
---|
| 93 | |
---|
| 94 | if (session.getHttpExchange().size() > 0) { |
---|
| 95 | assertEquals(1, events.size()); |
---|
| 96 | |
---|
| 97 | Iterator<List<Event>> iterator = events.iterator(); |
---|
| 98 | assertNotNull(iterator); |
---|
| 99 | List<Event> sequence = iterator.next(); |
---|
| 100 | assertEquals(session.getHttpExchange().size(), sequence.size()); |
---|
| 101 | assertFalse(iterator.hasNext()); |
---|
| 102 | |
---|
| 103 | System.out.println("{"); |
---|
| 104 | System.out.println(" {"); |
---|
| 105 | for (int j = 0; j < sequence.size(); j++) { |
---|
| 106 | System.out.print(" "); |
---|
| 107 | System.out.print(sequence.get(j)); |
---|
| 108 | System.out.println(","); |
---|
| 109 | |
---|
| 110 | assertNotNull(sequence.get(j)); |
---|
| 111 | assertNotNull(sequence.get(j).getType()); |
---|
| 112 | assertTrue(sequence.get(j).getType() instanceof HTTPEventType); |
---|
| 113 | |
---|
| 114 | assertNotNull(sequence.get(j).getTarget()); |
---|
| 115 | assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget); |
---|
| 116 | |
---|
| 117 | HTTPTestUtils.assertExchangeEquals |
---|
| 118 | (session.getHttpExchange().get(j), |
---|
| 119 | ((HTTPEventType) sequence.get(j).getType()).getExchange()); |
---|
| 120 | |
---|
| 121 | assertEquals(HTTPUtils.toString(session.getHttpExchange().get(j).getReceiver()), |
---|
| 122 | ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier()); |
---|
| 123 | } |
---|
| 124 | System.out.println(" }"); |
---|
| 125 | System.out.println("}"); |
---|
| 126 | System.out.println("\n\n"); |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
[1417] | 130 | |
---|
| 131 | /** |
---|
| 132 | * Tests the parseFile method with a given trace file. |
---|
| 133 | * @throws Exception |
---|
| 134 | */ |
---|
| 135 | @Test |
---|
| 136 | public void testParseFile_2() throws Exception { |
---|
| 137 | HTTPLogParser parser = new HTTPLogParser(); |
---|
| 138 | parser.parseFile |
---|
| 139 | (new File(ClassLoader.getSystemResource("httpmonitor_testtrace_1.xml").getFile())); |
---|
| 140 | Collection<List<Event>> events = parser.getSequences(); |
---|
[1369] | 141 | |
---|
[1417] | 142 | assertNotNull(events); |
---|
| 143 | assertEquals(1, events.size()); |
---|
| 144 | |
---|
| 145 | Iterator<List<Event>> iterator = events.iterator(); |
---|
| 146 | assertNotNull(iterator); |
---|
| 147 | assertEquals(876, iterator.next().size()); |
---|
| 148 | assertFalse(iterator.hasNext()); |
---|
| 149 | |
---|
| 150 | List<Event> soapEvents = new LinkedList<Event>(); |
---|
| 151 | System.out.println("{"); |
---|
| 152 | for (List<Event> session : events) { |
---|
| 153 | System.out.println(" {"); |
---|
| 154 | for (Event event : session) { |
---|
| 155 | System.out.print(" "); |
---|
| 156 | System.out.print(event); |
---|
| 157 | System.out.println(","); |
---|
| 158 | |
---|
| 159 | if (event.getType() instanceof SOAPEventType) { |
---|
| 160 | assertNotNull(((SOAPEventType) event.getType()).getCalledMethod()); |
---|
| 161 | soapEvents.add(event); |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | System.out.println(" }"); |
---|
| 165 | } |
---|
| 166 | System.out.println("}"); |
---|
| 167 | System.out.println("\n\n"); |
---|
| 168 | |
---|
| 169 | assertEquals(870, soapEvents.size()); |
---|
| 170 | } |
---|
| 171 | |
---|
[1369] | 172 | /** |
---|
| 173 | * |
---|
| 174 | */ |
---|
| 175 | private void writeToFile(Session session, File logFile) throws Exception { |
---|
| 176 | System.out.println("writing session to " + logFile); |
---|
| 177 | |
---|
| 178 | logFile.getParentFile().mkdirs(); |
---|
| 179 | |
---|
| 180 | JAXBContext jaxbContext = JAXBContext.newInstance(Session.class.getPackage().getName()); |
---|
| 181 | Marshaller marshaller = jaxbContext.createMarshaller(); |
---|
| 182 | StreamResult result = new StreamResult(new FileOutputStream(logFile)); |
---|
| 183 | marshaller.marshal(new ObjectFactory().createSession(session), result); |
---|
| 184 | |
---|
| 185 | result.getOutputStream().close(); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * |
---|
| 190 | */ |
---|
| 191 | private void deleteFiles(File file) { |
---|
| 192 | if (file.exists()) { |
---|
| 193 | if (file.isDirectory()) { |
---|
| 194 | for (File child : file.listFiles()) { |
---|
| 195 | deleteFiles(child); |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | try { |
---|
| 200 | file.delete(); |
---|
| 201 | } |
---|
| 202 | catch (Exception e) { |
---|
| 203 | // ignore and delete as much as possible |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|