[927] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees; |
---|
[790] | 16 | |
---|
[1123] | 17 | import static org.junit.Assert.*; |
---|
[790] | 18 | |
---|
| 19 | import java.util.ArrayList; |
---|
| 20 | import java.util.HashMap; |
---|
| 21 | import java.util.Iterator; |
---|
| 22 | import java.util.List; |
---|
| 23 | import java.util.Map; |
---|
| 24 | import java.util.regex.Matcher; |
---|
| 25 | import java.util.regex.Pattern; |
---|
| 26 | |
---|
[922] | 27 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
[1146] | 32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[790] | 36 | |
---|
| 37 | /** |
---|
| 38 | * TODO comment |
---|
| 39 | * |
---|
| 40 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
| 41 | * @author 2012, last modified by $Author: patrick$ |
---|
| 42 | */ |
---|
| 43 | public class TaskTreeChecker { |
---|
| 44 | |
---|
| 45 | /** */ |
---|
| 46 | private static Pattern taskPattern = Pattern.compile("([^{}]+)\\{|\\}"); |
---|
| 47 | |
---|
| 48 | /** */ |
---|
| 49 | private static Pattern taskDetailsPattern = |
---|
[977] | 50 | Pattern.compile("\\s*(\\w*)\\s*([\\w\\(\\)\"]*)\\s*((\\w*)|(\".*\"))?"); |
---|
[790] | 51 | |
---|
| 52 | /** */ |
---|
| 53 | private boolean doTrace; |
---|
| 54 | |
---|
| 55 | /** |
---|
[1146] | 56 | * |
---|
[790] | 57 | */ |
---|
| 58 | public TaskTreeChecker() { |
---|
| 59 | this(false); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
[1146] | 63 | * |
---|
[790] | 64 | */ |
---|
| 65 | public TaskTreeChecker(boolean doTrace) { |
---|
| 66 | this.doTrace = doTrace; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * |
---|
| 71 | */ |
---|
[1146] | 72 | public void assertTaskInstanceList(String userSessionSpec, ITaskInstanceList taskInstances) { |
---|
[790] | 73 | if (doTrace) { |
---|
[1146] | 74 | new TaskTreeEncoder().encode(taskInstances, System.err); |
---|
[790] | 75 | } |
---|
| 76 | |
---|
[1146] | 77 | TaskSpec taskInstanceSpec = null; |
---|
[790] | 78 | |
---|
[1146] | 79 | Matcher taskMatcher = taskPattern.matcher(userSessionSpec); |
---|
| 80 | |
---|
| 81 | Map<String, ITask> tasks = new HashMap<String, ITask>(); |
---|
[790] | 82 | |
---|
| 83 | while (taskMatcher.find()) { |
---|
| 84 | |
---|
[1146] | 85 | taskInstanceSpec = parseTaskInstance(taskMatcher); |
---|
[790] | 86 | |
---|
[1146] | 87 | if (taskInstanceSpec != null) { |
---|
| 88 | assertTaskInstanceList(taskInstanceSpec, taskInstances, tasks); |
---|
[790] | 89 | } |
---|
| 90 | } |
---|
[1146] | 91 | } |
---|
[790] | 92 | |
---|
[1146] | 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 | } |
---|
[790] | 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
[1123] | 125 | * |
---|
| 126 | */ |
---|
[1146] | 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 | { |
---|
[1123] | 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 | |
---|
[1146] | 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 | } |
---|
[1123] | 155 | |
---|
[1146] | 156 | List<ITaskInstance> expectedChildren = expected.getChildren(); |
---|
| 157 | List<ITaskInstance> checkedChildren = checked.getChildren(); |
---|
| 158 | |
---|
[1123] | 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++) { |
---|
[1146] | 166 | assertTaskInstancesEqual(expectedChildren.get(i), checkedChildren.get(i)); |
---|
[1123] | 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 { |
---|
[1146] | 174 | assertTaskInstancesEqual |
---|
[1123] | 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 | /** |
---|
[790] | 196 | * |
---|
| 197 | */ |
---|
[1146] | 198 | private TaskSpec parseTaskInstance(Matcher taskMatcher) { |
---|
[790] | 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))) { |
---|
[1146] | 229 | children.add(parseTaskInstance(taskMatcher)); |
---|
[790] | 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 | */ |
---|
[1146] | 243 | private void assertTaskInstanceList(TaskSpec taskSpec, |
---|
| 244 | ITaskInstanceList taskInstances, |
---|
| 245 | Map<String, ITask> tasks) |
---|
[790] | 246 | { |
---|
[1146] | 247 | if (doTrace) { |
---|
| 248 | System.err.println("\ncomparing " + taskSpec.type + " with " + taskInstances + "\n"); |
---|
| 249 | } |
---|
[790] | 250 | |
---|
[1146] | 251 | if ((taskInstances instanceof IUserSession) && (!"UserSession".equals(taskSpec.type))) { |
---|
| 252 | fail("can not compare a task instance with a user session"); |
---|
[790] | 253 | } |
---|
[1146] | 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 | } |
---|
[790] | 268 | } |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | * |
---|
| 272 | */ |
---|
[1146] | 273 | private boolean taskSpecEqualsTaskInstance(TaskSpec taskSpec, |
---|
| 274 | ITaskInstance taskInstance, |
---|
| 275 | Map<String, ITask> tasks) |
---|
| 276 | { |
---|
[790] | 277 | if (doTrace) { |
---|
| 278 | System.err.println("comparing " + taskSpec.name + " with"); |
---|
[1146] | 279 | new TaskTreeEncoder().encode(taskInstance, System.err); |
---|
[790] | 280 | } |
---|
| 281 | |
---|
[1146] | 282 | ITask task = taskInstance.getTask(); |
---|
| 283 | |
---|
[790] | 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 + " != " + |
---|
[1146] | 294 | task.getClass().getSimpleName() + "\n"); |
---|
[790] | 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 | } |
---|
[1146] | 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 | } |
---|
[790] | 359 | |
---|
[1146] | 360 | if (((taskSpec.children == null) && (taskInstance.getChildren().size() > 0)) || |
---|
| 361 | ((taskSpec.children != null) && |
---|
| 362 | (taskSpec.children.length != taskInstance.getChildren().size()))) |
---|
[790] | 363 | { |
---|
| 364 | if (doTrace) { |
---|
| 365 | System.err.println |
---|
| 366 | ("numbers of children do not match: " + |
---|
| 367 | (taskSpec.children == null ? "0" : taskSpec.children.length) + " != " + |
---|
[1146] | 368 | (taskInstance.getChildren() == null ? "0" : |
---|
| 369 | taskInstance.getChildren().size()) + "\n"); |
---|
[790] | 370 | } |
---|
| 371 | return false; |
---|
| 372 | } |
---|
| 373 | |
---|
[1146] | 374 | Iterator<ITaskInstance> children = taskInstance.getChildren().iterator(); |
---|
[790] | 375 | if (taskSpec.children != null) { |
---|
| 376 | for (TaskSpec child : taskSpec.children) { |
---|
[1146] | 377 | if (!taskSpecEqualsTaskInstance(child, children.next(), tasks)) { |
---|
[790] | 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 | } |
---|
[1146] | 399 | |
---|
[790] | 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 | } |
---|