]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/CompositeTreeTestDefinition.java
Minimal Hello World execution module
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / CompositeTreeTestDefinition.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.test.tree;
18
19 import java.util.List;
20 import java.util.Vector;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.core.structure.SimpleSElement;
25 import org.argeo.slc.core.structure.tree.TreeSPath;
26 import org.argeo.slc.structure.StructureAware;
27 import org.argeo.slc.structure.StructureElement;
28 import org.argeo.slc.structure.StructureElementProvider;
29 import org.argeo.slc.structure.StructureRegistry;
30 import org.argeo.slc.test.TestDefinition;
31 import org.argeo.slc.test.TestResult;
32 import org.argeo.slc.test.TestRun;
33
34 /**
35 * Collection of test definitions propagating tree structure information to its
36 * children.
37 */
38 public class CompositeTreeTestDefinition implements TestDefinition,
39 StructureAware<TreeSPath> {
40 private Log log = LogFactory.getLog(CompositeTreeTestDefinition.class);
41
42 private List<TestDefinition> tasks = null;
43 private List<TreeSPath> taskPaths = null;
44 private TreeSPath path;
45 private StructureRegistry<TreeSPath> registry;
46
47 @SuppressWarnings("unchecked")
48 public void execute(TestRun testRun) {
49 if (log.isTraceEnabled())
50 log.trace("Execute sequence of test definitions...");
51
52 int i = 0;
53 for (TestDefinition task : tasks) {
54 TestResult result = testRun.getTestResult();
55 if (result instanceof StructureAware) {
56 ((StructureAware) result).notifyCurrentPath(registry, taskPaths
57 .get(i));
58 }
59
60 task.execute(testRun);
61
62 // Reset current path in case it has been changed
63 if (result instanceof StructureAware) {
64 ((StructureAware) result).notifyCurrentPath(registry, path);
65 }
66 i++;
67 }
68 }
69
70 /** Sets the list of children test definitions */
71 public void setTasks(List<TestDefinition> tasks) {
72 this.tasks = tasks;
73 if (tasks != null) {
74 taskPaths = new Vector<TreeSPath>();
75 }
76 }
77
78 @SuppressWarnings("unchecked")
79 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
80 TreeSPath path) {
81 this.path = path;
82 this.registry = registry;
83
84 // clear task paths
85 taskPaths.clear();
86
87 Integer count = 0;
88 for (TestDefinition task : tasks) {
89 final StructureElement element;
90 if (task instanceof StructureElementProvider) {
91 element = ((StructureElementProvider) task)
92 .createStructureElement();
93 } else {
94 element = new SimpleSElement("[no desc]");
95 }
96 TreeSPath taskPath = this.path.createChild(count.toString());
97 registry.register(taskPath, element);
98 taskPaths.add(taskPath);
99 if (task instanceof StructureAware) {
100 ((StructureAware<TreeSPath>) task).notifyCurrentPath(registry,
101 taskPath);
102 }
103 count++;
104 }
105 }
106
107 }