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.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 | for (List<Event> currentSequence: sequences){ |
---|
132 | for (Event currentEvent: currentSequence){ |
---|
133 | String replayID = calculator.calculateReplayID(currentEvent); |
---|
134 | generatedIDs.add(replayID); |
---|
135 | System.out.println("Generated ID: " + replayID); |
---|
136 | System.out.println(); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | System.out.println(); |
---|
141 | |
---|
142 | // check if generatedIDs are known |
---|
143 | int known = 0; |
---|
144 | for (String replayID: generatedIDs){ |
---|
145 | if (validator.validateReplayID("w" + replayID.substring(1))){ |
---|
146 | System.out.println(replayID + "\t is known."); |
---|
147 | known++; |
---|
148 | } |
---|
149 | else |
---|
150 | System.out.println(replayID + "\t is unknown."); |
---|
151 | } |
---|
152 | System.out.println(); |
---|
153 | |
---|
154 | float percentage = (float) known/generatedIDs.size()*100; |
---|
155 | System.out.println(percentage + "% of the generated IDs are known."); |
---|
156 | |
---|
157 | assertTrue(percentage > 75); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | /** |
---|
162 | * Method to test if calculateReplayID throws the right exception when |
---|
163 | * it is called with a target of the wrong type. |
---|
164 | */ |
---|
165 | @Test |
---|
166 | public void testCalculateReplayIDIllegalArgumentException(){ |
---|
167 | try{ |
---|
168 | JFCReplayIDCalculator calculator = new JFCReplayIDCalculator(); |
---|
169 | Event event = new Event(mock(IEventType.class), mock(IEventTarget.class)); |
---|
170 | |
---|
171 | calculator.calculateReplayID(event); |
---|
172 | |
---|
173 | fail("Expected IllegalArgumentException!"); |
---|
174 | } |
---|
175 | catch(IllegalArgumentException e){ |
---|
176 | System.out.println("Expected exception thrown."); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Perform pre-test initialization. |
---|
182 | * |
---|
183 | * @throws Exception |
---|
184 | * if the initialization fails for some reason |
---|
185 | * |
---|
186 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
187 | */ |
---|
188 | @Before |
---|
189 | public void setUp() |
---|
190 | throws Exception { |
---|
191 | new TextConsole(Level.FINEST); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * Perform post-test clean-up. |
---|
196 | * |
---|
197 | * @throws Exception |
---|
198 | * if the clean-up fails for some reason |
---|
199 | * |
---|
200 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
201 | */ |
---|
202 | @After |
---|
203 | public void tearDown() |
---|
204 | throws Exception { |
---|
205 | // Add additional tear down code here |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * Launch the test. |
---|
210 | * |
---|
211 | * @param args the command line arguments |
---|
212 | * |
---|
213 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
214 | */ |
---|
215 | public static void main(String[] args) { |
---|
216 | new org.junit.runner.JUnitCore().run(JFCReplayIDCalculatorTest.class); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | /*$CPS$ This comment was generated by CodePro. Do not edit it. |
---|
221 | * patternId = com.instantiations.assist.eclipse.pattern.testCasePattern |
---|
222 | * strategyId = com.instantiations.assist.eclipse.pattern.testCasePattern.junitTestCase |
---|
223 | * additionalTestNames = |
---|
224 | * assertTrue = false |
---|
225 | * callTestMethod = true |
---|
226 | * createMain = false |
---|
227 | * createSetUp = false |
---|
228 | * createTearDown = false |
---|
229 | * createTestFixture = false |
---|
230 | * createTestStubs = false |
---|
231 | * methods = |
---|
232 | * package = de.ugoe.cs.eventbench.efg |
---|
233 | * package.sourceFolder = EventBenchConsoleTest/src |
---|
234 | * superclassType = junit.framework.TestCase |
---|
235 | * testCase = EFGEventIDCalculatorTest |
---|
236 | * testClassType = de.ugoe.cs.eventbench.efg.EFGEventIDCalculator |
---|
237 | */ |
---|