1 | // Module : $RCSfile: TaskTreeChecker.java,v $ |
---|
2 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 01.04.2012 $ |
---|
3 | // Project : TestUtils |
---|
4 | // Creation : 2012 by patrick |
---|
5 | // Copyright : Patrick Harms, 2012 |
---|
6 | |
---|
7 | package de.ugoe.cs.quest.tasktrees.testutils; |
---|
8 | |
---|
9 | import static org.junit.Assert.assertTrue; |
---|
10 | import static org.junit.Assert.fail; |
---|
11 | |
---|
12 | import java.io.FileNotFoundException; |
---|
13 | import java.io.FileOutputStream; |
---|
14 | import java.io.PrintWriter; |
---|
15 | import java.util.ArrayList; |
---|
16 | import java.util.HashMap; |
---|
17 | import java.util.Iterator; |
---|
18 | import java.util.Map; |
---|
19 | import java.util.regex.Matcher; |
---|
20 | import java.util.regex.Pattern; |
---|
21 | |
---|
22 | import de.ugoe.cs.quest.eventcore.guimodel.IGUIElement; |
---|
23 | import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; |
---|
24 | import de.ugoe.cs.quest.tasktrees.treeifc.IIteration; |
---|
25 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeInfo; |
---|
26 | import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; |
---|
27 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
28 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTree; |
---|
29 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
30 | import de.ugoe.cs.quest.tasktrees.treeifc.ITextInputEventTask; |
---|
31 | |
---|
32 | /** |
---|
33 | * TODO comment |
---|
34 | * |
---|
35 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
36 | * @author 2012, last modified by $Author: patrick$ |
---|
37 | */ |
---|
38 | public class TaskTreeChecker { |
---|
39 | |
---|
40 | /** */ |
---|
41 | private boolean doTrace; |
---|
42 | |
---|
43 | /** |
---|
44 | * TODO: comment |
---|
45 | * |
---|
46 | */ |
---|
47 | public TaskTreeChecker() { |
---|
48 | this(false); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * TODO: comment |
---|
53 | * |
---|
54 | */ |
---|
55 | public TaskTreeChecker(boolean doTrace) { |
---|
56 | this.doTrace = doTrace; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * |
---|
61 | */ |
---|
62 | public void assertTaskTree(String taskTreeSpec, ITaskTree taskTree) { |
---|
63 | Map<ITaskTreeNode, Integer> taskMapCopy = new HashMap<ITaskTreeNode, Integer>(); |
---|
64 | |
---|
65 | for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : taskTree.getTaskMap().entrySet()) { |
---|
66 | if (entry.getValue().getNoOfOccurencesInTree() > 0) { |
---|
67 | taskMapCopy.put(entry.getKey(), entry.getValue().getNoOfOccurencesInTree()); |
---|
68 | } |
---|
69 | else { |
---|
70 | taskMapCopy.put(entry.getKey(), 1); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | if (doTrace) { |
---|
75 | dumpTaskMap(taskMapCopy); |
---|
76 | } |
---|
77 | |
---|
78 | TaskSpec task = null; |
---|
79 | |
---|
80 | Matcher matcher = Pattern.compile |
---|
81 | ("(\\s*(\\w+)\\s+(\\w+)\\s+((\\w*\\s)*)(\\{))|((\\}))").matcher(taskTreeSpec); |
---|
82 | |
---|
83 | do { |
---|
84 | if (!matcher.find()) { |
---|
85 | if (!matcher.hitEnd()) { |
---|
86 | throw new IllegalArgumentException("could not parse task specification"); |
---|
87 | } |
---|
88 | else { |
---|
89 | break; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | task = parseTask(matcher); |
---|
94 | if (task != null) { |
---|
95 | assertTaskAndChildrenInMapAndRemove(task, taskMapCopy); |
---|
96 | } |
---|
97 | } |
---|
98 | while (task != null); |
---|
99 | |
---|
100 | assertTrue("more tasks in map, than expected", taskMapCopy.isEmpty()); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * TODO: comment |
---|
105 | * |
---|
106 | * @param taskTree |
---|
107 | */ |
---|
108 | public void dumpAsCheckString(ITaskTree taskTree) { |
---|
109 | dumpNodeAsCheckString(taskTree.getRoot(), new int[4], ""); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * TODO: comment |
---|
114 | * |
---|
115 | * @param root |
---|
116 | * @param string |
---|
117 | */ |
---|
118 | private void dumpNodeAsCheckString(ITaskTreeNode node, int[] typeCounters, String indent) { |
---|
119 | System.err.print(" \""); |
---|
120 | System.err.print(indent); |
---|
121 | |
---|
122 | if (node instanceof ISequence) { |
---|
123 | System.err.print("Sequence sequence"); |
---|
124 | System.err.print(typeCounters[0]++); |
---|
125 | System.err.println(" {\" +"); |
---|
126 | } |
---|
127 | else if (node instanceof IIteration) { |
---|
128 | System.err.print("Iteration iteration"); |
---|
129 | System.err.print(typeCounters[1]++); |
---|
130 | System.err.println(" {\" +"); |
---|
131 | } |
---|
132 | else if (node instanceof ISelection) { |
---|
133 | System.err.print("Selection selection"); |
---|
134 | System.err.print(typeCounters[2]++); |
---|
135 | System.err.println(" {\" +"); |
---|
136 | } |
---|
137 | else if (node instanceof ITextInputEventTask) { |
---|
138 | System.err.print("TextInputEvent textInput"); |
---|
139 | System.err.print(typeCounters[3]++); |
---|
140 | System.err.print(" "); |
---|
141 | System.err.print(((ITextInputEventTask) node).getEnteredText()); |
---|
142 | System.err.println(" {\" +"); |
---|
143 | } |
---|
144 | else if (node instanceof IEventTask) { |
---|
145 | System.err.print("Event "); |
---|
146 | System.err.print(((IEventTask) node).getEventType().getName()); |
---|
147 | System.err.print(" {}\" +"); |
---|
148 | } |
---|
149 | else { |
---|
150 | fail("unknown type of node in task tree " + node); |
---|
151 | } |
---|
152 | |
---|
153 | for (ITaskTreeNode child : node.getChildren()) { |
---|
154 | dumpNodeAsCheckString(child, typeCounters, indent + " "); |
---|
155 | } |
---|
156 | |
---|
157 | if (!(node instanceof IEventTask) || (node instanceof ITextInputEventTask)) { |
---|
158 | System.err.print(" \""); |
---|
159 | System.err.print(indent); |
---|
160 | System.err.print("}\" +"); |
---|
161 | } |
---|
162 | |
---|
163 | System.err.println(); |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * TODO: comment |
---|
168 | * |
---|
169 | * @param taskTree |
---|
170 | */ |
---|
171 | public void dumpFullTaskTree(ITaskTree taskTree) throws FileNotFoundException { |
---|
172 | PrintWriter out = null; |
---|
173 | try { |
---|
174 | out = new PrintWriter(new FileOutputStream("taskTree.txt")); |
---|
175 | dumpFullNode(taskTree.getRoot(), out, ""); |
---|
176 | } |
---|
177 | finally { |
---|
178 | if (out != null) { |
---|
179 | out.close(); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * |
---|
187 | */ |
---|
188 | private void dumpFullNode(ITaskTreeNode node, PrintWriter out, String indent) { |
---|
189 | out.print(indent); |
---|
190 | if (node instanceof ISequence) { |
---|
191 | out.println("Sequence"); |
---|
192 | out.print(indent); |
---|
193 | out.println("{"); |
---|
194 | } |
---|
195 | else if (node instanceof IIteration) { |
---|
196 | out.println("Iteration"); |
---|
197 | out.print(indent); |
---|
198 | out.println("{"); |
---|
199 | } |
---|
200 | else if (node instanceof ISelection) { |
---|
201 | out.println("Selection"); |
---|
202 | out.print(indent); |
---|
203 | out.println("{"); |
---|
204 | } |
---|
205 | else if (node instanceof ITextInputEventTask) { |
---|
206 | out.print("TextInputEvent"); |
---|
207 | out.print(" "); |
---|
208 | out.println(((ITextInputEventTask) node).getEnteredText()); |
---|
209 | out.print(indent); |
---|
210 | out.println("{"); |
---|
211 | } |
---|
212 | else if (node instanceof IEventTask) { |
---|
213 | out.print(((IEventTask) node).getEventType().getName()); |
---|
214 | out.print(" "); |
---|
215 | out.print(((IEventTask) node).getEventTarget()); |
---|
216 | if (((IEventTask) node).getEventTarget() instanceof IGUIElement) |
---|
217 | { |
---|
218 | out.print(" "); |
---|
219 | out.print(((IGUIElement) ((IEventTask) node).getEventTarget()).getSpecification()); |
---|
220 | } |
---|
221 | } |
---|
222 | else { |
---|
223 | fail("unknown type of node in task tree " + node); |
---|
224 | } |
---|
225 | |
---|
226 | for (ITaskTreeNode child : node.getChildren()) { |
---|
227 | dumpFullNode(child, out, indent + " "); |
---|
228 | } |
---|
229 | |
---|
230 | if (!(node instanceof IEventTask) || (node instanceof ITextInputEventTask)) { |
---|
231 | out.print(indent); |
---|
232 | out.print("}"); |
---|
233 | } |
---|
234 | |
---|
235 | out.println(); |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * |
---|
240 | */ |
---|
241 | private TaskSpec parseTask(Matcher tokenMatcher) { |
---|
242 | String firstToken = tokenMatcher.group(); |
---|
243 | |
---|
244 | if ("}".equals(firstToken)) { |
---|
245 | throw new IllegalArgumentException("found a closing bracket at an unexpected place"); |
---|
246 | } |
---|
247 | |
---|
248 | TaskSpec task = new TaskSpec(); |
---|
249 | task.type = tokenMatcher.group(2); |
---|
250 | task.name = tokenMatcher.group(3); |
---|
251 | task.additionalInfo = tokenMatcher.group(4).trim(); |
---|
252 | |
---|
253 | if ("".equals(task.name)) { |
---|
254 | task.name = null; |
---|
255 | } |
---|
256 | |
---|
257 | if (!tokenMatcher.find()) { |
---|
258 | throw new IllegalArgumentException("could not parse task specification"); |
---|
259 | } |
---|
260 | |
---|
261 | firstToken = tokenMatcher.group(); |
---|
262 | |
---|
263 | if (!"}".equals(firstToken)) { |
---|
264 | ArrayList<TaskSpec> children = new ArrayList<TaskSpec>(); |
---|
265 | |
---|
266 | TaskSpec child = null; |
---|
267 | |
---|
268 | do { |
---|
269 | child = parseTask(tokenMatcher); |
---|
270 | |
---|
271 | if (child != null) { |
---|
272 | children.add(child); |
---|
273 | |
---|
274 | if (!tokenMatcher.find()) { |
---|
275 | throw new IllegalArgumentException("could not parse task specification"); |
---|
276 | } |
---|
277 | |
---|
278 | firstToken = tokenMatcher.group(); |
---|
279 | |
---|
280 | if ("}".equals(firstToken)) { |
---|
281 | break; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | } |
---|
286 | while (child != null); |
---|
287 | |
---|
288 | task.children = children.toArray(new TaskSpec[children.size()]); |
---|
289 | } |
---|
290 | |
---|
291 | return task; |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * @param task |
---|
296 | * @param taskMapCopy |
---|
297 | */ |
---|
298 | private void assertTaskAndChildrenInMapAndRemove(TaskSpec task, |
---|
299 | Map<ITaskTreeNode, Integer> taskMap) |
---|
300 | { |
---|
301 | for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) { |
---|
302 | if (taskSpecEqualsTask(task, entry.getKey())) { |
---|
303 | if (task.children != null) { |
---|
304 | for (TaskSpec child : task.children) { |
---|
305 | assertTaskAndChildrenInMapAndRemove(child, taskMap); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | int count = taskMap.get(entry.getKey()); |
---|
310 | if (count == 1) { |
---|
311 | taskMap.remove(entry.getKey()); |
---|
312 | } |
---|
313 | else { |
---|
314 | taskMap.put(entry.getKey(), count - 1); |
---|
315 | } |
---|
316 | return; |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | fail("expected task " + task.type + " " + task.name + |
---|
321 | " not included in task map"); |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * |
---|
326 | */ |
---|
327 | private boolean taskSpecEqualsTask(TaskSpec taskSpec, ITaskTreeNode task) { |
---|
328 | if (doTrace) { |
---|
329 | System.err.println("comparing " + taskSpec.name + " with"); |
---|
330 | dumpTask(task, 0, ""); |
---|
331 | } |
---|
332 | |
---|
333 | if (("Event".equals(taskSpec.type) && (!(task instanceof IEventTask))) || |
---|
334 | ("TextInputEvent".equals(taskSpec.type) && (!(task instanceof ITextInputEventTask))) || |
---|
335 | ("Sequence".equals(taskSpec.type) && (!(task instanceof ISequence))) || |
---|
336 | ("Selection".equals(taskSpec.type) && (!(task instanceof ISelection))) || |
---|
337 | ("Iteration".equals(taskSpec.type) && (!(task instanceof IIteration)))) |
---|
338 | { |
---|
339 | if (doTrace) { |
---|
340 | System.err.println("task types do not match: " + taskSpec.type + " != " + |
---|
341 | task.getClass().getSimpleName() + "\n"); |
---|
342 | } |
---|
343 | return false; |
---|
344 | } |
---|
345 | else if (!"Event".equals(taskSpec.type) && |
---|
346 | !"TextInputEvent".equals(taskSpec.type) && |
---|
347 | !"Sequence".equals(taskSpec.type) && |
---|
348 | !"Selection".equals(taskSpec.type) && |
---|
349 | !"Iteration".equals(taskSpec.type)) |
---|
350 | { |
---|
351 | fail("unknown task type " + taskSpec.type + " --> please extend test case"); |
---|
352 | } |
---|
353 | |
---|
354 | if ("TextInputEvent".equals(taskSpec.type)) { |
---|
355 | if (!"".equals(taskSpec.additionalInfo) && |
---|
356 | !(taskSpec.additionalInfo.equals(((ITextInputEventTask) task).getEnteredText()))) |
---|
357 | { |
---|
358 | if (doTrace) { |
---|
359 | System.err.println("expected text \"" + taskSpec.additionalInfo + |
---|
360 | "\" is not equal to the text " + "provided by the task \"" + |
---|
361 | ((ITextInputEventTask) task).getEnteredText() + "\"\n"); |
---|
362 | } |
---|
363 | return false; |
---|
364 | } |
---|
365 | } |
---|
366 | else if ((task instanceof IEventTask) && (((IEventTask) task).getEventType() != null) && |
---|
367 | (!taskSpec.name.equals(((IEventTask) task).getEventType().getName()))) |
---|
368 | { |
---|
369 | // simple event names do not match. But what about the event name in |
---|
370 | // combination with the additional info |
---|
371 | String complexName = |
---|
372 | taskSpec.name + |
---|
373 | (!"".equals(taskSpec.additionalInfo) ? " " + taskSpec.additionalInfo : ""); |
---|
374 | |
---|
375 | if (!complexName.equals(((IEventTask) task).getEventType().getName())) { |
---|
376 | if (doTrace) { |
---|
377 | System.err.println("event names do not match: " + taskSpec.name + " != " + |
---|
378 | ((IEventTask) task).getEventType().getName() + "\n"); |
---|
379 | } |
---|
380 | return false; |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | if (((taskSpec.children == null) && (task.getChildren().size() > 0)) || |
---|
385 | ((taskSpec.children != null) && (taskSpec.children.length != task.getChildren().size()))) |
---|
386 | { |
---|
387 | if (doTrace) { |
---|
388 | System.err.println |
---|
389 | ("numbers of children do not match: " + |
---|
390 | (taskSpec.children == null ? "0" : taskSpec.children.length) + " != " + |
---|
391 | (task.getChildren() == null ? "0" : task.getChildren().size()) + "\n"); |
---|
392 | } |
---|
393 | return false; |
---|
394 | } |
---|
395 | |
---|
396 | Iterator<ITaskTreeNode> children = task.getChildren().iterator(); |
---|
397 | if (taskSpec.children != null) { |
---|
398 | for (TaskSpec child : taskSpec.children) { |
---|
399 | if (!taskSpecEqualsTask(child, children.next())) { |
---|
400 | if (doTrace) { |
---|
401 | System.err.println("one of the children does not match\n"); |
---|
402 | } |
---|
403 | return false; |
---|
404 | } |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | if (!children.hasNext()) { |
---|
409 | if (doTrace) { |
---|
410 | System.err.println("nodes match\n"); |
---|
411 | } |
---|
412 | return true; |
---|
413 | } |
---|
414 | else { |
---|
415 | if (doTrace) { |
---|
416 | System.err.println("number of children does not match\n"); |
---|
417 | } |
---|
418 | return false; |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | /** |
---|
423 | * |
---|
424 | */ |
---|
425 | private void dumpTaskMap(Map<ITaskTreeNode, Integer> taskMap) { |
---|
426 | System.err.println(); |
---|
427 | for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) { |
---|
428 | dumpTask(entry.getKey(), entry.getValue(), ""); |
---|
429 | System.err.println(); |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | /** |
---|
434 | * |
---|
435 | */ |
---|
436 | private void dumpTask(ITaskTreeNode task, int count, String indent) { |
---|
437 | System.err.print(indent); |
---|
438 | System.err.print(task); |
---|
439 | System.err.print(" "); |
---|
440 | System.err.print(task.getDescription()); |
---|
441 | System.err.print(" "); |
---|
442 | |
---|
443 | if (count > 0) { |
---|
444 | System.err.print("("); |
---|
445 | System.err.print(count); |
---|
446 | System.err.print(" occurrences)"); |
---|
447 | } |
---|
448 | |
---|
449 | System.err.println(); |
---|
450 | |
---|
451 | if ((task.getChildren() != null) && (task.getChildren().size() > 0)) { |
---|
452 | for (ITaskTreeNode child : task.getChildren()) { |
---|
453 | dumpTask(child, 0, indent + " "); |
---|
454 | } |
---|
455 | } |
---|
456 | } |
---|
457 | |
---|
458 | /** |
---|
459 | * TODO comment |
---|
460 | * |
---|
461 | * @version $Revision: $ $Date: $ |
---|
462 | * @author 2011, last modified by $Author: $ |
---|
463 | */ |
---|
464 | private class TaskSpec { |
---|
465 | public String type; |
---|
466 | public String name; |
---|
467 | public String additionalInfo; |
---|
468 | public TaskSpec[] children; |
---|
469 | } |
---|
470 | |
---|
471 | } |
---|