View Javadoc

1   /*
2    * Copyright 2007 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.ken.test;
17  
18  import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
19  import org.kuali.rice.core.api.lifecycle.Lifecycle;
20  import org.kuali.rice.core.impl.resourceloader.RiceResourceLoaderFactory;
21  import org.kuali.rice.core.impl.resourceloader.SpringResourceLoader;
22  import org.kuali.rice.ken.core.SpringNotificationServiceLocator;
23  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
24  import org.kuali.rice.test.BaselineTestCase;
25  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
26  import org.kuali.rice.test.BaselineTestCase.Mode;
27  import org.kuali.rice.test.CompositeBeanFactory;
28  import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
29  import org.kuali.rice.test.lifecycles.SQLDataLoaderLifecycle;
30  import org.quartz.Scheduler;
31  import org.quartz.SchedulerException;
32  import org.springframework.beans.factory.BeanFactory;
33  import org.springframework.transaction.PlatformTransactionManager;
34  
35  import javax.xml.namespace.QName;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  
40  /**
41   * Base test case for KEN that extends RiceTestCase
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  @BaselineMode(Mode.CLEAR_DB)
45  public abstract class KENTestCase extends BaselineTestCase {
46      private static final String KEN_MODULE_NAME = "ken";
47      private static final String TX_MGR_BEAN_NAME = "transactionManager";
48  
49      protected SpringNotificationServiceLocator services;
50      protected PlatformTransactionManager transactionManager;
51  
52      public KENTestCase() {
53          super(KEN_MODULE_NAME);
54      }   
55      
56      
57      
58      @Override
59  	protected List<Lifecycle> getSuiteLifecycles() {
60  		List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
61  		suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultSuiteTestData.xml"));
62  		return suiteLifecycles;
63  	}
64      
65      @Override
66  	protected Lifecycle getLoadApplicationLifecycle() {
67      	SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KENTestHarnessApplicationResourceLoader"), "classpath:KENTestHarnessSpringBeans.xml", null);
68      	springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
69      	return springResourceLoader;
70  	}
71  
72  	@Override
73      protected List<Lifecycle> getPerTestLifecycles() {
74      	List<Lifecycle> lifecycles = super.getPerTestLifecycles();
75      	lifecycles.add(new ClearCacheLifecycle());
76      	lifecycles.addAll(getNotificationPerTestLifecycles());
77      	return lifecycles;
78      }
79      
80  	protected List<Lifecycle> getNotificationPerTestLifecycles() {
81          List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
82          lifecycles.add(new BaseLifecycle() {
83              @Override
84              public void start() throws Exception {
85                  // get the composite Rice Spring context
86                  BeanFactory moduleContext = CompositeBeanFactory.createBeanFactory(RiceResourceLoaderFactory.getSpringResourceLoaders());
87                  // This method sets up the Spring services so that they can be accessed by the tests.
88                  services = new SpringNotificationServiceLocator(moduleContext);
89                  // grab the module's transaction manager
90                  transactionManager = (PlatformTransactionManager) moduleContext.getBean(TX_MGR_BEAN_NAME, PlatformTransactionManager.class);
91                  super.start();
92              }
93  
94          });
95  
96          // clear out the KEW cache
97          lifecycles.add(new BaseLifecycle() {
98              @Override
99              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 }