Ignore:
Timestamp:
2018-07-26T22:01:31+02:00 (7 years ago)
Author:
Don-vip
Message:

see #16010 - use JMockit to enable more extensive test coverage (patch by ris, modified)

see https://github.com/openstreetmap/josm/pull/24/commits for details

Location:
trunk/test/unit/org/openstreetmap/josm/actions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/actions/ExitActionTest.java

    r11101 r14052  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.actions;
     3
     4import static org.junit.Assert.assertTrue;
    35
    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;
     
    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}
  • trunk/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java

    r13300 r14052  
    5656    @Test
    5757    public void testUpdatePluginValid() throws Exception {
    58         this.pluginPath = "plugin/dummy_plugin.jar";
     58        this.pluginPath = "plugin/dummy_plugin.v31772.jar";
    5959        this.mockHttp();
    6060
     
    7474
    7575        // get PluginInformation from jar file
    76         final PluginInformation pluginInformation = new PluginInformation(srcPluginFile);
     76        final PluginInformation pluginInformation = new PluginInformation(srcPluginFile, "dummy_plugin");
    7777        // ...and grafting on the downloadlink
    7878        pluginInformation.downloadlink = this.getRemoteFileUrl();
     
    8787        // the ".jar.new" file should have been deleted
    8888        assertFalse(pluginFileNew.exists());
    89         // the ".jar" file should still exist
    90         assertTrue(pluginFile.exists());
    91         try (
    92             FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
    93             FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
    94         ) {
    95             // and its contents should equal those that were served to the task
    96             assertArrayEquals(
    97                 ByteStreams.toByteArray(pluginDirPluginStream),
    98                 ByteStreams.toByteArray(srcPluginStream)
    99             );
    100         }
     89        // the ".jar" file should still exist and its contents should equal those that were served to the task
     90        TestUtils.assertFileContentsEqual(pluginFile, srcPluginFile);
    10191    }
    10292
     
    140130        }
    141131
    142         // the ".jar.new" file should exist, even though invalid
    143         assertTrue(pluginFileNew.exists());
     132        // assert that the "corrupt" jar file made it through in tact
     133        TestUtils.assertFileContentsEqual(pluginFileNew, srcPluginFile);
    144134        // the ".jar" file should still exist
    145135        assertTrue(pluginFile.exists());
    146136        try (
    147             FileInputStream pluginDirPluginNewStream = new FileInputStream(pluginFileNew);
    148137            FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
    149             FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
    150138        ) {
    151139            // the ".jar" file's contents should be as before
     
    154142                ByteStreams.toByteArray(pluginDirPluginStream)
    155143            );
    156             // just assert that the "corrupt" jar file made it through in tact
    157             assertArrayEquals(
    158                 ByteStreams.toByteArray(pluginDirPluginNewStream),
    159                 ByteStreams.toByteArray(srcPluginStream)
    160             );
    161144        }
    162145    }
Note: See TracChangeset for help on using the changeset viewer.