Changeset 1146 for trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeChecker.java
- Timestamp:
- 04/04/13 16:06:07 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeChecker.java
r1123 r1146 17 17 import static org.junit.Assert.*; 18 18 19 import java.io.FileNotFoundException;20 import java.io.FileOutputStream;21 import java.io.PrintWriter;22 19 import java.util.ArrayList; 23 20 import java.util.HashMap; … … 33 30 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; 34 31 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; 35 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree; 36 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; 37 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeInfo; 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; 38 36 39 37 /** … … 56 54 57 55 /** 58 * TODO: comment 59 * 56 * 60 57 */ 61 58 public TaskTreeChecker() { … … 64 61 65 62 /** 66 * TODO: comment 67 * 63 * 68 64 */ 69 65 public TaskTreeChecker(boolean doTrace) { … … 74 70 * 75 71 */ 76 public void assertTaskTree(String taskTreeSpec, ITaskTree taskTree) { 77 Map<ITaskTreeNode, Integer> taskMapCopy = new HashMap<ITaskTreeNode, Integer>(); 78 79 for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : taskTree.getTaskMap().entrySet()) { 80 if (entry.getValue().getNoOfOccurencesInTree() > 0) { 81 taskMapCopy.put(entry.getKey(), entry.getValue().getNoOfOccurencesInTree()); 82 } 83 else { 84 taskMapCopy.put(entry.getKey(), 1); 85 } 86 } 87 72 public void assertTaskInstanceList(String userSessionSpec, ITaskInstanceList taskInstances) { 88 73 if (doTrace) { 89 dumpTaskMap(taskMapCopy); 90 } 91 92 TaskSpec task = null; 93 94 Matcher taskMatcher = taskPattern.matcher(taskTreeSpec); 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>(); 95 82 96 83 while (taskMatcher.find()) { 97 84 98 task = parseTask(taskMatcher); 99 100 if (task != null) { 101 assertTaskAndChildrenInMapAndRemove(task, taskMapCopy); 102 } 103 } 104 105 assertTrue("more tasks in map, than expected", taskMapCopy.isEmpty()); 106 } 107 108 /** 109 * <p> 110 * TODO: comment 111 * </p> 112 * 113 * @param oracle 114 * @param result 115 */ 116 public void assertTaskNodesEqual(ITaskTreeNode expected, ITaskTreeNode checked) { 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 { 117 98 if (expected == null) { 118 99 assertNull("null", checked); … … 123 104 assertEquals(expected.toString() + ": types do not match", 124 105 expected.getClass(), checked.getClass()); 125 assertEquals(expected.toString() + ": names do not match", 126 expected.getName(), checked.getName()); 127 128 List<ITaskTreeNode> expectedChildren = expected.getChildren(); 129 List<ITaskTreeNode> checkedChildren = checked.getChildren(); 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(); 130 158 131 159 if ((expectedChildren != null) && (expectedChildren.size() > 0)) { … … 136 164 if (expected instanceof ISequence) { 137 165 for (int i = 0; i < expectedChildren.size(); i++) { 138 assertTask NodesEqual(expectedChildren.get(i), checkedChildren.get(i));166 assertTaskInstancesEqual(expectedChildren.get(i), checkedChildren.get(i)); 139 167 } 140 168 } … … 144 172 for (int j = 0; j < checkedChildren.size(); j++) { 145 173 try { 146 assertTask NodesEqual174 assertTaskInstancesEqual 147 175 (expectedChildren.get(i), checkedChildren.get(j)); 148 176 found = true; … … 166 194 167 195 /** 168 * TODO: comment169 196 * 170 * @param taskTree 171 */ 172 public void dumpAsCheckString(ITaskTree taskTree) { 173 dumpNodeAsCheckString(taskTree.getRoot(), new int[4], ""); 174 } 175 176 /** 177 * TODO: comment 178 * 179 * @param root 180 * @param string 181 */ 182 private void dumpNodeAsCheckString(ITaskTreeNode node, int[] typeCounters, String indent) { 183 System.out.print(" \""); 184 System.out.print(indent); 185 186 if (node instanceof ISequence) { 187 System.out.print("Sequence sequence"); 188 System.out.print(typeCounters[0]++); 189 System.out.println(" {\" +"); 190 } 191 else if (node instanceof IIteration) { 192 System.out.print("Iteration iteration"); 193 System.out.print(typeCounters[1]++); 194 System.out.println(" {\" +"); 195 } 196 else if (node instanceof ISelection) { 197 System.out.print("Selection selection"); 198 System.out.print(typeCounters[2]++); 199 System.out.println(" {\" +"); 200 } 201 else if (node instanceof IEventTask) { 202 if (((IEventTask) node).getEventType() instanceof TextInput) { 203 System.out.print("TextInputEvent textInput"); 204 System.out.print(typeCounters[3]++); 205 System.out.print(" \""); 206 System.out.print(((TextInput) ((IEventTask) node).getEventType()).getEnteredText()); 207 System.out.print("\""); 208 } 209 else { 210 System.out.print("Event "); 211 System.out.print(((IEventTask) node).getEventType().getName()); 212 } 213 System.out.print(" {}\" +"); 214 } 215 else { 216 fail("unknown type of node in task tree " + node); 217 } 218 219 for (ITaskTreeNode child : node.getChildren()) { 220 dumpNodeAsCheckString(child, typeCounters, indent + " "); 221 } 222 223 if (!(node instanceof IEventTask)) { 224 System.out.print(" \""); 225 System.out.print(indent); 226 System.out.print("}\" +"); 227 } 228 229 System.out.println(); 230 } 231 232 /** 233 * TODO: comment 234 * 235 * @param taskTree 236 */ 237 public void dumpFullTaskTree(ITaskTree taskTree) throws FileNotFoundException { 238 PrintWriter out = null; 239 try { 240 out = new PrintWriter(new FileOutputStream("taskTree.txt")); 241 dumpFullNode(taskTree.getRoot(), out, "", 0); 242 } 243 finally { 244 if (out != null) { 245 out.close(); 246 } 247 } 248 249 } 250 251 /** 252 * 253 */ 254 private void dumpFullNode(ITaskTreeNode node, PrintWriter out, String indent, int index) { 255 if (node instanceof ISequence) { 256 if (index > 0) { 257 out.println(); 258 } 259 out.print(indent); 260 out.print(node.toString()); 261 out.println(" {"); 262 } 263 else if (node instanceof IIteration) { 264 if (index > 0) { 265 out.println(); 266 } 267 out.print(indent); 268 out.print(node.toString()); 269 out.println(" {"); 270 } 271 else if (node instanceof ISelection) { 272 if (index > 0) { 273 out.println(); 274 } 275 out.print(indent); 276 out.print(node.toString()); 277 out.println(" {"); 278 } 279 else if (node instanceof IEventTask) { 280 out.print(indent); 281 out.print(((IEventTask) node).getEventType()); 282 out.print(" "); 283 out.print(((IEventTask) node).getEventTarget().getStringIdentifier()); 284 // if (((IEventTask) node).getEventTarget() instanceof IGUIElement) { 285 // out.print(" "); 286 // out.print(((IGUIElement) ((IEventTask) node).getEventTarget()).getSpecification()); 287 // } 288 } 289 else { 290 fail("unknown type of node in task tree " + node); 291 } 292 293 int i = 0; 294 for (ITaskTreeNode child : node.getChildren()) { 295 dumpFullNode(child, out, indent + " ", i++); 296 } 297 298 if (!(node instanceof IEventTask)) { 299 out.print(indent); 300 out.print("}"); 301 } 302 303 out.println(); 304 } 305 306 /** 307 * 308 */ 309 private TaskSpec parseTask(Matcher taskMatcher) { 197 */ 198 private TaskSpec parseTaskInstance(Matcher taskMatcher) { 310 199 if ("}".equals(taskMatcher.group(1))) { 311 200 throw new IllegalArgumentException("invalid task specification"); … … 338 227 List<TaskSpec> children = new ArrayList<TaskSpec>(); 339 228 while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) { 340 children.add(parseTask (taskMatcher));229 children.add(parseTaskInstance(taskMatcher)); 341 230 } 342 231 … … 352 241 * @param taskMapCopy 353 242 */ 354 private void assertTaskAndChildrenInMapAndRemove(TaskSpec task, 355 Map<ITaskTreeNode, Integer> taskMap) 243 private void assertTaskInstanceList(TaskSpec taskSpec, 244 ITaskInstanceList taskInstances, 245 Map<String, ITask> tasks) 356 246 { 357 for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) { 358 if (taskSpecEqualsTask(task, entry.getKey())) { 359 if (task.children != null) { 360 for (TaskSpec child : task.children) { 361 assertTaskAndChildrenInMapAndRemove(child, taskMap); 362 } 363 } 364 365 int count = taskMap.get(entry.getKey()); 366 if (count == 1) { 367 taskMap.remove(entry.getKey()); 368 } 369 else { 370 taskMap.put(entry.getKey(), count - 1); 371 } 372 return; 373 } 374 } 375 376 fail("expected task " + task.type + " " + task.name + " not included in task map"); 377 } 378 379 /** 380 * 381 */ 382 private boolean taskSpecEqualsTask(TaskSpec taskSpec, ITaskTreeNode task) { 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 { 383 277 if (doTrace) { 384 278 System.err.println("comparing " + taskSpec.name + " with"); 385 dumpTask(task, 0, ""); 386 } 387 279 new TaskTreeEncoder().encode(taskInstance, System.err); 280 } 281 282 ITask task = taskInstance.getTask(); 283 388 284 if (("Event".equals(taskSpec.type) && (!(task instanceof IEventTask))) || 389 285 ("TextInputEvent".equals(taskSpec.type) && … … 396 292 if (doTrace) { 397 293 System.err.println("task types do not match: " + taskSpec.type + " != " + 398 task.getClass().getSimpleName() + "\n");294 task.getClass().getSimpleName() + "\n"); 399 295 } 400 296 return false; … … 440 336 } 441 337 } 442 443 if (((taskSpec.children == null) && (task.getChildren().size() > 0)) || 444 ((taskSpec.children != null) && (taskSpec.children.length != task.getChildren().size()))) 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()))) 445 363 { 446 364 if (doTrace) { … … 448 366 ("numbers of children do not match: " + 449 367 (taskSpec.children == null ? "0" : taskSpec.children.length) + " != " + 450 (task.getChildren() == null ? "0" : task.getChildren().size()) + "\n"); 368 (taskInstance.getChildren() == null ? "0" : 369 taskInstance.getChildren().size()) + "\n"); 451 370 } 452 371 return false; 453 372 } 454 373 455 Iterator<ITask TreeNode> children = task.getChildren().iterator();374 Iterator<ITaskInstance> children = taskInstance.getChildren().iterator(); 456 375 if (taskSpec.children != null) { 457 376 for (TaskSpec child : taskSpec.children) { 458 if (!taskSpecEqualsTask (child, children.next())) {377 if (!taskSpecEqualsTaskInstance(child, children.next(), tasks)) { 459 378 if (doTrace) { 460 379 System.err.println("one of the children does not match\n"); … … 478 397 } 479 398 } 480 481 /**482 *483 */484 private void dumpTaskMap(Map<ITaskTreeNode, Integer> taskMap) {485 System.err.println();486 for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) {487 dumpTask(entry.getKey(), entry.getValue(), "");488 System.err.println();489 }490 }491 492 /**493 *494 */495 private void dumpTask(ITaskTreeNode task, int count, String indent) {496 System.err.print(indent);497 System.err.print(task);498 System.err.print(" ");499 System.err.print(task.getDescription());500 System.err.print(" ");501 502 if (count > 0) {503 System.err.print("(");504 System.err.print(count);505 System.err.print(" occurrences)");506 }507 508 System.err.println();509 510 if ((task.getChildren() != null) && (task.getChildren().size() > 0)) {511 for (ITaskTreeNode child : task.getChildren()) {512 dumpTask(child, 0, indent + " ");513 }514 }515 }516 399 517 400 /** 518 * TODO comment 519 * 520 * @version $Revision: $ $Date: $ 521 * @author 2011, last modified by $Author: $ 401 * 522 402 */ 523 403 private class TaskSpec {
Note: See TracChangeset
for help on using the changeset viewer.