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

Last change on this file since 806 was 806, checked in by fglaser, 12 years ago
  • new helper class JFCReplayIDValidator added, that is able to extract all IDs from

GUITAR Gui file and to check if a given (generated) replayID is contained in this GUI file.

  • JFCReplayIDCalculator modified for test purposes (can be initialized with a Validator

to check if IDs that are generated "on the way" are valid)

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