Ignore:
Timestamp:
2018-11-05T01:16:59+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #16946, fix #16947 - unit test fixes for non-headless mode (patch by ris)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java

    r14168 r14412  
    1010import static org.junit.Assert.fail;
    1111
     12import java.awt.Desktop;
    1213import java.io.File;
    1314import java.io.IOException;
     
    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;
     27
     28import mockit.Expectations;
     29import mockit.Injectable;
    2530
    2631/**
     
    117122
    118123    /**
    119      * Test method for {@code PlatformHookWindows#openUrl}
     124     * Test method for {@code PlatformHookWindows#openUrl} when Desktop works as expected
     125     * @param mockDesktop desktop mock
    120126     * @throws IOException if an error occurs
    121127     */
    122128    @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         }
     129    public void testOpenUrlSuccess(@Injectable final Desktop mockDesktop) throws IOException {
     130        TestUtils.assumeWorkingJMockit();
     131        new Expectations(Desktop.class) {{
     132            // real implementation would raise HeadlessException
     133            Desktop.getDesktop(); result = mockDesktop; times = 1;
     134        }};
     135        new Expectations() {{
     136            mockDesktop.browse(withNotNull()); times = 1;
     137        }};
     138
     139        hook.openUrl(Config.getUrls().getJOSMWebsite());
     140    }
     141
     142    /**
     143     * Test method for {@code PlatformHookWindows#openUrl} when Desktop fails
     144     * @param mockDesktop desktop mock
     145     * @throws IOException if an error occurs
     146     */
     147    @Test
     148    public void testOpenUrlFallback(@Injectable final Desktop mockDesktop) throws IOException {
     149        TestUtils.assumeWorkingJMockit();
     150        new Expectations(Desktop.class) {{
     151            // real implementation would raise HeadlessException
     152            Desktop.getDesktop(); result = mockDesktop; times = 1;
     153        }};
     154        new Expectations() {{
     155            mockDesktop.browse(withNotNull()); result = new IOException(); times = 1;
     156        }};
     157        final Runtime anyRuntime = Runtime.getRuntime();
     158        new Expectations(Runtime.class) {{
     159            anyRuntime.exec(new String[] {"rundll32", "url.dll,FileProtocolHandler", Config.getUrls().getJOSMWebsite()});
     160            result = null;
     161            times = 1;
     162            // prevent a non-matching invocation being executed
     163            anyRuntime.exec((String[]) withNotNull()); result = null; times = 0;
     164        }};
     165
     166        hook.openUrl(Config.getUrls().getJOSMWebsite());
    134167    }
    135168
Note: See TracChangeset for help on using the changeset viewer.