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