source: trunk/autoquest-plugin-jfc-test/src/test/java/de/ugoe/cs/autoquest/plugin/jfc/JFCReplayIDCalculatorTest.java @ 922

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