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