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