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.kew.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.SpringResourceLoader;
21  import org.kuali.rice.kew.api.WorkflowRuntimeException;
22  import org.kuali.rice.kew.batch.KEWXmlDataLoader;
23  import org.kuali.rice.kew.service.KEWServiceLocator;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
26  import org.kuali.rice.test.BaselineTestCase;
27  import org.kuali.rice.test.ClearDatabaseLifecycle;
28  import org.kuali.rice.test.SQLDataLoader;
29  import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
30  import org.springframework.cache.CacheManager;
31  import org.springframework.transaction.support.TransactionTemplate;
32  
33  import javax.xml.namespace.QName;
34  import java.io.InputStream;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * Useful superclass for all KEW test cases. Handles setup of test utilities and
40   * a test environment. Configures the Spring test environment providing a
41   * template method for custom context files in test mode. Also provides a
42   * template method for running custom transactional setUp. Tear down handles
43   * automatic tear down of objects created inside the test environment.
44   */
45  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
46  public abstract class KEWTestCase extends BaselineTestCase {
47  
48      private static final String SQL_FILE = "classpath:org/kuali/rice/kew/test/DefaultSuiteTestData.sql";
49  	private static final String XML_FILE = "classpath:org/kuali/rice/kew/test/DefaultSuiteTestData.xml";
50      private static final String KRAD_MODULE_NAME = "kew";
51  
52      public KEWTestCase() {
53  		super(KRAD_MODULE_NAME);
54  	}
55  
56      @Override
57  	protected List<Lifecycle> getSuiteLifecycles() {
58  		List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
59  		suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
60  
61  		return suiteLifecycles;
62  	}
63  
64  	@Override
65  	protected void loadSuiteTestData() throws Exception {
66  		super.loadSuiteTestData();
67          new SQLDataLoader(SQL_FILE, ";").runSql();
68  	}
69  
70  
71      /**
72  	 * By default this loads the "default" data set from the DefaultTestData.sql
73  	 * and DefaultTestData.xml files. Subclasses can override this to change
74  	 * this behaviour
75  	 */
76  	protected void loadDefaultTestData() throws Exception {
77  		// at this point this is constants. loading these through xml import is
78  		// problematic because of cache notification
79  		// issues in certain low level constants.
80  		new SQLDataLoader(
81  				"classpath:org/kuali/rice/kew/test/DefaultPerTestData.sql", ";")
82  				.runSql();
83  
84  		KEWXmlDataLoader.loadXmlClassLoaderResource(KEWTestCase.class,
85  				"DefaultPerTestData.xml");
86  	}
87  
88  	@Override
89  	protected Lifecycle getLoadApplicationLifecycle() {
90          SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KEWTestResourceLoader"), "classpath:org/kuali/rice/kew/config/TestKEWSpringBeans.xml", null);
91      	springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
92      	return springResourceLoader;
93  	}
94  
95      /**
96  	 * Default implementation does nothing. Subclasses should override this
97  	 * method if they want to perform setup work inside of a database
98  	 * transaction.
99  	 */
100 	protected void setUpAfterDataLoad() throws Exception {
101 		// override
102 	}
103 
104 	protected void loadTestData() throws Exception {
105 		// override this to load your own test data
106 	}
107 
108 	protected TransactionTemplate getTransactionTemplate() {
109 		return TestUtilities.getTransactionTemplate();
110 	}
111 
112    /**
113 	 * Initiates loading of per-test data
114 	 */
115 	@Override
116 	protected void loadPerTestData() throws Exception {
117         final long t1 = System.currentTimeMillis();
118         loadDefaultTestData();
119 
120 		final long t2 = System.currentTimeMillis();
121 		loadTestData();
122 
123 		final long t3 = System.currentTimeMillis();
124 		report("Time to load test-specific test data: " + (t3 - t2));
125 
126 		setUpAfterDataLoad();
127 
128 		final long t4 = System.currentTimeMillis();
129 		report("Time to run test-specific setup: " + (t4 - t3));
130 	}
131 
132 	/**
133 	 * Override the standard per-test lifecycles to prepend
134 	 * ClearDatabaseLifecycle and ClearCacheLifecycle
135 	 *
136 	 * @see org.kuali.rice.test.RiceTestCase#getPerTestLifecycles()
137 	 */
138 	@Override
139 	protected List<Lifecycle> getPerTestLifecycles() {
140 		List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
141 		lifecycles.add(new ClearDatabaseLifecycle(getPerTestTablesToClear(),
142 				getPerTestTablesNotToClear()));
143 		lifecycles.add(new ClearCacheLifecycle());
144 		lifecycles.addAll(super.getPerTestLifecycles());
145 		return lifecycles;
146 	}
147 
148 	/**
149 	 * Flushes the KEW cache(s)
150 	 */
151 	public class ClearCacheLifecycle extends BaseLifecycle {
152 		@Override
153 		public void stop() throws Exception {
154             clearCacheManagers(KimImplServiceLocator.getLocalCacheManager(), KEWServiceLocator.getLocalCacheManager());
155 			super.stop();
156 		}
157 	}
158 
159     protected void clearCacheManagers(CacheManager... cacheManagers) {
160         for (CacheManager cacheManager : cacheManagers) {
161             for (String cacheName : cacheManager.getCacheNames()) {
162                 LOG.info("Clearing cache: " + cacheName);
163                 cacheManager.getCache(cacheName).clear();
164             }
165         }
166     }
167 
168 	/**
169 	 * Returns the List of tables that should be cleared on every test run.
170 	 */
171 	protected List<String> getPerTestTablesToClear() {
172 		List<String> tablesToClear = new ArrayList<String>();
173 		tablesToClear.add("KREW_.*");
174 		tablesToClear.add("KRSB_.*");
175 		tablesToClear.add("KREN_.*");
176         tablesToClear.add("KRMS_.*");
177 		return tablesToClear;
178 	}
179 
180 	protected List<String> getPerTestTablesNotToClear() {
181 		return new ArrayList<String>();
182 	}
183 
184 	protected void loadXmlFile(String fileName) {
185 		try {
186 			KEWXmlDataLoader.loadXmlClassLoaderResource(getClass(), fileName);
187 		} catch (Exception e) {
188 			throw new WorkflowRuntimeException(e);
189 		}
190 	}
191 
192 	protected void loadXmlFile(Class clazz, String fileName) {
193 		try {
194 			KEWXmlDataLoader.loadXmlClassLoaderResource(clazz, fileName);
195 		} catch (Exception e) {
196 			throw new WorkflowRuntimeException(e);
197 		}
198 	}
199 
200 	protected void loadXmlFileFromFileSystem(String fileName) {
201 		try {
202 			KEWXmlDataLoader.loadXmlFile(fileName);
203 		} catch (Exception e) {
204 			throw new WorkflowRuntimeException(e);
205 		}
206 	}
207 
208 	protected void loadXmlStream(InputStream xmlStream) {
209 		try {
210 			KEWXmlDataLoader.loadXmlStream(xmlStream);
211 		} catch (Exception e) {
212 			throw new WorkflowRuntimeException(e);
213 		}
214 	}
215 
216 	protected String getPrincipalIdForName(String principalName) {
217 		return KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName).getPrincipalId();
218 	}
219 
220 	protected String getPrincipalNameForId(String principalId) {
221 		return KimApiServiceLocator.getIdentityService().getPrincipal(principalId).getPrincipalName();
222 	}
223 
224 	protected String getGroupIdForName(String namespace, String groupName) {
225 		return KimApiServiceLocator.getGroupService().getGroupByNameAndNamespaceCode(namespace, groupName).getId();
226 	}
227 }