From ceb55c444e3ce729b8366c6c13b04a0d19ffb47a Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Thu, 11 Oct 2018 23:45:29 +0100
Subject: [PATCH v1] PluginHandlerTest: fix for non-headless mode by properly
 mocking dialogs

---
 .../openstreetmap/josm/plugins/PluginHandler.java  | 20 ++---
 .../josm/plugins/PluginHandlerTest.java            | 88 ++++++++++++++++++----
 2 files changed, 83 insertions(+), 25 deletions(-)

diff --git a/src/org/openstreetmap/josm/plugins/PluginHandler.java b/src/org/openstreetmap/josm/plugins/PluginHandler.java
index 2fd2b5637..431f5ba15 100644
--- a/src/org/openstreetmap/josm/plugins/PluginHandler.java
+++ b/src/org/openstreetmap/josm/plugins/PluginHandler.java
@@ -182,10 +182,8 @@ public final class PluginHandler {
             a.setEditable(false);
             a.setText(text);
             a.setCaretPosition(0);
-            if (!GraphicsEnvironment.isHeadless()) {
-                JOptionPane.showMessageDialog(MainApplication.getMainFrame(), new JScrollPane(a), tr("Plugin information"),
-                        JOptionPane.INFORMATION_MESSAGE);
-            }
+            JOptionPane.showMessageDialog(MainApplication.getMainFrame(), new JScrollPane(a), tr("Plugin information"),
+                    JOptionPane.INFORMATION_MESSAGE);
         }
     }
 
@@ -351,14 +349,12 @@ public final class PluginHandler {
             sb.append("</li>");
         }
         sb.append("</ul></html>");
-        if (!GraphicsEnvironment.isHeadless()) {
-            JOptionPane.showMessageDialog(
-                    parent,
-                    sb.toString(),
-                    tr("Warning"),
-                    JOptionPane.WARNING_MESSAGE
-            );
-        }
+        JOptionPane.showMessageDialog(
+                parent,
+                sb.toString(),
+                tr("Warning"),
+                JOptionPane.WARNING_MESSAGE
+        );
     }
 
     /**
diff --git a/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTest.java b/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTest.java
index 5ca507e8e..c73d5931c 100644
--- a/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTest.java
+++ b/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTest.java
@@ -9,17 +9,23 @@ import static org.junit.Assert.assertTrue;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import javax.swing.JScrollPane;
 
 import org.junit.Rule;
 import org.junit.Test;
 import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.gui.MainApplication;
 import org.openstreetmap.josm.gui.preferences.plugin.PluginPreferenceTest;
+import org.openstreetmap.josm.gui.widgets.JosmTextArea;
 import org.openstreetmap.josm.plugins.PluginHandler.DeprecatedPlugin;
 import org.openstreetmap.josm.plugins.PluginHandler.PluginInformationAction;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
+import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
+import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
 import org.openstreetmap.josm.tools.Utils;
 
+import com.google.common.collect.ImmutableMap;
+
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import nl.jqno.equalsverifier.EqualsVerifier;
 
@@ -49,6 +55,17 @@ public class PluginHandlerTest {
      */
     @Test
     public void testBuildListOfPluginsToLoad() {
+        TestUtils.assumeWorkingJMockit();
+        final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker() {
+            @Override
+            public String getStringFromMessage(final Object message) {
+                return ((String) message).substring(0, 66);
+            }
+        };
+        haMocker.getMockResultMap().put(
+            "<html>JOSM could not find information about the following plugins:",
+            "OK"
+        );
         final String old = System.getProperty("josm.plugins");
         try {
             System.setProperty("josm.plugins",
@@ -64,6 +81,11 @@ public class PluginHandlerTest {
                 System.clearProperty("josm.plugins");
             }
         }
+
+        assertEquals(1, haMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
+        assertEquals(0, (int) invocationLogEntry[0]);
+        assertEquals("Warning", invocationLogEntry[2]);
     }
 
     /**
@@ -71,10 +93,23 @@ public class PluginHandlerTest {
      */
     @Test
     public void testFilterDeprecatedPlugins() {
+        TestUtils.assumeWorkingJMockit();
+        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
+            ImmutableMap.<String, Object>of(
+                "<html>The following plugin is no longer necessary and has been deactivated:<ul><li>imagery (integrated into main program)</li></ul></html>",
+                0
+            )
+        );
+
         List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "imagery"));
         PluginHandler.filterDeprecatedPlugins(MainApplication.getMainFrame(), plugins);
         assertEquals(2, plugins.size());
         assertFalse(plugins.contains("imagery"));
