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 java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.kuali.rice.core.lifecycle.BaseLifecycle;
24  import org.kuali.rice.core.lifecycle.Lifecycle;
25  import org.kuali.rice.core.resourceloader.RiceResourceLoaderFactory;
26  import org.kuali.rice.core.resourceloader.SpringResourceLoader;
27  import org.kuali.rice.ken.core.SpringNotificationServiceLocator;
28  import org.kuali.rice.kew.batch.KEWXmlDataLoaderLifecycle;
29  import org.kuali.rice.kew.service.KEWServiceLocator;
30  import org.kuali.rice.kim.service.KIMServiceLocator;
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.lifecycles.SQLDataLoaderLifecycle;
35  import org.quartz.Scheduler;
36  import org.quartz.SchedulerException;
37  import org.springframework.context.ConfigurableApplicationContext;
38  import org.springframework.transaction.PlatformTransactionManager;
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.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");
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                  ConfigurableApplicationContext moduleContext = RiceResourceLoaderFactory.getSpringResourceLoader().getContext();
88                  // This method sets up the Spring services so that they can be accessed by the tests.
89                  services = new SpringNotificationServiceLocator(moduleContext);
90                  // grab the module's transaction manager
91                  transactionManager = (PlatformTransactionManager) moduleContext.getBean(TX_MGR_BEAN_NAME, PlatformTransactionManager.class);
92                  super.start();
93              }
94  
95          });
96  
97          // clear out the KEW cache
98          lifecycles.add(new BaseLifecycle() {
99              @Override
100             public void start() throws Exception {
101                 super.start();
102 
103                 LOG.info("Status of Ken scheduler on start: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
104                 // stop quartz if a test failed to do so
105                 disableQuartzJobs();
106             }
107             public void stop() throws Exception {
108                 KEWServiceLocator.getCacheAdministrator().flushAll();
109 
110                 LOG.info("Status of Ken scheduler on stop: " + (services.getScheduler().isStarted() ? "started" : "stopped"));
111                 // stop quartz if a test failed to do so
112                 disableQuartzJobs();
113 
114                 super.stop();
115             }
116         });
117 
118         // load the default SQL
119         lifecycles.add(new SQLDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.sql", ";"));
120         
121         lifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/ken/test/DefaultPerTestData.xml"));
122         
123 
124         return lifecycles;
125 
126     }
127     
128     /**
129 	 * Returns the List of tables that should be cleared on every test run.
130 	 */
131 	protected List<String> getPerTestTablesToClear() {
132 		List<String> tablesToClear = new ArrayList<String>();
133 		tablesToClear.add("KREW_.*");
134 		tablesToClear.add("KRSB_.*");
135 		tablesToClear.add("KREN_.*");
136 		return tablesToClear;
137 	}
138 
139     /**
140      * Flushes the KEW cache(s)
141      */
142     public class ClearCacheLifecycle extends BaseLifecycle {
143         @Override
144         public void stop() throws Exception {
145             KEWServiceLocator.getCacheAdministrator().flushAll();
146             KIMServiceLocator.getIdentityManagementService().flushAllCaches();
147             KIMServiceLocator.getRoleManagementService().flushRoleCaches();
148             super.stop();
149         }
150 
151     }
152     /**
153      * This method makes sure to disable the Quartz scheduler
154      * @throws SchedulerException
155      */
156     protected void disableQuartzJobs() throws SchedulerException {
157         // do this so that our quartz jobs don't go off - we don't care about
158         // these in our unit tests
159         Scheduler scheduler = services.getScheduler();
160         scheduler.standby();
161         //scheduler.shutdown();
162     }
163 
164     /**
165      * This method enables the Quartz scheduler
166      * @throws SchedulerException
167      */
168     protected void enableQuartzJobs() throws SchedulerException {
169         // do this so that our quartz jobs don't go off - we don't care about
170         // these in our unit tests
171         Scheduler scheduler = services.getScheduler();
172         scheduler.start();
173     }
174 
175 }