]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/test/SlcTestTask.java
Simplify ANt-Spring bridge
[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.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 TestDefinitionArg testDefinitionArg;
29 private TestDataArg testDataArg;
30 private DeployedSystemArg deployedSystemArg;
31 private TestResultArg 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 if (log.isTraceEnabled())
56 log.trace("Created simple test run");
57 } else {
58 if (log.isTraceEnabled())
59 log.trace("Load test run from scanning Spring context");
60 }
61 }
62
63 // set overridden references
64 if (testDataArg != null) {
65 testRun.setTestData(testDataArg.getTestData());
66 log.trace("Overrides test data");
67 }
68
69 if (testDefinitionArg != null) {
70 testRun.setTestDefinition(testDefinitionArg.getTestDefinition());
71 log.trace("Overrides test definition");
72 }
73
74 if (deployedSystemArg != null) {
75 testRun.setDeployedSystem(deployedSystemArg.getDeployedSystem());
76 log.trace("Overrides deployed system");
77 }
78
79 if (testResultArg != null) {
80 testRun.setTestResult(testResultArg.getTestResult());
81 log.trace("Overrides test result");
82 }
83
84 // notify path to test result
85 TestResult result = testRun.getTestResult();
86 if (result == null) {
87 result = loadSingleFromContext(TestResult.class);
88 if (result == null) {
89 result = new SimpleTestResult();
90 if (log.isTraceEnabled())
91 log.trace("Created simple test result");
92 } else {
93 if (log.isTraceEnabled())
94 log.trace("Load test result from scanning Spring context");
95 }
96 testRun.setTestResult(result);
97 }
98
99 if (result != null && result instanceof StructureAware) {
100 ((StructureAware<TreeSPath>) result).notifyCurrentPath(
101 getRegistry(), getTreeSPath());
102 }
103
104 ((ExecutableTestRun) testRun).execute();
105 }
106
107 /**
108 * The bean name of the test run to use. If not set the default is used.
109 *
110 * @see SlcAntConfig
111 */
112 public void setTestRun(String testRunBean) {
113 this.testRunBean = testRunBean;
114 }
115
116 /** Creates sub tag. */
117 public TestDefinitionArg createTestDefinition() {
118 testDefinitionArg = new TestDefinitionArg();
119 // only test definitions can add to path
120 addSAwareArg(testDefinitionArg);
121 return testDefinitionArg;
122 }
123
124 /** Creates sub tag. */
125 public TestDataArg createTestData() {
126 testDataArg = new TestDataArg();
127 return testDataArg;
128 }
129
130 /** Creates sub tag. */
131 public DeployedSystemArg createDeployedSystem() {
132 deployedSystemArg = new DeployedSystemArg();
133 return deployedSystemArg;
134 }
135
136 /** Creates sub tag. */
137 public TestResultArg createTestResult() {
138 testResultArg = new TestResultArg();
139 return testResultArg;
140 }
141
142 protected <T> T loadSingleFromContext(Class<T> clss) {
143 return SpringUtils.loadSingleFromContext(getContext(), clss);
144 }
145 }
146
147 class TestDefinitionArg extends AbstractSpringArg {
148 TestDefinition getTestDefinition() {
149 return (TestDefinition) getBeanInstance();
150 }
151 }
152
153 class TestDataArg extends AbstractSpringArg {
154 TestData getTestData() {
155 return (TestData) getBeanInstance();
156 }
157
158 }
159
160 class DeployedSystemArg extends AbstractSpringArg {
161 DeployedSystem getDeployedSystem() {
162 return (DeployedSystem) getBeanInstance();
163 }
164
165 }
166
167 class TestResultArg extends AbstractSpringArg {
168 TestResult getTestResult() {
169 return (TestResult) getBeanInstance();
170 }
171
172 }