]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/test/SlcTestTask.java
Introduce unit tests framework
[gpl/argeo-slc.git] / org.argeo.slc / 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.test.TestData;
13 import org.argeo.slc.core.test.TestDefinition;
14 import org.argeo.slc.core.test.TestResult;
15 import org.argeo.slc.core.test.WritableTestRun;
16
17 /** Ant task wrapping a test run. */
18 public class SlcTestTask extends SAwareTask {
19 private Log log = LogFactory.getLog(SlcTestTask.class);
20
21 private String testRunBean = null;
22
23 private TestDefinitionArg testDefinitionArg;
24 private TestDataArg testDataArg;
25 private DeployedSystemArg deployedSystemArg;
26 private TestResultArg testResultArg;
27
28 @Override
29 public void executeActions(String mode) throws BuildException {
30 final String testRunBeanT;
31 if (testRunBean != null) {
32 testRunBeanT = testRunBean;
33 } else {
34 testRunBeanT = getProject().getUserProperty(
35 SlcAntConfig.DEFAULT_TEST_RUN_PROPERTY);
36 }
37 WritableTestRun testRun = (WritableTestRun) getContext().getBean(
38 testRunBeanT);
39
40 // set overridden references
41 if (testDataArg != null) {
42 testRun.setTestData(testDataArg.getTestData());
43 log.trace("Overrides test data");
44 }
45
46 if (testDefinitionArg != null) {
47 testRun.setTestDefinition(testDefinitionArg.getTestDefinition());
48 log.trace("Overrides test definition");
49 }
50
51 if (deployedSystemArg != null) {
52 testRun.setDeployedSystem(deployedSystemArg.getDeployedSystem());
53 log.trace("Overrides deployed system");
54 }
55
56 if (testResultArg != null) {
57 testRun.setTestResult(testResultArg.getTestResult());
58 log.trace("Overrides test result");
59 }
60
61 // notify path to test result
62 TestResult result = testRun.getTestResult();
63 if (result != null && result instanceof StructureAware) {
64 ((StructureAware) result).notifyCurrentPath(getRegistry(),
65 getPath());
66 }
67
68 testRun.execute();
69 }
70
71 /**
72 * The bean name of the test run to use. If not set the default is used.
73 *
74 * @see SlcAntConfig
75 */
76 public void setTestRun(String testRunBean) {
77 this.testRunBean = testRunBean;
78 }
79
80 public TestDefinitionArg createTestDefinition() {
81 testDefinitionArg = new TestDefinitionArg();
82 // only test definitions can add to path
83 addSAwareArg(testDefinitionArg);
84 return testDefinitionArg;
85 }
86
87 public TestDataArg createTestData() {
88 testDataArg = new TestDataArg();
89 return testDataArg;
90 }
91
92 public DeployedSystemArg createDeployedSystem() {
93 deployedSystemArg = new DeployedSystemArg();
94 return deployedSystemArg;
95 }
96
97 public TestResultArg createTestResult() {
98 testResultArg = new TestResultArg();
99 return testResultArg;
100 }
101
102 }
103
104 class TestDefinitionArg extends AbstractSpringArg {
105 public TestDefinition getTestDefinition() {
106 return (TestDefinition) getBeanInstance();
107 }
108 }
109
110 class TestDataArg extends AbstractSpringArg {
111 public TestData getTestData() {
112 return (TestData) getBeanInstance();
113 }
114
115 }
116
117 class DeployedSystemArg extends AbstractSpringArg {
118 public DeployedSystem getDeployedSystem() {
119 return (DeployedSystem) getBeanInstance();
120 }
121
122 }
123
124 class TestResultArg extends AbstractSpringArg {
125 public TestResult getTestResult() {
126 return (TestResult) getBeanInstance();
127 }
128
129 }