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.plugin.mfc;
|
---|
16 |
|
---|
17 | import static org.junit.Assert.assertEquals;
|
---|
18 | import static org.junit.Assert.assertNotNull;
|
---|
19 | import static org.junit.Assert.assertNotSame;
|
---|
20 | import static org.junit.Assert.assertTrue;
|
---|
21 |
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.FileInputStream;
|
---|
24 | import java.io.FileOutputStream;
|
---|
25 | import java.io.InputStreamReader;
|
---|
26 | import java.io.PrintStream;
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Collection;
|
---|
29 | import java.util.List;
|
---|
30 |
|
---|
31 | import org.junit.After;
|
---|
32 | import org.junit.Before;
|
---|
33 | import org.junit.Test;
|
---|
34 |
|
---|
35 | import de.ugoe.cs.autoquest.commands.sequences.CMDgenerateReplayfile;
|
---|
36 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
37 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
38 | import de.ugoe.cs.autoquest.plugin.mfc.LogPreprocessor;
|
---|
39 | import de.ugoe.cs.autoquest.plugin.mfc.MFCLogParser;
|
---|
40 | import de.ugoe.cs.util.console.Console;
|
---|
41 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
42 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @author Steffen Herbold
|
---|
46 | */
|
---|
47 | public class MFCLogParserTest implements IOutputListener {
|
---|
48 |
|
---|
49 | /** */
|
---|
50 | private File tmpFile;
|
---|
51 |
|
---|
52 | /** */
|
---|
53 | private PrintStream outputStream;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | *
|
---|
57 | */
|
---|
58 | @Before
|
---|
59 | public void setUp() throws Exception {
|
---|
60 | outputStream = new PrintStream(new FileOutputStream("blub.log"));
|
---|
61 | Console.getInstance().registerOutputListener(this);
|
---|
62 |
|
---|
63 | tmpFile = new File("tmp.xml");
|
---|
64 |
|
---|
65 | if (tmpFile.exists()) {
|
---|
66 | assertTrue(tmpFile.delete());
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | *
|
---|
72 | */
|
---|
73 | @After
|
---|
74 | public void tearDown() throws Exception {
|
---|
75 | if (!System.out.equals(outputStream) && !System.err.equals(outputStream)) {
|
---|
76 | outputStream.close();
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (tmpFile.exists()) {
|
---|
80 | assertTrue(tmpFile.delete());
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | *
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | public void testTrace() throws Exception {
|
---|
90 | File traceFile = new File(ClassLoader.getSystemResource("trace.txt").getFile());
|
---|
91 |
|
---|
92 | new LogPreprocessor().convertToXml(traceFile.getAbsolutePath(),
|
---|
93 | tmpFile.getAbsolutePath());
|
---|
94 |
|
---|
95 | MFCLogParser parser = new MFCLogParser();
|
---|
96 | parser.parseFile(tmpFile);
|
---|
97 | Collection<List<Event>> events = parser.getSequences();
|
---|
98 |
|
---|
99 | assertNotNull(events);
|
---|
100 | assertTrue(events.size() > 0);
|
---|
101 |
|
---|
102 | System.err.println("{");
|
---|
103 | for (List<Event> session : events) {
|
---|
104 | System.err.println(" {");
|
---|
105 | for (Event event : session) {
|
---|
106 | System.err.print(" ");
|
---|
107 | System.err.print(event);
|
---|
108 | System.err.println(",");
|
---|
109 | }
|
---|
110 | System.err.println(" }");
|
---|
111 | }
|
---|
112 | System.err.println("}");
|
---|
113 | System.err.println("\n\n");
|
---|
114 |
|
---|
115 | GUIModel guiModel = parser.getGuiModel();
|
---|
116 | assertNotNull(guiModel);
|
---|
117 | assertNotSame(0, guiModel.getRootElements().size());
|
---|
118 |
|
---|
119 | guiModel.dump(System.err, "UTF-8");
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | *
|
---|
124 | */
|
---|
125 | @Test
|
---|
126 | public void testLogfile() throws Exception {
|
---|
127 | File logFile = new File(ClassLoader.getSystemResource("log.xml").getFile());
|
---|
128 |
|
---|
129 | MFCLogParser parser = new MFCLogParser();
|
---|
130 | parser.parseFile(logFile);
|
---|
131 | Collection<List<Event>> events = parser.getSequences();
|
---|
132 |
|
---|
133 | assertNotNull(events);
|
---|
134 | assertTrue(events.size() > 0);
|
---|
135 |
|
---|
136 | System.err.println("{");
|
---|
137 | for (List<Event> session : events) {
|
---|
138 | System.err.println(" {");
|
---|
139 | for (Event event : session) {
|
---|
140 | System.err.print(" ");
|
---|
141 | System.err.print(event);
|
---|
142 | System.err.println(",");
|
---|
143 | }
|
---|
144 | System.err.println(" }");
|
---|
145 | }
|
---|
146 | System.err.println("}");
|
---|
147 | System.err.println("\n\n");
|
---|
148 |
|
---|
149 | GUIModel guiModel = parser.getGuiModel();
|
---|
150 | assertNotNull(guiModel);
|
---|
151 |
|
---|
152 | guiModel.dump(System.err, "UTF-8");
|
---|
153 |
|
---|
154 |
|
---|
155 | GlobalDataContainer.getInstance().addData("seqs", parser.getSequences());
|
---|
156 |
|
---|
157 | List<Object> parameters = new ArrayList<Object>();
|
---|
158 | parameters.add(tmpFile.getAbsolutePath());
|
---|
159 | parameters.add("seqs");
|
---|
160 |
|
---|
161 | CMDgenerateReplayfile cmd = new CMDgenerateReplayfile();
|
---|
162 | cmd.run(parameters);
|
---|
163 |
|
---|
164 | InputStreamReader reader1 =
|
---|
165 | new InputStreamReader(ClassLoader.getSystemResourceAsStream("replay.xml"), "UTF-8");
|
---|
166 |
|
---|
167 | InputStreamReader reader2 = new InputStreamReader(new FileInputStream(tmpFile), "UTF-8");
|
---|
168 |
|
---|
169 | try {
|
---|
170 | int sign1;
|
---|
171 | do {
|
---|
172 | sign1 = reader1.read();
|
---|
173 | assertEquals(sign1, reader2.read());
|
---|
174 | }
|
---|
175 | while (sign1 > -1);
|
---|
176 | }
|
---|
177 | finally {
|
---|
178 | reader1.close();
|
---|
179 | reader2.close();
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /* (non-Javadoc)
|
---|
184 | * @see de.ugoe.cs.util.console.listener.IOutputListener#outputMsg(java.lang.String)
|
---|
185 | */
|
---|
186 | @Override
|
---|
187 | public void outputMsg(String newMessage) {
|
---|
188 | outputStream.println(newMessage);
|
---|
189 | }
|
---|
190 |
|
---|
191 | }
|
---|