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