source: trunk/autoquest-core-tasktrees-test/src/test/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskModelTest.java @ 1402

Last change on this file since 1402 was 1402, checked in by pharms, 10 years ago
  • removed some useless TODOs
File size: 44.3 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.treeimpl;
16
17import static org.junit.Assert.*;
18
19import java.util.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23
24import org.junit.Test;
25
26import de.ugoe.cs.autoquest.eventcore.Event;
27import de.ugoe.cs.autoquest.eventcore.IEventTarget;
28import de.ugoe.cs.autoquest.eventcore.IEventType;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
35import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
36import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
37import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
38import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
39import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
40import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
41import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
42import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
43import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
44import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo;
45import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
46import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskInfo;
47import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskBuilder;
48import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskFactory;
49import de.ugoe.cs.autoquest.test.DummyGUIElement;
50import de.ugoe.cs.autoquest.test.DummyInteraction;
51
52/**
53 *
54 */
55public class TaskModelTest {
56   
57    /** */
58    private static final int MAX_TREE_DEPTH = 15;
59
60    /** */
61    private ITaskBuilder taskBuilder = new TaskBuilder();
62
63    /** */
64    private ITaskFactory taskFactory = new TaskFactory();
65
66    /**
67     *
68     */
69    @Test
70    public void test_EventTask_01() throws Exception {
71        IEventType eventType = new DummyInteraction("interaction", 1);
72        IEventTarget eventTarget = new DummyGUIElement("elem");
73       
74        IEventTask task = createNewEventTask(eventType, eventTarget);
75       
76        assertNotNull(task);
77        assertNotNull(task.getDescription());
78        assertNotNull(task.getId());
79        assertTrue(task.equals(task));
80       
81        IEventTaskInstance instance = (IEventTaskInstance) task.getInstances().iterator().next();
82        assertEquals(eventType, instance.getEvent().getType());
83        assertEquals(eventTarget, instance.getEvent().getTarget());
84    }
85
86    /**
87     *
88     */
89    @Test
90    public void test_EventTask_02() throws Exception {
91        IEventType eventType = new DummyInteraction("interaction", 1);
92        IEventTarget eventTarget = new DummyGUIElement("elem");
93       
94        IEventTask task1 = createNewEventTask(eventType, eventTarget);
95        IEventTask task2 = createNewEventTask(eventType, eventTarget);
96       
97        // the tasks will not be equal as they should have a different id
98        assertFalse(task1.equals(task2));
99    }
100
101    /**
102     *
103     */
104    @Test
105    public void test_EventTask_03() throws Exception {
106        IEventType eventType = new DummyInteraction("interaction", 1);
107        IEventTarget eventTarget = new DummyGUIElement("elem");
108       
109        IEventTask task = createNewEventTask(eventType, eventTarget);
110        ITaskInstance taskInstance1 = createNewTaskInstance(task);
111        ITaskInstance taskInstance2 = createNewTaskInstance(task);
112       
113        assertFalse(taskInstance1.equals(taskInstance2));
114    }
115
116    /**
117     *
118     */
119    @Test
120    public void test_Sequence_01() throws Exception {
121        ISequence task = taskFactory.createNewSequence();
122       
123        assertNotNull(task);
124        assertNotNull(task.getDescription());
125        assertNotNull(task.getId());
126        assertNotNull(task.getChildren());
127        assertEquals(0, task.getChildren().size());
128        assertTrue(task.equals(task));
129    }
130
131    /**
132     *
133     */
134    @Test
135    public void test_Sequence_02() throws Exception {
136        IEventType eventType = new DummyInteraction("interaction", 1);
137        IEventTarget eventTarget = new DummyGUIElement("elem");
138       
139        IEventTask child = createNewEventTask(eventType, eventTarget);
140
141        ISequence task = taskFactory.createNewSequence();
142       
143        taskBuilder.addChild(task, child);
144       
145        assertNotNull(task.getChildren());
146        assertEquals(1, task.getChildren().size());
147        assertEquals(child, task.getChildren().get(0));
148    }
149
150    /**
151     *
152     */
153    @Test
154    public void test_Sequence_03() throws Exception {
155        IEventType eventType = new DummyInteraction("interaction", 1);
156        IEventTarget eventTarget = new DummyGUIElement("elem");
157       
158        IEventTask child1 = createNewEventTask(eventType, eventTarget);
159        IEventTask child2 = createNewEventTask(eventType, eventTarget);
160        IEventTask child3 = createNewEventTask(eventType, eventTarget);
161        IEventTask child4 = createNewEventTask(eventType, eventTarget);
162        IEventTask child5 = createNewEventTask(eventType, eventTarget);
163
164        ISequence task = taskFactory.createNewSequence();
165       
166        taskBuilder.addChild(task, child1);
167        taskBuilder.addChild(task, child2);
168        taskBuilder.addChild(task, child3);
169        taskBuilder.addChild(task, child4);
170        taskBuilder.addChild(task, child5);
171       
172        assertNotNull(task.getChildren());
173        assertEquals(5, task.getChildren().size());
174        assertEquals(child1, task.getChildren().get(0));
175        assertEquals(child2, task.getChildren().get(1));
176        assertEquals(child3, task.getChildren().get(2));
177        assertEquals(child4, task.getChildren().get(3));
178        assertEquals(child5, task.getChildren().get(4));
179    }
180
181    /**
182     *
183     */
184    @Test
185    public void test_Selection_01() throws Exception {
186        ISelection task = taskFactory.createNewSelection();
187       
188        assertNotNull(task);
189        assertNotNull(task.getDescription());
190        assertNotNull(task.getId());
191        assertNotNull(task.getChildren());
192        assertEquals(0, task.getChildren().size());
193        assertTrue(task.equals(task));
194    }
195
196    /**
197     *
198     */
199    @Test
200    public void test_Selection_02() throws Exception {
201        IEventType eventType = new DummyInteraction("interaction", 1);
202        IEventTarget eventTarget = new DummyGUIElement("elem");
203       
204        IEventTask child = createNewEventTask(eventType, eventTarget);
205
206        ISelection task = taskFactory.createNewSelection();
207       
208        taskBuilder.addChild(task, child);
209       
210        assertNotNull(task.getChildren());
211        assertEquals(1, task.getChildren().size());
212        assertEquals(child, task.getChildren().get(0));
213    }
214
215    /**
216     *
217     */
218    @Test
219    public void test_Selection_03() throws Exception {
220        IEventType eventType = new DummyInteraction("interaction", 1);
221        IEventTarget eventTarget = new DummyGUIElement("elem");
222       
223        IEventTask child1 = createNewEventTask(eventType, eventTarget);
224        IEventTask child2 = createNewEventTask(eventType, eventTarget);
225        IEventTask child3 = createNewEventTask(eventType, eventTarget);
226        IEventTask child4 = createNewEventTask(eventType, eventTarget);
227        IEventTask child5 = createNewEventTask(eventType, eventTarget);
228
229        ISelection task = taskFactory.createNewSelection();
230       
231        taskBuilder.addChild(task, child1);
232        taskBuilder.addChild(task, child2);
233        taskBuilder.addChild(task, child3);
234        taskBuilder.addChild(task, child4);
235        taskBuilder.addChild(task, child5);
236       
237        assertNotNull(task.getChildren());
238        assertEquals(5, task.getChildren().size());
239        assertEquals(child1, task.getChildren().get(0));
240        assertEquals(child2, task.getChildren().get(1));
241        assertEquals(child3, task.getChildren().get(2));
242        assertEquals(child4, task.getChildren().get(3));
243        assertEquals(child5, task.getChildren().get(4));
244    }
245
246    /**
247     *
248     */
249    @Test
250    public void test_Iteration_01() throws Exception {
251        IIteration task = taskFactory.createNewIteration();
252       
253        assertNotNull(task);
254        assertNotNull(task.getDescription());
255        assertNotNull(task.getId());
256        assertNull(task.getMarkedTask());
257        assertTrue(task.equals(task));
258    }
259
260    /**
261     *
262     */
263    @Test
264    public void test_Iteration_02() throws Exception {
265        IEventType eventType = new DummyInteraction("interaction", 1);
266        IEventTarget eventTarget = new DummyGUIElement("elem");
267       
268        IEventTask child = createNewEventTask(eventType, eventTarget);
269
270        IIteration task = taskFactory.createNewIteration();
271       
272        taskBuilder.setMarkedTask(task, child);
273       
274        assertEquals(child, task.getMarkedTask());
275    }
276
277    /**
278     *
279     */
280    @Test
281    public void test_Iteration_03() throws Exception {
282        IEventType eventType = new DummyInteraction("interaction", 1);
283        IEventTarget eventTarget = new DummyGUIElement("elem");
284       
285        IEventTask child1 = createNewEventTask(eventType, eventTarget);
286        IEventTask child2 = createNewEventTask(eventType, eventTarget);
287
288        IIteration task = taskFactory.createNewIteration();
289       
290        taskBuilder.setMarkedTask(task, child1);
291        taskBuilder.setMarkedTask(task, child2);
292       
293        assertEquals(child2, task.getMarkedTask());
294    }
295
296    /**
297     *
298     */
299    @Test
300    public void test_Optional_01() throws Exception {
301        IOptional task = taskFactory.createNewOptional();
302       
303        assertNotNull(task);
304        assertNotNull(task.getDescription());
305        assertNotNull(task.getId());
306        assertNull(task.getMarkedTask());
307        assertTrue(task.equals(task));
308    }
309
310    /**
311     *
312     */
313    @Test
314    public void test_Optional_02() throws Exception {
315        IEventType eventType = new DummyInteraction("interaction", 1);
316        IEventTarget eventTarget = new DummyGUIElement("elem");
317       
318        IEventTask child = createNewEventTask(eventType, eventTarget);
319
320        IOptional task = taskFactory.createNewOptional();
321       
322        taskBuilder.setMarkedTask(task, child);
323       
324        assertEquals(child, task.getMarkedTask());
325    }
326
327    /**
328     *
329     */
330    @Test
331    public void test_Optional_03() throws Exception {
332        IEventType eventType = new DummyInteraction("interaction", 1);
333        IEventTarget eventTarget = new DummyGUIElement("elem");
334       
335        IEventTask child1 = createNewEventTask(eventType, eventTarget);
336        IEventTask child2 = createNewEventTask(eventType, eventTarget);
337
338        IOptional task = taskFactory.createNewOptional();
339       
340        taskBuilder.setMarkedTask(task, child1);
341        taskBuilder.setMarkedTask(task, child2);
342       
343        assertEquals(child2, task.getMarkedTask());
344    }
345
346    /**
347     *
348     */
349    @Test
350    public void test_EventTaskInstance_01() throws Exception {
351        IEventType eventType = new DummyInteraction("interaction", 1);
352        IEventTarget eventTarget = new DummyGUIElement("elem");
353       
354        IEventTask task = createNewEventTask(eventType, eventTarget);
355       
356        IEventTaskInstance taskInstance = createNewTaskInstance(task);
357       
358        assertNotNull(taskInstance);
359        assertEquals(task, taskInstance.getTask());
360        assertTrue(taskInstance.equals(taskInstance));
361        assertFalse(taskInstance.equals(task));
362    }
363
364    /**
365     *
366     */
367    @Test
368    public void test_EventTaskInstance_02() throws Exception {
369        IEventType eventType = new DummyInteraction("interaction", 1);
370        IEventTarget eventTarget = new DummyGUIElement("elem");
371       
372        IEventTask task = createNewEventTask(eventType, eventTarget);
373       
374        ITaskInstance taskInstance1 = createNewTaskInstance(task);
375        ITaskInstance taskInstance2 = createNewTaskInstance(task);
376       
377        assertFalse(taskInstance1.equals(taskInstance2));
378    }
379
380    /**
381     *
382     */
383    @Test(expected=IllegalArgumentException.class)
384    public void test_SequenceInstance_01() throws Exception {
385        IEventType eventType = new DummyInteraction("interaction", 1);
386        IEventTarget eventTarget = new DummyGUIElement("elem");
387       
388        IEventTask task = createNewEventTask(eventType, eventTarget);
389       
390        ISequence sequence = taskFactory.createNewSequence();
391       
392        ITaskInstance taskInstance = createNewTaskInstance(task);
393        ISequenceInstance sequenceInstance = createNewTaskInstance(sequence);
394       
395        taskBuilder.addChild(sequenceInstance, taskInstance);
396    }
397
398    /**
399     *
400     */
401    @Test
402    public void test_SequenceInstance_02() throws Exception {
403        IEventType eventType = new DummyInteraction("interaction", 1);
404        IEventTarget eventTarget = new DummyGUIElement("elem");
405       
406        IEventTask task = createNewEventTask(eventType, eventTarget);
407       
408        ISequence sequence = taskFactory.createNewSequence();
409        taskBuilder.addChild(sequence, task);
410       
411        ITaskInstance taskInstance = createNewTaskInstance(task);
412        ISequenceInstance sequenceInstance = createNewTaskInstance(sequence);
413       
414        taskBuilder.addChild(sequenceInstance, taskInstance);
415       
416        assertEquals(1, sequenceInstance.size());
417        assertEquals(taskInstance, sequenceInstance.get(0));
418    }
419
420    /**
421     *
422     */
423    @Test
424    public void test_SequenceInstance_03() throws Exception {
425        IEventType eventType = new DummyInteraction("interaction", 1);
426        IEventTarget eventTarget = new DummyGUIElement("elem");
427       
428        IEventTask task1 = createNewEventTask(eventType, eventTarget);
429        IEventTask task2 = createNewEventTask(eventType, eventTarget);
430        IEventTask task3 = createNewEventTask(eventType, eventTarget);
431        IEventTask task4 = createNewEventTask(eventType, eventTarget);
432        IEventTask task5 = createNewEventTask(eventType, eventTarget);
433       
434        ISequence sequence = taskFactory.createNewSequence();
435        taskBuilder.addChild(sequence, task1);
436        taskBuilder.addChild(sequence, task2);
437        taskBuilder.addChild(sequence, task3);
438        taskBuilder.addChild(sequence, task4);
439        taskBuilder.addChild(sequence, task5);
440       
441        ITaskInstance taskInstance1 = createNewTaskInstance(task1);
442        ITaskInstance taskInstance2 = createNewTaskInstance(task2);
443        ITaskInstance taskInstance3 = createNewTaskInstance(task3);
444        ITaskInstance taskInstance4 = createNewTaskInstance(task4);
445        ITaskInstance taskInstance5 = createNewTaskInstance(task5);
446        ISequenceInstance sequenceInstance = createNewTaskInstance(sequence);
447       
448        taskBuilder.addChild(sequenceInstance, taskInstance1);
449        taskBuilder.addChild(sequenceInstance, taskInstance2);
450        taskBuilder.addChild(sequenceInstance, taskInstance3);
451        taskBuilder.addChild(sequenceInstance, taskInstance4);
452        taskBuilder.addChild(sequenceInstance, taskInstance5);
453       
454        assertEquals(5, sequenceInstance.size());
455        assertEquals(taskInstance1, sequenceInstance.get(0));
456        assertEquals(taskInstance2, sequenceInstance.get(1));
457        assertEquals(taskInstance3, sequenceInstance.get(2));
458        assertEquals(taskInstance4, sequenceInstance.get(3));
459        assertEquals(taskInstance5, sequenceInstance.get(4));
460    }
461
462    /**
463     *
464     */
465    @Test(expected=IllegalArgumentException.class)
466    public void test_SelectionInstance_01() throws Exception {
467        IEventType eventType = new DummyInteraction("interaction", 1);
468        IEventTarget eventTarget = new DummyGUIElement("elem");
469       
470        IEventTask task = createNewEventTask(eventType, eventTarget);
471       
472        ISelection selection = taskFactory.createNewSelection();
473       
474        ITaskInstance taskInstance = createNewTaskInstance(task);
475        ISelectionInstance selectionInstance = createNewTaskInstance(selection);
476       
477        taskBuilder.setChild(selectionInstance, taskInstance);
478    }
479
480    /**
481     *
482     */
483    @Test
484    public void test_SelectionInstance_02() throws Exception {
485        IEventType eventType = new DummyInteraction("interaction", 1);
486        IEventTarget eventTarget = new DummyGUIElement("elem");
487       
488        IEventTask task = createNewEventTask(eventType, eventTarget);
489       
490        ISelection selection = taskFactory.createNewSelection();
491        taskBuilder.addChild(selection, task);
492       
493        ITaskInstance taskInstance = createNewTaskInstance(task);
494        ISelectionInstance selectionInstance = createNewTaskInstance(selection);
495       
496        taskBuilder.setChild(selectionInstance, taskInstance);
497       
498        assertNotNull(selectionInstance.getChild());
499        assertEquals(taskInstance, selectionInstance.getChild());
500    }
501
502    /**
503     *
504     */
505    @Test
506    public void test_SelectionInstance_03() throws Exception {
507        IEventType eventType = new DummyInteraction("interaction", 1);
508        IEventTarget eventTarget = new DummyGUIElement("elem");
509       
510        IEventTask task1 = createNewEventTask(eventType, eventTarget);
511        IEventTask task2 = createNewEventTask(eventType, eventTarget);
512        IEventTask task3 = createNewEventTask(eventType, eventTarget);
513        IEventTask task4 = createNewEventTask(eventType, eventTarget);
514        IEventTask task5 = createNewEventTask(eventType, eventTarget);
515       
516        ISelection selection = taskFactory.createNewSelection();
517        taskBuilder.addChild(selection, task1);
518        taskBuilder.addChild(selection, task2);
519        taskBuilder.addChild(selection, task3);
520        taskBuilder.addChild(selection, task4);
521        taskBuilder.addChild(selection, task5);
522       
523        ITaskInstance taskInstance1 = createNewTaskInstance(task1);
524        ITaskInstance taskInstance2 = createNewTaskInstance(task2);
525        ISelectionInstance selectionInstance = createNewTaskInstance(selection);
526       
527        taskBuilder.setChild(selectionInstance, taskInstance1);
528        taskBuilder.setChild(selectionInstance, taskInstance2);
529       
530        assertEquals(taskInstance2, selectionInstance.getChild());
531    }
532
533    /**
534     *
535     */
536    @Test(expected=IllegalArgumentException.class)
537    public void test_IterationInstance_01() throws Exception {
538        IEventType eventType = new DummyInteraction("interaction", 1);
539        IEventTarget eventTarget = new DummyGUIElement("elem");
540       
541        IEventTask task = createNewEventTask(eventType, eventTarget);
542       
543        IIteration iteration = taskFactory.createNewIteration();
544       
545        ITaskInstance taskInstance = createNewTaskInstance(task);
546        IIterationInstance iterationInstance = createNewTaskInstance(iteration);
547       
548        taskBuilder.addChild(iterationInstance, taskInstance);
549    }
550
551    /**
552     *
553     */
554    @Test
555    public void test_IterationInstance_02() throws Exception {
556        IEventType eventType = new DummyInteraction("interaction", 1);
557        IEventTarget eventTarget = new DummyGUIElement("elem");
558       
559        IEventTask task = createNewEventTask(eventType, eventTarget);
560       
561        IIteration iteration = taskFactory.createNewIteration();
562        taskBuilder.setMarkedTask(iteration, task);
563       
564        ITaskInstance taskInstance = createNewTaskInstance(task);
565        IIterationInstance iterationInstance = createNewTaskInstance(iteration);
566       
567        taskBuilder.addChild(iterationInstance, taskInstance);
568       
569        assertEquals(1, iterationInstance.size());
570        assertEquals(taskInstance, iterationInstance.get(0));
571    }
572
573    /**
574     *
575     */
576    @Test(expected=IllegalArgumentException.class)
577    public void test_IterationInstance_03() throws Exception {
578        IEventType eventType = new DummyInteraction("interaction", 1);
579        IEventTarget eventTarget = new DummyGUIElement("elem");
580       
581        IEventTask task1 = createNewEventTask(eventType, eventTarget);
582        IEventTask task2 = createNewEventTask(eventType, eventTarget);
583       
584        IIteration iteration = taskFactory.createNewIteration();
585        taskBuilder.setMarkedTask(iteration, task1);
586        taskBuilder.setMarkedTask(iteration, task2);
587       
588        ITaskInstance taskInstance1 = createNewTaskInstance(task1);
589        IIterationInstance iterationInstance = createNewTaskInstance(iteration);
590       
591        taskBuilder.addChild(iterationInstance, taskInstance1);
592    }
593
594    /**
595     *
596     */
597    @Test
598    public void test_IterationInstance_04() throws Exception {
599        IEventType eventType = new DummyInteraction("interaction", 1);
600        IEventTarget eventTarget = new DummyGUIElement("elem");
601       
602        IEventTask task1 = createNewEventTask(eventType, eventTarget);
603        IEventTask task2 = createNewEventTask(eventType, eventTarget);
604       
605        IIteration iteration = taskFactory.createNewIteration();
606        taskBuilder.setMarkedTask(iteration, task1);
607        taskBuilder.setMarkedTask(iteration, task2);
608       
609        ITaskInstance taskInstance2 = createNewTaskInstance(task2);
610        IIterationInstance iterationInstance = createNewTaskInstance(iteration);
611       
612        taskBuilder.addChild(iterationInstance, taskInstance2);
613       
614        assertEquals(1, iterationInstance.size());
615        assertEquals(taskInstance2, iterationInstance.get(0));
616    }
617
618    /**
619     *
620     */
621    @Test(expected=IllegalArgumentException.class)
622    public void test_OptionalInstance_01() throws Exception {
623        IEventType eventType = new DummyInteraction("interaction", 1);
624        IEventTarget eventTarget = new DummyGUIElement("elem");
625       
626        IEventTask task = createNewEventTask(eventType, eventTarget);
627       
628        IOptional optional = taskFactory.createNewOptional();
629       
630        ITaskInstance taskInstance = createNewTaskInstance(task);
631        IOptionalInstance optionalInstance = createNewTaskInstance(optional);
632       
633        taskBuilder.setChild(optionalInstance, taskInstance);
634    }
635
636    /**
637     *
638     */
639    @Test
640    public void test_OptionalInstance_02() throws Exception {
641        IEventType eventType = new DummyInteraction("interaction", 1);
642        IEventTarget eventTarget = new DummyGUIElement("elem");
643       
644        IEventTask task = createNewEventTask(eventType, eventTarget);
645       
646        IOptional optional = taskFactory.createNewOptional();
647        taskBuilder.setMarkedTask(optional, task);
648       
649        ITaskInstance taskInstance = createNewTaskInstance(task);
650        IOptionalInstance optionalInstance = createNewTaskInstance(optional);
651       
652        taskBuilder.setChild(optionalInstance, taskInstance);
653       
654        assertNotNull(optionalInstance.getChild());
655        assertEquals(taskInstance, optionalInstance.getChild());
656    }
657
658    /**
659     *
660     */
661    @Test(expected=IllegalArgumentException.class)
662    public void test_OptionalInstance_03() throws Exception {
663        IEventType eventType = new DummyInteraction("interaction", 1);
664        IEventTarget eventTarget = new DummyGUIElement("elem");
665       
666        IEventTask task1 = createNewEventTask(eventType, eventTarget);
667        IEventTask task2 = createNewEventTask(eventType, eventTarget);
668       
669        IOptional optional = taskFactory.createNewOptional();
670        taskBuilder.setMarkedTask(optional, task1);
671        taskBuilder.setMarkedTask(optional, task2);
672       
673        ITaskInstance taskInstance1 = createNewTaskInstance(task1);
674        IOptionalInstance optionalInstance = createNewTaskInstance(optional);
675       
676        taskBuilder.setChild(optionalInstance, taskInstance1);
677    }
678
679    /**
680     *
681     */
682    @Test
683    public void test_OptionalInstance_04() throws Exception {
684        IEventType eventType = new DummyInteraction("interaction", 1);
685        IEventTarget eventTarget = new DummyGUIElement("elem");
686       
687        IEventTask task1 = createNewEventTask(eventType, eventTarget);
688        IEventTask task2 = createNewEventTask(eventType, eventTarget);
689       
690        IOptional optional = taskFactory.createNewOptional();
691        taskBuilder.setMarkedTask(optional, task1);
692        taskBuilder.setMarkedTask(optional, task2);
693       
694        ITaskInstance taskInstance2 = createNewTaskInstance(task2);
695        IOptionalInstance optionalInstance = createNewTaskInstance(optional);
696       
697        taskBuilder.setChild(optionalInstance, taskInstance2);
698       
699        assertNotNull(optionalInstance.getChild());
700        assertEquals(taskInstance2, optionalInstance.getChild());
701    }
702
703    /**
704     *
705     */
706    @Test
707    public void test_UserSession_01() throws Exception {
708        IUserSession userSession = taskFactory.createUserSession();
709       
710        assertNotNull(userSession);
711        assertEquals(0, userSession.size());
712    }
713
714    /**
715     *
716     */
717    @Test
718    public void test_UserSession_02() throws Exception {
719        IEventType eventType = new DummyInteraction("interaction", 1);
720        IEventTarget eventTarget = new DummyGUIElement("elem");
721       
722        IEventTask task = createNewEventTask(eventType, eventTarget);
723       
724        IUserSession userSession = taskFactory.createUserSession();
725       
726        ITaskInstance taskInstance = createNewTaskInstance(task);
727       
728        taskBuilder.addExecutedTask(userSession, taskInstance);
729       
730        assertEquals(1, userSession.size());
731        assertEquals(taskInstance, userSession.get(0));
732    }
733
734    /**
735     *
736     */
737    @Test
738    public void test_UserSession_03() throws Exception {
739        IEventType eventType = new DummyInteraction("interaction", 1);
740        IEventTarget eventTarget = new DummyGUIElement("elem");
741       
742        IEventTask task = createNewEventTask(eventType, eventTarget);
743        ISequence sequence = taskFactory.createNewSequence();
744        ISelection selection = taskFactory.createNewSelection();
745        IIteration iteration = taskFactory.createNewIteration();
746        IOptional optional = taskFactory.createNewOptional();
747       
748        taskBuilder.addChild(sequence, task);
749        taskBuilder.addChild(selection, task);
750        taskBuilder.setMarkedTask(iteration, task);
751        taskBuilder.setMarkedTask(optional, task);
752       
753        ITaskInstance taskInstance = createNewTaskInstance(task);
754        ISequenceInstance sequenceInstance = createNewTaskInstance(sequence);
755        ISelectionInstance selectionInstance = createNewTaskInstance(selection);
756        IIterationInstance iterationInstance = createNewTaskInstance(iteration);
757        IOptionalInstance optionalInstance = createNewTaskInstance(optional);
758       
759        taskBuilder.addChild(sequenceInstance, createNewTaskInstance(task));
760        taskBuilder.setChild(selectionInstance, createNewTaskInstance(task));
761        taskBuilder.addChild(iterationInstance, createNewTaskInstance(task));
762        taskBuilder.setChild(optionalInstance, createNewTaskInstance(task));
763       
764        IUserSession userSession = taskFactory.createUserSession();
765       
766        taskBuilder.addExecutedTask(userSession, taskInstance);
767        taskBuilder.addExecutedTask(userSession, sequenceInstance);
768        taskBuilder.addExecutedTask(userSession, selectionInstance);
769        taskBuilder.addExecutedTask(userSession, iterationInstance);
770        taskBuilder.addExecutedTask(userSession, optionalInstance);
771       
772        assertEquals(5, userSession.size());
773        assertEquals(taskInstance, userSession.get(0));
774        assertEquals(sequenceInstance, userSession.get(1));
775        assertEquals(selectionInstance, userSession.get(2));
776        assertEquals(iterationInstance, userSession.get(3));
777        assertEquals(optionalInstance, userSession.get(4));
778    }
779   
780    /**
781     *
782     */
783    @Test
784    public void testRandomTrees() throws Exception {
785        int noOfTrees = 10;
786        int noOfMaxChildren = 8;
787        int maxDepth = MAX_TREE_DEPTH;
788
789        for (int i = 0; i < noOfTrees; i++) {
790            System.err.println("\niteration " + (i + 1) + ":");
791            System.err.println("  creating tasks");
792            Map<ITask, ITaskInfo> expectedTaskInfos = new HashMap<ITask, ITaskInfo>();
793            ITask task = createTaskTree(noOfMaxChildren, maxDepth, expectedTaskInfos);
794            if (!(task instanceof ISequence)) {
795                ISequence sequence = taskFactory.createNewSequence();
796                taskBuilder.addChild(sequence, task);
797                task = sequence;
798            }
799            else {
800                expectedTaskInfos.remove(task);
801            }
802           
803            ISequenceInstance sequenceInstance =
804                (ISequenceInstance) instantiateTask(task, noOfMaxChildren);
805           
806            System.err.println("  creating user session");
807           
808            IUserSession session = taskFactory.createUserSession();
809           
810            for (ITaskInstance child : sequenceInstance) {
811                taskBuilder.addExecutedTask(session, child);
812            }
813           
814            List<IUserSession> sessions = new LinkedList<IUserSession>();
815            sessions.add(session);
816           
817            ITaskModel taskModel = taskFactory.createTaskModel(sessions);
818
819            System.err.println("  validating task tree");
820            Map<ITask, ITaskInfo> actualTaskInfos = new HashMap<ITask, ITaskInfo>();
821           
822            for (ITask currentTask : taskModel.getTasks()) {
823                actualTaskInfos.put(currentTask, taskModel.getTaskInfo(currentTask));
824            }
825           
826            assertMapsEqual(expectedTaskInfos, actualTaskInfos);
827        }
828    }
829
830    /**
831     *
832     */
833    private void assertMapsEqual(Map<ITask, ITaskInfo> map1,
834                                 Map<ITask, ITaskInfo> map2)
835    {
836        try {
837            if (map1 == null) {
838                assertNull(map2);
839                return;
840            }
841
842            assertEquals(map1.size(), map2.size());
843
844            for (Map.Entry<ITask, ITaskInfo> entry : map1.entrySet()) {
845                ITaskInfo value2 = map2.get(entry.getKey());
846                assertNotNull(value2);
847                assertEquals(entry.getValue().getTask(), value2.getTask());
848                //assertEquals(entry.getValue().getNoOfOccurencesInTree(),
849                //             value2.getNoOfOccurencesInTree());
850            }
851        }
852        catch (AssertionError e) {
853            dumpMap(map1);
854            dumpMap(map2);
855            throw e;
856        }
857    }
858
859    /**
860     *
861     */
862    private void dumpMap(Map<ITask, ITaskInfo> map) {
863        System.err.println();
864
865        if (map == null) {
866            System.err.println("map is null");
867        }
868        else {
869            System.err.println("map:");
870            for (Map.Entry<ITask, ITaskInfo> entry : map.entrySet()) {
871                System.err.print("  ");
872                System.err.print(entry.getKey());
873                for (int i = entry.getKey().toString().length(); i < 60; i++) {
874                    System.err.print(" ");
875                }
876                System.err.print(" : ");
877                System.err.println(entry.getValue());
878            }
879        }
880
881        System.err.println();
882    }
883
884    /**
885     *
886     */
887    private ITask createTaskTree(int                   maxNoOfChildren,
888                                 int                   maxDepth,
889                                 Map<ITask, ITaskInfo> taskInfos)
890        throws Exception
891    {
892
893        ITask task;
894
895        // integrating the maximum depth here assures, that either something between 0 and 8 will
896        // be the type, or if the max depth decreases near 0 only event tasks will be created
897        // to finish the tree creation
898        int type = randomize(Math.min(10, maxDepth));
899
900        switch (type)
901        {
902            case 0: {
903                // System.err.print("creating new event task ");
904                task = createNewEventTask(taskInfos);
905                break;
906            }
907            case 1: {
908                // System.err.print("reusing event task ");
909                task = reuseEventTask(taskInfos);
910                break;
911            }
912            case 2: {
913                // System.err.println("creating new sequence {");
914                task = createNewSequence(maxNoOfChildren, maxDepth, taskInfos);
915                break;
916            }
917            case 3: {
918                // System.err.println("reusing sequence {");
919                task = reuseSequence(maxNoOfChildren, maxDepth, taskInfos);
920                break;
921            }
922            case 4: {
923                // System.err.println("creating new selection {");
924                task = createNewSelection(maxNoOfChildren, maxDepth, taskInfos);
925                break;
926            }
927            case 5: {
928                // System.err.println("reusing selection {");
929                task = reuseSelection(maxNoOfChildren, maxDepth, taskInfos);
930                break;
931            }
932            case 6: {
933                // System.err.println("creating new iteration {");
934                task = createNewIteration(maxNoOfChildren, maxDepth, taskInfos);
935                break;
936            }
937            case 7: {
938                // System.err.println("reusing iteration {");
939                task = reuseIteration(maxNoOfChildren, maxDepth, taskInfos);
940                break;
941            }
942            case 8: {
943                // System.err.println("creating new optional {");
944                task = createNewOptional(maxNoOfChildren, maxDepth, taskInfos);
945                break;
946            }
947            case 9: {
948                // System.err.println("reusing optional {");
949                task = reuseOptional(maxNoOfChildren, maxDepth, taskInfos);
950                break;
951            }
952            default: {
953                // System.err.print("creating new event task per default ");
954                task = createNewEventTask(taskInfos);
955            }
956        }
957
958        return task;
959    }
960
961    /**
962     *
963     */
964    private ITask createNewEventTask(Map<ITask, ITaskInfo> taskInfos)
965        throws Exception
966    {
967        Thread.sleep(2);
968        long id = System.currentTimeMillis();
969        IEventTask task = createNewEventTask
970            (new DummyInteraction("interaction" + id, 1), new DummyGUIElement("elem" + id));
971
972        taskInfos.put(task, new TaskInfo(task));
973
974        return task;
975    }
976
977    /**
978     *
979     */
980    private ITask reuseEventTask(Map<ITask, ITaskInfo> taskInfos)
981        throws Exception
982    {
983        ITask eventTask = reuseTask(taskInfos, IEventTask.class);
984       
985        if (eventTask == null) {
986            eventTask = createNewEventTask(taskInfos);
987        }
988
989        return eventTask;
990    }
991
992    /**
993     *
994     */
995    private ITask createNewSequence(int                   maxNoOfChildren,
996                                    int                   maxDepth,
997                                    Map<ITask, ITaskInfo> taskInfos)
998        throws Exception
999    {
1000        ISequence sequence = taskFactory.createNewSequence();
1001
1002        // ensure at the minimum 2 children
1003        int noOfChildren = randomize(2, maxNoOfChildren);
1004
1005        for (int i = 0; i < noOfChildren; i++) {
1006            ITask child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos);
1007            taskBuilder.addChild(sequence, child);
1008        }
1009
1010        taskInfos.put(sequence, new TaskInfo(sequence));
1011        return sequence;
1012    }
1013
1014    /**
1015     *
1016     */
1017    private ITask reuseSequence(int                   maxNoOfChildren,
1018                                int                   maxDepth,
1019                                Map<ITask, ITaskInfo> taskInfos)
1020        throws Exception
1021    {
1022        ITask sequence = reuseTask(taskInfos, ISequence.class);
1023       
1024        if (sequence == null) {
1025            sequence = createNewSequence(maxNoOfChildren, maxDepth, taskInfos);
1026        }
1027
1028        return sequence;
1029    }
1030
1031    /**
1032     *
1033     */
1034    private ITask createNewSelection(int                   maxNoOfChildren,
1035                                     int                   maxDepth,
1036                                     Map<ITask, ITaskInfo> taskInfos)
1037        throws Exception
1038    {
1039        ISelection selection = taskFactory.createNewSelection();
1040
1041        // ensure at the minimum 1 child
1042        int noOfChildren = randomize(1, maxNoOfChildren);
1043       
1044        for (int i = 0; i < noOfChildren; i++) {
1045            ITask child;
1046            do {
1047                child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos);
1048            }
1049            while (child instanceof ISelection);
1050           
1051            taskBuilder.addChild(selection, child);
1052        }
1053
1054        taskInfos.put(selection, new TaskInfo(selection));
1055        return selection;
1056    }
1057
1058    /**
1059     *
1060     */
1061    private ITask reuseSelection(int                   maxNoOfChildren,
1062                                 int                   maxDepth,
1063                                 Map<ITask, ITaskInfo> taskInfos)
1064        throws Exception
1065    {
1066        ITask selection = reuseTask(taskInfos, ISelection.class);
1067       
1068        if (selection == null) {
1069            selection = createNewSelection(maxNoOfChildren, maxDepth, taskInfos);
1070        }
1071
1072        return selection;
1073    }
1074
1075    /**
1076     *
1077     */
1078    private ITask createNewIteration(int                   maxNoOfChildren,
1079                                     int                   maxDepth,
1080                                     Map<ITask, ITaskInfo> taskInfos)
1081        throws Exception
1082    {
1083        IIteration iteration = taskFactory.createNewIteration();
1084
1085        ITask child;
1086        do {
1087            child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos);
1088        }
1089        while ((child instanceof IOptional) || (child instanceof IIteration));
1090       
1091        taskBuilder.setMarkedTask(iteration, child);
1092
1093        taskInfos.put(iteration, new TaskInfo(iteration));
1094        return iteration;
1095    }
1096
1097    /**
1098     *
1099     */
1100    private ITask reuseIteration(int                   maxNoOfChildren,
1101                                 int                   maxDepth,
1102                                 Map<ITask, ITaskInfo> taskInfos)
1103        throws Exception
1104    {
1105        ITask iteration = reuseTask(taskInfos, IIteration.class);
1106       
1107        if (iteration == null) {
1108            iteration = createNewIteration(maxNoOfChildren, maxDepth, taskInfos);
1109        }
1110
1111        return iteration;
1112    }
1113
1114    /**
1115     *
1116     */
1117    private ITask createNewOptional(int                   maxNoOfChildren,
1118                                    int                   maxDepth,
1119                                    Map<ITask, ITaskInfo> taskInfos)
1120        throws Exception
1121    {
1122        IOptional optional = taskFactory.createNewOptional();
1123
1124        ITask child;
1125        do {
1126            child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos);
1127        }
1128        while (child instanceof IOptional);
1129       
1130        taskBuilder.setMarkedTask(optional, child);
1131
1132        taskInfos.put(optional, new TaskInfo(optional));
1133        return optional;
1134    }
1135
1136    /**
1137     *
1138     */
1139    private ITask reuseOptional(int                   maxNoOfChildren,
1140                                int                   maxDepth,
1141                                Map<ITask, ITaskInfo> taskInfos)
1142        throws Exception
1143    {
1144        ITask optional = reuseTask(taskInfos, IOptional.class);
1145       
1146        if (optional == null) {
1147            optional = createNewOptional(maxNoOfChildren, maxDepth, taskInfos);
1148        }
1149
1150        return optional;
1151    }
1152
1153    /**
1154     *
1155     */
1156    private ITask reuseTask(Map<ITask, ITaskInfo> taskInfos, Class<? extends ITask> type)
1157        throws Exception
1158    {
1159        int noOfTasks = 0;
1160
1161        for (Map.Entry<ITask, ITaskInfo> entry : taskInfos.entrySet()) {
1162            if (type.isInstance(entry.getKey())) {
1163                noOfTasks++;
1164            }
1165        }
1166
1167        if (noOfTasks > 0) {
1168            noOfTasks = randomize(noOfTasks);
1169
1170            for (Map.Entry<ITask, ITaskInfo> entry : taskInfos.entrySet()) {
1171                if (type.isInstance(entry.getKey())) {
1172                    if (--noOfTasks <= 0) {
1173                        return entry.getKey();
1174                    }
1175                }
1176            }
1177        }
1178        else {
1179            return null;
1180        }
1181
1182        throw new RuntimeException("this is an implementation error");
1183    }
1184
1185    /**
1186     *
1187     */
1188    private ITaskInstance instantiateTask(ITask task, int maxIterationCount) throws Exception {
1189        ITaskInstance instance = createNewTaskInstance(task);
1190
1191        if (task instanceof ISequence) {
1192            for (ITask child : ((ISequence) task).getChildren()) {
1193                taskBuilder.addChild
1194                    ((ISequenceInstance) instance, instantiateTask(child, maxIterationCount));
1195            }
1196        }
1197        else if (task instanceof ISelection) {
1198            List<ITask> children = ((ISelection) task).getChildren();
1199            int index = randomize(children.size());
1200            taskBuilder.setChild((ISelectionInstance) instance,
1201                                 instantiateTask(children.get(index),maxIterationCount));
1202        }
1203        else if (task instanceof IIteration) {
1204            int count = randomize(maxIterationCount);
1205            ITask child = ((IIteration) task).getMarkedTask();
1206           
1207            for (int i = 0; i < count; i++) {
1208                taskBuilder.addChild
1209                    ((IIterationInstance) instance, instantiateTask(child, maxIterationCount));
1210            }
1211        }
1212        else if (task instanceof IOptional) {
1213            ITask child = ((IOptional) task).getMarkedTask();
1214           
1215            if (randomize(1) == 0) {
1216                taskBuilder.setChild((IOptionalInstance) instance,
1217                                     instantiateTask(child, maxIterationCount));
1218            }
1219        }
1220       
1221        return instance;
1222    }
1223
1224    /**
1225     *
1226     */
1227    private int randomize(int max) throws Exception {
1228        return randomize(0, max);
1229    }
1230   
1231    /**
1232     *
1233     */
1234    private int randomize(int min, int max) throws Exception {
1235        if (min > max) {
1236            throw new IllegalArgumentException("min must always be smaller or equal than max");
1237        }
1238       
1239        int deviation = max - min;
1240        int value = (int) (Math.random() * deviation);
1241       
1242        return value + min;
1243    }
1244   
1245    /**
1246     *
1247     */
1248    protected IEventTask createNewEventTask(IEventType eventType, IEventTarget eventTarget) {
1249        IEventTask eventTask = taskFactory.createNewEventTask(eventType + " --> " + eventTarget);
1250        taskFactory.createNewTaskInstance(eventTask, new Event(eventType, eventTarget));
1251        return eventTask;
1252    }
1253
1254    /**
1255     *
1256     */
1257    private ITaskInstance createNewTaskInstance(ITask task) {
1258        if (task instanceof IEventTask) {
1259            return createNewTaskInstance((IEventTask) task);
1260        }
1261        else if (task instanceof ISequence) {
1262            return createNewTaskInstance((ISequence) task);
1263        }
1264        else if (task instanceof ISelection) {
1265            return createNewTaskInstance((ISelection) task);
1266        }
1267        else if (task instanceof IIteration) {
1268            return createNewTaskInstance((IIteration) task);
1269        }
1270        else if (task instanceof IOptional) {
1271            return createNewTaskInstance((IOptional) task);
1272        }
1273       
1274        throw new IllegalArgumentException("unknown type of task");
1275    }
1276
1277    /**
1278     *
1279     */
1280    private IEventTaskInstance createNewTaskInstance(IEventTask task) {
1281        IEventTaskInstance existingInstance =
1282            (IEventTaskInstance) task.getInstances().iterator().next();
1283        return taskFactory.createNewTaskInstance(task, existingInstance.getEvent());
1284    }
1285
1286    /**
1287     *
1288     */
1289    private ISequenceInstance createNewTaskInstance(ISequence task) {
1290        return taskFactory.createNewTaskInstance(task);
1291    }
1292
1293    /**
1294     *
1295     */
1296    private ISelectionInstance createNewTaskInstance(ISelection task) {
1297        return taskFactory.createNewTaskInstance(task);
1298    }
1299
1300    /**
1301     *
1302     */
1303    private IIterationInstance createNewTaskInstance(IIteration task) {
1304        return taskFactory.createNewTaskInstance(task);
1305    }
1306
1307    /**
1308     *
1309     */
1310    private IOptionalInstance createNewTaskInstance(IOptional task) {
1311        return taskFactory.createNewTaskInstance(task);
1312    }
1313
1314}
Note: See TracBrowser for help on using the repository browser.