001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr; 017 018 import java.io.File; 019 import java.util.ArrayList; 020 import java.util.Arrays; 021 import java.util.List; 022 023 import org.apache.log4j.Logger; 024 import org.kuali.kpme.core.rice.test.lifecycles.KPMEXmlDataLoaderLifecycle; 025 import org.kuali.kpme.core.util.ClearDatabaseLifecycle; 026 import org.kuali.kpme.core.util.DatabaseCleanupDataLifecycle; 027 import org.kuali.kpme.core.util.HrContext; 028 import org.kuali.kpme.core.util.LoadDatabaseDataLifeCycle; 029 import org.kuali.rice.core.api.config.property.Config; 030 import org.kuali.rice.core.api.config.property.ConfigContext; 031 import org.kuali.rice.core.api.lifecycle.BaseLifecycle; 032 import org.kuali.rice.core.api.lifecycle.Lifecycle; 033 import org.kuali.rice.core.impl.services.CoreImplServiceLocator; 034 import org.kuali.rice.krad.UserSession; 035 import org.kuali.rice.krad.util.GlobalVariables; 036 import org.kuali.rice.krad.util.MessageMap; 037 import org.kuali.rice.test.RiceInternalSuiteDataTestCase; 038 import org.kuali.rice.test.TransactionalLifecycle; 039 import org.kuali.rice.test.lifecycles.JettyServerLifecycle; 040 import org.kuali.rice.test.lifecycles.JettyServerLifecycle.ConfigMode; 041 import org.openqa.selenium.WebDriver; 042 import org.openqa.selenium.htmlunit.HtmlUnitDriver; 043 import org.openqa.selenium.remote.CapabilityType; 044 import org.openqa.selenium.remote.DesiredCapabilities; 045 import org.springframework.cache.CacheManager; 046 047 import com.gargoylesoftware.htmlunit.BrowserVersion; 048 049 public abstract class KPMESeleniumTestCase extends RiceInternalSuiteDataTestCase { 050 051 private static final String FILE_PREFIX = System.getProperty("user.dir") + "/../db/src/main/config/workflow/"; 052 053 private static final String RELATIVE_WEBAPP_ROOT = "/src/main/webapp"; 054 055 private TransactionalLifecycle transactionalLifecycle; 056 private WebDriver driver; 057 058 @Override 059 protected String getModuleName() { 060 return "kpme"; 061 } 062 063 @Override 064 public void setUp() throws Exception { 065 if (System.getProperty("basedir") == null) { 066 System.setProperty("basedir", System.getProperty("user.dir") + "/"); 067 } 068 069 super.setUp(); 070 071 GlobalVariables.setMessageMap(new MessageMap()); 072 073 final boolean needsSpring = false; 074 if (needsSpring) { 075 transactionalLifecycle = new TransactionalLifecycle(); 076 //transactionalLifecycle.setTransactionManager(KRADServiceLocatorInternal.getTransactionManager()); 077 transactionalLifecycle.start(); 078 } 079 080 new ClearDatabaseLifecycle().start(); 081 082 new LoadDatabaseDataLifeCycle(this.getClass()).start(); 083 084 //lets try to create a user session 085 GlobalVariables.setUserSession(new UserSession("admin")); 086 087 DesiredCapabilities capabilities = new DesiredCapabilities(); 088 capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true); 089 capabilities.setCapability(CapabilityType.BROWSER_NAME, BrowserVersion.FIREFOX_17); 090 driver = new HtmlUnitDriver(capabilities); 091 } 092 093 @Override 094 public void tearDown() throws Exception { 095 // runs custom SQL at the end of each test. 096 // useful for difficult to reset test additions, not handled by 097 // our ClearDatabaseLifecycle. 098 HrContext.clearTargetUser(); 099 100 new DatabaseCleanupDataLifecycle(this.getClass()).start(); 101 102 final boolean needsSpring = true; 103 if (needsSpring) { 104 if ( (transactionalLifecycle != null) && (transactionalLifecycle.isStarted()) ) { 105 transactionalLifecycle.stop(); 106 } 107 } 108 109 GlobalVariables.setMessageMap(new MessageMap()); 110 111 super.tearDown(); 112 } 113 114 @Override 115 protected List<Lifecycle> getPerTestLifecycles() { 116 List<Lifecycle> lifecycles = super.getPerTestLifecycles(); 117 lifecycles.add(new ClearCacheLifecycle()); 118 return lifecycles; 119 } 120 121 @Override 122 protected List<Lifecycle> getSuiteLifecycles() { 123 List<Lifecycle> lifecycles = super.getPerTestLifecycles(); 124 lifecycles.add(new Lifecycle() { 125 boolean started = false; 126 127 public boolean isStarted() { 128 return this.started; 129 } 130 131 public void start() throws Exception { 132 setModuleName(getModuleName()); 133 setBaseDirSystemProperty(getModuleName()); 134 Config config = getTestHarnessConfig(); 135 ConfigContext.init(config); 136 this.started = true; 137 } 138 139 public void stop() throws Exception { 140 this.started = false; 141 } 142 }); 143 /** 144 * Loads the TestHarnessSpringBeans.xml file which obtains connections to the DB for us 145 */ 146 /* lifecycles.add(getTestHarnessSpringResourceLoader());*/ 147 148 /** 149 * Establishes the TestHarnessServiceLocator so that it has a reference to the Spring context 150 * created from TestHarnessSpringBeans.xml 151 */ 152 /* lifecycles.add(new BaseLifecycle() { 153 @Override 154 public void start() throws Exception { 155 TestHarnessServiceLocator.setContext(getTestHarnessSpringResourceLoader().getContext()); 156 super.start(); 157 } 158 });*/ 159 160 lifecycles.add(new Lifecycle() { 161 private JettyServerLifecycle jettyServerLifecycle; 162 163 public boolean isStarted() { 164 return jettyServerLifecycle.isStarted(); 165 } 166 167 public void start() throws Exception { 168 System.setProperty("web.bootstrap.spring.file", "classpath:TestHarnessSpringBeans.xml"); 169 jettyServerLifecycle = new JettyServerLifecycle(getPort(), getContext(), RELATIVE_WEBAPP_ROOT); 170 jettyServerLifecycle.setConfigMode(ConfigMode.OVERRIDE); 171 jettyServerLifecycle.start(); 172 } 173 174 public void stop() throws Exception { 175 this.jettyServerLifecycle.stop(); 176 } 177 }); 178 179 ClearDatabaseLifecycle clearDatabaseLifecycle = new ClearDatabaseLifecycle(); 180 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_T"); 181 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_RSP_T"); 182 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_DLGN_RSP_T"); 183 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_ATTR_T"); 184 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_TMPL_T"); 185 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_DOC_TYP_T"); 186 lifecycles.add(clearDatabaseLifecycle); 187 188 File[] files = new File(FILE_PREFIX).listFiles(); 189 if (files != null) { 190 Arrays.sort(files); 191 for (File file : files) { 192 if (file.getName().endsWith(".xml")) { 193 lifecycles.add(new KPMEXmlDataLoaderLifecycle(FILE_PREFIX + file.getName())); 194 } 195 } 196 } 197 return lifecycles; 198 } 199 200 public class ClearCacheLifecycle extends BaseLifecycle { 201 private final Logger LOG = Logger.getLogger(ClearCacheLifecycle.class); 202 203 @Override 204 public void start() throws Exception { 205 long startTime = System.currentTimeMillis(); 206 LOG.info("Starting cache flushing"); 207 List<CacheManager> cms = new ArrayList<CacheManager>(CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagers()); 208 for (CacheManager cm : cms) { 209 for (String cacheName : cm.getCacheNames()) { 210 //LOG.info("Clearing cache: " + cacheName); 211 cm.getCache(cacheName).clear(); 212 } 213 } 214 long endTime = System.currentTimeMillis(); 215 LOG.info("Caches cleared in " + (endTime - startTime) + "ms"); 216 } 217 218 @Override 219 public void stop() throws Exception { 220 super.stop(); 221 } 222 223 } 224 225 public static String getBaseURL() { 226 return ConfigContext.getCurrentContextConfig().getProperty("application.url"); 227 } 228 229 public static String getContext() { 230 return "/" + ConfigContext.getCurrentContextConfig().getProperty("app.context.name"); 231 } 232 233 public static String getTempDir() { 234 return ConfigContext.getCurrentContextConfig().getProperty("temp.dir"); 235 } 236 237 public static Integer getPort() { 238 return new Integer(ConfigContext.getCurrentContextConfig().getProperty("kns.test.port")); 239 } 240 241 }