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