Ticket #16010: v2-0025-MainApplication-shutdown-don-t-use-isHeadless-to-.patch

File v2-0025-MainApplication-shutdown-don-t-use-isHeadless-to-.patch, 4.8 KB (added by ris, 3 years ago)
  • src/org/openstreetmap/josm/gui/MainApplication.java

    From fde55b943923359bf9018d7e4e8dd660635e5a74 Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Sat, 12 May 2018 21:45:59 +0100
    Subject: [PATCH v2 25/28] MainApplication$shutdown(): don't use isHeadless to
     detect test mode & skip shutdown calls
    
    instead mock out these shutdown methods in ExitActionTest, allowing the test
    to work similarly in both headless and non-headless modes
    ---
     .../openstreetmap/josm/gui/MainApplication.java    | 25 ++++++------
     .../openstreetmap/josm/actions/ExitActionTest.java | 46 ++++++++++++++++++++++
     2 files changed, 58 insertions(+), 13 deletions(-)
    
    diff --git a/src/org/openstreetmap/josm/gui/MainApplication.java b/src/org/openstreetmap/josm/gui/MainApplication.java
    index 6e7cdebea..70fe44744 100644
    a b public class MainApplication extends Main { 
    487487
    488488    @Override
    489489    protected void shutdown() {
    490         if (!GraphicsEnvironment.isHeadless()) {
    491             try {
    492                 worker.shutdown();
    493             } catch (SecurityException e) {
    494                 Logging.log(Logging.LEVEL_ERROR, "Unable to shutdown worker", e);
    495             }
    496             JCSCacheManager.shutdown();
     490        try {
     491            worker.shutdown();
     492        } catch (SecurityException e) {
     493            Logging.log(Logging.LEVEL_ERROR, "Unable to shutdown worker", e);
    497494        }
     495        JCSCacheManager.shutdown();
     496
    498497        if (mainFrame != null) {
    499498            mainFrame.storeState();
    500499        }
    public class MainApplication extends Main { 
    504503        // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
    505504        layerManager.resetState();
    506505        super.shutdown();
    507         if (!GraphicsEnvironment.isHeadless()) {
    508             try {
    509                 worker.shutdownNow();
    510             } catch (SecurityException e) {
    511                 Logging.log(Logging.LEVEL_ERROR, "Unable to shutdown worker", e);
    512             }
     506
     507        try {
     508            // in case the current task still hasn't finished
     509            worker.shutdownNow();
     510        } catch (SecurityException e) {
     511            Logging.log(Logging.LEVEL_ERROR, "Unable to shutdown worker", e);
    513512        }
    514513    }
    515514
  • test/unit/org/openstreetmap/josm/actions/ExitActionTest.java

    diff --git a/test/unit/org/openstreetmap/josm/actions/ExitActionTest.java b/test/unit/org/openstreetmap/josm/actions/ExitActionTest.java
    index 7612752b2..e4c27002a 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.actions;
    33
     4import static org.junit.Assert.assertTrue;
     5
    46import org.junit.Rule;
    57import org.junit.Test;
    68import org.junit.contrib.java.lang.system.ExpectedSystemExit;
     9import org.openstreetmap.josm.gui.MainApplication;
     10import org.openstreetmap.josm.gui.progress.swing.ProgressMonitorExecutor;
    711import org.openstreetmap.josm.testutils.JOSMTestRules;
     12import org.openstreetmap.josm.tools.ImageProvider;
     13
     14import mockit.Invocation;
     15import mockit.Mock;
     16import mockit.MockUp;
    817
    918import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1019
    public final class ExitActionTest { 
    3241    @Test
    3342    public void testActionPerformed() {
    3443        exit.expectSystemExitWithStatus(0);
     44
     45        boolean[] workerShutdownCalled = {false};
     46        boolean[] workerShutdownNowCalled = {false};
     47        boolean[] imageProviderShutdownCalled = {false};
     48
     49        // critically we don't proceed into the actual implementation in any of these mock methods -
     50        // that would be quite annoying for tests following this one which were expecting to use any
     51        // of these
     52        new MockUp<ProgressMonitorExecutor>() {
     53            @Mock
     54            private void shutdown(Invocation invocation) {
     55                if (invocation.getInvokedInstance() == MainApplication.worker) {
     56                    workerShutdownCalled[0] = true;
     57                }
     58            }
     59
     60            @Mock
     61            private void shutdownNow(Invocation invocation) {
     62                if (invocation.getInvokedInstance() == MainApplication.worker) {
     63                    // regular shutdown should have been called first
     64                    assertTrue(workerShutdownCalled[0]);
     65                    workerShutdownNowCalled[0] = true;
     66                }
     67            }
     68        };
     69        new MockUp<ImageProvider>() {
     70            @Mock
     71            private void shutdown(Invocation invocation) {
     72                imageProviderShutdownCalled[0] = true;
     73            }
     74        };
     75
    3576        // No layer
     77
    3678        new ExitAction().actionPerformed(null);
     79
     80        assertTrue(workerShutdownCalled[0]);
     81        assertTrue(workerShutdownNowCalled[0]);
     82        assertTrue(imageProviderShutdownCalled[0]);
    3783    }
    3884}