From 0bd5ce3174b2f211bb459d719133155868dcd744 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 17 Mar 2018 19:35:03 +0000
Subject: [PATCH v2 07/28] JOSMTestRules: add EDTAssertionMocker and
.assertionsInEDT() method for controlling it
This should allow tests to throw assertions from *within* the EDT and have
them cause a test failure.
---
src/org/openstreetmap/josm/gui/util/GuiHelper.java | 10 ++++++---
.../josm/testutils/JOSMTestRules.java | 24 ++++++++++++++++++++++
.../josm/testutils/mockers/EDTAssertionMocker.java | 24 ++++++++++++++++++++++
3 files changed, 55 insertions(+), 3 deletions(-)
create mode 100644 test/unit/org/openstreetmap/josm/testutils/mockers/EDTAssertionMocker.java
diff --git a/src/org/openstreetmap/josm/gui/util/GuiHelper.java b/src/org/openstreetmap/josm/gui/util/GuiHelper.java
index e11f26f9d..c7b9df753 100644
a
|
b
|
public final class GuiHelper {
|
201 | 201 | } |
202 | 202 | } |
203 | 203 | |
| 204 | private static void handleEDTException(Throwable t) { |
| 205 | Logging.logWithStackTrace(Logging.LEVEL_ERROR, t, "Exception raised in EDT"); |
| 206 | } |
| 207 | |
204 | 208 | /** |
205 | 209 | * Executes synchronously a runnable in |
206 | 210 | * <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html">Event Dispatch Thread</a>. |
… |
… |
public final class GuiHelper {
|
214 | 218 | try { |
215 | 219 | SwingUtilities.invokeAndWait(task); |
216 | 220 | } catch (InterruptedException | InvocationTargetException e) { |
217 | | Logging.error(e); |
| 221 | handleEDTException(e); |
218 | 222 | } |
219 | 223 | } |
220 | 224 | } |
… |
… |
public final class GuiHelper {
|
255 | 259 | try { |
256 | 260 | return callable.call(); |
257 | 261 | } catch (Exception e) { // NOPMD |
258 | | Logging.error(e); |
| 262 | handleEDTException(e); |
259 | 263 | return null; |
260 | 264 | } |
261 | 265 | } else { |
… |
… |
public final class GuiHelper {
|
264 | 268 | try { |
265 | 269 | return task.get(); |
266 | 270 | } catch (InterruptedException | ExecutionException e) { |
267 | | Logging.error(e); |
| 271 | handleEDTException(e); |
268 | 272 | return null; |
269 | 273 | } |
270 | 274 | } |
diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
index 24394108f..03d7cb3b8 100644
a
|
b
|
public class JOSMTestRules implements TestRule {
|
65 | 65 | private Version originalVersion; |
66 | 66 | private Runnable mapViewStateMockingRunnable; |
67 | 67 | private Runnable navigableComponentMockingRunnable; |
| 68 | private Runnable edtAssertionMockingRunnable; |
68 | 69 | private boolean platform; |
69 | 70 | private boolean useProjection; |
70 | 71 | private boolean useProjectionNadGrids; |
… |
… |
public class JOSMTestRules implements TestRule {
|
261 | 262 | } |
262 | 263 | |
263 | 264 | /** |
| 265 | * Re-raise AssertionErrors thrown in the EDT where they would have normally been swallowed. |
| 266 | * @return this instance, for easy chaining |
| 267 | */ |
| 268 | public JOSMTestRules assertionsInEDT() { |
| 269 | return this.assertionsInEDT(EDTAssertionMocker::new); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Re-raise AssertionErrors thrown in the EDT where they would have normally been swallowed. |
| 274 | * @param edtAssertionMockingRunnable Runnable for initializing this functionality |
| 275 | * |
| 276 | * @return this instance, for easy chaining |
| 277 | */ |
| 278 | public JOSMTestRules assertionsInEDT(final Runnable edtAssertionMockingRunnable) { |
| 279 | this.edtAssertionMockingRunnable = edtAssertionMockingRunnable; |
| 280 | return this; |
| 281 | } |
| 282 | |
| 283 | /** |
264 | 284 | * Replace imagery sources with a default set of mock tile sources |
265 | 285 | * |
266 | 286 | * @return this instance, for easy chaining |
… |
… |
public class JOSMTestRules implements TestRule {
|
472 | 492 | RightAndLefthandTraffic.initialize(); |
473 | 493 | } |
474 | 494 | |
| 495 | if (this.edtAssertionMockingRunnable != null) { |
| 496 | this.edtAssertionMockingRunnable.run(); |
| 497 | } |
| 498 | |
475 | 499 | if (commands) { |
476 | 500 | // TODO: Implement a more selective version of this once Main is restructured. |
477 | 501 | JOSMFixture.createUnitTestFixture().init(true); |
diff --git a/test/unit/org/openstreetmap/josm/testutils/mockers/EDTAssertionMocker.java b/test/unit/org/openstreetmap/josm/testutils/mockers/EDTAssertionMocker.java
new file mode 100644
index 000000000..f9aaa388d
-
|
+
|
|
| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.testutils; |
| 3 | |
| 4 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| 5 | |
| 6 | import mockit.Invocation; |
| 7 | import mockit.Mock; |
| 8 | import mockit.MockUp; |
| 9 | |
| 10 | /** |
| 11 | * MockUp that, when applied, should cause calls to the EDT which would normally swallow generated |
| 12 | * AssertionErrors to instead re-raise them. |
| 13 | */ |
| 14 | public class EDTAssertionMocker extends MockUp<GuiHelper> { |
| 15 | @Mock |
| 16 | private static void handleEDTException(final Invocation invocation, final Throwable t) { |
| 17 | final Throwable cause = t.getCause(); |
| 18 | if (cause instanceof AssertionError) { |
| 19 | throw (AssertionError) cause; |
| 20 | } |
| 21 | |
| 22 | invocation.proceed(t); |
| 23 | } |
| 24 | } |