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.html.commands; |
---|
16 | |
---|
17 | import static org.junit.Assert.*; |
---|
18 | |
---|
19 | import java.util.Arrays; |
---|
20 | import java.util.HashMap; |
---|
21 | import java.util.LinkedList; |
---|
22 | import java.util.List; |
---|
23 | import java.util.Map; |
---|
24 | |
---|
25 | import org.junit.Before; |
---|
26 | import org.junit.Test; |
---|
27 | |
---|
28 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementFactory; |
---|
29 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementGroup; |
---|
30 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
31 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel.Traverser; |
---|
32 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModelException; |
---|
33 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
34 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec; |
---|
35 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLDocument; |
---|
36 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLDocumentSpec; |
---|
37 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElement; |
---|
38 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElementSpec; |
---|
39 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer; |
---|
40 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServerSpec; |
---|
41 | import de.ugoe.cs.util.console.GlobalDataContainer; |
---|
42 | import de.ugoe.cs.util.console.TextConsole; |
---|
43 | |
---|
44 | /** |
---|
45 | * <p> |
---|
46 | * TODO comment |
---|
47 | * </p> |
---|
48 | * |
---|
49 | * @author Patrick Harms |
---|
50 | */ |
---|
51 | public class CMDcondenseHTMLGUIModelTest { |
---|
52 | |
---|
53 | /** |
---|
54 | * |
---|
55 | */ |
---|
56 | @Before |
---|
57 | public void setUp() { |
---|
58 | new TextConsole(); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * |
---|
63 | */ |
---|
64 | @Test |
---|
65 | public void test_condense_01() throws Exception { |
---|
66 | String[] guiSpec = |
---|
67 | { "/server/doc1/html/body/div_0" }; |
---|
68 | |
---|
69 | GUIModel guiModel = generateGUIModel(guiSpec); |
---|
70 | GlobalDataContainer.getInstance().addData("sequences_targets", guiModel); |
---|
71 | |
---|
72 | executeCommand(); |
---|
73 | |
---|
74 | String[] assertionSpec = |
---|
75 | { "/server/doc1/html/body/div" }; |
---|
76 | |
---|
77 | assertGUIModel(guiModel, assertionSpec); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * |
---|
82 | */ |
---|
83 | @Test |
---|
84 | public void test_condense_02() throws Exception { |
---|
85 | String[] guiSpec = |
---|
86 | { "/server/doc1/html/body/div_0", |
---|
87 | "/server/doc1/html/body/div_1", |
---|
88 | "/server/doc1/html/body/div_2", |
---|
89 | "/server/doc1/html/body/div_3"}; |
---|
90 | |
---|
91 | GUIModel guiModel = generateGUIModel(guiSpec); |
---|
92 | GlobalDataContainer.getInstance().addData("sequences_targets", guiModel); |
---|
93 | |
---|
94 | executeCommand(); |
---|
95 | |
---|
96 | String[] assertionSpec = |
---|
97 | { "/server/doc1/html/body/div", |
---|
98 | "/server/doc1/html/body/div_1", |
---|
99 | "/server/doc1/html/body/div_2", |
---|
100 | "/server/doc1/html/body/div_3" }; |
---|
101 | |
---|
102 | assertGUIModel(guiModel, assertionSpec); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * |
---|
107 | */ |
---|
108 | @Test |
---|
109 | public void test_condense_03() throws Exception { |
---|
110 | String[] guiSpec = |
---|
111 | { "/server/doc1/html/body/div_0", |
---|
112 | "/server/doc1/html/body/div_1", |
---|
113 | "/server/doc2/html/body/div_2", |
---|
114 | "/server/doc2/html/body/div_3"}; |
---|
115 | |
---|
116 | GUIModel guiModel = generateGUIModel(guiSpec); |
---|
117 | GlobalDataContainer.getInstance().addData("sequences_targets", guiModel); |
---|
118 | |
---|
119 | executeCommand(); |
---|
120 | |
---|
121 | String[] assertionSpec = |
---|
122 | { "/server/doc1/html/body/group_doc1/div", |
---|
123 | "/server/doc1/html/body/group_doc1/div_1", |
---|
124 | "/server/doc1/html/body/group_doc2/div_2", |
---|
125 | "/server/doc1/html/body/group_doc2/div_3" }; |
---|
126 | |
---|
127 | assertGUIModel(guiModel, assertionSpec); |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * |
---|
132 | */ |
---|
133 | @Test |
---|
134 | public void test_condense_04() throws Exception { |
---|
135 | String[] guiSpec = |
---|
136 | { "/server/doc1/html/body/div", |
---|
137 | "/server/doc1/html/body/div_1", |
---|
138 | "/server/doc2/html/body/div", |
---|
139 | "/server/doc2/html/body/div_1"}; |
---|
140 | |
---|
141 | GUIModel guiModel = generateGUIModel(guiSpec); |
---|
142 | GlobalDataContainer.getInstance().addData("sequences_targets", guiModel); |
---|
143 | |
---|
144 | executeCommand(); |
---|
145 | |
---|
146 | String[] assertionSpec = |
---|
147 | { "/server/doc1/html/body/div", |
---|
148 | "/server/doc1/html/body/div_1" }; |
---|
149 | |
---|
150 | assertGUIModel(guiModel, assertionSpec); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * |
---|
155 | */ |
---|
156 | @Test |
---|
157 | public void test_condense_05() throws Exception { |
---|
158 | String[] guiSpec = |
---|
159 | { "/server/doc1/html/body/div", |
---|
160 | "/server/doc1/html/body/div_1", |
---|
161 | "/server/doc2/html/body/div", |
---|
162 | "/server/doc2/html/body/div_2"}; |
---|
163 | |
---|
164 | GUIModel guiModel = generateGUIModel(guiSpec); |
---|
165 | GlobalDataContainer.getInstance().addData("sequences_targets", guiModel); |
---|
166 | |
---|
167 | executeCommand(); |
---|
168 | |
---|
169 | String[] assertionSpec = |
---|
170 | { "/server/doc1/html/body/div", |
---|
171 | "/server/doc1/html/body/group_doc1/div_1", |
---|
172 | "/server/doc1/html/body/group_doc2/div_2" }; |
---|
173 | |
---|
174 | assertGUIModel(guiModel, assertionSpec); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * |
---|
179 | */ |
---|
180 | @Test |
---|
181 | public void test_condense_06() throws Exception { |
---|
182 | String[] guiSpec = |
---|
183 | { "/server/doc1/html/body/div/div_1", |
---|
184 | "/server/doc1/html/body/div/div_2", |
---|
185 | "/server/doc1/html/body/div/div_3", |
---|
186 | "/server/doc1/html/body/div/div_4", |
---|
187 | "/server/doc1/html/body/div/div_5", |
---|
188 | "/server/doc1/html/body/div/div_6", |
---|
189 | "/server/doc2/html/body/div/div_1", |
---|
190 | "/server/doc2/html/body/div/div_2", |
---|
191 | "/server/doc2/html/body/div/div_7", |
---|
192 | "/server/doc2/html/body/div/div_8", |
---|
193 | "/server/doc2/html/body/div/div_6", |
---|
194 | "/server/doc3/html/body/div/div_9", |
---|
195 | "/server/doc3/html/body/div/div_10", |
---|
196 | "/server/doc3/html/body/div/div_6", |
---|
197 | "/server/doc3/html/body/div/div_11", |
---|
198 | "/server/doc4/html/body/div/div_12", |
---|
199 | "/server/doc4/html/body/div/div_13", |
---|
200 | "/server/doc5/html/body/div/div_4", |
---|
201 | "/server/doc5/html/body/div/div_11" }; |
---|
202 | |
---|
203 | GUIModel guiModel = generateGUIModel(guiSpec); |
---|
204 | GlobalDataContainer.getInstance().addData("sequences_targets", guiModel); |
---|
205 | |
---|
206 | executeCommand(); |
---|
207 | |
---|
208 | String[] assertionSpec = |
---|
209 | { "/server/doc1/html/body/div/group_doc1doc2doc3/group_doc1doc2/div_1", |
---|
210 | "/server/doc1/html/body/div/group_doc1doc2doc3/group_doc1doc2/div_2", |
---|
211 | "/server/doc1/html/body/div/group_doc1doc2doc3/group_doc1doc2/group_doc1/div_3", |
---|
212 | "/server/doc1/html/body/div/group_doc1doc2doc3/group_doc1doc2/group_doc1/div_5", |
---|
213 | "/server/doc1/html/body/div/group_doc1doc2doc3/group_doc1doc2/group_doc2/div_7", |
---|
214 | "/server/doc1/html/body/div/group_doc1doc2doc3/group_doc1doc2/group_doc2/div_8", |
---|
215 | "/server/doc1/html/body/div/group_doc1doc2doc3/div_6", |
---|
216 | "/server/doc1/html/body/div/group_doc1doc5/div_4", |
---|
217 | "/server/doc1/html/body/div/group_doc3doc5/div_11", |
---|
218 | "/server/doc1/html/body/div/group_doc3doc5/group_doc3/div_9", |
---|
219 | "/server/doc1/html/body/div/group_doc3doc5/group_doc3/div_10", |
---|
220 | "/server/doc1/html/body/div/group_doc4/div_12", |
---|
221 | "/server/doc1/html/body/div/group_doc4/div_13" }; |
---|
222 | |
---|
223 | assertGUIModel(guiModel, assertionSpec); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * <p> |
---|
228 | * TODO: comment |
---|
229 | * </p> |
---|
230 | * |
---|
231 | * @param guiSpec |
---|
232 | * @return |
---|
233 | * @throws GUIModelException |
---|
234 | * @throws IllegalArgumentException |
---|
235 | */ |
---|
236 | private GUIModel generateGUIModel(String[] guiSpec) |
---|
237 | throws IllegalArgumentException, GUIModelException |
---|
238 | { |
---|
239 | GUIModel model = new GUIModel(true); |
---|
240 | |
---|
241 | Map<String, HTMLServerSpec> servers = new HashMap<String, HTMLServerSpec>(); |
---|
242 | Map<String, HTMLDocumentSpec> documents = new HashMap<String, HTMLDocumentSpec>(); |
---|
243 | Map<String, HTMLPageElementSpec> pageElements = new HashMap<String, HTMLPageElementSpec>(); |
---|
244 | |
---|
245 | for (String path : guiSpec) { |
---|
246 | String[] elements = path.split("/"); |
---|
247 | |
---|
248 | if ("".equals(elements[0])) { |
---|
249 | elements = Arrays.copyOfRange(elements, 1, elements.length); |
---|
250 | } |
---|
251 | |
---|
252 | List<IGUIElementSpec> pathList = new LinkedList<IGUIElementSpec>(); |
---|
253 | for (int i = 0; i < elements.length; i++) { |
---|
254 | if (i == 0) { |
---|
255 | if (!servers.containsKey(elements[i])) { |
---|
256 | servers.put(elements[i], new HTMLServerSpec(elements[i], 80)); |
---|
257 | } |
---|
258 | pathList.add(servers.get(elements[i])); |
---|
259 | } |
---|
260 | else if (i == 1) { |
---|
261 | if (!documents.containsKey(elements[i])) { |
---|
262 | HTMLDocumentSpec doc = new HTMLDocumentSpec |
---|
263 | ((HTMLServerSpec) pathList.get(0), elements[i], null, elements[i]); |
---|
264 | documents.put(elements[i], doc); |
---|
265 | } |
---|
266 | pathList.add(documents.get(elements[i])); |
---|
267 | } |
---|
268 | else { |
---|
269 | if (!pageElements.containsKey(elements[i])) { |
---|
270 | String[] infos = elements[i].split("_"); |
---|
271 | |
---|
272 | String tagName = infos[0]; |
---|
273 | |
---|
274 | int index = 0; |
---|
275 | String id = null; |
---|
276 | |
---|
277 | if (infos.length > 1) { |
---|
278 | try { |
---|
279 | index = Integer.parseInt(infos[1]); |
---|
280 | } |
---|
281 | catch (Exception e) { |
---|
282 | id = infos[1]; |
---|
283 | index = -1; |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | HTMLPageElementSpec pageElem = new HTMLPageElementSpec |
---|
288 | ((HTMLDocumentSpec) pathList.get(1), tagName, id, index); |
---|
289 | pageElements.put(elements[i], pageElem); |
---|
290 | } |
---|
291 | |
---|
292 | pathList.add(pageElements.get(elements[i])); |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | model.integratePath(pathList, GUIElementFactory.getInstance()); |
---|
297 | } |
---|
298 | |
---|
299 | return model; |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * <p> |
---|
304 | * TODO: comment |
---|
305 | * </p> |
---|
306 | * |
---|
307 | */ |
---|
308 | private void executeCommand() { |
---|
309 | CMDcondenseHTMLGUIModel command = new CMDcondenseHTMLGUIModel(); |
---|
310 | |
---|
311 | List<Object> parameters = new LinkedList<Object>(); |
---|
312 | parameters.add("sequences"); |
---|
313 | |
---|
314 | command.run(parameters); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * <p> |
---|
319 | * TODO: comment |
---|
320 | * </p> |
---|
321 | * |
---|
322 | * @param guiModel |
---|
323 | * @param assertionSpec |
---|
324 | */ |
---|
325 | private void assertGUIModel(GUIModel guiModel, String[] assertionSpec) { |
---|
326 | List<String> paths = new LinkedList<String>(); |
---|
327 | |
---|
328 | for (String path : assertionSpec) { |
---|
329 | paths.add(path); |
---|
330 | } |
---|
331 | |
---|
332 | GUIModel.Traverser traverser = guiModel.getTraverser(); |
---|
333 | |
---|
334 | assertPaths(traverser, "", paths); |
---|
335 | |
---|
336 | assertEquals(0, paths.size()); |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | * <p> |
---|
341 | * TODO: comment |
---|
342 | * </p> |
---|
343 | * |
---|
344 | * @param traverser |
---|
345 | * @param paths |
---|
346 | */ |
---|
347 | private void assertPaths(Traverser traverser, String path, List<String> paths) { |
---|
348 | if (traverser.hasFirstChild()) { |
---|
349 | IGUIElement childElement = traverser.firstChild(); |
---|
350 | |
---|
351 | while (childElement != null) { |
---|
352 | String currentPath = path + "/" + getPathElement(childElement); |
---|
353 | assertPaths(traverser, currentPath, paths); |
---|
354 | childElement = traverser.nextSibling(); |
---|
355 | } |
---|
356 | |
---|
357 | traverser.parent(); |
---|
358 | } |
---|
359 | else { |
---|
360 | assertTrue("unexpected path: " + path, paths.remove(path)); |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | * <p> |
---|
366 | * TODO: comment |
---|
367 | * </p> |
---|
368 | * |
---|
369 | * @param childElement |
---|
370 | * @return |
---|
371 | */ |
---|
372 | private String getPathElement(IGUIElement childElement) { |
---|
373 | StringBuffer name = new StringBuffer(); |
---|
374 | |
---|
375 | if (childElement instanceof HTMLServer) { |
---|
376 | name.append(((HTMLServer) childElement).getHost()); |
---|
377 | } |
---|
378 | else if (childElement instanceof HTMLDocument) { |
---|
379 | name.append(((HTMLDocument) childElement).getPath()); |
---|
380 | } |
---|
381 | else if (childElement instanceof HTMLPageElement) { |
---|
382 | name.append(((HTMLPageElement) childElement).getTagName()); |
---|
383 | |
---|
384 | if (((HTMLPageElement) childElement).getHtmlId() != null) { |
---|
385 | name.append("_"); |
---|
386 | name.append(((HTMLPageElement) childElement).getHtmlId()); |
---|
387 | } |
---|
388 | else if (((HTMLPageElement) childElement).getIndex() > 0) { |
---|
389 | name.append("_"); |
---|
390 | name.append(((HTMLPageElement) childElement).getIndex()); |
---|
391 | } |
---|
392 | } |
---|
393 | else if (childElement instanceof GUIElementGroup) { |
---|
394 | name.append(((GUIElementGroup) childElement).getStringIdentifier()); |
---|
395 | |
---|
396 | /*for (IGUIElement groupedElement : ((GUIElementGroup) childElement).getGroupedElements()) |
---|
397 | { |
---|
398 | name.append(getPathElement(groupedElement)); |
---|
399 | }*/ |
---|
400 | } |
---|
401 | |
---|
402 | return name.toString(); |
---|
403 | } |
---|
404 | |
---|
405 | } |
---|