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 javax.xml.parsers.DocumentBuilder; |
---|
12 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
13 | |
---|
14 | import junit.framework.TestCase; |
---|
15 | |
---|
16 | import org.junit.After; |
---|
17 | import org.junit.Before; |
---|
18 | import org.junit.Test; |
---|
19 | import org.w3c.dom.Document; |
---|
20 | import org.w3c.dom.Node; |
---|
21 | import org.w3c.dom.NodeList; |
---|
22 | |
---|
23 | import de.ugoe.cs.quest.eventcore.Event; |
---|
24 | import de.ugoe.cs.quest.eventcore.IEventTarget; |
---|
25 | import de.ugoe.cs.quest.plugin.jfc.eventcore.JFCEventId; |
---|
26 | import de.ugoe.cs.quest.plugin.jfc.guimodel.JFCGUIElement; |
---|
27 | import de.ugoe.cs.quest.plugin.jfc.guimodel.JFCGUIElementSpec; |
---|
28 | import 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 | * @generatedBy CodePro at 7/30/12 4:50 PM |
---|
37 | * |
---|
38 | * @author fabian.glaser |
---|
39 | * |
---|
40 | * @version $Revision$ |
---|
41 | */ |
---|
42 | public class JFCReplayIDCalculatorTest extends TestCase { |
---|
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 | * Run the String calculateReplayID(List<JFCGUIElementSpec>) method test. |
---|
110 | * |
---|
111 | * @throws Exception |
---|
112 | */ |
---|
113 | @Test |
---|
114 | public void testCalculateReplayIDAllEvents() |
---|
115 | throws Exception { |
---|
116 | // generate list of known replayIDs from guitar efg file |
---|
117 | File guiFile = new File(ClassLoader.getSystemResource("freemind.efg").getFile()); |
---|
118 | |
---|
119 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
120 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
121 | Document freemindGUI = builder.parse(guiFile); |
---|
122 | |
---|
123 | Node currentNode = freemindGUI.getFirstChild().getFirstChild(); |
---|
124 | NodeList eventNodes = null; |
---|
125 | List<String> knownIDs = new ArrayList<String>(); |
---|
126 | |
---|
127 | while ((currentNode = currentNode.getNextSibling()) != null){ |
---|
128 | if (currentNode.getNodeName().equals("Events")) |
---|
129 | eventNodes = currentNode.getChildNodes(); |
---|
130 | } |
---|
131 | |
---|
132 | for (int i = 0; i < eventNodes.getLength(); i++){ |
---|
133 | if (eventNodes.item(i).getNodeName().equals("Event")){ |
---|
134 | NodeList attributes = eventNodes.item(i).getChildNodes(); |
---|
135 | for (int j = 0; j < attributes.getLength(); j++){ |
---|
136 | if (attributes.item(j).getNodeName().equals("EventId")){ |
---|
137 | knownIDs.add(attributes.item(j).getTextContent()); |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | // calculate replayIDs from trace file |
---|
144 | Collection<JFCEventId> ignoredEvents = new HashSet<JFCEventId>(); |
---|
145 | ignoredEvents.add(JFCEventId.FOCUS_GAINED); |
---|
146 | JFCLogParser parser = new JFCLogParser(ignoredEvents); |
---|
147 | parser.parseFile(new File(ClassLoader.getSystemResource("freemind_trace2.xml").getFile())); |
---|
148 | JFCReplayIDCalculator calculator = new JFCReplayIDCalculator(); |
---|
149 | |
---|
150 | Set<String> generatedIDs = new HashSet<String>(); |
---|
151 | |
---|
152 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
153 | |
---|
154 | assertTrue(sequences.size() > 0); |
---|
155 | |
---|
156 | |
---|
157 | for (List<Event> currentSequence: sequences){ |
---|
158 | for (Event currentEvent: currentSequence){ |
---|
159 | String replayID = calculator.calculateReplayID(currentEvent); |
---|
160 | generatedIDs.add(replayID); |
---|
161 | System.out.println("Generated ID: " + replayID); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | System.out.println(); |
---|
166 | |
---|
167 | // check if generatedIDs are known |
---|
168 | int known = 0; |
---|
169 | for (String replayID: generatedIDs){ |
---|
170 | if (knownIDs.contains(replayID)){ |
---|
171 | System.out.println(replayID + "\t is known."); |
---|
172 | known++; |
---|
173 | } |
---|
174 | else |
---|
175 | System.out.println(replayID + "\t is unknown."); |
---|
176 | } |
---|
177 | System.out.println(); |
---|
178 | |
---|
179 | float percentage = (float) known/generatedIDs.size()*100; |
---|
180 | System.out.println(percentage + "% of the generated IDs are known."); |
---|
181 | |
---|
182 | assertTrue(percentage > 75); |
---|
183 | |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * Perform pre-test initialization. |
---|
189 | * |
---|
190 | * @throws Exception |
---|
191 | * if the initialization fails for some reason |
---|
192 | * |
---|
193 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
194 | */ |
---|
195 | @Before |
---|
196 | public void setUp() |
---|
197 | throws Exception { |
---|
198 | new TextConsole(Level.FINEST); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * Perform post-test clean-up. |
---|
203 | * |
---|
204 | * @throws Exception |
---|
205 | * if the clean-up fails for some reason |
---|
206 | * |
---|
207 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
208 | */ |
---|
209 | @After |
---|
210 | public void tearDown() |
---|
211 | throws Exception { |
---|
212 | // Add additional tear down code here |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Launch the test. |
---|
217 | * |
---|
218 | * @param args the command line arguments |
---|
219 | * |
---|
220 | * @generatedBy CodePro at 7/30/12 4:58 PM |
---|
221 | */ |
---|
222 | public static void main(String[] args) { |
---|
223 | new org.junit.runner.JUnitCore().run(JFCReplayIDCalculatorTest.class); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | /*$CPS$ This comment was generated by CodePro. Do not edit it. |
---|
228 | * patternId = com.instantiations.assist.eclipse.pattern.testCasePattern |
---|
229 | * strategyId = com.instantiations.assist.eclipse.pattern.testCasePattern.junitTestCase |
---|
230 | * additionalTestNames = |
---|
231 | * assertTrue = false |
---|
232 | * callTestMethod = true |
---|
233 | * createMain = false |
---|
234 | * createSetUp = false |
---|
235 | * createTearDown = false |
---|
236 | * createTestFixture = false |
---|
237 | * createTestStubs = false |
---|
238 | * methods = |
---|
239 | * package = de.ugoe.cs.eventbench.efg |
---|
240 | * package.sourceFolder = EventBenchConsoleTest/src |
---|
241 | * superclassType = junit.framework.TestCase |
---|
242 | * testCase = EFGEventIDCalculatorTest |
---|
243 | * testClassType = de.ugoe.cs.eventbench.efg.EFGEventIDCalculator |
---|
244 | */ |
---|