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 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper;
|
---|
16 |
|
---|
17 | import static org.fest.assertions.api.Assertions.assertThat;
|
---|
18 |
|
---|
19 | import java.lang.reflect.Field;
|
---|
20 | import java.util.Arrays;
|
---|
21 | import java.util.Collection;
|
---|
22 | import java.util.List;
|
---|
23 | import java.util.Map;
|
---|
24 |
|
---|
25 | import org.junit.Test;
|
---|
26 |
|
---|
27 | import de.ugoe.cs.autoquest.plugin.html.commands.CMDparseHTML;
|
---|
28 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.Follows;
|
---|
29 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.IFilter;
|
---|
30 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.Label;
|
---|
31 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.filter.EventTypeFilter;
|
---|
32 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.visitors.Contains;
|
---|
33 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.visitors.FindContained;
|
---|
34 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper.AbstractTaskEntryVisitor;
|
---|
35 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper.FollowedByUtil;
|
---|
36 | import de.ugoe.cs.autoquest.plugin.usability2.rules.operator.wrapper.ITaskEntry;
|
---|
37 | import de.ugoe.cs.autoquest.plugin.usability2.rules.results.IMatch;
|
---|
38 | import de.ugoe.cs.autoquest.plugin.usability2.rules.results.IResult;
|
---|
39 | import de.ugoe.cs.autoquest.plugin.usability2.tools.TaskUtilities;
|
---|
40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
|
---|
41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
|
---|
43 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
44 | import de.ugoe.cs.autoquest.usability.testutil.GenerateTaskModelUtil;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <p>
|
---|
48 | * Test of the FollowedBy Operator
|
---|
49 | * </p>
|
---|
50 | *
|
---|
51 | * @author Konni Hartmann
|
---|
52 | */
|
---|
53 | public class PreparsingTest {
|
---|
54 |
|
---|
55 | private static Class<? extends ITaskInstance> task_instance_class = null;
|
---|
56 |
|
---|
57 | private static void extractTaskInstanceClass(ITaskInstance instance) {
|
---|
58 | Class<? extends ITaskInstance> clazz = instance.getClass();
|
---|
59 |
|
---|
60 | while (!clazz.getSimpleName().equals("TaskInstance")) {
|
---|
61 | Class<?> superclass = clazz.getSuperclass();
|
---|
62 | if (ITaskInstance.class.isAssignableFrom(superclass)) {
|
---|
63 | clazz = (Class<? extends ITaskInstance>) superclass;
|
---|
64 | } else {
|
---|
65 | clazz = null;
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (clazz != null)
|
---|
71 | task_instance_class = clazz;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private static int getInstanceID(ITaskInstance instance) {
|
---|
75 | try {
|
---|
76 | if(task_instance_class == null)
|
---|
77 | extractTaskInstanceClass(instance);
|
---|
78 |
|
---|
79 | Field field = task_instance_class.getDeclaredField("id");
|
---|
80 | field.setAccessible(true);
|
---|
81 | return field.getInt(instance);
|
---|
82 | }
|
---|
83 | catch (IllegalArgumentException e) {
|
---|
84 | // TODO Auto-generated catch block
|
---|
85 | e.printStackTrace();
|
---|
86 | }
|
---|
87 | catch (IllegalAccessException e) {
|
---|
88 | // TODO Auto-generated catch block
|
---|
89 | e.printStackTrace();
|
---|
90 | }
|
---|
91 | catch (NoSuchFieldException e) {
|
---|
92 | // TODO Auto-generated catch block
|
---|
93 | e.printStackTrace();
|
---|
94 | }
|
---|
95 | catch (SecurityException e) {
|
---|
96 | // TODO Auto-generated catch block
|
---|
97 | e.printStackTrace();
|
---|
98 | }
|
---|
99 | return -1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static class TestVisitor extends AbstractTaskEntryVisitor {
|
---|
103 | int indentionLevel = 0;
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public void visitEntry(ITaskEntry value) {
|
---|
107 | StringBuilder indent = new StringBuilder((indentionLevel - 1) * 2);
|
---|
108 | for (int i = 1; i < indentionLevel; i++)
|
---|
109 | indent.append(" ");
|
---|
110 | // System.out.printf("%s%s -- R: %s P: %s N: %s\n", indent, value, value.getReference(),
|
---|
111 | // value.getParent(), value.getNext());
|
---|
112 | System.out.printf("%s%s -- P: %s N: %s\n", indent, value, value.getParent(), value.getNext());
|
---|
113 |
|
---|
114 | List<ITaskInstance> filters = value.getAvailableFilters();
|
---|
115 | for (ITaskInstance filter : filters) {
|
---|
116 | System.out.printf("%s+ (#%s): %s\n", indent, getInstanceID(filter), value.getFilteredInstances(filter));
|
---|
117 | }
|
---|
118 | System.out.println();
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public void visit(ITaskEntry value) {
|
---|
123 | indentionLevel++;
|
---|
124 | super.visit(value);
|
---|
125 | indentionLevel--;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test
|
---|
130 | public void contains_event_positive() {
|
---|
131 | // Given
|
---|
132 | String spec_session1 =
|
---|
133 | "UserSession {" +
|
---|
134 | " Iteration it {" +
|
---|
135 | " Sequence s2 {" +
|
---|
136 | " MouseClick c1 { }" +
|
---|
137 | " Scroll scl { }" +
|
---|
138 | " }" +
|
---|
139 | " Sequence s2 {" +
|
---|
140 | " MouseClick c1 { }" +
|
---|
141 | " Scroll scl { }" +
|
---|
142 | " }" +
|
---|
143 | " }" +
|
---|
144 | "}";
|
---|
145 |
|
---|
146 | String spec_session2 =
|
---|
147 | "UserSession {" +
|
---|
148 | " Iteration it {" +
|
---|
149 | " Sequence s2 {" +
|
---|
150 | " MouseClick c1 { }" +
|
---|
151 | " Scroll scl { }" +
|
---|
152 | " }" +
|
---|
153 | " }" +
|
---|
154 | "}";
|
---|
155 |
|
---|
156 | // ITaskModel taskTree = GenerateTaskModelUtil.getTaskTreeFromFile("logs/log.xml", CMDparseHTML.class);
|
---|
157 | ITaskModel taskTree = GenerateTaskModelUtil.getTaskModelFromSpec(spec_session1, spec_session2);
|
---|
158 | Collection<ITask> tasks = taskTree.getTasks();
|
---|
159 | GenerateInstanceListVisitor gILV = new GenerateInstanceListVisitor();
|
---|
160 |
|
---|
161 | // When
|
---|
162 |
|
---|
163 | for (ITask iTask : TaskUtilities.findRootTasks(tasks)) {
|
---|
164 | // ITask iTask = root;
|
---|
165 | ITaskEntry followTree = FollowedByUtil.generateFollowList(iTask);
|
---|
166 | TestVisitor v = new TestVisitor();
|
---|
167 | v.visit(followTree);
|
---|
168 |
|
---|
169 | for (List<IEventTaskInstance> sequence : followTree.getInstanceSequences()) {
|
---|
170 | System.out.println(sequence);
|
---|
171 | }
|
---|
172 |
|
---|
173 | System.out.println();
|
---|
174 | }
|
---|
175 |
|
---|
176 | IFilter f = new Follows(new FindContained(new Label("X", EventTypeFilter.SCROLL)),
|
---|
177 | new Contains(new Label("Y", EventTypeFilter.MOUSE_CLICK))
|
---|
178 | );
|
---|
179 |
|
---|
180 | IResult result = null;
|
---|
181 |
|
---|
182 | for (ITask root : TaskUtilities.findRootTasks(tasks)) {
|
---|
183 | result = f.match(root);
|
---|
184 |
|
---|
185 | if(!result.isPresent())
|
---|
186 | continue;
|
---|
187 |
|
---|
188 | System.out.println("\n>>RESULTS:");
|
---|
189 | System.out.println(result.isPresent());
|
---|
190 | for (IMatch m : result) {
|
---|
191 | Map<String, ITask> map = m.getLabeledResults();
|
---|
192 | System.out.println(">"+map);
|
---|
193 | }
|
---|
194 |
|
---|
195 | Collection<List<IEventTaskInstance>> sequences = gILV.generateInstanceList(root);
|
---|
196 | Collection<List<IEventTaskInstance>> matched = InstanceSequenceFinder.containsSequence(sequences, Arrays.asList(EventTypeFilter.SCROLL, EventTypeFilter.MOUSE_CLICK));
|
---|
197 |
|
---|
198 | System.out.printf("Found match in %d of %d instances.\n", matched.size(), sequences.size());
|
---|
199 | }
|
---|
200 |
|
---|
201 | // Then
|
---|
202 | assertThat(result).isNotNull();
|
---|
203 | assertThat(result.isPresent()).isTrue();
|
---|
204 | }
|
---|
205 | }
|
---|