]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/test/SlcTestTask.java
Simplify TreeSPath
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / ant / test / SlcTestTask.java
1 package org.argeo.slc.ant.test;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.tools.ant.BuildException;
6
7 import org.argeo.slc.ant.SlcAntConfig;
8 import org.argeo.slc.ant.spring.AbstractSpringArg;
9 import org.argeo.slc.ant.structure.SAwareTask;
10 import org.argeo.slc.core.deploy.DeployedSystem;
11 import org.argeo.slc.core.process.SlcExecution;
12 import org.argeo.slc.core.process.SlcExecutionAware;
13 import org.argeo.slc.core.structure.StructureAware;
14 import org.argeo.slc.core.structure.tree.TreeSPath;
15 import org.argeo.slc.core.test.ExecutableTestRun;
16 import org.argeo.slc.core.test.SimpleTestResult;
17 import org.argeo.slc.core.test.SimpleTestRun;
18 import org.argeo.slc.core.test.TestData;
19 import org.argeo.slc.core.test.TestDefinition;
20 import org.argeo.slc.core.test.TestResult;
21 import org.argeo.slc.core.test.WritableTestRun;
22 import org.argeo.slc.spring.SpringUtils;
23
24 /** Ant task wrapping a test run. */
25 public class SlcTestTask extends SAwareTask {
26 private Log log = LogFactory.getLog(SlcTestTask.class);
27
28 private String testRunBean = null;
29
30 private TestDefinitionArg testDefinitionArg;
31 private TestDataArg testDataArg;
32 private DeployedSystemArg deployedSystemArg;
33 private TestResultArg testResultArg;
34
35 @Override
36 public void executeActions(String mode) throws BuildException {
37 // find test run
38 final String testRunBeanT;
39 if (testRunBean != null) {
40 testRunBeanT = testRunBean;
41 } else {
42 testRunBeanT = getProject().getUserProperty(
43 SlcAntConfig.DEFAULT_TEST_RUN_PROPERTY);
44 }
45 WritableTestRun testRun = null;
46
47 if (testRunBeanT != null) {
48 testRun = (WritableTestRun) getContext().getBean(testRunBeanT);
49 if (log.isTraceEnabled())
50 log.trace("Load test run bean from bean name " + testRunBeanT);
51 }
52
53 if (testRun == null) {
54 testRun = loadSingleFromContext(WritableTestRun.class);
55 if (testRun == null) {
56 testRun = new SimpleTestRun();
57 if (log.isTraceEnabled())
58 log.trace("Created simple test run");
59 } else {
60 if (log.isTraceEnabled())
61 log.trace("Load test run from scanning Spring context");
62 }
63 }
64
65 // set overridden references
66 if (testDataArg != null) {
67 testRun.setTestData(testDataArg.getTestData());
68 log.trace("Overrides test data");
69 }
70
71 if (testDefinitionArg != null) {
72 testRun.setTestDefinition(testDefinitionArg.getTestDefinition());
73 log.trace("Overrides test definition");
74 }
75
76 if (deployedSystemArg != null) {
77 testRun.setDeployedSystem(deployedSystemArg.getDeployedSystem());
78 log.trace("Overrides deployed system");
79 }
80
81 if (testResultArg != null) {
82 testRun.setTestResult(testResultArg.getTestResult());
83 log.trace("Overrides test result");
84 }
85
86 // notify path to test result
87 TestResult result = testRun.getTestResult();
88 if (result == null) {
89 result = loadSingleFromContext(TestResult.class);
90 if (result == null) {
91 result = new SimpleTestResult();
92 if (log.isTraceEnabled())
93 log.trace("Created simple test result");
94 } else {
95 if (log.isTraceEnabled())
96 log.trace("Load test result from scanning Spring context");
97 }
98 testRun.setTestResult(result);
99 }
100
101 SlcExecution slcExecution = getSlcExecution();
102 if (result != null && slcExecution != null
103 && result instanceof SlcExecutionAware) {
104 ((SlcExecutionAware) result).notifySlcExecution(slcExecution);
105 }
106
107 if (result != null && result instanceof StructureAware) {
108 ((StructureAware<TreeSPath>) result).notifyCurrentPath(
109 getRegistry(), getTreeSPath());
110 }
111
112 ((ExecutableTestRun) testRun).execute();
113 }
114
115 /**
116 * The bean name of the test run to use. If not set the default is used.
117 *
118 * @see SlcAntConfig
119 */
120 public void setTestRun(String testRunBean) {
121 this.testRunBean = testRunBean;
122 }
123
124 /** Creates sub tag. */
125 public TestDefinitionArg createTestDefinition() {
126 testDefinitionArg = new TestDefinitionArg();
127 // only test definitions can add to path
128 addSAwareArg(testDefinitionArg);
129 return testDefinitionArg;
130 }
131
132 /** Creates sub tag. */
133 public TestDataArg createTestData() {
134 testDataArg = new TestDataArg();
135 return testDataArg;
136 }
137
138 /** Creates sub tag. */
139 public DeployedSystemArg createDeployedSystem() {
140 deployedSystemArg = new DeployedSystemArg();
141 return deployedSystemArg;
142 }
143
144 /** Creates sub tag. */
145 public TestResultArg createTestResult() {
146 testResultArg = new TestResultArg();
147 return testResultArg;
148 }
149
150 protected <T> T loadSingleFromContext(Class<T> clss) {
151 return SpringUtils.loadSingleFromContext(getContext(), clss);
152 }
153 }
154
155 class TestDefinitionArg extends AbstractSpringArg {
156 TestDefinition getTestDefinition() {
157 return (TestDefinition) getBeanInstance();
158 }
159 }
160
161 class TestDataArg extends AbstractSpringArg {
162 TestData getTestData() {
163 return (TestData) getBeanInstance();
164 }
165
166 }
167
168 class DeployedSystemArg extends AbstractSpringArg {
169 DeployedSystem getDeployedSystem() {
170 return (DeployedSystem) getBeanInstance();
171 }
172
173 }
174
175 class TestResultArg extends AbstractSpringArg {
176 TestResult getTestResult() {
177 return (TestResult) getBeanInstance();
178 }
179
180 }