From c1c417ac2c903ca05ee81985eb26d8548c4cb4ce Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 3 Nov 2018 19:20:11 +0000
Subject: [PATCH v1 2/2] OpenBrowser-covering tests: fix for windows through
 mocking

---
 .../josm/gui/help/HelpBrowserTest.java             | 57 ++++++++++++++++++++--
 .../josm/gui/layer/markerlayer/WebMarkerTest.java  | 19 ++++++--
 2 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java b/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java
index f4271d519..62f54ab49 100644
--- a/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java
@@ -12,9 +12,14 @@ import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
 import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
 import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
+import org.openstreetmap.josm.tools.PlatformManager;
+import org.openstreetmap.josm.tools.PlatformHook;
 
 import com.google.common.collect.ImmutableMap;
 
+import mockit.Expectations;
+import mockit.Injectable;
+
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 /**
@@ -90,19 +95,56 @@ public class HelpBrowserTest {
     }
 
     /**
-     * Unit test of {@link HelpBrowser.EditAction} class.
+     * Unit test of {@link HelpBrowser.EditAction} class handling a null url.
      */
     @Test
-    public void testEditAction() {
+    public void testEditActionNull(@Injectable final PlatformHook mockPlatformHook) throws Exception {
         TestUtils.assumeWorkingJMockit();
+        new Expectations(PlatformManager.class) {{
+            PlatformManager.getPlatform(); result = mockPlatformHook; minTimes = 0;
+        }};
+        new Expectations() {{
+            // should not be called
+            mockPlatformHook.openUrl((String) any); times = 0;
+        }};
+
         IHelpBrowser browser = newHelpBrowser();
         assertNull(browser.getUrl());
         new HelpBrowser.EditAction(browser).actionPerformed(null);
+    }
 
+    /**
+     * Unit test of {@link HelpBrowser.EditAction} class handling an "internal" url.
+     */
+    @Test
+    public void testEditActionInternal(@Injectable final PlatformHook mockPlatformHook) throws Exception {
+        TestUtils.assumeWorkingJMockit();
+        new Expectations(PlatformManager.class) {{
+            PlatformManager.getPlatform(); result = mockPlatformHook;
+        }};
+        new Expectations() {{
+            mockPlatformHook.openUrl(URL_2 + "?action=edit"); result = null; times = 1;
+        }};
+
+        IHelpBrowser browser = newHelpBrowser();
         browser.openUrl(URL_2);
         assertEquals(URL_2, browser.getUrl());
         new HelpBrowser.EditAction(browser).actionPerformed(null);
+    }
 
+    /**
+     * Unit test of {@link HelpBrowser.EditAction} class handling an "external" url.
+     */
+    @Test
+    public void testEditActionExternal(@Injectable final PlatformHook mockPlatformHook) throws Exception {
+        TestUtils.assumeWorkingJMockit();
+        new Expectations(PlatformManager.class) {{
+            PlatformManager.getPlatform(); result = mockPlatformHook; minTimes = 0;
+        }};
+        new Expectations() {{
+            // should not be called
+            mockPlatformHook.openUrl((String) any); times = 0;
+        }};
         final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
             ImmutableMap.<String, Object>of(
                 "<html>The current URL <tt>https://josm.openstreetmap.de/javadoc</tt><br>is an external "
@@ -112,6 +154,7 @@ public class HelpBrowserTest {
             )
         );
 
+        IHelpBrowser browser = newHelpBrowser();
         browser.openUrl(URL_3);
         assertEquals(URL_3, browser.getUrl());
         new HelpBrowser.EditAction(browser).actionPerformed(null);
@@ -126,7 +169,15 @@ public class HelpBrowserTest {
      * Unit test of {@link HelpBrowser.OpenInBrowserAction} class.
      */
     @Test
-    public void testOpenInBrowserAction() {
+    public void testOpenInBrowserAction(@Injectable final PlatformHook mockPlatformHook) throws Exception {
+        TestUtils.assumeWorkingJMockit();
+        new Expectations(PlatformManager.class) {{
+            PlatformManager.getPlatform(); result = mockPlatformHook;
+        }};
+        new Expectations() {{
+            mockPlatformHook.openUrl(URL_1); result = null; times = 1;
+        }};
+
         IHelpBrowser browser = newHelpBrowser();
         browser.openUrl(URL_1);
         assertEquals(URL_1, browser.getUrl());
diff --git a/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java b/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java
index 11c2eaa43..e1ba802d1 100644
--- a/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java
@@ -3,15 +3,21 @@ package org.openstreetmap.josm.gui.layer.markerlayer;
 
 import static org.junit.Assert.assertEquals;
 
-import java.net.MalformedURLException;
 import java.net.URL;
 
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.openstreetmap.josm.JOSMFixture;
+import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.data.coor.LatLon;
 import org.openstreetmap.josm.data.gpx.GpxData;
 import org.openstreetmap.josm.data.gpx.WayPoint;
+import org.openstreetmap.josm.tools.PlatformManager;
+import org.openstreetmap.josm.tools.PlatformHook;
+
+
+import mockit.Expectations;
+import mockit.Injectable;
 
 /**
  * Unit tests of {@link WebMarker} class.
@@ -28,10 +34,17 @@ public class WebMarkerTest {
 
     /**
      * Unit test of {@link WebMarker#WebMarker}.
-     * @throws MalformedURLException never
      */
     @Test
-    public void testWebMarker() throws MalformedURLException {
+    public void testWebMarker(@Injectable final PlatformHook mockPlatformHook) throws Exception {
+        TestUtils.assumeWorkingJMockit();
+        new Expectations(PlatformManager.class) {{
+            PlatformManager.getPlatform(); result = mockPlatformHook;
+        }};
+        new Expectations() {{
+            mockPlatformHook.openUrl("http://example.com"); result = null; times = 1;
+        }};
+
         WebMarker marker = new WebMarker(
                 LatLon.ZERO,
                 new URL("http://example.com"),
-- 
2.11.0

