]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/test/SlcTestTask.java
Simplify new runtime
[gpl/argeo-slc.git] / org.argeo.slc.agent / 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 import org.argeo.slc.ant.SlcAntConfig;
7 import org.argeo.slc.ant.SlcAntConstants;
8 import org.argeo.slc.ant.spring.SpringArg;
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.structure.StructureAware;
13 import org.argeo.slc.core.structure.tree.TreeSPath;
14 import org.argeo.slc.core.test.ExecutableTestRun;
15 import org.argeo.slc.core.test.SimpleTestResult;
16 import org.argeo.slc.core.test.SimpleTestRun;
17 import org.argeo.slc.core.test.TestData;
18 import org.argeo.slc.core.test.TestDefinition;
19 import org.argeo.slc.core.test.TestResult;
20 import org.argeo.slc.core.test.WritableTestRun;
21 import org.argeo.slc.spring.SpringUtils;
22 import org.springframework.beans.BeansException;
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 SpringArg<TestDefinition> testDefinitionArg;
31 private SpringArg<TestData> testDataArg;
32 private SpringArg<DeployedSystem> deployedSystemArg;
33 private SpringArg<TestResult> 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().getProperty(
43 SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY);
44 }
45 WritableTestRun testRun = null;
46
47 if (testRunBeanT != null) {
48 try {
49 testRun = (WritableTestRun) getContext().getBean(testRunBeanT);
50 if (log.isTraceEnabled())
51 log.trace("Load test run bean from bean name " + testRunBeanT);
52 } catch (BeansException e) {
53 // silent, will try defaults
54 }
55 }
56
57 if (testRun == null) {
58 testRun = loadSingleFromContext(WritableTestRun.class);
59 if (testRun == null) {
60 testRun = new SimpleTestRun();
61 log.warn("Created default simple test run");
62 } else {
63 if (log.isTraceEnabled())
64 log.trace("Load test run from scanning Spring context");
65 }
66 }
67
68 // set overridden references
69 if (testDataArg != null) {
70 testRun.setTestData(testDataArg.getBeanInstance());
71 log.trace("Overrides test data");
72 }
73
74 if (testDefinitionArg != null) {
75 testRun.setTestDefinition(testDefinitionArg.getBeanInstance());
76 log.trace("Overrides test definition");
77 }
78
79 if (deployedSystemArg != null) {
80 testRun.setDeployedSystem(deployedSystemArg.getBeanInstance());
81 log.trace("Overrides deployed system");
82 }
83
84 if (testResultArg != null) {
85 testRun.setTestResult(testResultArg.getBeanInstance());
86 log.trace("Overrides test result");
87 }
88
89 // notify path to test result
90 TestResult result = testRun.getTestResult();
91 if (result == null) {
92 result = loadSingleFromContext(TestResult.class);
93 if (result == null) {
94 result = new SimpleTestResult();
95 log.warn("Created default simple test result");
96 } else {
97 if (log.isTraceEnabled())
98 log.trace("Load test result from scanning Spring context");
99 }
100 testRun.setTestResult(result);
101 }
102
103 SlcExecution slcExecution = getSlcExecution();
104 testRun.notifySlcExecution(slcExecution);
105
106 if (result != null && result instanceof StructureAware) {
107 ((StructureAware<TreeSPath>) result).notifyCurrentPath(
108 getRegistry(), getTreeSPath());
109 }
110
111 ((ExecutableTestRun) testRun).execute();
112 }
113
114 /**
115 * The bean name of the test run to use. If not set the default is used.
116 *
117 * @see SlcAntConfig
118 */
119 public void setTestRun(String testRunBean) {
120 this.testRunBean = testRunBean;
121 }
122
123 /** Creates sub tag. */
124 public SpringArg<TestDefinition> createTestDefinition() {
125 testDefinitionArg = new SpringArg<TestDefinition>();
126 // only test definitions can add to path
127 addSAwareArg(testDefinitionArg);
128 return testDefinitionArg;
129 }
130
131 /** Creates sub tag. */
132 public SpringArg<TestData> createTestData() {
133 testDataArg = new SpringArg<TestData>();
134 return testDataArg;
135 }
136
137 /** Creates sub tag. */
138 public SpringArg<DeployedSystem> createDeployedSystem() {
139 deployedSystemArg = new SpringArg<DeployedSystem>();
140 return deployedSystemArg;
141 }
142
143 /** Creates sub tag. */
144 public SpringArg<TestResult> createTestResult() {
145 testResultArg = new SpringArg<TestResult>();
146 return testResultArg;
147 }
148
149 protected <T> T loadSingleFromContext(Class<T> clss) {
150 return SpringUtils.loadSingleFromContext(getContext(), clss);
151 }
152 }