001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.javascript;
039:
040: import java.util.ArrayList;
041: import java.util.Collections;
042: import java.util.List;
043:
044: import junit.framework.Test;
045: import junit.framework.TestSuite;
046:
047: import com.gargoylesoftware.htmlunit.WebTestCase;
048: import com.gargoylesoftware.htmlunit.html.HtmlPage;
049:
050: /**
051: * Multi-threaded JavaScript engine test.
052: *
053: * @version $Revision: 2132 $
054: * @author David D. Kilzer
055: */
056: public class ThreadTest extends WebTestCase {
057:
058: /**
059: * Create an instance
060: *
061: * @param name The name of the test
062: */
063: public ThreadTest(final String name) {
064: super (name);
065: }
066:
067: /**
068: * @throws InterruptedException if the test fails
069: */
070: public void testJavaScriptEngineInMultipleThreads()
071: throws InterruptedException {
072: final TestThread thread1 = new TestThread("thread1");
073: final TestThread thread2 = new TestThread("thread2");
074: final TestThread thread3 = new TestThread("thread3");
075: final TestThread thread4 = new TestThread("thread4");
076:
077: thread1.start();
078: thread2.start();
079: thread3.start();
080: thread4.start();
081:
082: thread1.join();
083: thread2.join();
084: thread3.join();
085: thread4.join();
086:
087: assertTrue("thread1 not successful", thread1.isSuccessful());
088: assertTrue("thread2 not successful", thread2.isSuccessful());
089: assertTrue("thread3 not successful", thread3.isSuccessful());
090: assertTrue("thread4 not successful", thread4.isSuccessful());
091: }
092:
093: /**
094: * run this test many times
095: * @return a suite
096: */
097: public static Test suite() {
098: final TestSuite suite = new TestSuite("Run this many times");
099: suite.addTestSuite(ThreadTest.class);
100: for (int i = 1; i < 100; i++) {
101: suite.addTest(new ThreadTest(
102: "testJavaScriptEngineInMultipleThreads"));
103: }
104: return suite;
105: }
106:
107: /** A test object for threads*/
108: private static class TestThread extends Thread {
109:
110: private boolean successful_ = false;
111:
112: /**
113: * Create an instance
114: * @param name the name of the thread.
115: */
116: public TestThread(final String name) {
117: super (name);
118: }
119:
120: /** @see Thread#run() */
121: public void run() {
122: try {
123: testCallInheritedFunction();
124: successful_ = true;
125: } catch (final Exception e) {
126: System.err.println(">>> Thread " + getName());
127: e.printStackTrace(System.err);
128: successful_ = false;
129: }
130: }
131:
132: /** @return true if the test was successful */
133: public boolean isSuccessful() {
134: return successful_;
135: }
136:
137: /**
138: * @see SimpleScriptableTest#testCallInheritedFunction()
139: * @throws Exception if the test failed
140: */
141: public void testCallInheritedFunction() throws Exception {
142: final String content = "<html><head><title>foo</title><script>\n"
143: + "function doTest() {\n"
144: + " document.form1.textfield1.focus();\n"
145: + " alert('past focus');\n"
146: + "}\n"
147: + "</script></head><body onload='doTest()'>\n"
148: + "<p>hello world</p>\n"
149: + "<form name='form1'>\n"
150: + " <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
151: + "</form>\n" + "</body></html>";
152:
153: final List collectedAlerts = new ArrayList();
154: final HtmlPage page = loadPage(content, collectedAlerts);
155:
156: assertEquals("foo", page.getTitleText());
157: assertEquals("focus not changed to textfield1", page
158: .getFormByName("form1")
159: .getInputByName("textfield1"), page
160: .getElementWithFocus());
161: final List expectedAlerts = Collections
162: .singletonList("past focus");
163: assertEquals(expectedAlerts, collectedAlerts);
164: }
165: }
166:
167: }
|