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.time.qunit;
017
018 import java.io.File;
019 import java.net.URL;
020 import java.util.ArrayList;
021 import java.util.List;
022
023 import org.apache.log4j.Logger;
024 import org.junit.Assert;
025 import org.junit.Ignore;
026 import org.junit.Test;
027 import org.kuali.hr.test.KPMETestCase;
028 import org.kuali.hr.time.test.HtmlUnitUtil;
029
030 import com.gargoylesoftware.htmlunit.BrowserVersion;
031 import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
032 import com.gargoylesoftware.htmlunit.WebClient;
033 import com.gargoylesoftware.htmlunit.html.HtmlElement;
034 import com.gargoylesoftware.htmlunit.html.HtmlForm;
035 import com.gargoylesoftware.htmlunit.html.HtmlPage;
036 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
037
038 @Ignore
039 public class QUnitTest extends KPMETestCase {
040
041 private static final Logger LOG = Logger.getLogger(QUnitTest.class);
042 private List<String> failures;
043
044 @Test
045 public void testQUnit() throws Exception {
046 failures = new ArrayList<String>();
047
048 File qunitDir = new File("src/main/webapp/qunit");
049 if (!qunitDir.exists() || !qunitDir.isDirectory()) {
050 Assert.fail("Test dir does not exist or is not a directory: " + qunitDir);
051 }
052
053 for (File file : qunitDir.listFiles()) {
054 if (file.getName().endsWith(".jsp")) {
055 runTest(file);
056 }
057 }
058
059 if (failures.size() > 0) {
060 StringBuffer sb = new StringBuffer();
061 for (String failMsg : failures) {
062 if (sb.length() > 0)
063 sb.append("\n");
064 sb.append(failMsg);
065 }
066 LOG.debug("\n\n" + sb.toString() + "\n\n");
067
068 Assert.fail("Tests failed: " + sb.toString());
069 }
070 }
071
072 private void runTest(File file) throws Exception {
073 setWebClient(new WebClient(BrowserVersion.FIREFOX_10));
074 WebClient client = getWebClient();
075 client.setJavaScriptEnabled(true);
076 client.setThrowExceptionOnScriptError(false);
077 client.setAjaxController(new NicelyResynchronizingAjaxController());
078 client.waitForBackgroundJavaScript(1000);
079
080 // pass the login filter
081 // need to change to the testing port 8090
082 HtmlPage page = client.getPage(new URL(HtmlUnitUtil.getBaseURL() + "/TimeDetail.do"));
083 HtmlForm form = page.getFormByName("login-form");
084 HtmlSubmitInput button = form.getInputByName("login");
085 page = button.click();
086
087 // run the unit test
088 // need to change to the testing port 8090
089 page = client.getPage(new URL("http://localhost:8080/tk-dev/qunit/" + file.getName()));
090 synchronized (page) {
091 page.wait(5000);
092 }
093 HtmlUnitUtil.createTempFile(page);
094 HtmlElement element = page.getHtmlElementById("qunit-tests");
095 if (element.asText().indexOf("0 tests of 0") != -1)
096 failures.add(file.getName() + " - No tests were run - " + element.asText());
097 else if (element.asText().indexOf("0 failed") == -1)
098 failures.add(file.getName() + " - " + element.asText());
099 }
100
101 }