Ticket #16010: v2-0007-JOSMTestRules-add-EDTAssertionMocker-and-.asserti.patch

File v2-0007-JOSMTestRules-add-EDTAssertionMocker-and-.asserti.patch, 5.3 KB (added by ris, 7 years ago)
  • src/org/openstreetmap/josm/gui/util/GuiHelper.java

    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 {  
    201201        }
    202202    }
    203203
     204    private static void handleEDTException(Throwable t) {
     205        Logging.logWithStackTrace(Logging.LEVEL_ERROR, t, "Exception raised in EDT");
     206    }
     207
    204208    /**
    205209     * Executes synchronously a runnable in
    206210     * <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html">Event Dispatch Thread</a>.
    public final class GuiHelper {  
    214218            try {
    215219                SwingUtilities.invokeAndWait(task);
    216220            } catch (InterruptedException | InvocationTargetException e) {
    217                 Logging.error(e);
     221                handleEDTException(e);
    218222            }
    219223        }
    220224    }
    public final class GuiHelper {  
    255259            try {
    256260                return callable.call();
    257261            } catch (Exception e) { // NOPMD
    258                 Logging.error(e);
     262                handleEDTException(e);
    259263                return null;
    260264            }
    261265        } else {
    public final class GuiHelper {  
    264268            try {
    265269                return task.get();
    266270            } catch (InterruptedException | ExecutionException e) {
    267                 Logging.error(e);
     271                handleEDTException(e);
    268272                return null;
    269273            }
    270274        }
  • test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    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 {  
    6565    private Version originalVersion;
    6666    private Runnable mapViewStateMockingRunnable;
    6767    private Runnable navigableComponentMockingRunnable;
     68    private Runnable edtAssertionMockingRunnable;
    6869    private boolean platform;
    6970    private boolean useProjection;
    7071    private boolean useProjectionNadGrids;
    public class JOSMTestRules implements TestRule {  
    261262    }
    262263
    263264    /**
     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    /**
    264284     * Replace imagery sources with a default set of mock tile sources
    265285     *
    266286     * @return this instance, for easy chaining
    public class JOSMTestRules implements TestRule {  
    472492            RightAndLefthandTraffic.initialize();
    473493        }
    474494
     495        if (this.edtAssertionMockingRunnable != null) {
     496            this.edtAssertionMockingRunnable.run();
     497        }
     498
    475499        if (commands) {
    476500            // TODO: Implement a more selective version of this once Main is restructured.
    477501            JOSMFixture.createUnitTestFixture().init(true);
  • new file test/unit/org/openstreetmap/josm/testutils/mockers/EDTAssertionMocker.java

    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.
     2package org.openstreetmap.josm.testutils;
     3
     4import org.openstreetmap.josm.gui.util.GuiHelper;
     5
     6import mockit.Invocation;
     7import mockit.Mock;
     8import 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 */
     14public 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}