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.kim.test; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import javax.xml.namespace.QName; 022 023import org.junit.Test; 024import org.junit.runner.RunWith; 025import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 026import org.kuali.rice.core.api.lifecycle.BaseLifecycle; 027import org.kuali.rice.core.api.lifecycle.Lifecycle; 028import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader; 029import org.kuali.rice.core.impl.services.CoreImplServiceLocator; 030import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory; 031import org.kuali.rice.test.BaselineTestCase; 032import org.kuali.rice.test.BaselineTestCase.BaselineMode; 033import org.kuali.rice.test.BaselineTestCase.Mode; 034import org.kuali.rice.test.SQLDataLoader; 035import org.kuali.rice.test.TestHarnessServiceLocator; 036import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle; 037import org.kuali.rice.test.runners.BootstrapTest; 038import org.kuali.rice.test.runners.LoadTimeWeavableTestRunner; 039import org.springframework.cache.Cache; 040import org.springframework.cache.CacheManager; 041 042/** 043 * This is test base that should be used for all KIM unit tests. All non-web unit tests for KIM should extend this base 044 * class. 045 * 046 * @author Kuali Rice Team (rice.collab@kuali.org) 047 */ 048@BaselineMode(Mode.ROLLBACK_CLEAR_DB) 049@RunWith(LoadTimeWeavableTestRunner.class) 050@BootstrapTest(KIMTestCase.BootstrapTest.class) 051public abstract class KIMTestCase extends BaselineTestCase { 052 053 private static final String KIM_MODULE_NAME = "kim"; 054 055 public KIMTestCase() { 056 super(KIM_MODULE_NAME); 057 } 058 059 @Override 060 protected List<Lifecycle> getSuiteLifecycles() { 061 List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles(); 062 suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.xml")); 063 return suiteLifecycles; 064 } 065 066 @Override 067 protected void loadSuiteTestData() throws Exception { 068 super.loadSuiteTestData(); 069 new SQLDataLoader("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.sql", "/").runSql(); 070 new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularRolesTestData.sql", "/").runSql(); 071 new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularGroupsTestData.sql", "/").runSql(); 072 new SQLDataLoader("classpath:org/kuali/rice/kim/test/DefaultSuiteLDAPTestData.sql", "/").runSql(); 073 } 074 075 @Override 076 protected Lifecycle getLoadApplicationLifecycle() { 077 SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KIMTestHarnessApplicationResourceLoader"), "classpath:KIMTestHarnessSpringBeans.xml", null); 078 springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader()); 079 return springResourceLoader; 080 } 081 082 /** 083 * Override the standard per-test lifecycles to prepend ClearDatabaseLifecycle and ClearCacheLifecycle 084 * @see org.kuali.rice.test.RiceTestCase#getPerTestLifecycles() 085 */ 086 @Override 087 protected List<Lifecycle> getPerTestLifecycles() { 088 List<Lifecycle> lifecycles = super.getPerTestLifecycles(); 089 lifecycles.add(new ClearCacheLifecycle()); 090 return lifecycles; 091 } 092 093 public class ClearCacheLifecycle extends BaseLifecycle { 094 @Override 095 public void stop() throws Exception { 096 //KimApiServiceLocator.getIdentityManagementService().flushAllCaches(); 097 //KimApiServiceLocator.getRoleService().flushRoleCaches(); 098 super.stop(); 099 } 100 101 } 102 103 @Override 104 protected List<String> getPerTestTablesNotToClear() { 105 List<String> tablesNotToClear = new ArrayList<String>(); 106 tablesNotToClear.add("KRIM_.*"); 107 tablesNotToClear.add("KRNS_.*"); 108 tablesNotToClear.add("KRCR_.*"); 109 tablesNotToClear.add("KREW_.*"); 110 return tablesNotToClear; 111 } 112 113 114 /** 115 * @see org.kuali.rice.test.RiceTestCase#getModuleName() 116 */ 117 @Override 118 protected String getModuleName() { 119 return KIM_MODULE_NAME; 120 } 121 122 protected String getNextSequenceStringValue(String sequenceName) { 123 return MaxValueIncrementerFactory.getIncrementer(TestHarnessServiceLocator.getDataSource(), sequenceName).nextStringValue(); 124 } 125 126 public static final class BootstrapTest extends KIMTestCase { 127 @Test 128 public void bootstrapTest() {}; 129 } 130 131 public void clearNamedCache(String cacheName) { 132 133 try { 134 CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(cacheName); 135 if ( cm != null ) { 136 Cache cache = cm.getCache(cacheName); 137 if (cache != null) { 138 cache.clear(); 139 if ( LOG.isDebugEnabled() ) { 140 LOG.debug( "Cleared " + cacheName + " cache." ); 141 } 142 } else { 143 // this is at debug level intentionally, since not all BOs have caches 144 LOG.debug( "Unable to find cache for " + cacheName + "."); 145 } 146 } else { 147 LOG.info( "Unable to find cache manager when attempting to clear " + cacheName ); 148 } 149 } catch (RiceIllegalArgumentException e) { 150 LOG.info( "Cache manager not found when attempting to clear " + cacheName ); 151 } 152 153 } 154}