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

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