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 {
|
182 | 182 | a.setEditable(false); |
183 | 183 | a.setText(text); |
184 | 184 | 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); |
189 | 187 | } |
190 | 188 | } |
191 | 189 | |
… |
… |
public final class PluginHandler {
|
351 | 349 | sb.append("</li>"); |
352 | 350 | } |
353 | 351 | 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 | ); |
362 | 358 | } |
363 | 359 | |
364 | 360 | /** |
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;
|
9 | 9 | import java.util.ArrayList; |
10 | 10 | import java.util.Arrays; |
11 | 11 | import java.util.List; |
| 12 | import javax.swing.JScrollPane; |
12 | 13 | |
13 | 14 | import org.junit.Rule; |
14 | 15 | import org.junit.Test; |
15 | 16 | import org.openstreetmap.josm.TestUtils; |
16 | 17 | import org.openstreetmap.josm.gui.MainApplication; |
17 | 18 | import org.openstreetmap.josm.gui.preferences.plugin.PluginPreferenceTest; |
| 19 | import org.openstreetmap.josm.gui.widgets.JosmTextArea; |
18 | 20 | import org.openstreetmap.josm.plugins.PluginHandler.DeprecatedPlugin; |
19 | 21 | import org.openstreetmap.josm.plugins.PluginHandler.PluginInformationAction; |
20 | 22 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| 23 | import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker; |
| 24 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; |
21 | 25 | import org.openstreetmap.josm.tools.Utils; |
22 | 26 | |
| 27 | import com.google.common.collect.ImmutableMap; |
| 28 | |
23 | 29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
24 | 30 | import nl.jqno.equalsverifier.EqualsVerifier; |
25 | 31 | |
… |
… |
public class PluginHandlerTest {
|
49 | 55 | */ |
50 | 56 | @Test |
51 | 57 | 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 | ); |
52 | 69 | final String old = System.getProperty("josm.plugins"); |
53 | 70 | try { |
54 | 71 | System.setProperty("josm.plugins", |
… |
… |
public class PluginHandlerTest {
|
64 | 81 | System.clearProperty("josm.plugins"); |
65 | 82 | } |
66 | 83 | } |
| 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]); |
67 | 89 | } |
68 | 90 | |
69 | 91 | /** |
… |
… |
public class PluginHandlerTest {
|
71 | 93 | */ |
72 | 94 | @Test |
73 | 95 | 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 | |
74 | 104 | List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "imagery")); |
75 | 105 | PluginHandler.filterDeprecatedPlugins(MainApplication.getMainFrame(), plugins); |
76 | 106 | assertEquals(2, plugins.size()); |
77 | 107 | 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]); |
78 | 113 | } |
79 | 114 | |
80 | 115 | /** |
… |
… |
public class PluginHandlerTest {
|
82 | 117 | */ |
83 | 118 | @Test |
84 | 119 | 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 | |
85 | 128 | List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "gpsbabelgui")); |
86 | 129 | PluginHandler.filterUnmaintainedPlugins(MainApplication.getMainFrame(), plugins); |
87 | 130 | assertEquals(2, plugins.size()); |
88 | 131 | 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]); |
89 | 137 | } |
90 | 138 | |
91 | 139 | /** |
… |
… |
public class PluginHandlerTest {
|
94 | 142 | */ |
95 | 143 | @Test |
96 | 144 | 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 | |
97 | 166 | 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()); |
111 | 168 | 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]); |
112 | 174 | } |
113 | 175 | } |