001 /*
002 * Copyright 2007 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.impl.resourceloader.RiceResourceLoaderFactory;
021 import org.kuali.rice.core.impl.resourceloader.SpringResourceLoader;
022 import org.kuali.rice.ken.core.SpringNotificationServiceLocator;
023 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
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.lifecycles.KEWXmlDataLoaderLifecycle;
029 import org.kuali.rice.test.lifecycles.SQLDataLoaderLifecycle;
030 import org.quartz.Scheduler;
031 import org.quartz.SchedulerException;
032 import org.springframework.beans.factory.BeanFactory;
033 import org.springframework.transaction.PlatformTransactionManager;
034
035 import javax.xml.namespace.QName;
036 import java.util.ArrayList;
037 import java.util.List;
038
039
040 /**
041 * Base test case for KEN that extends RiceTestCase
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044 @BaselineMode(Mode.CLEAR_DB)
045 public abstract class KENTestCase extends BaselineTestCase {
046 private static final String KEN_MODULE_NAME = "ken";
047 private static final String TX_MGR_BEAN_NAME = "transactionManager";
048
049 protected SpringNotificationServiceLocator services;
050 protected PlatformTransactionManager transactionManager;
051
052 public KENTestCase() {
053 super(KEN_MODULE_NAME);
054 }
055
056
057
058 @Override
059 protected List<Lifecycle> getSuiteLifecycles() {
060 List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
061 suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultSuiteTestData.xml"));
062 return suiteLifecycles;
063 }
064
065 @Override
066 protected Lifecycle getLoadApplicationLifecycle() {
067 SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KENTestHarnessApplicationResourceLoader"), "classpath:KENTestHarnessSpringBeans.xml", null);
068 springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
069 return springResourceLoader;
070 }
071
072 @Override
073 protected List<Lifecycle> getPerTestLifecycles() {
074 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
075 lifecycles.add(new ClearCacheLifecycle());
076 lifecycles.addAll(getNotificationPerTestLifecycles());
077 return lifecycles;
078 }
079
080 protected List<Lifecycle> getNotificationPerTestLifecycles() {
081 List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
082 lifecycles.add(new BaseLifecycle() {
083 @Override
084 public void start() throws Exception {
085 // get the composite Rice Spring context
086 BeanFactory moduleContext = CompositeBeanFactory.createBeanFactory(RiceResourceLoaderFactory.getSpringResourceLoaders());
087 // This method sets up the Spring services so that they can be accessed by the tests.
088 services = new SpringNotificationServiceLocator(moduleContext);
089 // grab the module's transaction manager
090 transactionManager = (PlatformTransactionManager) moduleContext.getBean(TX_MGR_BEAN_NAME, PlatformTransactionManager.class);
091 super.start();
092 }
093
094 });
095
096 // clear out the KEW cache
097 lifecycles.add(new BaseLifecycle() {
098 @Override
099 public void start() throws Exception {
100 super.start();
101
102 LOG.info("Status of Ken scheduler on start: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
103 // stop quartz if a test failed to do so
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 // stop quartz if a test failed to do so
111 disableQuartzJobs();
112
113 super.stop();
114 }
115 });
116
117 // load the default SQL
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 * Returns the List of tables that should be cleared on every test run.
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 * Flushes the KEW cache(s)
140 */
141 public class ClearCacheLifecycle extends BaseLifecycle {
142 @Override
143 public void stop() throws Exception {
144 //KsbApiServiceLocator.getCacheAdministrator().flushAll();
145 //KimApiServiceLocator.getIdentityManagementService().flushAllCaches();
146 //KimApiServiceLocator.getRoleService().flushRoleCaches();
147 super.stop();
148 }
149
150 }
151 /**
152 * This method makes sure to disable the Quartz scheduler
153 * @throws SchedulerException
154 */
155 protected void disableQuartzJobs() throws SchedulerException {
156 // do this so that our quartz jobs don't go off - we don't care about
157 // these in our unit tests
158 Scheduler scheduler = services.getScheduler();
159 scheduler.standby();
160 //scheduler.shutdown();
161 }
162
163 /**
164 * This method enables the Quartz scheduler
165 * @throws SchedulerException
166 */
167 protected void enableQuartzJobs() throws SchedulerException {
168 // do this so that our quartz jobs don't go off - we don't care about
169 // these in our unit tests
170 Scheduler scheduler = services.getScheduler();
171 scheduler.start();
172 }
173
174 }