Skip to content

Commit d99d1d0

Browse files
Adds test cases for uncaught exception handling in GWTTestCase.
This patch also includes refactoring to share code in GWTTestCaseXXXTests. Change-Id: I5dc3d37d0f49cce94cfe5494b0631a3f1ae463db Review-Link: https://gwt-review.googlesource.com/#/c/1940/ git-svn-id: http://google-web-toolkit.googlecode.com/svn/trunk@11520 8db76d5a-ed1c-0410-87a9-c151d255dfc7
1 parent d53a6b7 commit d99d1d0

6 files changed

+233
-65
lines changed

user/test/com/google/gwt/junit/JUnitSuite.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2009 Google Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
66
* the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -19,6 +19,7 @@
1919
import com.google.gwt.junit.client.GWTTestCaseAsyncTest;
2020
import com.google.gwt.junit.client.GWTTestCaseSetupTearDownTest;
2121
import com.google.gwt.junit.client.GWTTestCaseTest;
22+
import com.google.gwt.junit.client.GWTTestCaseUncaughtExceptionHandlerTest;
2223
import com.google.gwt.junit.client.PropertyDefiningGWTTest;
2324

2425
import junit.framework.Test;
@@ -36,6 +37,7 @@ public static Test suite() {
3637
// suite.addTestSuite(BenchmarkTest.class);
3738

3839
suite.addTestSuite(GWTTestCaseTest.class);
40+
suite.addTestSuite(GWTTestCaseUncaughtExceptionHandlerTest.class);
3941
suite.addTest(new TestSuiteWithOrder(GWTTestCaseAsyncTest.class));
4042
suite.addTest(new TestSuiteWithOrder(GWTTestCaseSetupTearDownTest.class));
4143

user/test/com/google/gwt/junit/TestResultWithExpectedFailures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private boolean isAnExpectedException(Test test, Throwable t) {
7575
ExpectedFailure annotation = getExpectedFailureAnnotation(test);
7676
if (annotation != null) {
7777
t = normalizeGwtTestException(t);
78-
return t.getClass().isAssignableFrom(annotation.withType())
78+
return annotation.withType().isAssignableFrom(t.getClass())
7979
&& getExceptionMessage(t).contains(annotation.withMessage());
8080
}
8181
return false;

user/test/com/google/gwt/junit/client/GWTTestCaseAsyncTest.java

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2012 Google Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
66
* the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -15,24 +15,15 @@
1515
*/
1616
package com.google.gwt.junit.client;
1717

18-
import com.google.gwt.dom.client.ButtonElement;
19-
import com.google.gwt.dom.client.Document;
2018
import com.google.gwt.junit.ExpectedFailure;
21-
import com.google.gwt.user.client.DOM;
22-
import com.google.gwt.user.client.Event;
23-
import com.google.gwt.user.client.EventListener;
2419
import com.google.gwt.user.client.Timer;
2520

2621
/**
2722
* This class tests GwtTestCase in async mode.
2823
*
2924
* Note: This test requires some test methods to be executed in a specific order.
3025
*/
31-
public class GWTTestCaseAsyncTest extends GWTTestCase {
32-
33-
public String getModuleName() {
34-
return "com.google.gwt.junit.JUnit";
35-
}
26+
public class GWTTestCaseAsyncTest extends GWTTestCaseTestBase {
3627

3728
// The following tests (all prefixed with test_) are intended to test the
3829
// interaction of synchronous failures (within event handlers) with various
@@ -41,86 +32,75 @@ public String getModuleName() {
4132
//
4233
// Nomenclature for these tests:
4334
// DTF => delayTestFinish()
44-
// SF => synchronous failure (from event handler)
35+
// SF => synchronous failure (from event handler using failViaUncaughtException)
4536
// FT => finishTest()
4637
// F => fail()
4738
// R => return;
4839

4940
@ExpectedFailure(withMessage = "test_dtf_sf")
5041
public void test_dtf_sf() {
5142
delayTestFinish();
52-
synchronousFailure("test_dtf_sf");
43+
failViaUncaughtException("test_dtf_sf");
5344
}
5445

5546
@ExpectedFailure(withMessage = "test_dtf_sf_f")
5647
public void test_dtf_sf_f() {
5748
delayTestFinish();
58-
synchronousFailure("test_dtf_sf_f");
49+
failViaUncaughtException("test_dtf_sf_f");
5950
failNow("test_dtf_sf_f");
6051
}
6152

6253
@ExpectedFailure(withMessage = "test_dtf_sf_ft")
6354
public void test_dtf_sf_ft() {
6455
delayTestFinish();
65-
synchronousFailure("test_dtf_sf_ft");
56+
failViaUncaughtException("test_dtf_sf_ft");
6657
finishTest();
6758
}
6859

6960
// Issue: http://code.google.com/p/google-web-toolkit/issues/detail?id=7846
7061
@ExpectedFailure(withMessage = "test_dtf_sf_r_f")
7162
public void _suppressed_test_dtf_sf_r_f() {
7263
delayTestFinish();
73-
synchronousFailure("test_dtf_sf_r_f");
64+
failViaUncaughtException("test_dtf_sf_r_f");
7465
failLater("test_dtf_sf_r_f");
7566
}
7667

7768
@ExpectedFailure(withMessage = "test_dtf_sf_r_ft")
7869
public void test_dtf_sf_r_ft() {
7970
delayTestFinish();
80-
synchronousFailure("test_dtf_sf_r_ft");
71+
failViaUncaughtException("test_dtf_sf_r_ft");
8172
finishTestLater();
8273
}
8374

84-
@ExpectedFailure(withMessage = "test_sf")
85-
public void test_sf() {
86-
synchronousFailure("test_sf");
87-
}
88-
8975
@ExpectedFailure(withMessage = "test_sf_dtf_f")
9076
public void test_sf_dtf_f() {
91-
synchronousFailure("test_sf_dtf_f");
77+
failViaUncaughtException("test_sf_dtf_f");
9278
delayTestFinish();
9379
failNow("test_sf_dtf_f");
9480
}
9581

9682
@ExpectedFailure(withMessage = "test_sf_dtf_ft")
9783
public void test_sf_dtf_ft() {
98-
synchronousFailure("test_sf_dtf_ft");
84+
failViaUncaughtException("test_sf_dtf_ft");
9985
delayTestFinish();
10086
finishTest();
10187
}
10288

10389
// Issue: http://code.google.com/p/google-web-toolkit/issues/detail?id=7846
10490
@ExpectedFailure(withMessage = "test_sf_dtf_r_f")
10591
public void _suppressed_test_sf_dtf_r_f() {
106-
synchronousFailure("test_sf_dtf_r_f");
92+
failViaUncaughtException("test_sf_dtf_r_f");
10793
delayTestFinish();
10894
failLater("test_sf_dtf_r_f");
10995
}
11096

11197
@ExpectedFailure(withMessage = "test_sf_dtf_r_ft")
11298
public void test_sf_dtf_r_ft() {
113-
synchronousFailure("test_sf_dtf_r_ft");
99+
failViaUncaughtException("test_sf_dtf_r_ft");
114100
delayTestFinish();
115101
finishTestLater();
116102
}
117103

118-
@ExpectedFailure(withMessage = "test_sf_f")
119-
public void test_sf_f() {
120-
synchronousFailure("test_sf_f");
121-
failNow("test_sf_f");
122-
}
123-
124104
/**
125105
* Fails normally.
126106
*/
@@ -179,6 +159,7 @@ public void testSpuriousFinishTest() {
179159
finishTest();
180160
fail("finishTest should have failed");
181161
} catch (IllegalStateException e) {
162+
// Expected
182163
}
183164
}
184165

@@ -227,10 +208,6 @@ public void run() {
227208
}.schedule(delay);
228209
}
229210

230-
private void failNow(String failMsg) {
231-
fail("Expected failure (" + failMsg + ")");
232-
}
233-
234211
private void finishTestLater() {
235212
finishTestLater(1);
236213
}
@@ -243,21 +220,4 @@ public void run() {
243220
}
244221
}.schedule(delay);
245222
}
246-
247-
// Trigger a test failure synchronously, but from within an event handler.
248-
// (The exception thrown from fail() will get caught by the GWT UncaughtExceptionHandler).
249-
private void synchronousFailure(final String failMsg) {
250-
ButtonElement btn = Document.get().createPushButtonElement();
251-
Document.get().getBody().appendChild(btn);
252-
Event.sinkEvents(btn, Event.ONCLICK);
253-
254-
EventListener listener = new EventListener() {
255-
public void onBrowserEvent(Event event) {
256-
failNow(failMsg);
257-
}
258-
};
259-
260-
DOM.setEventListener(btn.<com.google.gwt.user.client.Element>cast(), listener);
261-
btn.click();
262-
}
263223
}

