]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/ExecutionFlowTest.java
ExecutionParameterPostProcessor improved (placeholder resolved for each instance)
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / test / java / org / argeo / slc / core / execution / ExecutionFlowTest.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import junit.framework.TestCase;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.slc.core.test.BasicTestData;
11 import org.argeo.slc.core.test.SimpleTestResult;
12 import org.argeo.slc.execution.ExecutionContext;
13 import org.argeo.slc.execution.ExecutionFlow;
14 import org.argeo.slc.test.TestResultPart;
15 import org.argeo.slc.test.TestStatus;
16 import org.argeo.slc.unit.AbstractSpringTestCase;
17 import org.springframework.beans.factory.BeanCreationException;
18 import org.springframework.context.ConfigurableApplicationContext;
19 import org.springframework.context.support.ClassPathXmlApplicationContext;
20
21 public class ExecutionFlowTest extends TestCase {
22
23 protected final Log log = LogFactory.getLog(getClass());
24
25
26 // to test: case with listener
27
28 // public void testMyTest() throws Exception {
29 // ConfigurableApplicationContext applicationContext = createApplicationContext("test.xml");
30 // log.info("Start Execution");
31 // ((ExecutionFlow) applicationContext.getBean("fileDiff.SimulationView_Risk")).execute();
32 // applicationContext.close();
33 // }
34 //
35 public void testMultipleFlows() throws Exception {
36 ConfigurableApplicationContext applicationContext = createApplicationContext("multipleFlow.xml");
37 log.info("Start Execution");
38 ((ExecutionFlow) applicationContext.getBean("flow1")).execute();
39 ((ExecutionFlow) applicationContext.getBean("flow2")).execute();
40 applicationContext.close();
41 }
42
43 /**
44 * Test placeholder resolution in a context without scope execution or proxy
45 * and with cascading flows (the flow A contains the flow B)
46 * @throws Exception
47 */
48 public void testPlaceHolders() throws Exception {
49 ConfigurableApplicationContext applicationContext = createApplicationContext("placeHolders.cascading.xml");
50 ((ExecutionFlow) applicationContext.getBean("flowA")).execute();
51 validateTestResult((SimpleTestResult) applicationContext.getBean("myTestResult"));
52 applicationContext.close();
53 }
54
55 /**
56 * Test placeholder resolution in a context without scope execution or proxy
57 * and with cascading flows (the flow A contains the flow B)
58 * setting execution values (should have no effect)
59 * @throws Exception
60 */
61 public void testPlaceHoldersWithExecutionValues() throws Exception {
62 ConfigurableApplicationContext applicationContext = createApplicationContext("placeHolders.cascading.xml");
63
64 ExecutionContext executionContext = (ExecutionContext)applicationContext.getBean("executionContext");
65 Map<String, String> executionParameters = new HashMap<String,String>();
66 executionParameters.put("p1", "e1");
67 executionParameters.put("p2", "e2");
68 executionParameters.put("p3", "e3");
69 executionParameters.put("p4", "e4");
70 executionParameters.put("p5", "e5");
71 executionParameters.put("p6", "e6");
72 executionParameters.put("p7", "e7");
73 executionParameters.put("p8", "e8");
74 executionContext.addVariables(executionParameters);
75
76 ((ExecutionFlow) applicationContext.getBean("flowA")).execute();
77 validateTestResult((SimpleTestResult) applicationContext.getBean("myTestResult"));
78 applicationContext.close();
79 }
80
81 public void testPlaceHoldersExec() throws Exception {
82 ConfigurableApplicationContext applicationContext = createApplicationContext("placeHolders.cascading.exec.xml");
83
84 ExecutionContext executionContext = (ExecutionContext)applicationContext.getBean("executionContext");
85 Map<String, String> executionParameters = new HashMap<String,String>();
86 executionParameters.put("p1", "e1");
87 executionParameters.put("p2", "e2");
88 executionParameters.put("p3", "e3");
89 executionParameters.put("p4", "e4");
90 executionParameters.put("p5", "e5");
91 executionParameters.put("p6", "e6");
92 executionContext.addVariables(executionParameters);
93
94 ((ExecutionFlow) applicationContext.getBean("flowA")).execute();
95 validateTestResult((SimpleTestResult) applicationContext.getBean("myTestResult"));
96 applicationContext.close();
97 }
98
99 public void testCanonicFlowParameters() throws Exception {
100 configureAndExecuteSlcFlow("canonic-001.xml", "canonic.001");
101 }
102
103 public void testCanonicDefaultValues() throws Exception {
104 configureAndExecuteSlcFlow("canonic-002.xml", "canonic.002");
105 }
106
107 public void testCanonicMissingValues() throws Exception {
108 try {
109 configureAndExecuteSlcFlow("canonic-003.error.xml", "canonic.003");
110 fail("Parameter not set - should be rejected.");
111 } catch (BeanCreationException e) {
112 // exception expected
113 logException(e);
114 } }
115
116 public void testCanonicUnknownParameter() throws Exception {
117 try {
118 configureAndExecuteSlcFlow("canonic-004.error.xml", "canonic.004");
119 fail("Unknown parameter set - should be rejected.");
120 } catch (BeanCreationException e) {
121 // exception expected
122 logException(e);
123 }
124 }
125
126 public void testListSetMap() throws Exception {
127 ConfigurableApplicationContext applicationContext = createApplicationContext("listSetMap.xml");
128 ExecutionFlow executionFlow = (ExecutionFlow) applicationContext.getBean("myFlow");
129 executionFlow.execute();
130
131 validateTestResult((SimpleTestResult) applicationContext.getBean("myTestResult"));
132
133 // BasicTestData res = (BasicTestData) applicationContext.getBean("cascadingComplex.testData");
134 // log.info("res=" + res.getReached().toString());
135
136 applicationContext.close();
137 }
138
139 // public void testListSetMapMultipleFlows() throws Exception {
140 // ConfigurableApplicationContext applicationContext = createApplicationContext("listSetMapMultipleFlow.xml");
141 // ((ExecutionFlow) applicationContext.getBean("flow1")).execute();
142 // SimpleTestResult res = (SimpleTestResult) applicationContext.getBean("myTestResult");
143 // validateTestResult(res);
144 // res.getParts().clear();
145 // ((ExecutionFlow) applicationContext.getBean("flow2")).execute();
146 // validateTestResult(res, TestStatus.FAILED);
147 // applicationContext.close();
148 // }
149
150 protected void logException(Throwable ex) {
151 log.info("Got Exception of class " + ex.getClass().toString()
152 + " with message '" + ex.getMessage() + "'.");
153 }
154
155 protected void validateTestResult(SimpleTestResult testResult) {
156 validateTestResult(testResult, TestStatus.PASSED);
157 }
158
159 protected void validateTestResult(SimpleTestResult testResult, int expectedStatus) {
160 for(TestResultPart part : testResult.getParts()) {
161 if(part.getStatus() != expectedStatus) {
162 fail("Error found in TestResult: " + part.getMessage());
163 }
164 }
165 }
166
167 protected ConfigurableApplicationContext createApplicationContext(String applicationContextSuffix) {
168 ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(inPackage(applicationContextSuffix));
169 applicationContext.start();
170 return applicationContext;
171 }
172
173 protected void configureAndExecuteSlcFlow(String applicationContextSuffix, String beanName) {
174 ConfigurableApplicationContext applicationContext = createApplicationContext(applicationContextSuffix);
175 ExecutionFlow executionFlow = (ExecutionFlow) applicationContext.getBean(beanName);
176 executionFlow.execute();
177 applicationContext.close();
178 }
179
180 protected String inPackage(String suffix) {
181 String prefix = getClass().getPackage().getName().replace('.', '/');
182 return prefix + '/' + suffix;
183 }
184 }