Ticket #16946: v1-0001-PlatformHookWindowsTest-fix-openUrl-test-using-jm.patch

File v1-0001-PlatformHookWindowsTest-fix-openUrl-test-using-jm.patch, 3.7 KB (added by ris, 7 years ago)
  • test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java

    From 117702751d824cb7ee62b25d3e82074ebbb073dd Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Sat, 3 Nov 2018 15:36:46 +0000
    Subject: [PATCH v1 1/2] PlatformHookWindowsTest: fix openUrl test using
     jmockit, expand cases covered
    
    ---
     .../josm/tools/PlatformHookWindowsTest.java        | 53 +++++++++++++++++-----
     1 file changed, 41 insertions(+), 12 deletions(-)
    
    diff --git a/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java b/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java
    index 7255cb500..726e0a3b3 100644
    a b import static org.junit.Assert.assertTrue;  
    99import static org.junit.Assume.assumeFalse;
    1010import static org.junit.Assert.fail;
    1111
     12import java.awt.Desktop;
    1213import java.io.File;
    1314import java.io.IOException;
    1415import java.security.KeyStore;
    import java.util.Collection;  
    1920import org.junit.BeforeClass;
    2021import org.junit.Test;
    2122import org.openstreetmap.josm.JOSMFixture;
     23import org.openstreetmap.josm.TestUtils;
    2224import org.openstreetmap.josm.io.remotecontrol.RemoteControlHttpsServer;
    2325import org.openstreetmap.josm.io.remotecontrol.RemoteControlTest;
    2426import org.openstreetmap.josm.spi.preferences.Config;
    2527
     28import mockit.Expectations;
     29import mockit.Injectable;
     30
    2631/**
    2732 * Unit tests of {@link PlatformHookWindows} class.
    2833 */
    public class PlatformHookWindowsTest {  
    116121    }
    117122
    118123    /**
    119      * Test method for {@code PlatformHookWindows#openUrl}
     124     * Test method for {@code PlatformHookWindows#openUrl} when Desktop works as expected
    120125     * @throws IOException if an error occurs
    121126     */
    122127    @Test
    123     public void testOpenUrl() throws IOException {
    124         if (PlatformManager.isPlatformWindows()) {
    125             hook.openUrl(Config.getUrls().getJOSMWebsite());
    126         } else {
    127             try {
    128                 hook.openUrl(Config.getUrls().getJOSMWebsite());
    129                 fail("Expected IOException");
    130             } catch (IOException e) {
    131                 Logging.info(e.getMessage());
    132             }
    133         }
     128    public void testOpenUrlSuccess(@Injectable final Desktop mockDesktop) throws IOException {
     129        TestUtils.assumeWorkingJMockit();
     130        new Expectations(Desktop.class) {{
     131            // real implementation would raise HeadlessException
     132            Desktop.getDesktop(); result = mockDesktop; times = 1;
     133        }};
     134        new Expectations() {{
     135            mockDesktop.browse(withNotNull()); times = 1;
     136        }};
     137
     138        hook.openUrl(Config.getUrls().getJOSMWebsite());
     139    }
     140
     141    /**
     142     * Test method for {@code PlatformHookWindows#openUrl} when Desktop fails
     143     * @throws IOException if an error occurs
     144     */
     145    @Test
     146    public void testOpenUrlFallback(@Injectable final Desktop mockDesktop) throws IOException {
     147        TestUtils.assumeWorkingJMockit();
     148        new Expectations(Desktop.class) {{
     149            // real implementation would raise HeadlessException
     150            Desktop.getDesktop(); result = mockDesktop; times = 1;
     151        }};
     152        new Expectations() {{
     153            mockDesktop.browse(withNotNull()); result = new IOException(); times = 1;
     154        }};
     155        final Runtime anyRuntime = Runtime.getRuntime();
     156        new Expectations(Runtime.class) {{
     157            anyRuntime.exec(new String[] {"rundll32", "url.dll,FileProtocolHandler", Config.getUrls().getJOSMWebsite()}); result = null; times = 1;
     158            // prevent a non-matching invocation being executed
     159            anyRuntime.exec((String[]) withNotNull()); result = null; times = 0;
     160        }};
     161
     162        hook.openUrl(Config.getUrls().getJOSMWebsite());
    134163    }
    135164
    136165    /**