1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.test;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.Calendar;
21 import java.util.List;
22
23 import com.gargoylesoftware.htmlunit.BrowserVersion;
24 import com.gargoylesoftware.htmlunit.WebClient;
25 import com.gargoylesoftware.htmlunit.html.*;
26 import org.apache.log4j.Logger;
27 import org.junit.Assert;
28 import org.kuali.hr.time.test.HtmlUnitUtil;
29 import org.kuali.hr.time.test.TkTestConstants;
30 import org.kuali.hr.time.util.*;
31 import org.kuali.rice.core.api.config.property.Config;
32 import org.kuali.rice.core.api.config.property.ConfigContext;
33 import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
34 import org.kuali.rice.core.api.lifecycle.Lifecycle;
35 import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
36 import org.kuali.rice.krad.UserSession;
37 import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
38 import org.kuali.rice.krad.util.GlobalVariables;
39 import org.kuali.rice.krad.util.MessageMap;
40 import org.kuali.rice.test.RiceInternalSuiteDataTestCase;
41 import org.kuali.rice.test.TransactionalLifecycle;
42 import org.kuali.rice.test.lifecycles.JettyServerLifecycle;
43 import org.kuali.rice.test.lifecycles.JettyServerLifecycle.ConfigMode;
44 import org.kuali.rice.test.lifecycles.KPMEXmlDataLoaderLifecycle;
45 import org.springframework.cache.CacheManager;
46 import org.springframework.mock.web.MockHttpServletRequest;
47
48
49
50
51 public abstract class KPMETestCase extends RiceInternalSuiteDataTestCase {
52
53 private static final String FILE_PREFIX = System.getProperty("user.dir") + "/src/main/config/workflow/";
54
55 private static final String RELATIVE_WEBAPP_ROOT = "/src/main/webapp";
56
57 private TransactionalLifecycle transactionalLifecycle;
58 private WebClient webClient;
59
60 @Override
61 protected String getModuleName() {
62 return "kpme";
63 }
64
65 @Override
66 public void setUp() throws Exception {
67 if (System.getProperty("basedir") == null) {
68 System.setProperty("basedir", System.getProperty("user.dir") + "/");
69 }
70
71 super.setUp();
72
73 GlobalVariables.setMessageMap(new MessageMap());
74
75 final boolean needsSpring = false;
76 if (needsSpring) {
77 transactionalLifecycle = new TransactionalLifecycle();
78 transactionalLifecycle.setTransactionManager(KRADServiceLocatorInternal.getTransactionManager());
79 transactionalLifecycle.start();
80 }
81
82 new ClearDatabaseLifecycle().start();
83
84 new LoadDatabaseDataLifeCycle(this.getClass()).start();
85
86
87 GlobalVariables.setUserSession(new UserSession("admin"));
88 TKContext.setHttpServletRequest(new MockHttpServletRequest());
89 setWebClient(new WebClient(BrowserVersion.FIREFOX_10));
90 getWebClient().setJavaScriptEnabled(true);
91 getWebClient().setTimeout(0);
92 }
93
94 @Override
95 public void tearDown() throws Exception {
96
97
98
99 TKUser.clearTargetUser();
100 getWebClient().closeAllWindows();
101 new DatabaseCleanupDataLifecycle(this.getClass()).start();
102
103 final boolean needsSpring = true;
104 if (needsSpring) {
105 if ( (transactionalLifecycle != null) && (transactionalLifecycle.isStarted()) ) {
106 transactionalLifecycle.stop();
107 }
108 }
109
110 GlobalVariables.setMessageMap(new MessageMap());
111
112 super.tearDown();
113 }
114
115 @Override
116 protected List<Lifecycle> getPerTestLifecycles() {
117 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
118 lifecycles.add(new ClearCacheLifecycle());
119 return lifecycles;
120 }
121
122 @Override
123 protected List<Lifecycle> getSuiteLifecycles() {
124 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
125 lifecycles.add(new Lifecycle() {
126 boolean started = false;
127
128 public boolean isStarted() {
129 return this.started;
130 }
131
132 public void start() throws Exception {
133 setModuleName(getModuleName());
134 setBaseDirSystemProperty(getModuleName());
135 Config config = getTestHarnessConfig();
136 ConfigContext.init(config);
137 this.started = true;
138 }
139
140 public void stop() throws Exception {
141 this.started = false;
142 }
143 });
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 lifecycles.add(new Lifecycle() {
162 private JettyServerLifecycle jettyServerLifecycle;
163
164 public boolean isStarted() {
165 return jettyServerLifecycle.isStarted();
166 }
167
168 public void start() throws Exception {
169 System.setProperty("web.bootstrap.spring.file", "classpath:TestHarnessSpringBeans.xml");
170 jettyServerLifecycle = new JettyServerLifecycle(HtmlUnitUtil.getPort(), HtmlUnitUtil.getContext(), RELATIVE_WEBAPP_ROOT);
171 jettyServerLifecycle.setConfigMode(ConfigMode.OVERRIDE);
172 jettyServerLifecycle.start();
173 }
174
175 public void stop() throws Exception {
176 this.jettyServerLifecycle.stop();
177 }
178 });
179
180 ClearDatabaseLifecycle clearDatabaseLifecycle = new ClearDatabaseLifecycle();
181 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_T");
182 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_RSP_T");
183 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_DLGN_RSP_T");
184 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_ATTR_T");
185 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_RULE_TMPL_T");
186 clearDatabaseLifecycle.getAlternativeTablesToClear().add("KREW_DOC_TYP_T");
187 lifecycles.add(clearDatabaseLifecycle);
188
189 File[] files = new File(FILE_PREFIX).listFiles();
190 if (files != null) {
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 protected final void setFieldValue(HtmlPage page, String fieldId, String fieldValue) {
201 HtmlElement element = page.getHtmlElementById(fieldId);
202 Assert.assertTrue("element " + fieldId + " is null, page: " + page.asText(), element != null);
203
204 if (element instanceof HtmlTextInput) {
205 HtmlTextInput textField = (HtmlTextInput) element;
206 textField.setValueAttribute(fieldValue);
207 } else if (element instanceof HtmlTextArea) {
208 HtmlTextArea textAreaField = (HtmlTextArea) element;
209 textAreaField.setText(fieldValue);
210 } else if (element instanceof HtmlHiddenInput) {
211 HtmlHiddenInput hiddenField = (HtmlHiddenInput) element;
212 hiddenField.setValueAttribute(fieldValue);
213 } else if (element instanceof HtmlSelect) {
214 HtmlSelect selectField = (HtmlSelect) element;
215 try {
216 selectField.setSelectedAttribute(fieldValue, true);
217 } catch (IllegalArgumentException e) {
218 Assert.fail("select element [" + element.asText() + "] " + e.getMessage());
219 }
220 } else if (element instanceof HtmlCheckBoxInput) {
221 HtmlCheckBoxInput checkboxField = (HtmlCheckBoxInput) element;
222 if (fieldValue.equals("on")) {
223 checkboxField.setChecked(true);
224 } else if (fieldValue.equals("off")) {
225 checkboxField.setChecked(false);
226 } else {
227 Assert.assertTrue("Invalid checkbox value", false);
228 }
229 } else if (element instanceof HtmlFileInput) {
230 HtmlFileInput fileInputField = (HtmlFileInput) element;
231 fileInputField.setValueAttribute(fieldValue);
232 } else if (element instanceof HtmlRadioButtonInput) {
233 HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) element;
234 if (fieldValue.equals("on")) {
235 radioButton.setChecked(true);
236 } else if (fieldValue.equals("off")) {
237 radioButton.setChecked(false);
238 }
239 } else {
240 Assert.fail("Unknown control field: " + fieldId);
241 }
242 }
243
244 public void futureEffectiveDateValidation(String baseUrl) throws Exception {
245 HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
246 Assert.assertNotNull(page);
247
248 HtmlForm form = page.getFormByName("KualiForm");
249 Assert.assertNotNull("Search form was missing from page.", form);
250
251 setFieldValue(page, "document.newMaintainableObject.effectiveDate", "04/01/2011");
252 HtmlInput input = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
253 Assert.assertNotNull("Could not locate submit button", input);
254 page = ((HtmlButtonInput)page.getElementByName("methodToCall.route")).click();
255 Assert.assertTrue("page text does not contain:\n" + TkTestConstants.EFFECTIVE_DATE_ERROR, page.asText().contains(TkTestConstants.EFFECTIVE_DATE_ERROR));
256 Calendar futureDate = Calendar.getInstance();
257 futureDate.add(java.util.Calendar.YEAR, 2);
258 String futureDateString = "01/01/" + Integer.toString(futureDate.get(Calendar.YEAR));
259
260
261 setFieldValue(page, "document.newMaintainableObject.effectiveDate", futureDateString);
262 page = ((HtmlButtonInput)page.getElementByName("methodToCall.route")).click();
263 Assert.assertTrue("page text does not contain:\n" + TkTestConstants.EFFECTIVE_DATE_ERROR, page.asText().contains(TkTestConstants.EFFECTIVE_DATE_ERROR));
264 Calendar validDate = Calendar.getInstance();
265 validDate.add(java.util.Calendar.MONTH, 5);
266 String validDateString = Integer.toString(validDate.get(Calendar.MONTH)) + '/' + Integer.toString(validDate.get(Calendar.DAY_OF_MONTH))
267 + '/' + Integer.toString(validDate.get(Calendar.YEAR));
268 setFieldValue(page, "document.newMaintainableObject.effectiveDate", validDateString);
269 page = ((HtmlElement)page.getElementByName("methodToCall.route")).click();
270 Assert.assertFalse("page text contains:\n" + TkTestConstants.EFFECTIVE_DATE_ERROR, page.asText().contains(TkTestConstants.EFFECTIVE_DATE_ERROR));
271 }
272
273 public class ClearCacheLifecycle extends BaseLifecycle {
274 private final Logger LOG = Logger.getLogger(ClearCacheLifecycle.class);
275
276 @Override
277 public void start() throws Exception {
278 long startTime = System.currentTimeMillis();
279 LOG.info("Starting cache flushing");
280 List<CacheManager> cms = new ArrayList<CacheManager>(CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagers());
281 for (CacheManager cm : cms) {
282 for (String cacheName : cm.getCacheNames()) {
283
284 cm.getCache(cacheName).clear();
285 }
286 }
287 long endTime = System.currentTimeMillis();
288 LOG.info("Caches cleared in " + (endTime - startTime) + "ms");
289 }
290
291 @Override
292 public void stop() throws Exception {
293 super.stop();
294 }
295
296 }
297
298 public WebClient getWebClient() {
299 return this.webClient;
300 }
301
302 public void setWebClient(WebClient webClient) {
303 this.webClient = webClient;
304 }
305
306 }