View Javadoc

1   /**
2    * Copyright 2004-2014 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.hr;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.kuali.kpme.core.rice.test.lifecycles.KPMEXmlDataLoaderLifecycle;
25  import org.kuali.kpme.core.util.ClearDatabaseLifecycle;
26  import org.kuali.kpme.core.util.DatabaseCleanupDataLifecycle;
27  import org.kuali.kpme.core.util.HrContext;
28  import org.kuali.kpme.core.util.LoadDatabaseDataLifeCycle;
29  import org.kuali.rice.core.api.config.property.Config;
30  import org.kuali.rice.core.api.config.property.ConfigContext;
31  import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
32  import org.kuali.rice.core.api.lifecycle.Lifecycle;
33  import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
34  import org.kuali.rice.krad.UserSession;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.MessageMap;
37  import org.kuali.rice.test.RiceInternalSuiteDataTestCase;
38  import org.kuali.rice.test.TransactionalLifecycle;
39  import org.kuali.rice.test.lifecycles.JettyServerLifecycle;
40  import org.kuali.rice.test.lifecycles.JettyServerLifecycle.ConfigMode;
41  import org.openqa.selenium.WebDriver;
42  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
43  import org.openqa.selenium.remote.CapabilityType;
44  import org.openqa.selenium.remote.DesiredCapabilities;
45  import org.springframework.cache.CacheManager;
46  
47  import com.gargoylesoftware.htmlunit.BrowserVersion;
48  
49  public abstract class KPMESeleniumTestCase extends RiceInternalSuiteDataTestCase {
50  
51  	private static final String FILE_PREFIX = System.getProperty("user.dir") + "/../db/src/main/config/workflow/";
52  
53  	private static final String RELATIVE_WEBAPP_ROOT = "/src/main/webapp";
54  	
55  	private TransactionalLifecycle transactionalLifecycle;
56      private WebDriver driver;
57  	
58  	@Override
59  	protected String getModuleName() {
60  		return "kpme";
61  	}
62  
63  	@Override
64  	public void setUp() throws Exception {
65  	    if (System.getProperty("basedir") == null) {
66  	        System.setProperty("basedir", System.getProperty("user.dir") + "/");
67  	    }
68  	    
69  		super.setUp();
70  
71  		GlobalVariables.setMessageMap(new MessageMap());
72  		
73  		final boolean needsSpring = false;
74  		if (needsSpring) {
75  			transactionalLifecycle = new TransactionalLifecycle();
76  			//transactionalLifecycle.setTransactionManager(KRADServiceLocatorInternal.getTransactionManager());
77  			transactionalLifecycle.start();
78  		}
79  
80  	    new ClearDatabaseLifecycle().start();
81  	
82  		new LoadDatabaseDataLifeCycle(this.getClass()).start();
83  	
84  	    //lets try to create a user session
85  	    GlobalVariables.setUserSession(new UserSession("admin"));
86  
87          DesiredCapabilities capabilities = new DesiredCapabilities();
88          capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
89          capabilities.setCapability(CapabilityType.BROWSER_NAME, BrowserVersion.FIREFOX_17);
90          driver = new HtmlUnitDriver(capabilities);
91  	}
92  
93  	@Override
94  	public void tearDown() throws Exception {
95  	    // runs custom SQL at the end of each test.
96  	    // useful for difficult to reset test additions, not handled by
97  	    // our ClearDatabaseLifecycle.
98          HrContext.clearTargetUser();
99  
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 }