View Javadoc

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