source: trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeChecker.java @ 922

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