1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.test;
17
18 import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
19 import org.kuali.rice.core.api.lifecycle.Lifecycle;
20 import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
21 import org.kuali.rice.kew.api.WorkflowRuntimeException;
22 import org.kuali.rice.kew.batch.KEWXmlDataLoader;
23 import org.kuali.rice.kew.service.KEWServiceLocator;
24 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25 import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
26 import org.kuali.rice.test.BaselineTestCase;
27 import org.kuali.rice.test.ClearDatabaseLifecycle;
28 import org.kuali.rice.test.SQLDataLoader;
29 import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
30 import org.springframework.cache.CacheManager;
31 import org.springframework.transaction.support.TransactionTemplate;
32
33 import javax.xml.namespace.QName;
34 import java.io.InputStream;
35 import java.util.ArrayList;
36 import java.util.List;
37
38
39
40
41
42
43
44
45 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
46 public abstract class KEWTestCase extends BaselineTestCase {
47
48 private static final String SQL_FILE = "classpath:org/kuali/rice/kew/test/DefaultSuiteTestData.sql";
49 private static final String XML_FILE = "classpath:org/kuali/rice/kew/test/DefaultSuiteTestData.xml";
50 private static final String KRAD_MODULE_NAME = "kew";
51
52 public KEWTestCase() {
53 super(KRAD_MODULE_NAME);
54 }
55
56 @Override
57 protected List<Lifecycle> getSuiteLifecycles() {
58 List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
59 suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
60
61 return suiteLifecycles;
62 }
63
64 @Override
65 protected void loadSuiteTestData() throws Exception {
66 super.loadSuiteTestData();
67 new SQLDataLoader(SQL_FILE, ";").runSql();
68 }
69
70
71
72
73
74
75
76 protected void loadDefaultTestData() throws Exception {
77
78
79
80 new SQLDataLoader(
81 "classpath:org/kuali/rice/kew/test/DefaultPerTestData.sql", ";")
82 .runSql();
83
84 KEWXmlDataLoader.loadXmlClassLoaderResource(KEWTestCase.class,
85 "DefaultPerTestData.xml");
86 }
87
88 @Override
89 protected Lifecycle getLoadApplicationLifecycle() {
90 SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KEWTestResourceLoader"), "classpath:org/kuali/rice/kew/config/TestKEWSpringBeans.xml", null);
91 springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
92 return springResourceLoader;
93 }
94
95
96
97
98
99
100 protected void setUpAfterDataLoad() throws Exception {
101
102 }
103
104 protected void loadTestData() throws Exception {
105
106 }
107
108 protected TransactionTemplate getTransactionTemplate() {
109 return TestUtilities.getTransactionTemplate();
110 }
111
112
113
114
115 @Override
116 protected void loadPerTestData() throws Exception {
117 final long t1 = System.currentTimeMillis();
118 loadDefaultTestData();
119
120 final long t2 = System.currentTimeMillis();
121 loadTestData();
122
123 final long t3 = System.currentTimeMillis();
124 report("Time to load test-specific test data: " + (t3 - t2));
125
126 setUpAfterDataLoad();
127
128 final long t4 = System.currentTimeMillis();
129 report("Time to run test-specific setup: " + (t4 - t3));
130 }
131
132
133
134
135
136
137
138 @Override
139 protected List<Lifecycle> getPerTestLifecycles() {
140 List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
141 lifecycles.add(new ClearDatabaseLifecycle(getPerTestTablesToClear(),
142 getPerTestTablesNotToClear()));
143 lifecycles.add(new ClearCacheLifecycle());
144 lifecycles.addAll(super.getPerTestLifecycles());
145 return lifecycles;
146 }
147
148
149
150
151 public class ClearCacheLifecycle extends BaseLifecycle {
152 @Override
153 public void stop() throws Exception {
154 clearCacheManagers(KimImplServiceLocator.getLocalCacheManager(), KEWServiceLocator.getLocalCacheManager());
155 super.stop();
156 }
157 }
158
159 protected void clearCacheManagers(CacheManager... cacheManagers) {
160 for (CacheManager cacheManager : cacheManagers) {
161 for (String cacheName : cacheManager.getCacheNames()) {
162 LOG.info("Clearing cache: " + cacheName);
163 cacheManager.getCache(cacheName).clear();
164 }
165 }
166 }
167
168
169
170
171 protected List<String> getPerTestTablesToClear() {
172 List<String> tablesToClear = new ArrayList<String>();
173 tablesToClear.add("KREW_.*");
174 tablesToClear.add("KRSB_.*");
175 tablesToClear.add("KREN_.*");
176 tablesToClear.add("KRMS_.*");
177 return tablesToClear;
178 }
179
180 protected List<String> getPerTestTablesNotToClear() {
181 return new ArrayList<String>();
182 }
183
184 protected void loadXmlFile(String fileName) {
185 try {
186 KEWXmlDataLoader.loadXmlClassLoaderResource(getClass(), fileName);
187 } catch (Exception e) {
188 throw new WorkflowRuntimeException(e);
189 }
190 }
191
192 protected void loadXmlFile(Class clazz, String fileName) {
193 try {
194 KEWXmlDataLoader.loadXmlClassLoaderResource(clazz, fileName);
195 } catch (Exception e) {
196 throw new WorkflowRuntimeException(e);
197 }
198 }
199
200 protected void loadXmlFileFromFileSystem(String fileName) {
201 try {
202 KEWXmlDataLoader.loadXmlFile(fileName);
203 } catch (Exception e) {
204 throw new WorkflowRuntimeException(e);
205 }
206 }
207
208 protected void loadXmlStream(InputStream xmlStream) {
209 try {
210 KEWXmlDataLoader.loadXmlStream(xmlStream);
211 } catch (Exception e) {
212 throw new WorkflowRuntimeException(e);
213 }
214 }
215
216 protected String getPrincipalIdForName(String principalName) {
217 return KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName).getPrincipalId();
218 }
219
220 protected String getPrincipalNameForId(String principalId) {
221 return KimApiServiceLocator.getIdentityService().getPrincipal(principalId).getPrincipalName();
222 }
223
224 protected String getGroupIdForName(String namespace, String groupName) {
225 return KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(namespace, groupName).getId();
226 }
227 }