Changeset 14412 in josm


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

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

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/ClipboardUtilsTest.java

    r11921 r14412  
    77import static org.junit.Assert.assertSame;
    88import static org.junit.Assert.assertTrue;
     9import static org.junit.Assume.assumeTrue;
    910
    1011import java.awt.GraphicsEnvironment;
     
    118119    @Test
    119120    public void testSystemSelectionDoesNotFail() {
    120         assertTrue(GraphicsEnvironment.isHeadless());
     121        assumeTrue(GraphicsEnvironment.isHeadless());
    121122        assertNull(ClipboardUtils.getSystemSelection());
    122123    }
  • trunk/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java

    r14369 r14412  
    1313import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
    1414import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
     15import org.openstreetmap.josm.tools.PlatformHook;
     16import org.openstreetmap.josm.tools.PlatformManager;
    1517
    1618import com.google.common.collect.ImmutableMap;
    1719
    1820import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     21import mockit.Expectations;
     22import mockit.Injectable;
    1923
    2024/**
     
    9195
    9296    /**
    93      * Unit test of {@link HelpBrowser.EditAction} class.
    94      */
    95     @Test
    96     public void testEditAction() {
    97         TestUtils.assumeWorkingJMockit();
     97     * Unit test of {@link HelpBrowser.EditAction} class handling a null url.
     98     * @param mockPlatformHook platform hook mock
     99     * @throws Exception  in case of error
     100     */
     101    @Test
     102    public void testEditActionNull(@Injectable final PlatformHook mockPlatformHook) throws Exception {
     103        TestUtils.assumeWorkingJMockit();
     104        new Expectations(PlatformManager.class) {{
     105            PlatformManager.getPlatform(); result = mockPlatformHook; minTimes = 0;
     106        }};
     107        new Expectations() {{
     108            // should not be called
     109            mockPlatformHook.openUrl((String) any); times = 0;
     110        }};
     111
    98112        IHelpBrowser browser = newHelpBrowser();
    99113        assertNull(browser.getUrl());
    100114        new HelpBrowser.EditAction(browser).actionPerformed(null);
    101 
     115    }
     116
     117    /**
     118     * Unit test of {@link HelpBrowser.EditAction} class handling an "internal" url.
     119     * @param mockPlatformHook platform hook mock
     120     * @throws Exception  in case of error
     121     */
     122    @Test
     123    public void testEditActionInternal(@Injectable final PlatformHook mockPlatformHook) throws Exception {
     124        TestUtils.assumeWorkingJMockit();
     125        new Expectations(PlatformManager.class) {{
     126            PlatformManager.getPlatform(); result = mockPlatformHook;
     127        }};
     128        new Expectations() {{
     129            mockPlatformHook.openUrl(URL_2 + "?action=edit"); result = null; times = 1;
     130        }};
     131
     132        IHelpBrowser browser = newHelpBrowser();
    102133        browser.openUrl(URL_2);
    103134        assertEquals(URL_2, browser.getUrl());
    104135        new HelpBrowser.EditAction(browser).actionPerformed(null);
    105 
     136    }
     137
     138    /**
     139     * Unit test of {@link HelpBrowser.EditAction} class handling an "external" url.
     140     * @param mockPlatformHook platform hook mock
     141     * @throws Exception  in case of error
     142     */
     143    @Test
     144    public void testEditActionExternal(@Injectable final PlatformHook mockPlatformHook) throws Exception {
     145        TestUtils.assumeWorkingJMockit();
     146        new Expectations(PlatformManager.class) {{
     147            PlatformManager.getPlatform(); result = mockPlatformHook; minTimes = 0;
     148        }};
     149        new Expectations() {{
     150            // should not be called
     151            mockPlatformHook.openUrl((String) any); times = 0;
     152        }};
    106153        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
    107154            ImmutableMap.<String, Object>of(
     
    113160        );
    114161
     162        IHelpBrowser browser = newHelpBrowser();
    115163        browser.openUrl(URL_3);
    116164        assertEquals(URL_3, browser.getUrl());
     
    125173    /**
    126174     * Unit test of {@link HelpBrowser.OpenInBrowserAction} class.
    127      */
    128     @Test
    129     public void testOpenInBrowserAction() {
     175     * @param mockPlatformHook platform hook mock
     176     * @throws Exception  in case of error
     177     */
     178    @Test
     179    public void testOpenInBrowserAction(@Injectable final PlatformHook mockPlatformHook) throws Exception {
     180        TestUtils.assumeWorkingJMockit();
     181        new Expectations(PlatformManager.class) {{
     182            PlatformManager.getPlatform(); result = mockPlatformHook;
     183        }};
     184        new Expectations() {{
     185            mockPlatformHook.openUrl(URL_1); result = null; times = 1;
     186        }};
     187
    130188        IHelpBrowser browser = newHelpBrowser();
    131189        browser.openUrl(URL_1);
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java

    r13031 r14412  
    44import static org.junit.Assert.assertEquals;
    55
    6 import java.net.MalformedURLException;
    76import java.net.URL;
    87
     
    109import org.junit.Test;
    1110import org.openstreetmap.josm.JOSMFixture;
     11import org.openstreetmap.josm.TestUtils;
    1212import org.openstreetmap.josm.data.coor.LatLon;
    1313import org.openstreetmap.josm.data.gpx.GpxData;
    1414import org.openstreetmap.josm.data.gpx.WayPoint;
     15import org.openstreetmap.josm.tools.PlatformManager;
     16import org.openstreetmap.josm.tools.PlatformHook;
     17
     18
     19import mockit.Expectations;
     20import mockit.Injectable;
    1521
    1622/**
     
    2935    /**
    3036     * Unit test of {@link WebMarker#WebMarker}.
    31      * @throws MalformedURLException never
     37     * @param mockPlatformHook platform hook mock
     38     * @throws Exception  in case of error
    3239     */
    3340    @Test
    34     public void testWebMarker() throws MalformedURLException {
     41    public void testWebMarker(@Injectable final PlatformHook mockPlatformHook) throws Exception {
     42        TestUtils.assumeWorkingJMockit();
     43        new Expectations(PlatformManager.class) {{
     44            PlatformManager.getPlatform(); result = mockPlatformHook;
     45        }};
     46        new Expectations() {{
     47            mockPlatformHook.openUrl("http://example.com"); result = null; times = 1;
     48        }};
     49
    3550        WebMarker marker = new WebMarker(
    3651                LatLon.ZERO,
  • 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.