View Javadoc

1   /**
2    * Copyright 2004-2013 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.time.qunit;
17  
18  import java.io.File;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.junit.Assert;
25  import org.junit.Ignore;
26  import org.junit.Test;
27  import org.kuali.hr.test.KPMETestCase;
28  import org.kuali.hr.time.test.HtmlUnitUtil;
29  
30  import com.gargoylesoftware.htmlunit.BrowserVersion;
31  import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
32  import com.gargoylesoftware.htmlunit.WebClient;
33  import com.gargoylesoftware.htmlunit.html.HtmlElement;
34  import com.gargoylesoftware.htmlunit.html.HtmlForm;
35  import com.gargoylesoftware.htmlunit.html.HtmlPage;
36  import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
37  
38  @Ignore
39  public class QUnitTest extends KPMETestCase {
40  
41  	private static final Logger LOG = Logger.getLogger(QUnitTest.class);
42  	private List<String> failures;
43  
44  	@Test
45  	public void testQUnit() throws Exception {
46  		failures = new ArrayList<String>();
47  
48  		File qunitDir = new File("src/main/webapp/qunit");
49  		if (!qunitDir.exists() || !qunitDir.isDirectory()) {
50  			Assert.fail("Test dir does not exist or is not a directory: " + qunitDir);
51  		}
52  
53  		for (File file : qunitDir.listFiles()) {
54  			if (file.getName().endsWith(".jsp")) {
55  				runTest(file);
56  			}
57  		}
58  
59  		if (failures.size() > 0) {
60  			StringBuffer sb = new StringBuffer();
61  			for (String failMsg : failures) {
62  				if (sb.length() > 0)
63  					sb.append("\n");
64  				sb.append(failMsg);
65  			}
66  			LOG.debug("\n\n" + sb.toString() + "\n\n");
67  
68  			Assert.fail("Tests failed: " + sb.toString());
69  		}
70  	}
71  
72  	private void runTest(File file) throws Exception {
73  		WebClient client = new WebClient(BrowserVersion.FIREFOX_3);
74  		client.setJavaScriptEnabled(true);
75  		client.setThrowExceptionOnScriptError(false);
76  		client.setAjaxController(new NicelyResynchronizingAjaxController());
77  		client.waitForBackgroundJavaScript(1000);
78  
79  		// pass the login filter
80  		// need to change to the testing port 8090
81  		HtmlPage page = client.getPage(new URL(HtmlUnitUtil.getBaseURL() + "/TimeDetail.do"));
82  		HtmlForm form = page.getFormByName("login-form");
83  		HtmlSubmitInput button = form.getInputByName("login");
84  		page = button.click();
85  
86  		// run the unit test
87  		// need to change to the testing port 8090
88  		page = client.getPage(new URL("http://localhost:8080/tk-dev/qunit/" + file.getName()));
89          synchronized (page) {
90              page.wait(5000);
91          }
92          HtmlUnitUtil.createTempFile(page);
93  		HtmlElement element = page.getHtmlElementById("qunit-tests");
94  		if (element.asText().indexOf("0 tests of 0") != -1)
95  			failures.add(file.getName() + " - No tests were run - " + element.asText());
96  		else if (element.asText().indexOf("0 failed") == -1)
97  			failures.add(file.getName() + " - " + element.asText());
98  	}
99  
100 }