View Javadoc

1   /**
2    * Copyright 2005-2011 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.framework.resourceloader.RiceResourceLoaderFactory;
21  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
22  import org.kuali.rice.ken.core.SpringNotificationServiceLocator;
23  import org.kuali.rice.kew.batch.KEWXmlDataLoader;
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.SQLDataLoader;
29  import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
30  import org.kuali.rice.test.lifecycles.SQLDataLoaderLifecycle;
31  import org.quartz.Scheduler;
32  import org.quartz.SchedulerException;
33  import org.springframework.beans.factory.BeanFactory;
34  import org.springframework.transaction.PlatformTransactionManager;
35  
36  import javax.xml.namespace.QName;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  
41  /**
42   * Base test case for KEN that extends RiceTestCase
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  @BaselineMode(Mode.ROLLBACK_CLEAR_DB)
46  public abstract class KENTestCase extends BaselineTestCase {
47      private static final String KEN_MODULE_NAME = "ken";
48      private static final String TX_MGR_BEAN_NAME = "transactionManager";
49  
50      protected SpringNotificationServiceLocator services;
51      protected PlatformTransactionManager transactionManager;
52  
53      public KENTestCase() {
54          super(KEN_MODULE_NAME);
55      }   
56      
57      
58      
59      @Override
60  	protected List<Lifecycle> getSuiteLifecycles() {
61  		List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
62  		suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultSuiteTestData.xml"));
63  		return suiteLifecycles;
64  	}
65      
66      @Override
67  	protected Lifecycle getLoadApplicationLifecycle() {
68      	SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KENTestHarnessApplicationResourceLoader"), "classpath:KENTestHarnessSpringBeans.xml", null);
69      	springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
70      	return springResourceLoader;
71  	}
72  
73  	@Override
74      protected List<Lifecycle> getPerTestLifecycles() {
75      	List<Lifecycle> lifecycles = super.getPerTestLifecycles();
76      	lifecycles.add(new ClearCacheLifecycle());
77      	lifecycles.addAll(getNotificationPerTestLifecycles());
78      	return lifecycles;
79      }
80      
81  	protected List<Lifecycle> getNotificationPerTestLifecycles() {
82          List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
83          lifecycles.add(new BaseLifecycle() {
84              @Override
85              public void start() throws Exception {
86                  // get the composite Rice Spring context
87                  BeanFactory moduleContext = CompositeBeanFactory.createBeanFactory(
88                          RiceResourceLoaderFactory.getSpringResourceLoaders());
89                  // This method sets up the Spring services so that they can be accessed by the tests.
90                  services = new SpringNotificationServiceLocator(moduleContext);
91                  // grab the module's transaction manager
92                  transactionManager = (PlatformTransactionManager) moduleContext.getBean(TX_MGR_BEAN_NAME, PlatformTransactionManager.class);
93                  super.start();
94              }
95  
96          });
97  
98          // clear out the KEW cache
99          lifecycles.add(new BaseLifecycle() {
100             @Override
101             public void start() throws Exception {
102                 super.start();
103 
104                 LOG.info("Status of Ken scheduler on start: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
105                 // stop quartz if a test failed to do so
106                 disableQuartzJobs();
107             }
108             public void stop() throws Exception {
109                 //KsbApiServiceLocator.getCacheAdministrator().flushAll();
110 
111                 LOG.info("Status of Ken scheduler on stop: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
112                 // stop quartz if a test failed to do so
113                 disableQuartzJobs();
114 
115                 super.stop();
116             }
117         });
118 
119         // load the default SQL
120         //lifecycles.add(new SQLDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.sql", ";"));
121         
122         //lifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.xml"));
123         
124 
125         return lifecycles;
126 
127     }
128 
129     /**
130 	 * By default this loads the "default" data set from the DefaultTestData.sql
131 	 * and DefaultTestData.xml files. Subclasses can override this to change
132 	 * this behaviour
133 	 */
134 	protected void loadDefaultTestData() throws Exception {
135 		// at this point this is constants. loading these through xml import is
136 		// problematic because of cache notification
137 		// issues in certain low level constants.
138 		new SQLDataLoader(
139 				"classpath:org/kuali/rice/ken/test/DefaultPerTestData.sql", ";")
140 				.runSql();
141 
142 		KEWXmlDataLoader.loadXmlClassLoaderResource(KENTestCase.class, "DefaultPerTestData.xml");
143 	}
144     /**
145 	 * Returns the List of tables that should be cleared on every test run.
146 	 */
147     @Override
148 	protected List<String> getPerTestTablesToClear() {
149 		List<String> tablesToClear = new ArrayList<String>();
150 		tablesToClear.add("KREW_.*");
151 		tablesToClear.add("KRSB_.*");
152 		tablesToClear.add("KREN_.*");
153 		return tablesToClear;
154 	}
155 
156     protected void setUpAfterDataLoad() throws Exception {
157 		// override this to load your own test data
158 	}
159 
160     /**
161 	 * Initiates loading of per-test data
162 	 */
163 	@Override
164 	protected void loadPerTestData() throws Exception {
165         loadDefaultTestData();
166 
167 
168 		setUpAfterDataLoad();
169 
170 		final long t4 = System.currentTimeMillis();
171 	}
172 
173     /**
174      * Flushes the KEW cache(s)
175      */
176     public class ClearCacheLifecycle extends BaseLifecycle {
177         @Override
178         public void stop() throws Exception {
179             //KsbApiServiceLocator.getCacheAdministrator().flushAll();
180             //KimApiServiceLocator.getIdentityManagementService().flushAllCaches();
181             //KimApiServiceLocator.getRoleService().flushRoleCaches();
182             super.stop();
183         }
184 
185     }
186     /**
187      * This method makes sure to disable the Quartz scheduler
188      * @throws SchedulerException
189      */
190     protected void disableQuartzJobs() throws SchedulerException {
191         // do this so that our quartz jobs don't go off - we don't care about
192         // these in our unit tests
193         Scheduler scheduler = services.getScheduler();
194         scheduler.standby();
195         //scheduler.shutdown();
196     }
197 
198     /**
199      * This method enables the Quartz scheduler
200      * @throws SchedulerException
201      */
202     protected void enableQuartzJobs() throws SchedulerException {
203         // do this so that our quartz jobs don't go off - we don't care about
204         // these in our unit tests
205         Scheduler scheduler = services.getScheduler();
206         scheduler.start();
207     }
208 
209 }