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