1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
---|
5 | import static org.junit.Assert.assertEquals;
|
---|
6 | import static org.junit.Assert.assertFalse;
|
---|
7 | import static org.junit.Assert.assertNotEquals;
|
---|
8 |
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.nio.file.Files;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.junit.Before;
|
---|
15 | import org.junit.Rule;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.TestUtils;
|
---|
19 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 | import org.openstreetmap.josm.testutils.PluginServer;
|
---|
22 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
|
---|
23 | import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
|
---|
24 |
|
---|
25 | import com.github.tomakehurst.wiremock.client.WireMock;
|
---|
26 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
---|
27 | import com.google.common.collect.ImmutableList;
|
---|
28 | import com.google.common.collect.ImmutableMap;
|
---|
29 |
|
---|
30 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Test parts of {@link PluginHandler} class when the reported JOSM version is too old for the plugin.
|
---|
34 | */
|
---|
35 | public class PluginHandlerJOSMTooOldTest {
|
---|
36 | /**
|
---|
37 | * Setup test.
|
---|
38 | */
|
---|
39 | @Rule
|
---|
40 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
41 | public JOSMTestRules test = new JOSMTestRules().preferences().main().assumeRevision(
|
---|
42 | "Revision: 6000\n"
|
---|
43 | );
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Plugin server mock.
|
---|
47 | */
|
---|
48 | @Rule
|
---|
49 | public WireMockRule pluginServerRule = new WireMockRule(
|
---|
50 | options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot())
|
---|
51 | );
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Setup test.
|
---|
55 | */
|
---|
56 | @Before
|
---|
57 | public void setUp() {
|
---|
58 | Config.getPref().putInt("pluginmanager.version", 999);
|
---|
59 | Config.getPref().put("pluginmanager.lastupdate", "999");
|
---|
60 | Config.getPref().putList("pluginmanager.sites",
|
---|
61 | ImmutableList.of(String.format("http://localhost:%s/plugins", this.pluginServerRule.port()))
|
---|
62 | );
|
---|
63 |
|
---|
64 | this.referenceDummyJarOld = new File(TestUtils.getTestDataRoot(), "__files/plugin/dummy_plugin.v31701.jar");
|
---|
65 | this.referenceDummyJarNew = new File(TestUtils.getTestDataRoot(), "__files/plugin/dummy_plugin.v31772.jar");
|
---|
66 | this.referenceBazJarOld = new File(TestUtils.getTestDataRoot(), "__files/plugin/baz_plugin.v6.jar");
|
---|
67 | this.referenceBazJarNew = new File(TestUtils.getTestDataRoot(), "__files/plugin/baz_plugin.v7.jar");
|
---|
68 | this.pluginDir = Main.pref.getPluginsDirectory();
|
---|
69 | this.targetDummyJar = new File(this.pluginDir, "dummy_plugin.jar");
|
---|
70 | this.targetDummyJarNew = new File(this.pluginDir, "dummy_plugin.jar.new");
|
---|
71 | this.targetBazJar = new File(this.pluginDir, "baz_plugin.jar");
|
---|
72 | this.targetBazJarNew = new File(this.pluginDir, "baz_plugin.jar.new");
|
---|
73 | this.pluginDir.mkdirs();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private File pluginDir;
|
---|
77 | private File referenceDummyJarOld;
|
---|
78 | private File referenceDummyJarNew;
|
---|
79 | private File referenceBazJarOld;
|
---|
80 | private File referenceBazJarNew;
|
---|
81 | private File targetDummyJar;
|
---|
82 | private File targetDummyJarNew;
|
---|
83 | private File targetBazJar;
|
---|
84 | private File targetBazJarNew;
|
---|
85 |
|
---|
86 | private final String bazPluginVersionReqString = "JOSM version 8,001 required for plugin baz_plugin.";
|
---|
87 | private final String dummyPluginVersionReqString = "JOSM version 7,001 required for plugin dummy_plugin.";
|
---|
88 | private final String dummyPluginFailedString = "<html>Updating the following plugin has failed:<ul><li>dummy_plugin</li></ul>"
|
---|
89 | + "Please open the Preference Dialog after JOSM has started and try to update it manually.</html>";
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * test update of plugins when those plugins turn out to require a higher JOSM version, but the
|
---|
93 | * user chooses to update them anyway.
|
---|
94 | * @throws IOException never
|
---|
95 | */
|
---|
96 | @Test
|
---|
97 | public void testUpdatePluginsDownloadBoth() throws IOException {
|
---|
98 | TestUtils.assumeWorkingJMockit();
|
---|
99 | final PluginServer pluginServer = new PluginServer(
|
---|
100 | new PluginServer.RemotePlugin(this.referenceDummyJarNew),
|
---|
101 | new PluginServer.RemotePlugin(this.referenceBazJarNew)
|
---|
102 | );
|
---|
103 | pluginServer.applyToWireMockServer(this.pluginServerRule);
|
---|
104 | Config.getPref().putList("plugins", ImmutableList.of("dummy_plugin", "baz_plugin"));
|
---|
105 |
|
---|
106 | final ExtendedDialogMocker edMocker = new ExtendedDialogMocker(ImmutableMap.<String, Object>builder()
|
---|
107 | .put(this.bazPluginVersionReqString, "Download Plugin")
|
---|
108 | .put(this.dummyPluginVersionReqString, "Download Plugin")
|
---|
109 | .build()
|
---|
110 | );
|
---|
111 |
|
---|
112 | Files.copy(this.referenceDummyJarOld.toPath(), this.targetDummyJar.toPath());
|
---|
113 | Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
|
---|
114 |
|
---|
115 | final List<PluginInformation> updatedPlugins = PluginHandler.updatePlugins(
|
---|
116 | Main.parent,
|
---|
117 | null,
|
---|
118 | null,
|
---|
119 | false
|
---|
120 | ).stream().sorted((a, b) -> a.name.compareTo(b.name)).collect(ImmutableList.toImmutableList());
|
---|
121 |
|
---|
122 | assertEquals(
|
---|
123 | ImmutableList.of(
|
---|
124 | this.dummyPluginVersionReqString,
|
---|
125 | this.bazPluginVersionReqString
|
---|
126 | ),
|
---|
127 | edMocker.getInvocationLog().stream().map(
|
---|
128 | invocationEntry -> invocationEntry[1]
|
---|
129 | ).sorted().collect(ImmutableList.toImmutableList())
|
---|
130 | );
|
---|
131 |
|
---|
132 | assertEquals(2, updatedPlugins.size());
|
---|
133 |
|
---|
134 | assertEquals(updatedPlugins.get(0).name, "baz_plugin");
|
---|
135 | assertEquals("7", updatedPlugins.get(0).localversion);
|
---|
136 |
|
---|
137 | assertEquals(updatedPlugins.get(1).name, "dummy_plugin");
|
---|
138 | assertEquals("31772", updatedPlugins.get(1).localversion);
|
---|
139 |
|
---|
140 | assertFalse(targetDummyJarNew.exists());
|
---|
141 | assertFalse(targetBazJarNew.exists());
|
---|
142 |
|
---|
143 | TestUtils.assertFileContentsEqual(this.referenceDummyJarNew, this.targetDummyJar);
|
---|
144 | TestUtils.assertFileContentsEqual(this.referenceBazJarNew, this.targetBazJar);
|
---|
145 |
|
---|
146 | this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
|
---|
147 | this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
|
---|
148 | this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.v7.jar")));
|
---|
149 |
|
---|
150 | assertEquals(Config.getPref().getInt("pluginmanager.version", 111), 6000);
|
---|
151 | // not mocking the time so just check it's not its original value
|
---|
152 | assertNotEquals(Config.getPref().get("pluginmanager.lastupdate", "999"), "999");
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * test update of plugins when those plugins turn out to require a higher JOSM version, but the
|
---|
157 | * user chooses to update one and skip the other.
|
---|
158 | * @throws IOException never
|
---|
159 | */
|
---|
160 | @Test
|
---|
161 | public void testUpdatePluginsSkipOne() throws IOException {
|
---|
162 | TestUtils.assumeWorkingJMockit();
|
---|
163 | final PluginServer pluginServer = new PluginServer(
|
---|
164 | new PluginServer.RemotePlugin(this.referenceDummyJarNew),
|
---|
165 | new PluginServer.RemotePlugin(this.referenceBazJarNew)
|
---|
166 | );
|
---|
167 | pluginServer.applyToWireMockServer(this.pluginServerRule);
|
---|
168 | Config.getPref().putList("plugins", ImmutableList.of("dummy_plugin", "baz_plugin"));
|
---|
169 |
|
---|
170 | final ExtendedDialogMocker edMocker = new ExtendedDialogMocker(ImmutableMap.<String, Object>builder()
|
---|
171 | .put(this.bazPluginVersionReqString, "Download Plugin")
|
---|
172 | .put(this.dummyPluginVersionReqString, "Skip Download")
|
---|
173 | .build()
|
---|
174 | );
|
---|
175 | final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(ImmutableMap.<String, Object>builder()
|
---|
176 | .put(this.dummyPluginFailedString, "OK")
|
---|
177 | .build()
|
---|
178 | );
|
---|
179 |
|
---|
180 | Files.copy(this.referenceDummyJarOld.toPath(), this.targetDummyJar.toPath());
|
---|
181 | Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
|
---|
182 |
|
---|
183 | final List<PluginInformation> updatedPlugins = PluginHandler.updatePlugins(
|
---|
184 | Main.parent,
|
---|
185 | null,
|
---|
186 | null,
|
---|
187 | false
|
---|
188 | ).stream().sorted((a, b) -> a.name.compareTo(b.name)).collect(ImmutableList.toImmutableList());
|
---|
189 |
|
---|
190 | assertEquals(
|
---|
191 | ImmutableList.of(
|
---|
192 | this.dummyPluginVersionReqString,
|
---|
193 | this.bazPluginVersionReqString
|
---|
194 | ),
|
---|
195 | edMocker.getInvocationLog().stream().map(
|
---|
196 | invocationEntry -> invocationEntry[1]
|
---|
197 | ).sorted().collect(ImmutableList.toImmutableList())
|
---|
198 | );
|
---|
199 |
|
---|
200 | assertEquals(
|
---|
201 | ImmutableList.of(
|
---|
202 | this.dummyPluginFailedString
|
---|
203 | ),
|
---|
204 | haMocker.getInvocationLog().stream().map(
|
---|
205 | invocationEntry -> invocationEntry[1]
|
---|
206 | ).sorted().collect(ImmutableList.toImmutableList())
|
---|
207 | );
|
---|
208 |
|
---|
209 | assertEquals(2, updatedPlugins.size());
|
---|
210 |
|
---|
211 | assertEquals(updatedPlugins.get(0).name, "baz_plugin");
|
---|
212 | assertEquals("7", updatedPlugins.get(0).localversion);
|
---|
213 |
|
---|
214 | assertEquals(updatedPlugins.get(1).name, "dummy_plugin");
|
---|
215 | assertEquals("31701", updatedPlugins.get(1).localversion);
|
---|
216 |
|
---|
217 | assertFalse(targetDummyJarNew.exists());
|
---|
218 | assertFalse(targetBazJarNew.exists());
|
---|
219 |
|
---|
220 | TestUtils.assertFileContentsEqual(this.referenceDummyJarOld, this.targetDummyJar);
|
---|
221 | TestUtils.assertFileContentsEqual(this.referenceBazJarNew, this.targetBazJar);
|
---|
222 |
|
---|
223 | this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
|
---|
224 | this.pluginServerRule.verify(0, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
|
---|
225 | this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.v7.jar")));
|
---|
226 |
|
---|
227 | // shouldn't have been updated
|
---|
228 | assertEquals(Config.getPref().getInt("pluginmanager.version", 111), 999);
|
---|
229 | assertEquals(Config.getPref().get("pluginmanager.lastupdate", "999"), "999");
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * When the plugin list suggests that the jar file at the provided URL *doesn't* require a newer JOSM
|
---|
234 | * but in fact the plugin served *does*, it is installed anyway.
|
---|
235 | *
|
---|
236 | * This is probably NOT desirable and should be fixed, however this test documents the behaviour.
|
---|
237 | * @throws IOException never
|
---|
238 | */
|
---|
239 | @Test
|
---|
240 | public void testUpdatePluginsUnexpectedlyJOSMTooOld() throws IOException {
|
---|
241 | TestUtils.assumeWorkingJMockit();
|
---|
242 | final PluginServer pluginServer = new PluginServer(
|
---|
243 | new PluginServer.RemotePlugin(this.referenceDummyJarNew),
|
---|
244 | new PluginServer.RemotePlugin(this.referenceBazJarNew, ImmutableMap.of(
|
---|
245 | "Plugin-Mainversion", "5500"
|
---|
246 | ))
|
---|
247 | );
|
---|
248 | pluginServer.applyToWireMockServer(this.pluginServerRule);
|
---|
249 | Config.getPref().putList("plugins", ImmutableList.of("baz_plugin"));
|
---|
250 |
|
---|
251 | // setting up blank ExtendedDialogMocker which would raise an exception if any attempt to show
|
---|
252 | // and ExtendedDialog were made
|
---|
253 | new ExtendedDialogMocker();
|
---|
254 |
|
---|
255 | Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
|
---|
256 |
|
---|
257 | final List<PluginInformation> updatedPlugins = ImmutableList.copyOf(PluginHandler.updatePlugins(
|
---|
258 | Main.parent,
|
---|
259 | null,
|
---|
260 | null,
|
---|
261 | false
|
---|
262 | ));
|
---|
263 |
|
---|
264 | // questionably correct
|
---|
265 | assertEquals(1, updatedPlugins.size());
|
---|
266 |
|
---|
267 | // questionably correct
|
---|
268 | assertEquals(updatedPlugins.get(0).name, "baz_plugin");
|
---|
269 | assertEquals("7", updatedPlugins.get(0).localversion);
|
---|
270 |
|
---|
271 | assertFalse(targetBazJarNew.exists());
|
---|
272 |
|
---|
273 | // questionably correct
|
---|
274 | TestUtils.assertFileContentsEqual(this.referenceBazJarNew, this.targetBazJar);
|
---|
275 |
|
---|
276 | this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
|
---|
277 | // questionably correct
|
---|
278 | this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.v7.jar")));
|
---|
279 |
|
---|
280 | // should have been updated
|
---|
281 | assertEquals(Config.getPref().getInt("pluginmanager.version", 111), 6000);
|
---|
282 | assertNotEquals(Config.getPref().get("pluginmanager.lastupdate", "999"), "999");
|
---|
283 | }
|
---|
284 | }
|
---|