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