Ticket #16835: v1-0001-PluginHandlerTest-fix-for-non-headless-mode-by-pr.patch

File v1-0001-PluginHandlerTest-fix-for-non-headless-mode-by-pr.patch, 8.9 KB (added by ris, 6 years ago)
  • src/org/openstreetmap/josm/plugins/PluginHandler.java

    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 b public final class PluginHandler {  
    182182            a.setEditable(false);
    183183            a.setText(text);
    184184            a.setCaretPosition(0);
    185             if (!GraphicsEnvironment.isHeadless()) {
    186                 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), new JScrollPane(a), tr("Plugin information"),
    187                         JOptionPane.INFORMATION_MESSAGE);
    188             }
     185            JOptionPane.showMessageDialog(MainApplication.getMainFrame(), new JScrollPane(a), tr("Plugin information"),
     186                    JOptionPane.INFORMATION_MESSAGE);
    189187        }
    190188    }
    191189
    public final class PluginHandler {  
    351349            sb.append("</li>");
    352350        }
    353351        sb.append("</ul></html>");
    354         if (!GraphicsEnvironment.isHeadless()) {
    355             JOptionPane.showMessageDialog(
    356                     parent,
    357                     sb.toString(),
    358                     tr("Warning"),
    359                     JOptionPane.WARNING_MESSAGE
    360             );
    361         }
     352        JOptionPane.showMessageDialog(
     353                parent,
     354                sb.toString(),
     355                tr("Warning"),
     356                JOptionPane.WARNING_MESSAGE
     357        );
    362358    }
    363359
    364360    /**
  • test/unit/org/openstreetmap/josm/plugins/PluginHandlerTest.java

    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 b import static org.junit.Assert.assertTrue;  
    99import java.util.ArrayList;
    1010import java.util.Arrays;
    1111import java.util.List;
     12import javax.swing.JScrollPane;
    1213
    1314import org.junit.Rule;
    1415import org.junit.Test;
    1516import org.openstreetmap.josm.TestUtils;
    1617import org.openstreetmap.josm.gui.MainApplication;
    1718import org.openstreetmap.josm.gui.preferences.plugin.PluginPreferenceTest;
     19import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    1820import org.openstreetmap.josm.plugins.PluginHandler.DeprecatedPlugin;
    1921import org.openstreetmap.josm.plugins.PluginHandler.PluginInformationAction;
    2022import org.openstreetmap.josm.testutils.JOSMTestRules;
     23import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
     24import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
    2125import org.openstreetmap.josm.tools.Utils;
    2226
     27import com.google.common.collect.ImmutableMap;
     28
    2329import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2430import nl.jqno.equalsverifier.EqualsVerifier;
    2531
    public class PluginHandlerTest {  
    4955     */
    5056    @Test
    5157    public void testBuildListOfPluginsToLoad() {
     58        TestUtils.assumeWorkingJMockit();
     59        final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker() {
     60            @Override
     61            public String getStringFromMessage(final Object message) {
     62                return ((String) message).substring(0, 66);
     63            }
     64        };
     65        haMocker.getMockResultMap().put(
     66            "<html>JOSM could not find information about the following plugins:",
     67            "OK"
     68        );
    5269        final String old = System.getProperty("josm.plugins");
    5370        try {
    5471            System.setProperty("josm.plugins",
    public class PluginHandlerTest {  
    6481                System.clearProperty("josm.plugins");
    6582            }
    6683        }
     84
     85        assertEquals(1, haMocker.getInvocationLog().size());
     86        Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
     87        assertEquals(0, (int) invocationLogEntry[0]);
     88        assertEquals("Warning", invocationLogEntry[2]);
    6789    }
    6890
    6991    /**
    public class PluginHandlerTest {  
    7193     */
    7294    @Test
    7395    public void testFilterDeprecatedPlugins() {
     96        TestUtils.assumeWorkingJMockit();
     97        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
     98            ImmutableMap.<String, Object>of(
     99                "<html>The following plugin is no longer necessary and has been deactivated:<ul><li>imagery (integrated into main program)</li></ul></html>",
     100                0
     101            )
     102        );
     103
    74104        List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "imagery"));
    75105        PluginHandler.filterDeprecatedPlugins(MainApplication.getMainFrame(), plugins);
    76106        assertEquals(2, plugins.size());
    77107        assertFalse(plugins.contains("imagery"));
     108
     109        assertEquals(1, jopsMocker.getInvocationLog().size());
     110        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     111        assertEquals(0, (int) invocationLogEntry[0]);
     112        assertEquals("Warning", invocationLogEntry[2]);
    78113    }
    79114
    80115    /**
    public class PluginHandlerTest {  
    82117     */
    83118    @Test
    84119    public void testFilterUnmaintainedPlugins() {
     120        TestUtils.assumeWorkingJMockit();
     121        final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
     122            ImmutableMap.<String, Object>of(
     123                "<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>",
     124                "Disable plugin"
     125            )
     126        );
     127
    85128        List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "gpsbabelgui"));
    86129        PluginHandler.filterUnmaintainedPlugins(MainApplication.getMainFrame(), plugins);
    87130        assertEquals(2, plugins.size());
    88131        assertFalse(plugins.contains("gpsbabelgui"));
     132
     133        assertEquals(1, haMocker.getInvocationLog().size());
     134        Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
     135        assertEquals(0, (int) invocationLogEntry[0]);
     136        assertEquals("Disable plugin", invocationLogEntry[2]);
    89137    }
    90138
    91139    /**
    public class PluginHandlerTest {  
    94142     */
    95143    @Test
    96144    public void testPluginInformationAction() throws PluginException {
     145        TestUtils.assumeWorkingJMockit();
     146        final String expectedText = "Ant-Version: Apache Ant 1.9.6\n" +
     147            "Author: Don-vip\n" +
     148            "Created-By: 1.7.0_91-b02 (Oracle Corporation)\n" +
     149            "Manifest-Version: 1.0\n" +
     150            "Plugin-Canloadatruntime: true\n" +
     151            "Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin\n" +
     152            "Plugin-Date: 2015-11-19T08:21:07.645033Z\n" +
     153            "Plugin-Description: Handling of French EPCIs (boundary=local_authority)\n" +
     154            "Plugin-Early: true\n" +
     155            "Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr\n" +
     156            "Plugin-Mainversion: 7001\n" +
     157            "Plugin-Version: 31772\n";
     158        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
     159            @Override
     160            public String getStringFromMessage(final Object message) {
     161                return ((JosmTextArea) ((JScrollPane) message).getViewport().getView()).getText();
     162            }
     163        };
     164        jopsMocker.getMockResultMap().put(expectedText, 0);
     165
    97166        PluginInformationAction action = new PluginInformationAction(PluginPreferenceTest.getDummyPluginInformation());
    98         assertEquals(
    99                 "Ant-Version: Apache Ant 1.9.6\n" +
    100                 "Author: Don-vip\n" +
    101                 "Created-By: 1.7.0_91-b02 (Oracle Corporation)\n" +
    102                 "Manifest-Version: 1.0\n" +
    103                 "Plugin-Canloadatruntime: true\n" +
    104                 "Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin\n" +
    105                 "Plugin-Date: 2015-11-19T08:21:07.645033Z\n" +
    106                 "Plugin-Description: Handling of French EPCIs (boundary=local_authority)\n" +
    107                 "Plugin-Early: true\n" +
    108                 "Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr\n" +
    109                 "Plugin-Mainversion: 7001\n" +
    110                 "Plugin-Version: 31772\n", action.getText());
     167        assertEquals(expectedText, action.getText());
    111168        action.actionPerformed(null);
     169
     170        assertEquals(1, jopsMocker.getInvocationLog().size());
     171        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     172        assertEquals(0, (int) invocationLogEntry[0]);
     173        assertEquals("Plugin information", invocationLogEntry[2]);
    112174    }
    113175}