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