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