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

Last change on this file since 1146 was 1146, checked in by pharms, 11 years ago
  • complete refactoring of task tree model with a separation of task models and task instances
  • appropriate adaptation of task tree generation process
  • appropriate adaptation of commands and task tree visualization
File size: 14.8 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.*;
18
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.Iterator;
22import java.util.List;
23import java.util.Map;
24import java.util.regex.Matcher;
25import java.util.regex.Pattern;
26
27import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
35import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
36
37/**
38 * TODO comment
39 *
40 * @version $Revision: $ $Date: 01.04.2012$
41 * @author 2012, last modified by $Author: patrick$
42 */
43public class TaskTreeChecker {
44   
45    /** */
46    private static Pattern taskPattern = Pattern.compile("([^{}]+)\\{|\\}");
47   
48    /** */
49    private static Pattern taskDetailsPattern =
50        Pattern.compile("\\s*(\\w*)\\s*([\\w\\(\\)\"]*)\\s*((\\w*)|(\".*\"))?");
51   
52    /** */
53    private boolean doTrace;
54
55    /**
56     *
57     */
58    public TaskTreeChecker() {
59        this(false);
60    }
61
62    /**
63     *
64     */
65    public TaskTreeChecker(boolean doTrace) {
66        this.doTrace = doTrace;
67    }
68
69    /**
70     *
71     */
72    public void assertTaskInstanceList(String userSessionSpec, ITaskInstanceList taskInstances) {
73        if (doTrace) {
74            new TaskTreeEncoder().encode(taskInstances, System.err);
75        }
76
77        TaskSpec taskInstanceSpec = null;
78
79        Matcher taskMatcher = taskPattern.matcher(userSessionSpec);
80       
81        Map<String, ITask> tasks = new HashMap<String, ITask>();
82
83        while (taskMatcher.find()) {
84
85            taskInstanceSpec = parseTaskInstance(taskMatcher);
86           
87            if (taskInstanceSpec != null) {
88                assertTaskInstanceList(taskInstanceSpec, taskInstances, tasks);
89            }
90        }
91    }
92
93    /**
94     *
95     */
96    public void assertTaskInstanceListsEqual(ITaskInstanceList expected, ITaskInstanceList checked)
97    {
98        if (expected == null) {
99            assertNull("null", checked);
100        }
101        else {
102            assertNotNull(expected.toString(), checked);
103           
104            assertEquals(expected.toString() + ": types do not match",
105                         expected.getClass(), checked.getClass());
106           
107            if ((expected != null) && (expected.size() > 0)) {
108                assertNotNull(expected.toString() + ": children not there", checked);
109                assertEquals(expected.toString() + ": different number of children",
110                             expected.size(), checked.size());
111               
112                Map<ITask, ITask> equalTasksMap = new HashMap<ITask, ITask>();
113                for (int i = 0; i < expected.size(); i++) {
114                    assertTaskInstancesEqual(expected.get(i), checked.get(i), equalTasksMap);
115                }
116            }
117            else {
118                assertTrue(expected.toString() + ": unexpected children",
119                           (checked == null) || (checked.size() == 0));
120            }
121        }
122    }
123
124    /**
125     *
126     */
127    public void assertTaskInstancesEqual(ITaskInstance expected, ITaskInstance checked) {
128        Map<ITask, ITask> equalTasksMap = new HashMap<ITask, ITask>();
129        assertTaskInstancesEqual(expected, checked, equalTasksMap);
130    }
131
132    /**
133     *
134     */
135    private void assertTaskInstancesEqual(ITaskInstance     expected,
136                                          ITaskInstance     checked,
137                                          Map<ITask, ITask> equalTasksMap)
138    {
139        if (expected == null) {
140            assertNull("null", checked);
141        }
142        else {
143            assertNotNull(expected.toString(), checked);
144           
145            assertEquals(expected.toString() + ": types do not match",
146                         expected.getClass(), checked.getClass());
147           
148            if (equalTasksMap.containsKey(expected.getTask())) {
149                assertEquals(expected.toString() + ": tasks do not match",
150                             checked.getTask(), equalTasksMap.get(expected.getTask()));
151            }
152            else {
153                equalTasksMap.put(expected.getTask(), checked.getTask());
154            }
155           
156            List<ITaskInstance> expectedChildren = expected.getChildren();
157            List<ITaskInstance> checkedChildren = checked.getChildren();
158           
159            if ((expectedChildren != null) && (expectedChildren.size() > 0)) {
160                assertNotNull(expected.toString() + ": children not there", checkedChildren);
161                assertEquals(expected.toString() + ": different number of children",
162                             expectedChildren.size(), checkedChildren.size());
163               
164                if (expected instanceof ISequence) {
165                    for (int i = 0; i < expectedChildren.size(); i++) {
166                        assertTaskInstancesEqual(expectedChildren.get(i), checkedChildren.get(i));
167                    }
168                }
169                else {
170                    for (int i = 0; i < expectedChildren.size(); i++) {
171                        boolean found = false;
172                        for (int j = 0; j < checkedChildren.size(); j++) {
173                            try {
174                                assertTaskInstancesEqual
175                                    (expectedChildren.get(i), checkedChildren.get(j));
176                                found = true;
177                                break;
178                            }
179                            catch (AssertionError e) {
180                                // try next
181                            }
182                        }
183                       
184                        assertTrue("one of the children not found", found);
185                    }
186                }
187            }
188            else {
189                assertTrue(expected.toString() + ": unexpected children",
190                           (checkedChildren == null) || (checkedChildren.size() == 0));
191            }
192        }
193    }
194
195    /**
196     *
197     */
198    private TaskSpec parseTaskInstance(Matcher taskMatcher) {
199        if ("}".equals(taskMatcher.group(1))) {
200            throw new IllegalArgumentException("invalid task specification");
201        }
202       
203        String taskDetails = taskMatcher.group(1);
204       
205        Matcher matcher = taskDetailsPattern.matcher(taskDetails);
206       
207        if (!matcher.find()) {
208            throw new IllegalArgumentException("could not parse task details");
209        }
210
211        TaskSpec task = new TaskSpec();
212        task.type = matcher.group(1);
213       
214        task.name = matcher.group(2);
215        if ((matcher.group(4) != null) && (!"".equals(matcher.group(4).trim()))) {
216            task.name += " " + matcher.group(4).trim();
217        }
218       
219        if ((matcher.group(5) != null) && (!"".equals(matcher.group(5).trim()))) {
220            task.additionalInfo = matcher.group(5).trim();
221        }
222
223        if ("".equals(task.name)) {
224            task.name = null;
225        }
226
227        List<TaskSpec> children = new ArrayList<TaskSpec>();
228        while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) {
229            children.add(parseTaskInstance(taskMatcher));
230        }
231
232        if (children.size() > 0) {
233            task.children = children.toArray(new TaskSpec[children.size()]);
234        }
235
236        return task;
237    }
238
239    /**
240     * @param task
241     * @param taskMapCopy
242     */
243    private void assertTaskInstanceList(TaskSpec           taskSpec,
244                                        ITaskInstanceList  taskInstances,
245                                        Map<String, ITask> tasks)
246    {
247        if (doTrace) {
248            System.err.println("\ncomparing " + taskSpec.type + " with " + taskInstances + "\n");
249        }
250
251        if ((taskInstances instanceof IUserSession) && (!"UserSession".equals(taskSpec.type))) {
252            fail("can not compare a task instance with a user session");
253        }
254        else if ((!(taskInstances instanceof IUserSession)) &&
255                 (!"TaskInstances".equals(taskSpec.type)))
256        {
257            fail("can not compare a task instance with a task instance list");
258        }
259       
260        if (taskSpec.children.length != taskInstances.size()) {
261            fail("number of task instances in task instance list does not match");
262        }
263       
264        for (int i = 0; i < taskInstances.size(); i++) {
265            TaskSpec childSpec = taskSpec.children[i];
266            assertTrue(taskSpecEqualsTaskInstance(childSpec, taskInstances.get(i), tasks));
267        }
268    }
269
270    /**
271     *
272     */
273    private boolean taskSpecEqualsTaskInstance(TaskSpec           taskSpec,
274                                               ITaskInstance      taskInstance,
275                                               Map<String, ITask> tasks)
276    {
277        if (doTrace) {
278            System.err.println("comparing " + taskSpec.name + " with");
279            new TaskTreeEncoder().encode(taskInstance, System.err);
280        }
281
282        ITask task = taskInstance.getTask();
283       
284        if (("Event".equals(taskSpec.type) && (!(task instanceof IEventTask))) ||
285            ("TextInputEvent".equals(taskSpec.type) &&
286             ((!(task instanceof IEventTask)) ||
287              (!(((IEventTask) task).getEventType() instanceof TextInput)))) ||
288            ("Sequence".equals(taskSpec.type) && (!(task instanceof ISequence))) ||
289            ("Selection".equals(taskSpec.type) && (!(task instanceof ISelection))) ||
290            ("Iteration".equals(taskSpec.type) && (!(task instanceof IIteration))))
291        {
292            if (doTrace) {
293                System.err.println("task types do not match: " + taskSpec.type + " != " +
294                                   task.getClass().getSimpleName() + "\n");
295            }
296            return false;
297        }
298        else if (!"Event".equals(taskSpec.type) &&
299                 !"TextInputEvent".equals(taskSpec.type) &&
300                 !"Sequence".equals(taskSpec.type) &&
301                 !"Selection".equals(taskSpec.type) &&
302                 !"Iteration".equals(taskSpec.type))
303        {
304            fail("unknown task type " + taskSpec.type + " --> please extend test case");
305        }
306
307        if ("TextInputEvent".equals(taskSpec.type)) {
308            TextInput eventType = (TextInput) ((IEventTask) task).getEventType();
309            if ((taskSpec.additionalInfo != null) &&
310                !"".equals(taskSpec.additionalInfo) &&
311                !(taskSpec.additionalInfo.equals(eventType.getEnteredText())))
312            {
313                if (doTrace) {
314                    System.err.println("expected text \"" + taskSpec.additionalInfo +
315                                       "\" is not equal to the text " + "provided by the task \"" +
316                                       eventType.getEnteredText() + "\"\n");
317                }
318                return false;
319            }
320        }
321        else if ((task instanceof IEventTask) && (((IEventTask) task).getEventType() != null) &&
322                 (!taskSpec.name.equals(((IEventTask) task).getEventType().getName())))
323        {
324            // simple event names do not match. But what about the event name in
325            // combination with the additional info
326            String complexName =
327                taskSpec.name +
328                    (!"".equals(taskSpec.additionalInfo) ? " " + taskSpec.additionalInfo : "");
329
330            if (!complexName.equals(((IEventTask) task).getEventType().getName())) {
331                if (doTrace) {
332                    System.err.println("event names do not match: " + taskSpec.name + " != " +
333                                       ((IEventTask) task).getEventType().getName() + "\n");
334                }
335                return false;
336            }
337        }
338       
339        // check the task name against the map
340        if (tasks.containsKey(taskSpec.name)) {
341            if (!tasks.get(taskSpec.name).equals(task)) {
342                if (doTrace) {
343                    System.err.println("the task instance is not of the expected task: " +
344                                       taskSpec.name + " != " + task + "\n");
345                }
346                return false;
347            }
348        }
349        else if (tasks.containsValue(task)) {
350            if (doTrace) {
351                System.err.println("the task of the task instance " + taskSpec.name +
352                                   " should be different to another one but it isn't\n");
353            }
354            return false;
355        }
356        else {
357            tasks.put(taskSpec.name, task);
358        }
359
360        if (((taskSpec.children == null) && (taskInstance.getChildren().size() > 0)) ||
361            ((taskSpec.children != null) &&
362             (taskSpec.children.length != taskInstance.getChildren().size())))
363        {
364            if (doTrace) {
365                System.err.println
366                    ("numbers of children do not match: " +
367                     (taskSpec.children == null ? "0" : taskSpec.children.length) + " != " +
368                     (taskInstance.getChildren() == null ? "0" :
369                          taskInstance.getChildren().size()) + "\n");
370            }
371            return false;
372        }
373
374        Iterator<ITaskInstance> children = taskInstance.getChildren().iterator();
375        if (taskSpec.children != null) {
376            for (TaskSpec child : taskSpec.children) {
377                if (!taskSpecEqualsTaskInstance(child, children.next(), tasks)) {
378                    if (doTrace) {
379                        System.err.println("one of the children does not match\n");
380                    }
381                    return false;
382                }
383            }
384        }
385
386        if (!children.hasNext()) {
387            if (doTrace) {
388                System.err.println("nodes match\n");
389            }
390            return true;
391        }
392        else {
393            if (doTrace) {
394                System.err.println("number of children does not match\n");
395            }
396            return false;
397        }
398    }
399   
400    /**
401     *
402     */
403    private class TaskSpec {
404        public String type;
405        public String name;
406        public String additionalInfo;
407        public TaskSpec[] children;
408    }
409
410}
Note: See TracBrowser for help on using the repository browser.