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