]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/test/SlcTestTask.java
Revert to Java 5 compatibility
[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.AntConstants;
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().getProperty(
42 AntConstants.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 "
51 + 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.trace("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 }