1 | package de.ugoe.cs.quest.plugin.jfc; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.Collection; |
---|
6 | import java.util.HashSet; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Set; |
---|
9 | import java.util.logging.Level; |
---|
10 | |
---|
11 | import junit.framework.TestCase; |
---|
12 | |
---|
13 | import org.junit.After; |
---|
14 | import org.junit.Before; |
---|
15 | import org.junit.Test; |
---|
16 | |
---|
17 | import static org.mockito.Mockito.*; |
---|
18 | |
---|
19 | import de.ugoe.cs.quest.eventcore.Event; |
---|
20 | import de.ugoe.cs.quest.eventcore.IEventTarget; |
---|
21 | import de.ugoe.cs.quest.eventcore.IEventType; |
---|
22 | import de.ugoe.cs.quest.plugin.jfc.eventcore.JFCEventId; |
---|
23 | import de.ugoe.cs.quest.plugin.jfc.guimodel.JFCGUIElement; |
---|
24 | import de.ugoe.cs.quest.plugin.jfc.guimodel.JFCGUIElementSpec; |
---|
25 | import de.ugoe.cs.util.console.TextConsole; |
---|
26 | |
---|
27 | /** |
---|
28 | * The class <code>EFGReplayIDCalculatorTest</code> contains tests for the class |
---|
29 | * {@link <code>EFGReplayIDCalculator</code>} |
---|
30 | * |
---|
31 | * @pattern JUnit Test Case |
---|
32 | * |
---|
33 | * @author fabian.glaser |
---|
34 | * |
---|
35 | * @version $Revision$ |
---|
36 | */ |
---|
37 | public class JFCReplayIDCalculatorTest extends TestCase { |
---|
38 | |
---|
39 | Set<String> knownIDs = new HashSet<String>(); |
---|
40 | |
---|
41 | /** |
---|
42 | * Construct new test instance |
---|
43 | * |
---|
44 | * @param name the test name |
---|
45 | */ |
---|
46 | public JFCReplayIDCalculatorTest(String name) { |
---|
47 | super(name); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Run the String calculateReplayID(JFCEvent) method test. |
---|
52 | * |
---|
53 | * @throws Exception |
---|
54 | * |
---|
55 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
56 | */ |
---|
57 | @Test |
---|
58 | public void testCalculateReplayIDwithEvent() |
---|
59 | throws Exception { |
---|
60 | Collection<JFCEventId> ignoredEvents = new HashSet<JFCEventId>(); |
---|
61 | ignoredEvents.add(JFCEventId.FOCUS_GAINED); |
---|
62 | JFCLogParser parser = new JFCLogParser(ignoredEvents); |
---|
63 | parser.parseFile(new File(ClassLoader.getSystemResource("freemind_trace.xml").getFile())); |
---|
64 | |
---|
65 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
66 | Event event = sequences.iterator().next().get(0); |
---|
67 | |
---|
68 | String result = new JFCReplayIDCalculator().calculateReplayID(event); |
---|
69 | assertEquals("e3561778462", result); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Run the String calculateReplayID(List<JFCGUIElementSpec>) method test. |
---|
74 | * |
---|
75 | * @throws Exception |
---|
76 | */ |
---|
77 | @Test |
---|
78 | public void testCalculateReplayIDwithGuiElementPath() |
---|
79 | throws Exception { |
---|
80 | Collection<JFCEventId> ignoredEvents = new HashSet<JFCEventId>(); |
---|
81 | ignoredEvents.add(JFCEventId.FOCUS_GAINED); |
---|
82 | JFCLogParser parser = new JFCLogParser(ignoredEvents); |
---|
83 | parser.parseFile(new File(ClassLoader.getSystemResource("freemind_trace.xml").getFile())); |
---|
84 | |
---|
85 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
86 | Event event = sequences.iterator().next().get(0); |
---|
87 | |
---|
88 | List<JFCGUIElementSpec> guiElementPath = new ArrayList<JFCGUIElementSpec>(); |
---|
89 | |
---|
90 | IEventTarget target = event.getTarget(); |
---|
91 | JFCGUIElement jfcTarget = (JFCGUIElement) target; |
---|
92 | |
---|
93 | // extract element path |
---|
94 | JFCGUIElement currentTarget = jfcTarget; |
---|
95 | while (currentTarget != null){ |
---|
96 | JFCGUIElementSpec currentSpec = (JFCGUIElementSpec) currentTarget.getSpecification(); |
---|
97 | guiElementPath.add(0, currentSpec); |
---|
98 | currentTarget = (JFCGUIElement) currentTarget.getParent(); |
---|
99 | } |
---|
100 | |
---|
101 | String result = new JFCReplayIDCalculator().calculateReplayID(guiElementPath); |
---|
102 | assertEquals("e3561778462", result); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Method to test if calculated IDs are included in guitar efg file. |
---|
107 | * If not more than 75 % can be matched, test fails. |
---|
108 | * |
---|
109 | * @throws Exception |
---|
110 | */ |
---|
111 | @Test |
---|
112 | public void testCalculateReplayIDAllEvents() |
---|
113 | throws Exception { |
---|
114 | // generate list of known replayIDs from guitar efg file |
---|
115 | File guiFile = new File(ClassLoader.getSystemResource("freemind.xml").getFile()); |
---|
116 | JFCReplayIDValidator validator = new JFCReplayIDValidator(guiFile); |
---|
117 | |
---|
118 | // calculate replayIDs from trace file |
---|
119 | Collection<JFCEventId> ignoredEvents = new HashSet<JFCEventId>(); |
---|
120 | ignoredEvents.add(JFCEventId.FOCUS_GAINED); |
---|
121 | JFCLogParser parser = new JFCLogParser(ignoredEvents); |
---|
122 | parser.parseFile(new File(ClassLoader.getSystemResource("freemind_trace3_corrected.xml").getFile())); |
---|
123 | JFCReplayIDCalculator calculator = new JFCReplayIDCalculator(validator); |
---|
124 | |
---|
125 | Set<String> generatedIDs = new HashSet<String>(); |
---|
126 | |
---|
127 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
128 | |
---|
129 | assertTrue(sequences.size() > 0); |
---|
130 | |
---|
131 | Set<Event> seenEvents = new HashSet<Event>(); |
---|
132 | |
---|
133 | for (List<Event> currentSequence: sequences){ |
---|
134 | seenEvents.addAll(currentSequence); |
---|
135 | } |
---|
136 | |
---|
137 | for (Event currentEvent: seenEvents){ |
---|
138 | String replayID = calculator.calculateReplayID(currentEvent); |
---|
139 | generatedIDs.add(replayID); |
---|
140 | System.out.println("Generated ID: " + replayID); |
---|
141 | System.out.println(); |
---|
142 | } |
---|
143 | |
---|
144 | System.out.println(); |
---|
145 | |
---|
146 | // check if generatedIDs are known |
---|
147 | int known = 0; |
---|
148 | for (String replayID: generatedIDs){ |
---|
149 | if (validator.validateReplayID("w" + replayID.substring(1))){ |
---|
150 | System.out.println(replayID + "\t is known."); |
---|
151 | known++; |
---|
152 | } |
---|
153 | else |
---|
154 | System.out.println(replayID + "\t is unknown."); |
---|
155 | } |
---|
156 | System.out.println(); |
---|
157 | |
---|
158 | float percentage = (float) known/generatedIDs.size()*100; |
---|
159 | System.out.println(percentage + "% of the generated IDs are known."); |
---|
160 | |
---|
161 | assertTrue(percentage > 75); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Method to test if calculated IDs are included in guitar efg file. |
---|
166 | * If not more than 75 % can be matched, test fails. |
---|
167 | * |
---|
168 | * @throws Exception |
---|
169 | */ |
---|
170 | @Test |
---|
171 | public void testCalculateReplayIDAllEventsArgo() |
---|
172 | throws Exception { |
---|
173 | // generate list of known replayIDs from guitar efg file |
---|
174 | File guiFile = new File(ClassLoader.getSystemResource("argo.xml").getFile()); |
---|
175 | JFCReplayIDValidator validator = new JFCReplayIDValidator(guiFile); |
---|
176 | |
---|
177 | // calculate replayIDs from trace file |
---|
178 | Collection<JFCEventId> ignoredEvents = new HashSet<JFCEventId>(); |
---|
179 | ignoredEvents.add(JFCEventId.FOCUS_GAINED); |
---|
180 | JFCLogParser parser = new JFCLogParser(ignoredEvents); |
---|
181 | parser.parseFile(new File(ClassLoader.getSystemResource("argouml_trace1_corrected.xml").getFile())); |
---|
182 | JFCReplayIDCalculator calculator = new JFCReplayIDCalculator(validator); |
---|
183 | |
---|
184 | Set<String> generatedIDs = new HashSet<String>(); |
---|
185 | |
---|
186 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
187 | |
---|
188 | assertTrue(sequences.size() > 0); |
---|
189 | |
---|
190 | Set<Event> seenEvents = new HashSet<Event>(); |
---|
191 | |
---|
192 | for (List<Event> currentSequence: sequences){ |
---|
193 | seenEvents.addAll(currentSequence); |
---|
194 | } |
---|
195 | |
---|
196 | for (Event currentEvent: seenEvents){ |
---|
197 | String replayID = calculator.calculateReplayID(currentEvent); |
---|
198 | generatedIDs.add(replayID); |
---|
199 | System.out.println("Generated ID: " + replayID); |
---|
200 | System.out.println(); |
---|
201 | } |
---|
202 | |
---|
203 | System.out.println(); |
---|
204 | |
---|
205 | // check if generatedIDs are known |
---|
206 | int known = 0; |
---|
207 | for (String replayID: generatedIDs){ |
---|
208 | if (validator.validateReplayID("w" + replayID.substring(1))){ |
---|
209 | System.out.println(replayID + "\t is known."); |
---|
210 | known++; |
---|
211 | } |
---|
212 | else |
---|
213 | System.out.println(replayID + "\t is unknown."); |
---|
214 | } |
---|
215 | System.out.println(); |
---|
216 | |
---|
217 | float percentage = (float) known/generatedIDs.size()*100; |
---|
218 | System.out.println(percentage + "% of the generated IDs are known."); |
---|
219 | |
---|
220 | assertTrue(percentage > 75); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | /** |
---|
225 | * Method to test if calculateReplayID throws the right exception when |
---|
226 | * it is called with a target of the wrong type. |
---|
227 | */ |
---|
228 | @Test |
---|
229 | public void testCalculateReplayIDIllegalArgumentException(){ |
---|
230 | try{ |
---|
231 | JFCReplayIDCalculator calculator = new JFCReplayIDCalculator(); |
---|
232 | Event event = new Event(mock(IEventType.class), mock(IEventTarget.class)); |
---|
233 | |
---|
234 | calculator.calculateReplayID(event); |
---|
235 | |
---|
236 | fail("Expected IllegalArgumentException!"); |
---|
237 | } |
---|
238 | catch(IllegalArgumentException e){ |
---|
239 | System.out.println("Expected exception thrown."); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * Perform pre-test initialization. |
---|
245 | * |
---|
246 | * @throws Exception |
---|
247 | * if the initialization fails for some reason |
---|
248 | * |
---|
249 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
250 | */ |
---|
251 | @Before |
---|
252 | public void setUp() |
---|
253 | throws Exception { |
---|
254 | new TextConsole(Level.FINEST); |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * Perform post-test clean-up. |
---|
259 | * |
---|
260 | * @throws Exception |
---|
261 | * if the clean-up fails for some reason |
---|
262 | * |
---|
263 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
264 | */ |
---|
265 | @After |
---|
266 | public void tearDown() |
---|
267 | throws Exception { |
---|
268 | // Add additional tear down code here |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * Launch the test. |
---|
273 | * |
---|
274 | * @param args the command line arguments |
---|
275 | * |
---|
276 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
277 | */ |
---|
278 | public static void main(String[] args) { |
---|
279 | new org.junit.runner.JUnitCore().run(JFCReplayIDCalculatorTest.class); |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | /*$CPS$ This comment was generated by CodePro. Do not edit it. |
---|
284 | * patternId = com.instantiations.assist.eclipse.pattern.testCasePattern |
---|
285 | * strategyId = com.instantiations.assist.eclipse.pattern.testCasePattern.junitTestCase |
---|
286 | * additionalTestNames = |
---|
287 | * assertTrue = false |
---|
288 | * callTestMethod = true |
---|
289 | * createMain = false |
---|
290 | * createSetUp = false |
---|
291 | * createTearDown = false |
---|
292 | * createTestFixture = false |
---|
293 | * createTestStubs = false |
---|
294 | * methods = |
---|
295 | * package = de.ugoe.cs.eventbench.efg |
---|
296 | * package.sourceFolder = EventBenchConsoleTest/src |
---|
297 | * superclassType = junit.framework.TestCase |
---|
298 | * testCase = EFGEventIDCalculatorTest |
---|
299 | * testClassType = de.ugoe.cs.eventbench.efg.EFGEventIDCalculator |
---|
300 | */ |
---|