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