1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.test;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.xml.namespace.QName;
22
23 import org.kuali.rice.core.lifecycle.BaseLifecycle;
24 import org.kuali.rice.core.lifecycle.Lifecycle;
25 import org.kuali.rice.core.resourceloader.RiceResourceLoaderFactory;
26 import org.kuali.rice.core.resourceloader.SpringResourceLoader;
27 import org.kuali.rice.ken.core.SpringNotificationServiceLocator;
28 import org.kuali.rice.kew.batch.KEWXmlDataLoaderLifecycle;
29 import org.kuali.rice.kew.service.KEWServiceLocator;
30 import org.kuali.rice.kim.service.KIMServiceLocator;
31 import org.kuali.rice.test.BaselineTestCase;
32 import org.kuali.rice.test.BaselineTestCase.BaselineMode;
33 import org.kuali.rice.test.BaselineTestCase.Mode;
34 import org.kuali.rice.test.lifecycles.SQLDataLoaderLifecycle;
35 import org.quartz.Scheduler;
36 import org.quartz.SchedulerException;
37 import org.springframework.context.ConfigurableApplicationContext;
38 import org.springframework.transaction.PlatformTransactionManager;
39
40
41
42
43
44
45 @BaselineMode(Mode.CLEAR_DB)
46 public abstract class KENTestCase extends BaselineTestCase {
47 private static final String KEN_MODULE_NAME = "ken";
48 private static final String TX_MGR_BEAN_NAME = "transactionManager";
49
50 protected SpringNotificationServiceLocator services;
51 protected PlatformTransactionManager transactionManager;
52
53 public KENTestCase() {
54 super(KEN_MODULE_NAME);
55 }
56
57
58
59 @Override
60 protected List<Lifecycle> getSuiteLifecycles() {
61 List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
62 suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultSuiteTestData.xml"));
63 return suiteLifecycles;
64 }
65
66 @Override
67 protected Lifecycle getLoadApplicationLifecycle() {
68 SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KENTestHarnessApplicationResourceLoader"), "classpath:KENTestHarnessSpringBeans.xml");
69 springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
70 return springResourceLoader;
71 }
72
73 @Override
74 protected List<Lifecycle> getPerTestLifecycles() {
75 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
76 lifecycles.add(new ClearCacheLifecycle());
77 lifecycles.addAll(getNotificationPerTestLifecycles());
78 return lifecycles;
79 }
80
81 protected List<Lifecycle> getNotificationPerTestLifecycles() {
82 List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
83 lifecycles.add(new BaseLifecycle() {
84 @Override
85 public void start() throws Exception {
86
87 ConfigurableApplicationContext moduleContext = RiceResourceLoaderFactory.getSpringResourceLoader().getContext();
88
89 services = new SpringNotificationServiceLocator(moduleContext);
90
91 transactionManager = (PlatformTransactionManager) moduleContext.getBean(TX_MGR_BEAN_NAME, PlatformTransactionManager.class);
92 super.start();
93 }
94
95 });
96
97
98 lifecycles.add(new BaseLifecycle() {
99 @Override
100 public void start() throws Exception {
101 super.start();
102
103 LOG.info("Status of Ken scheduler on start: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
104
105 disableQuartzJobs();
106 }
107 public void stop() throws Exception {
108 KEWServiceLocator.getCacheAdministrator().flushAll();
109
110 LOG.info("Status of Ken scheduler on stop: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
111
112 disableQuartzJobs();
113
114 super.stop();
115 }
116 });
117
118
119 lifecycles.add(new SQLDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.sql", ";"));
120
121 lifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.xml"));
122
123
124 return lifecycles;
125
126 }
127
128
129
130
131 protected List<String> getPerTestTablesToClear() {
132 List<String> tablesToClear = new ArrayList<String>();
133 tablesToClear.add("KREW_.*");
134 tablesToClear.add("KRSB_.*");
135 tablesToClear.add("KREN_.*");
136 return tablesToClear;
137 }
138
139
140
141
142 public class ClearCacheLifecycle extends BaseLifecycle {
143 @Override
144 public void stop() throws Exception {
145 KEWServiceLocator.getCacheAdministrator().flushAll();
146 KIMServiceLocator.getIdentityManagementService().flushAllCaches();
147 KIMServiceLocator.getRoleManagementService().flushRoleCaches();
148 super.stop();
149 }
150
151 }
152
153
154
155
156 protected void disableQuartzJobs() throws SchedulerException {
157
158
159 Scheduler scheduler = services.getScheduler();
160 scheduler.standby();
161
162 }
163
164
165
166
167
168 protected void enableQuartzJobs() throws SchedulerException {
169
170
171 Scheduler scheduler = services.getScheduler();
172 scheduler.start();
173 }
174
175 }