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