View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.test;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
26  import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
27  import org.kuali.rice.core.api.lifecycle.Lifecycle;
28  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
29  import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
30  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
31  import org.kuali.rice.test.BaselineTestCase;
32  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
33  import org.kuali.rice.test.BaselineTestCase.Mode;
34  import org.kuali.rice.test.SQLDataLoader;
35  import org.kuali.rice.test.TestHarnessServiceLocator;
36  import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
37  import org.kuali.rice.test.runners.BootstrapTest;
38  import org.kuali.rice.test.runners.LoadTimeWeavableTestRunner;
39  import org.springframework.cache.Cache;
40  import org.springframework.cache.CacheManager;
41  
42  /**
43   * This is test base that should be used for all KIM unit tests. All non-web unit tests for KIM should extend this base
44   * class.
45   *
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   */
48  @BaselineMode(Mode.ROLLBACK_CLEAR_DB)
49  @RunWith(LoadTimeWeavableTestRunner.class)
50  @BootstrapTest(KIMTestCase.BootstrapTest.class)
51  public abstract class KIMTestCase extends BaselineTestCase {
52  
53  	private static final String KIM_MODULE_NAME = "kim";
54  
55  	public KIMTestCase() {
56  		super(KIM_MODULE_NAME);
57  	}
58  
59  	@Override
60  	protected List<Lifecycle> getSuiteLifecycles() {
61  		List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
62  		suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.xml"));
63  		return suiteLifecycles;
64  	}
65  
66  	@Override
67  	protected void loadSuiteTestData() throws Exception {
68  		super.loadSuiteTestData();
69  		new SQLDataLoader("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.sql", "/").runSql();
70  		new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularRolesTestData.sql", "/").runSql();
71  		new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularGroupsTestData.sql", "/").runSql();
72          new SQLDataLoader("classpath:org/kuali/rice/kim/test/DefaultSuiteLDAPTestData.sql", "/").runSql();
73  	}
74  
75  	@Override
76  	protected Lifecycle getLoadApplicationLifecycle() {
77      	SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KIMTestHarnessApplicationResourceLoader"), "classpath:KIMTestHarnessSpringBeans.xml", null);
78      	springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
79      	return springResourceLoader;
80  	}
81  
82  	/**
83  	 * Override the standard per-test lifecycles to prepend ClearDatabaseLifecycle and ClearCacheLifecycle
84  	 * @see org.kuali.rice.test.RiceTestCase#getPerTestLifecycles()
85  	 */
86  	@Override
87  	protected List<Lifecycle> getPerTestLifecycles() {
88  		List<Lifecycle> lifecycles = super.getPerTestLifecycles();
89  		lifecycles.add(new ClearCacheLifecycle());
90  		return lifecycles;
91  	}
92  
93  	public class ClearCacheLifecycle extends BaseLifecycle {
94  		@Override
95  		public void stop() throws Exception {
96  			//KimApiServiceLocator.getIdentityManagementService().flushAllCaches();
97  			//KimApiServiceLocator.getRoleService().flushRoleCaches();
98  			super.stop();
99  		}
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 }