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