1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 |
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.stream.Collectors;
|
---|
14 | import java.util.stream.Stream;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 |
|
---|
19 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
20 | import org.junit.jupiter.api.Test;
|
---|
21 | import org.openstreetmap.josm.TestUtils;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.plugin.PluginPreferenceTest;
|
---|
24 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
25 | import org.openstreetmap.josm.plugins.PluginHandler.DeprecatedPlugin;
|
---|
26 | import org.openstreetmap.josm.plugins.PluginHandler.PluginInformationAction;
|
---|
27 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
28 | import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
|
---|
29 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
---|
30 |
|
---|
31 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
32 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unit tests of {@link PluginHandler} class.
|
---|
36 | */
|
---|
37 | class PluginHandlerTest {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Setup test.
|
---|
41 | */
|
---|
42 | @RegisterExtension
|
---|
43 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
44 | public JOSMTestRules test = new JOSMTestRules();
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Unit test of methods {@link DeprecatedPlugin#equals} and {@link DeprecatedPlugin#hashCode}.
|
---|
48 | */
|
---|
49 | @Test
|
---|
50 | void testEqualsContract() {
|
---|
51 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
52 | EqualsVerifier.forClass(DeprecatedPlugin.class).usingGetClass().verify();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Unit test of {@link PluginHandler#buildListOfPluginsToLoad}.
|
---|
57 | */
|
---|
58 | @Test
|
---|
59 | void testBuildListOfPluginsToLoad() {
|
---|
60 | TestUtils.assumeWorkingJMockit();
|
---|
61 | final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
|
---|
62 | PluginHandler.UNMAINTAINED_PLUGINS.stream().collect(
|
---|
63 | Collectors.toMap(PluginHandler::getUnmaintainedPluginMessage, p -> "Disable plugin")));
|
---|
64 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker();
|
---|
65 | jopsMocker.getMockResultMap().put(
|
---|
66 | PluginHandler.getRemovedPluginsMessage(PluginHandler.DEPRECATED_PLUGINS),
|
---|
67 | JOptionPane.OK_OPTION
|
---|
68 | );
|
---|
69 | final String old = System.getProperty("josm.plugins");
|
---|
70 | try {
|
---|
71 | final String plugins = Stream.concat(
|
---|
72 | PluginHandler.DEPRECATED_PLUGINS.stream().map(p -> p.name),
|
---|
73 | PluginHandler.UNMAINTAINED_PLUGINS.stream()
|
---|
74 | ).collect(Collectors.joining(","));
|
---|
75 | System.setProperty("josm.plugins", plugins);
|
---|
76 | List<PluginInformation> list = PluginHandler.buildListOfPluginsToLoad(null, null);
|
---|
77 | assertNotNull(list);
|
---|
78 | assertTrue(list.isEmpty());
|
---|
79 | } finally {
|
---|
80 | if (old != null) {
|
---|
81 | System.setProperty("josm.plugins", old);
|
---|
82 | } else {
|
---|
83 | System.clearProperty("josm.plugins");
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
---|
88 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
89 | assertEquals(0, (int) invocationLogEntry[0]);
|
---|
90 | assertEquals("Warning", invocationLogEntry[2]);
|
---|
91 |
|
---|
92 | assertEquals(PluginHandler.UNMAINTAINED_PLUGINS.size(), haMocker.getInvocationLog().size());
|
---|
93 | invocationLogEntry = haMocker.getInvocationLog().get(0);
|
---|
94 | assertEquals(0, (int) invocationLogEntry[0]);
|
---|
95 | assertEquals("Disable plugin", invocationLogEntry[2]);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Unit test of {@link PluginHandler#filterDeprecatedPlugins}.
|
---|
100 | */
|
---|
101 | @Test
|
---|
102 | void testFilterDeprecatedPlugins() {
|
---|
103 | TestUtils.assumeWorkingJMockit();
|
---|
104 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
|
---|
105 | Collections.singletonMap(
|
---|
106 | "<html>The following plugin is no longer necessary and has been deactivated:" +
|
---|
107 | "<ul><li>imagery (integrated into main program)</li></ul></html>",
|
---|
108 | 0
|
---|
109 | )
|
---|
110 | );
|
---|
111 |
|
---|
112 | List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "imagery"));
|
---|
113 | PluginHandler.filterDeprecatedPlugins(MainApplication.getMainFrame(), plugins);
|
---|
114 | assertEquals(2, plugins.size());
|
---|
115 | assertFalse(plugins.contains("imagery"));
|
---|
116 |
|
---|
117 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
---|
118 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
119 | assertEquals(0, (int) invocationLogEntry[0]);
|
---|
120 | assertEquals("Warning", invocationLogEntry[2]);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Unit test of {@link PluginHandler#filterUnmaintainedPlugins}.
|
---|
125 | */
|
---|
126 | @Test
|
---|
127 | void testFilterUnmaintainedPlugins() {
|
---|
128 | TestUtils.assumeWorkingJMockit();
|
---|
129 | final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
|
---|
130 | Collections.singletonMap(
|
---|
131 | "<html>Loading of the plugin \"gpsbabelgui\" was requested.<br>" +
|
---|
132 | "This plugin is no longer developed and very likely will produce errors.<br>" +
|
---|
133 | "It should be disabled.<br>Delete from preferences?</html>",
|
---|
134 | "Disable plugin"
|
---|
135 | )
|
---|
136 | );
|
---|
137 |
|
---|
138 | List<String> plugins = new ArrayList<>(Arrays.asList("foo", "bar", "gpsbabelgui"));
|
---|
139 | PluginHandler.filterUnmaintainedPlugins(MainApplication.getMainFrame(), plugins);
|
---|
140 | assertEquals(2, plugins.size());
|
---|
141 | assertFalse(plugins.contains("gpsbabelgui"));
|
---|
142 |
|
---|
143 | assertEquals(1, haMocker.getInvocationLog().size());
|
---|
144 | Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
|
---|
145 | assertEquals(0, (int) invocationLogEntry[0]);
|
---|
146 | assertEquals("Disable plugin", invocationLogEntry[2]);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Unit test of {@link PluginInformationAction} class.
|
---|
151 | * @throws PluginException if an error occurs
|
---|
152 | */
|
---|
153 | @Test
|
---|
154 | void testPluginInformationAction() throws PluginException {
|
---|
155 | TestUtils.assumeWorkingJMockit();
|
---|
156 | final String expectedText = "Ant-Version: Apache Ant 1.9.6\n" +
|
---|
157 | "Author: Don-vip\n" +
|
---|
158 | "Created-By: 1.7.0_91-b02 (Oracle Corporation)\n" +
|
---|
159 | "Manifest-Version: 1.0\n" +
|
---|
160 | "Plugin-Canloadatruntime: true\n" +
|
---|
161 | "Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin\n" +
|
---|
162 | "Plugin-Date: 2015-11-19T08:21:07.645033Z\n" +
|
---|
163 | "Plugin-Description: Handling of French EPCIs (boundary=local_authority)\n" +
|
---|
164 | "Plugin-Early: true\n" +
|
---|
165 | "Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr\n" +
|
---|
166 | "Plugin-Mainversion: 7001\n" +
|
---|
167 | "Plugin-Version: 31772\n";
|
---|
168 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
|
---|
169 | @Override
|
---|
170 | public String getStringFromMessage(final Object message) {
|
---|
171 | return ((JosmTextArea) ((JScrollPane) message).getViewport().getView()).getText();
|
---|
172 | }
|
---|
173 | };
|
---|
174 | jopsMocker.getMockResultMap().put(expectedText, 0);
|
---|
175 |
|
---|
176 | PluginInformationAction action = new PluginInformationAction(PluginPreferenceTest.getDummyPluginInformation());
|
---|
177 | assertEquals(expectedText, action.getText());
|
---|
178 | action.actionPerformed(null);
|
---|
179 |
|
---|
180 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
---|
181 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
182 | assertEquals(0, (int) invocationLogEntry[0]);
|
---|
183 | assertEquals("Plugin information", invocationLogEntry[2]);
|
---|
184 | }
|
---|
185 | }
|
---|