+
+        assertEquals(1, jopsMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
+        assertEquals(0, (int) invocationLogEntry[0]);
+        assertEquals("Warning", invocationLogEntry[2]);
     }
 
     /**
@@ -82,10 +117,23 @@ public class PluginHandlerTest {
      */
     @Test
     public void testFilterUnmaintainedPlugins() {
+        TestUtils.assumeWorkingJMockit();
+        final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
+            ImmutableMap.<String, Object>of(
+                "<html>Loading of the plugin \"gpsbabelgui\" was requested.<br>This plugin is no longer developed and very likely will produce errors.<br>It should be disabled.<br>Delete from preferences?</html>",
+                "Disable plugin"
+            )
+        );
+
         List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "gpsbabelgui"));
         PluginHandler.filterUnmaintainedPlugins(MainApplication.getMainFrame(), plugins);
         assertEquals(2, plugins.size());
         assertFalse(plugins.contains("gpsbabelgui"));
+
+        assertEquals(1, haMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
+        assertEquals(0, (int) invocationLogEntry[0]);
+        assertEquals("Disable plugin", invocationLogEntry[2]);
     }
 
     /**
@@ -94,20 +142,34 @@ public class PluginHandlerTest {
      */
     @Test
     public void testPluginInformationAction() throws PluginException {
+        TestUtils.assumeWorkingJMockit();
+        final String expectedText = "Ant-Version: Apache Ant 1.9.6\n" +
+            "Author: Don-vip\n" +
+            "Created-By: 1.7.0_91-b02 (Oracle Corporation)\n" +
+            "Manifest-Version: 1.0\n" +
+            "Plugin-Canloadatruntime: true\n" +
+            "Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin\n" +
+            "Plugin-Date: 2015-11-19T08:21:07.645033Z\n" +
+            "Plugin-Description: Handling of French EPCIs (boundary=local_authority)\n" +
+            "Plugin-Early: true\n" +
+            "Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr\n" +
+            "Plugin-Mainversion: 7001\n" +
+            "Plugin-Version: 31772\n";
+        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
+            @Override
+            public String getStringFromMessage(final Object message) {
+                return ((JosmTextArea) ((JScrollPane) message).getViewport().getView()).getText();
+            }
+        };
+        jopsMocker.getMockResultMap().put(expectedText, 0);
+
         PluginInformationAction action = new PluginInformationAction(PluginPreferenceTest.getDummyPluginInformation());
-        assertEquals(
-                "Ant-Version: Apache Ant 1.9.6\n" +
-                "Author: Don-vip\n" +
-                "Created-By: 1.7.0_91-b02 (Oracle Corporation)\n" +
-                "Manifest-Version: 1.0\n" +
-                "Plugin-Canloadatruntime: true\n" +
-                "Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin\n" +
-                "Plugin-Date: 2015-11-19T08:21:07.645033Z\n" +
-                "Plugin-Description: Handling of French EPCIs (boundary=local_authority)\n" +
-                "Plugin-Early: true\n" +
-                "Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr\n" +
-                "Plugin-Mainversion: 7001\n" +
-                "Plugin-Version: 31772\n", action.getText());
+        assertEquals(expectedText, action.getText());
         action.actionPerformed(null);
+
+        assertEquals(1, jopsMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
+        assertEquals(0, (int) invocationLogEntry[0]);
+        assertEquals("Plugin information", invocationLogEntry[2]);
     }
 }
-- 
2.11.0