user/test/com/google/gwt/junit/client/GWTTestCaseTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* This class tests our implementation of the GWTTestCase class which provides the behavior of
3535
* TestCase that is necessary in GWT.
3636
*/
37-
public class GWTTestCaseTest extends GWTTestCase {
37+
public class GWTTestCaseTest extends GWTTestCaseTestBase {
3838

3939
private static void assertNotEquals(double a, double b, double delta) {
4040
boolean failed = false;
@@ -68,10 +68,6 @@ private static void assertNotEquals(float a, float b, float delta) {
6868
private Object obj2 = Collections.nCopies(2, "data");
6969
private Object obj1Equal = Collections.nCopies(1, "data");
7070

71-
public String getModuleName() {
72-
return "com.google.gwt.junit.JUnit";
73-
}
74-
7571
@ExpectedFailure(withMessage = "testFail")
7672
public void testFail() {
7773
fail("testFail");
@@ -82,7 +78,7 @@ public void testThrowsException() throws Exception {
8278
throw new Exception();
8379
}
8480

85-
@ExpectedFailure(withType = JavaScriptException.class)
81+
@ExpectedFailure(withType = Exception.class)
8682
public void testThrowsNonSerializableException() {
8783
throw new JavaScriptException("name", "desc");
8884
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.junit.client;
17+
18+
19+
import com.google.gwt.core.client.JavaScriptException;
20+
import com.google.gwt.dom.client.ButtonElement;
21+
import com.google.gwt.dom.client.Document;
22+
import com.google.gwt.user.client.DOM;
23+
import com.google.gwt.user.client.Event;
24+
import com.google.gwt.user.client.EventListener;
25+
26+
/**
27+
* A base class for tests GWTTestCase.
28+
*/
29+
abstract class GWTTestCaseTestBase extends GWTTestCase {
30+
31+
public String getModuleName() {
32+
return "com.google.gwt.junit.JUnit";
33+
}
34+
35+
protected void failNow(String failMsg) {
36+
fail("Expected failure (" + failMsg + ")");
37+
}
38+
39+
protected void failViaUncaughtException(final String failMsg) {
40+
ButtonElement btn = Document.get().createPushButtonElement();
41+
Document.get().getBody().appendChild(btn);
42+
Event.sinkEvents(btn, Event.ONCLICK);
43+
44+
EventListener listener = new EventListener() {
45+
public void onBrowserEvent(Event event) {
46+
failNow(failMsg);
47+
}
48+
};
49+
50+
DOM.setEventListener(btn.<com.google.gwt.user.client.Element>cast(), listener);
51+
try {
52+
btn.click();
53+
} catch (JavaScriptException ignored) {
54+
// In HtmlUnit exception thrown by event handlers are propagated to #click() call.
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)