001    /**
002     * Copyright 2004-2012 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                    WebClient client = new WebClient(BrowserVersion.FIREFOX_3);
074                    client.setJavaScriptEnabled(true);
075                    client.setThrowExceptionOnScriptError(false);
076                    client.setAjaxController(new NicelyResynchronizingAjaxController());
077                    client.waitForBackgroundJavaScript(1000);
078    
079                    // pass the login filter
080                    // need to change to the testing port 8090
081                    HtmlPage page = client.getPage(new URL(HtmlUnitUtil.getBaseURL() + "/TimeDetail.do"));
082                    HtmlForm form = page.getFormByName("login-form");
083                    HtmlSubmitInput button = form.getInputByName("login");
084                    page = button.click();
085    
086                    // run the unit test
087                    // need to change to the testing port 8090
088                    page = client.getPage(new URL("http://localhost:8080/tk-dev/qunit/" + file.getName()));
089            synchronized (page) {
090                page.wait(5000);
091            }
092            HtmlUnitUtil.createTempFile(page);
093                    HtmlElement element = page.getHtmlElementById("qunit-tests");
094                    if (element.asText().indexOf("0 tests of 0") != -1)
095                            failures.add(file.getName() + " - No tests were run - " + element.asText());
096                    else if (element.asText().indexOf("0 failed") == -1)
097                            failures.add(file.getName() + " - " + element.asText());
098            }
099    
100    }