1 package org
.argeo
.slc
.ant
.test
;
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
.structure
.tree
.TreeSPath
;
10 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
11 import org
.argeo
.slc
.core
.test
.SimpleTestResult
;
12 import org
.argeo
.slc
.core
.test
.SimpleTestRun
;
13 import org
.argeo
.slc
.deploy
.DeployedSystem
;
14 import org
.argeo
.slc
.process
.SlcExecution
;
15 import org
.argeo
.slc
.spring
.SpringUtils
;
16 import org
.argeo
.slc
.structure
.StructureAware
;
17 import org
.argeo
.slc
.test
.ExecutableTestRun
;
18 import org
.argeo
.slc
.test
.TestData
;
19 import org
.argeo
.slc
.test
.TestDefinition
;
20 import org
.argeo
.slc
.test
.TestResult
;
21 import org
.argeo
.slc
.test
.TestResultPart
;
22 import org
.argeo
.slc
.test
.TestStatus
;
23 import org
.argeo
.slc
.test
.WritableTestRun
;
24 import org
.springframework
.beans
.BeansException
;
26 /** Ant task wrapping a test run. */
27 public class SlcTestTask
extends SAwareTask
{
28 private Log log
= LogFactory
.getLog(SlcTestTask
.class);
30 private String testRunBean
= null;
32 private SpringArg
<TestDefinition
> testDefinitionArg
;
33 private SpringArg
<TestData
> testDataArg
;
34 private SpringArg
<DeployedSystem
> deployedSystemArg
;
35 private SpringArg
<TestResult
> testResultArg
;
38 public void executeActions(String mode
) throws BuildException
{
40 final String testRunBeanT
;
41 if (testRunBean
!= null) {
42 testRunBeanT
= testRunBean
;
44 testRunBeanT
= getProject().getProperty(
45 AntConstants
.DEFAULT_TEST_RUN_PROPERTY
);
47 WritableTestRun testRun
= null;
49 if (testRunBeanT
!= null) {
51 testRun
= (WritableTestRun
) getContext().getBean(testRunBeanT
);
52 if (log
.isTraceEnabled())
53 log
.trace("Load test run bean from bean name "
55 } catch (BeansException e
) {
56 // silent, will try defaults
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");
66 if (log
.isTraceEnabled())
67 log
.trace("Load test run from scanning Spring context");
71 // set overridden references
72 if (testDataArg
!= null) {
73 testRun
.setTestData(testDataArg
.getInstance());
74 log
.trace("Overrides test data");
77 if (testDefinitionArg
!= null) {
78 testRun
.setTestDefinition(testDefinitionArg
.getInstance());
79 log
.trace("Overrides test definition");
82 if (deployedSystemArg
!= null) {
83 testRun
.setDeployedSystem(deployedSystemArg
.getInstance());
84 log
.trace("Overrides deployed system");
87 if (testResultArg
!= null) {
88 testRun
.setTestResult(testResultArg
.getInstance());
89 log
.trace("Overrides test result");
92 // notify path to test result
93 TestResult result
= testRun
.getTestResult();
95 result
= loadSingleFromContext(TestResult
.class);
97 result
= new SimpleTestResult();
98 log
.warn("Created default simple test result");
100 if (log
.isTraceEnabled())
101 log
.trace("Load test result from scanning Spring context");
103 testRun
.setTestResult(result
);
106 SlcExecution slcExecution
= getSlcExecution();
107 testRun
.notifySlcExecution(slcExecution
);
109 if (result
!= null && result
instanceof StructureAware
) {
110 ((StructureAware
<TreeSPath
>) result
).notifyCurrentPath(
111 getRegistry(), getTreeSPath());
115 ((ExecutableTestRun
) testRun
).run();
116 } catch (RuntimeException e
) {
117 if (result
!= null) {
118 SimpleResultPart errorPart
= new SimpleResultPart(
120 "Unexpected exception when running test", e
);
121 result
.addResultPart(errorPart
);
128 * The bean name of the test run to use. If not set the default is used.
132 public void setTestRun(String testRunBean
) {
133 this.testRunBean
= testRunBean
;
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
;
144 /** Creates sub tag. */
145 public SpringArg
<TestData
> createTestData() {
146 testDataArg
= new SpringArg
<TestData
>();
150 /** Creates sub tag. */
151 public SpringArg
<DeployedSystem
> createDeployedSystem() {
152 deployedSystemArg
= new SpringArg
<DeployedSystem
>();
153 return deployedSystemArg
;
156 /** Creates sub tag. */
157 public SpringArg
<TestResult
> createTestResult() {
158 testResultArg
= new SpringArg
<TestResult
>();
159 return testResultArg
;
162 protected <T
> T
loadSingleFromContext(Class
<T
> clss
) {
163 return SpringUtils
.loadSingleFromContext(getContext(), clss
);