KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > ThreadTest


1 /*
2  * Copyright (c) 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript;
39
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43
44 import com.gargoylesoftware.htmlunit.WebTestCase;
45 import com.gargoylesoftware.htmlunit.html.HtmlPage;
46
47
48
49 /**
50  * Multi-threaded JavaScript engine test.
51  *
52  * @author David D. Kilzer
53  * @version $Revision: 1.5 $
54  */

55 public class ThreadTest extends WebTestCase {
56
57     /**
58      * Create an instance
59      *
60      * @param name The name of the test
61      */

62     public ThreadTest(final String name) {
63         super(name);
64     }
65
66
67     /**
68      * @throws InterruptedException if the test fails
69      */

70     public void testJavaScriptEngineInMultipleThreads() throws InterruptedException {
71
72         final TestThread thread1 = new TestThread("thread1");
73         final TestThread thread2 = new TestThread("thread2");
74         final TestThread thread3 = new TestThread("thread3");
75         final TestThread thread4 = new TestThread("thread4");
76
77         thread1.start();
78         thread2.start();
79         thread3.start();
80         thread4.start();
81
82         thread1.join();
83         thread2.join();
84         thread3.join();
85         thread4.join();
86
87         assertTrue("thread1 not successful", thread1.isSuccessful());
88         assertTrue("thread2 not successful", thread2.isSuccessful());
89         assertTrue("thread3 not successful", thread3.isSuccessful());
90         assertTrue("thread4 not successful", thread4.isSuccessful());
91     }
92
93
94     /** A test object for threads*/
95     private static class TestThread extends Thread {
96
97         private boolean successful_ = false;
98
99         /**
100          * Create an instance
101          * @param name the name of the thread.
102          */

103         public TestThread(final String name) {
104             super(name);
105         }
106
107         /** @see Thread#run() */
108         public void run() {
109             try {
110                 testCallInheritedFunction();
111                 successful_ = true;
112             }
113             catch (final Exception e) {
114                 System.err.println(">>> Thread " + getName());
115                 e.printStackTrace(System.err);
116                 successful_ = false;
117             }
118         }
119
120         /** @return true if the test was successful */
121         public boolean isSuccessful() {
122             return successful_;
123         }
124
125
126         /**
127          * @see SimpleScriptableTest#testCallInheritedFunction()
128          * @throws Exception if the test failed
129          */

130         public void testCallInheritedFunction() throws Exception {
131             final String content
132                 = "<html><head><title>foo</title><script>"
133                 + "function doTest() {\n"
134                 + " document.form1.textfield1.focus();\n"
135                 + " alert('past focus');\n"
136                 + "}\n"
137                 + "</script></head><body onload='doTest()'>"
138                 + "<p>hello world</p>"
139                 + "<form name='form1'>"
140                 + " <input type='text' name='textfield1' id='textfield1' value='foo' />"
141                 + "</form>"
142                 + "</body></html>";
143
144             final List collectedAlerts = new ArrayList();
145             final HtmlPage page = loadPage(content, collectedAlerts);
146             
147             assertEquals("foo", page.getTitleText());
148             assertEquals("focus not changed to textfield1",
149                          page.getFormByName("form1").getInputByName("textfield1"),
150                          page.getWebClient().getElementWithFocus());
151             final List expectedAlerts = Collections.singletonList("past focus");
152             assertEquals(expectedAlerts, collectedAlerts);
153         }
154     }
155
156 }
157
Popular Tags