source: trunk/quest-core-tasktrees-test/src/test/java/de/ugoe/cs/quest/tasktrees/testutils/TaskTreeChecker.java @ 694

Last change on this file since 694 was 694, checked in by pharms, 12 years ago
  • moved text input detection from task tree generation to dedicated command to be run on sequences
File size: 14.6 KB
Line 
1package de.ugoe.cs.quest.tasktrees.testutils;
2
3import static org.junit.Assert.assertTrue;
4import static org.junit.Assert.fail;
5
6import java.io.FileNotFoundException;
7import java.io.FileOutputStream;
8import java.io.PrintWriter;
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.Iterator;
12import java.util.Map;
13import java.util.regex.Matcher;
14import java.util.regex.Pattern;
15
16import de.ugoe.cs.quest.eventcore.gui.TextInput;
17import de.ugoe.cs.quest.eventcore.guimodel.IGUIElement;
18import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask;
19import de.ugoe.cs.quest.tasktrees.treeifc.IIteration;
20import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeInfo;
21import de.ugoe.cs.quest.tasktrees.treeifc.ISelection;
22import de.ugoe.cs.quest.tasktrees.treeifc.ISequence;
23import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTree;
24import 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 */
32public 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            if (((IEventTask) node).getEventType() instanceof TextInput) {
205                out.print("\"");
206                out.print(((TextInput) ((IEventTask) node).getEventType()).getEnteredText());
207                out.print("\" ");
208            }
209            out.print(((IEventTask) node).getEventTarget());
210            if (((IEventTask) node).getEventTarget() instanceof IGUIElement)
211            {
212              out.print(" ");
213              out.print(((IGUIElement) ((IEventTask) node).getEventTarget()).getSpecification());
214            }
215        }
216        else {
217            fail("unknown type of node in task tree " + node);
218        }
219
220        for (ITaskTreeNode child : node.getChildren()) {
221            dumpFullNode(child, out, indent + "  ");
222        }
223
224        if (!(node instanceof IEventTask)) {
225            out.print(indent);
226            out.print("}");
227        }
228
229        out.println();
230    }
231
232    /**
233     *
234     */
235    private TaskSpec parseTask(Matcher tokenMatcher) {
236        String firstToken = tokenMatcher.group();
237
238        if ("}".equals(firstToken)) {
239            throw new IllegalArgumentException("found a closing bracket at an unexpected place");
240        }
241
242        TaskSpec task = new TaskSpec();
243        task.type = tokenMatcher.group(2);
244        task.name = tokenMatcher.group(3);
245        task.additionalInfo = tokenMatcher.group(4).trim();
246
247        if ("".equals(task.name)) {
248            task.name = null;
249        }
250
251        if (!tokenMatcher.find()) {
252            throw new IllegalArgumentException("could not parse task specification");
253        }
254
255        firstToken = tokenMatcher.group();
256
257        if (!"}".equals(firstToken)) {
258            ArrayList<TaskSpec> children = new ArrayList<TaskSpec>();
259
260            TaskSpec child = null;
261
262            do {
263                child = parseTask(tokenMatcher);
264
265                if (child != null) {
266                    children.add(child);
267
268                    if (!tokenMatcher.find()) {
269                        throw new IllegalArgumentException("could not parse task specification");
270                    }
271
272                    firstToken = tokenMatcher.group();
273
274                    if ("}".equals(firstToken)) {
275                        break;
276                    }
277                }
278
279            }
280            while (child != null);
281
282            task.children = children.toArray(new TaskSpec[children.size()]);
283        }
284
285        return task;
286    }
287
288    /**
289     * @param task
290     * @param taskMapCopy
291     */
292    private void assertTaskAndChildrenInMapAndRemove(TaskSpec                    task,
293                                                     Map<ITaskTreeNode, Integer> taskMap)
294    {
295        for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) {
296            if (taskSpecEqualsTask(task, entry.getKey())) {
297                if (task.children != null) {
298                    for (TaskSpec child : task.children) {
299                        assertTaskAndChildrenInMapAndRemove(child, taskMap);
300                    }
301                }
302
303                int count = taskMap.get(entry.getKey());
304                if (count == 1) {
305                    taskMap.remove(entry.getKey());
306                }
307                else {
308                    taskMap.put(entry.getKey(), count - 1);
309                }
310                return;
311            }
312        }
313
314        fail("expected task " + task.type + " " + task.name +
315             " not included in task map");
316    }
317
318    /**
319     *
320     */
321    private boolean taskSpecEqualsTask(TaskSpec taskSpec, ITaskTreeNode task) {
322        if (doTrace) {
323            System.err.println("comparing " + taskSpec.name + " with");
324            dumpTask(task, 0, "");
325        }
326
327        if (("Event".equals(taskSpec.type) && (!(task instanceof IEventTask))) ||
328            ("TextInputEvent".equals(taskSpec.type) &&
329             (!(task instanceof IEventTask)) &&
330             (!(((IEventTask) task).getEventType() instanceof TextInput))) ||
331            ("Sequence".equals(taskSpec.type) && (!(task instanceof ISequence))) ||
332            ("Selection".equals(taskSpec.type) && (!(task instanceof ISelection))) ||
333            ("Iteration".equals(taskSpec.type) && (!(task instanceof IIteration))))
334        {
335            if (doTrace) {
336                System.err.println("task types do not match: " + taskSpec.type + " != " +
337                    task.getClass().getSimpleName() + "\n");
338            }
339            return false;
340        }
341        else if (!"Event".equals(taskSpec.type) &&
342                 !"TextInputEvent".equals(taskSpec.type) &&
343                 !"Sequence".equals(taskSpec.type) &&
344                 !"Selection".equals(taskSpec.type) &&
345                 !"Iteration".equals(taskSpec.type))
346        {
347            fail("unknown task type " + taskSpec.type + " --> please extend test case");
348        }
349
350        if ("TextInputEvent".equals(taskSpec.type)) {
351            TextInput eventType = (TextInput) ((IEventTask) task).getEventType();
352            if (!"".equals(taskSpec.additionalInfo) &&
353                !(taskSpec.additionalInfo.equals(eventType.getEnteredText())))
354            {
355                if (doTrace) {
356                    System.err.println("expected text \"" + taskSpec.additionalInfo +
357                                       "\" is not equal to the text " + "provided by the task \"" +
358                                       eventType.getEnteredText() + "\"\n");
359                }
360                return false;
361            }
362        }
363        else if ((task instanceof IEventTask) && (((IEventTask) task).getEventType() != null) &&
364                 (!taskSpec.name.equals(((IEventTask) task).getEventType().getName())))
365        {
366            // simple event names do not match. But what about the event name in
367            // combination with the additional info
368            String complexName =
369                taskSpec.name +
370                    (!"".equals(taskSpec.additionalInfo) ? " " + taskSpec.additionalInfo : "");
371
372            if (!complexName.equals(((IEventTask) task).getEventType().getName())) {
373                if (doTrace) {
374                    System.err.println("event names do not match: " + taskSpec.name + " != " +
375                                       ((IEventTask) task).getEventType().getName() + "\n");
376                }
377                return false;
378            }
379        }
380
381        if (((taskSpec.children == null) && (task.getChildren().size() > 0)) ||
382            ((taskSpec.children != null) && (taskSpec.children.length != task.getChildren().size())))
383        {
384            if (doTrace) {
385                System.err.println
386                    ("numbers of children do not match: " +
387                     (taskSpec.children == null ? "0" : taskSpec.children.length) + " != " +
388                     (task.getChildren() == null ? "0" : task.getChildren().size()) + "\n");
389            }
390            return false;
391        }
392
393        Iterator<ITaskTreeNode> children = task.getChildren().iterator();
394        if (taskSpec.children != null) {
395            for (TaskSpec child : taskSpec.children) {
396                if (!taskSpecEqualsTask(child, children.next())) {
397                    if (doTrace) {
398                        System.err.println("one of the children does not match\n");
399                    }
400                    return false;
401                }
402            }
403        }
404
405        if (!children.hasNext()) {
406            if (doTrace) {
407                System.err.println("nodes match\n");
408            }
409            return true;
410        }
411        else {
412            if (doTrace) {
413                System.err.println("number of children does not match\n");
414            }
415            return false;
416        }
417    }
418
419    /**
420   *
421   */
422    private void dumpTaskMap(Map<ITaskTreeNode, Integer> taskMap) {
423        System.err.println();
424        for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) {
425            dumpTask(entry.getKey(), entry.getValue(), "");
426            System.err.println();
427        }
428    }
429
430    /**
431   *
432   */
433    private void dumpTask(ITaskTreeNode task, int count, String indent) {
434        System.err.print(indent);
435        System.err.print(task);
436        System.err.print(" ");
437        System.err.print(task.getDescription());
438        System.err.print(" ");
439
440        if (count > 0) {
441            System.err.print("(");
442            System.err.print(count);
443            System.err.print(" occurrences)");
444        }
445
446        System.err.println();
447
448        if ((task.getChildren() != null) && (task.getChildren().size() > 0)) {
449            for (ITaskTreeNode child : task.getChildren()) {
450                dumpTask(child, 0, indent + "  ");
451            }
452        }
453    }
454
455    /**
456     * TODO comment
457     *
458     * @version $Revision: $ $Date: $
459     * @author 2011, last modified by $Author: $
460     */
461    private class TaskSpec {
462        public String type;
463        public String name;
464        public String additionalInfo;
465        public TaskSpec[] children;
466    }
467
468}
Note: See TracBrowser for help on using the repository browser.