001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ken.test;
017
018 import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
019 import org.kuali.rice.core.api.lifecycle.Lifecycle;
020 import org.kuali.rice.core.framework.resourceloader.RiceResourceLoaderFactory;
021 import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
022 import org.kuali.rice.ken.core.SpringNotificationServiceLocator;
023 import org.kuali.rice.kew.batch.KEWXmlDataLoader;
024 import org.kuali.rice.test.BaselineTestCase;
025 import org.kuali.rice.test.BaselineTestCase.BaselineMode;
026 import org.kuali.rice.test.BaselineTestCase.Mode;
027 import org.kuali.rice.test.CompositeBeanFactory;
028 import org.kuali.rice.test.SQLDataLoader;
029 import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
030 import org.kuali.rice.test.lifecycles.SQLDataLoaderLifecycle;
031 import org.quartz.Scheduler;
032 import org.quartz.SchedulerException;
033 import org.springframework.beans.factory.BeanFactory;
034 import org.springframework.transaction.PlatformTransactionManager;
035
036 import javax.xml.namespace.QName;
037 import java.util.ArrayList;
038 import java.util.List;
039
040
041 /**
042 * Base test case for KEN that extends RiceTestCase
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045 @BaselineMode(Mode.ROLLBACK_CLEAR_DB)
046 public abstract class KENTestCase extends BaselineTestCase {
047 private static final String KEN_MODULE_NAME = "ken";
048 private static final String TX_MGR_BEAN_NAME = "transactionManager";
049
050 protected SpringNotificationServiceLocator services;
051 protected PlatformTransactionManager transactionManager;
052
053 public KENTestCase() {
054 super(KEN_MODULE_NAME);
055 }
056
057
058
059 @Override
060 protected List<Lifecycle> getSuiteLifecycles() {
061 List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
062 suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultSuiteTestData.xml"));
063 return suiteLifecycles;
064 }
065
066 @Override
067 protected Lifecycle getLoadApplicationLifecycle() {
068 SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KENTestHarnessApplicationResourceLoader"), "classpath:KENTestHarnessSpringBeans.xml", null);
069 springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
070 return springResourceLoader;
071 }
072
073 @Override
074 protected List<Lifecycle> getPerTestLifecycles() {
075 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
076 lifecycles.add(new ClearCacheLifecycle());
077 lifecycles.addAll(getNotificationPerTestLifecycles());
078 return lifecycles;
079 }
080
081 protected List<Lifecycle> getNotificationPerTestLifecycles() {
082 List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
083 lifecycles.add(new BaseLifecycle() {
084 @Override
085 public void start() throws Exception {
086 // get the composite Rice Spring context
087 BeanFactory moduleContext = CompositeBeanFactory.createBeanFactory(
088 RiceResourceLoaderFactory.getSpringResourceLoaders());
089 // This method sets up the Spring services so that they can be accessed by the tests.
090 services = new SpringNotificationServiceLocator(moduleContext);
091 // grab the module's transaction manager
092 transactionManager = (PlatformTransactionManager) moduleContext.getBean(TX_MGR_BEAN_NAME, PlatformTransactionManager.class);
093 super.start();
094 }
095
096 });
097
098 // clear out the KEW cache
099 lifecycles.add(new BaseLifecycle() {
100 @Override
101 public void start() throws Exception {
102 super.start();
103
104 LOG.info("Status of Ken scheduler on start: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
105 // stop quartz if a test failed to do so
106 disableQuartzJobs();
107 }
108 public void stop() throws Exception {
109 //KsbApiServiceLocator.getCacheAdministrator().flushAll();
110
111 LOG.info("Status of Ken scheduler on stop: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
112 // stop quartz if a test failed to do so
113 disableQuartzJobs();
114
115 super.stop();
116 }
117 });
118
119 // load the default SQL
120 //lifecycles.add(new SQLDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.sql", ";"));
121
122 //lifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.xml"));
123
124
125 return lifecycles;
126
127 }
128
129 /**
130 * By default this loads the "default" data set from the DefaultTestData.sql
131 * and DefaultTestData.xml files. Subclasses can override this to change
132 * this behaviour
133 */
134 protected void loadDefaultTestData() throws Exception {
135 // at this point this is constants. loading these through xml import is
136 // problematic because of cache notification
137 // issues in certain low level constants.
138 new SQLDataLoader(
139 "classpath:org/kuali/rice/ken/test/DefaultPerTestData.sql", ";")
140 .runSql();
141
142 KEWXmlDataLoader.loadXmlClassLoaderResource(KENTestCase.class, "DefaultPerTestData.xml");
143 }
144 /**
145 * Returns the List of tables that should be cleared on every test run.
146 */
147 @Override
148 protected List<String> getPerTestTablesToClear() {
149 List<String> tablesToClear = new ArrayList<String>();
150 tablesToClear.add("KREW_.*");
151 tablesToClear.add("KRSB_.*");
152 tablesToClear.add("KREN_.*");
153 return tablesToClear;
154 }
155
156 protected void setUpAfterDataLoad() throws Exception {
157 // override this to load your own test data
158 }
159
160 /**
161 * Initiates loading of per-test data
162 */
163 @Override
164 protected void loadPerTestData() throws Exception {
165 loadDefaultTestData();
166
167
168 setUpAfterDataLoad();
169
170 final long t4 = System.currentTimeMillis();
171 }
172
173 /**
174 * Flushes the KEW cache(s)
175 */
176 public class ClearCacheLifecycle extends BaseLifecycle {
177 @Override
178 public void stop() throws Exception {
179 //KsbApiServiceLocator.getCacheAdministrator().flushAll();
180 //KimApiServiceLocator.getIdentityManagementService().flushAllCaches();
181 //KimApiServiceLocator.getRoleService().flushRoleCaches();
182 super.stop();
183 }
184
185 }
186 /**
187 * This method makes sure to disable the Quartz scheduler
188 * @throws SchedulerException
189 */
190 protected void disableQuartzJobs() throws SchedulerException {
191 // do this so that our quartz jobs don't go off - we don't care about
192 // these in our unit tests
193 Scheduler scheduler = services.getScheduler();
194 scheduler.standby();
195 //scheduler.shutdown();
196 }
197
198 /**
199 * This method enables the Quartz scheduler
200 * @throws SchedulerException
201 */
202 protected void enableQuartzJobs() throws SchedulerException {
203 // do this so that our quartz jobs don't go off - we don't care about
204 // these in our unit tests
205 Scheduler scheduler = services.getScheduler();
206 scheduler.start();
207 }
208
209 }