source: josm/trunk/test/unit/org/openstreetmap/josm/gui/preferences/plugin/PluginPreferenceHighLevelTest.java@ 14153

Last change on this file since 14153 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

  • Property svn:eol-style set to native
File size: 36.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.plugin;
3
4import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
5import static java.util.concurrent.TimeUnit.MILLISECONDS;
6import static org.junit.Assert.assertEquals;
7import static org.junit.Assert.assertFalse;
8import static org.junit.Assert.assertTrue;
9
10import java.awt.Component;
11import java.io.File;
12import java.nio.file.Files;
13import java.util.Collection;
14
15import javax.swing.JOptionPane;
16
17import org.awaitility.Awaitility;
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Rule;
21import org.junit.Test;
22import org.openstreetmap.josm.TestUtils;
23import org.openstreetmap.josm.data.Preferences;
24import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
25import org.openstreetmap.josm.gui.util.GuiHelper;
26import org.openstreetmap.josm.plugins.PluginHandler;
27import org.openstreetmap.josm.plugins.PluginProxy;
28import org.openstreetmap.josm.spi.preferences.Config;
29import org.openstreetmap.josm.testutils.JOSMTestRules;
30import org.openstreetmap.josm.testutils.PluginServer;
31import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
32import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
33
34import com.github.tomakehurst.wiremock.client.WireMock;
35import com.github.tomakehurst.wiremock.junit.WireMockRule;
36import com.google.common.collect.ImmutableList;
37import com.google.common.collect.ImmutableMap;
38
39import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
40import mockit.MockUp;
41
42/**
43 * Higher level tests of {@link PluginPreference} class.
44 */
45public class PluginPreferenceHighLevelTest {
46 /**
47 * Setup test.
48 */
49 @Rule
50 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
51 public JOSMTestRules test = new JOSMTestRules().assumeRevision(
52 "Revision: 10000\n"
53 ).preferences().main().assertionsInEDT();
54
55 /**
56 * Plugin server mock.
57 */
58 @Rule
59 public WireMockRule pluginServerRule = new WireMockRule(
60 options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot())
61 );
62
63 /**
64 * Setup test.
65 * @throws ReflectiveOperationException never
66 */
67 @Before
68 public void setUp() throws ReflectiveOperationException {
69
70 // some other tests actually go ahead and load plugins (notably at time of writing,
71 // MainApplicationTest$testUpdateAndLoadPlugins), which really isn't a reversible operation.
72 // it is, however, possible to pretend to our tests temporarily that they *aren't* loaded by
73 // setting the PluginHandler#pluginList to empty for the duration of this test. ideally these
74 // other tests wouldn't be so badly behaved or would at least do this from a separate batch
75 // but this works for now
76 @SuppressWarnings("unchecked")
77 final Collection<PluginProxy> pluginList = (Collection<PluginProxy>) TestUtils.getPrivateStaticField(
78 PluginHandler.class,
79 "pluginList"
80 );
81 this.originalPluginList = ImmutableList.copyOf(pluginList);
82 pluginList.clear();
83
84 Config.getPref().putInt("pluginmanager.version", 999);
85 Config.getPref().put("pluginmanager.lastupdate", "999");
86 Config.getPref().putList("pluginmanager.sites",
87 ImmutableList.of(String.format("http://localhost:%s/plugins", this.pluginServerRule.port()))
88 );
89
90 this.referenceDummyJarOld = new File(TestUtils.getTestDataRoot(), "__files/plugin/dummy_plugin.v31701.jar");
91 this.referenceDummyJarNew = new File(TestUtils.getTestDataRoot(), "__files/plugin/dummy_plugin.v31772.jar");
92 this.referenceBazJarOld = new File(TestUtils.getTestDataRoot(), "__files/plugin/baz_plugin.v6.jar");
93 this.referenceBazJarNew = new File(TestUtils.getTestDataRoot(), "__files/plugin/baz_plugin.v7.jar");
94 this.pluginDir = Preferences.main().getPluginsDirectory();
95 this.targetDummyJar = new File(this.pluginDir, "dummy_plugin.jar");
96 this.targetDummyJarNew = new File(this.pluginDir, "dummy_plugin.jar.new");
97 this.targetBazJar = new File(this.pluginDir, "baz_plugin.jar");
98 this.targetBazJarNew = new File(this.pluginDir, "baz_plugin.jar.new");
99 this.pluginDir.mkdirs();
100 }
101
102 /**
103 * Tear down.
104 * @throws ReflectiveOperationException never
105 */
106 @After
107 public void tearDown() throws ReflectiveOperationException {
108 // restore actual PluginHandler#pluginList
109 @SuppressWarnings("unchecked")
110 final Collection<PluginProxy> pluginList = (Collection<PluginProxy>) TestUtils.getPrivateStaticField(
111 PluginHandler.class,
112 "pluginList"
113 );
114 pluginList.clear();
115 pluginList.addAll(this.originalPluginList);
116 }
117
118 private Collection<PluginProxy> originalPluginList;
119
120 private File pluginDir;
121 private File referenceDummyJarOld;
122 private File referenceDummyJarNew;
123 private File referenceBazJarOld;
124 private File referenceBazJarNew;
125 private File targetDummyJar;
126 private File targetDummyJarNew;
127 private File targetBazJar;
128 private File targetBazJarNew;
129
130 /**
131 * Tests choosing a new plugin to install without upgrading an already-installed plugin
132 * @throws Exception never
133 */
134 @Test
135 public void testInstallWithoutUpdate() throws Exception {
136 final PluginServer pluginServer = new PluginServer(
137 new PluginServer.RemotePlugin(this.referenceDummyJarNew),
138 new PluginServer.RemotePlugin(this.referenceBazJarOld),
139 new PluginServer.RemotePlugin(null, ImmutableMap.of("Plugin-Version", "2"), "irrelevant_plugin")
140 );
141 pluginServer.applyToWireMockServer(this.pluginServerRule);
142 Config.getPref().putList("plugins", ImmutableList.of("dummy_plugin"));
143
144 final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
145 ImmutableMap.<String, Object>of(
146 "<html>The following plugin has been downloaded <strong>successfully</strong>:"
147 + "<ul><li>baz_plugin (6)</li></ul>"
148 + "You have to restart JOSM for some settings to take effect."
149 + "<br/><br/>Would you like to restart now?</html>",
150 "Cancel"
151 )
152 );
153
154 Files.copy(this.referenceDummyJarOld.toPath(), this.targetDummyJar.toPath());
155
156 final PreferenceTabbedPane tabbedPane = new PreferenceTabbedPane();
157
158 tabbedPane.buildGui();
159 // PluginPreference is already added to PreferenceTabbedPane by default
160 tabbedPane.selectTabByPref(PluginPreference.class);
161
162 GuiHelper.runInEDTAndWait(
163 () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "downloadListButton")).doClick()
164 );
165
166 Awaitility.await().atMost(2000, MILLISECONDS).until(() -> Config.getPref().getInt("pluginmanager.version", 999) != 999);
167
168 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
169 WireMock.resetAllRequests();
170
171 final PluginPreferencesModel model = (PluginPreferencesModel) TestUtils.getPrivateField(
172 tabbedPane.getPluginPreference(),
173 "model"
174 );
175
176 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
177 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
178 // questionably correct
179 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
180 assertEquals(model.getDisplayedPlugins(), model.getAvailablePlugins());
181
182 assertEquals(
183 ImmutableList.of("baz_plugin", "dummy_plugin", "irrelevant_plugin"),
184 model.getAvailablePlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
185 );
186 assertEquals(
187 ImmutableList.of("dummy_plugin"),
188 model.getSelectedPlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
189 );
190 assertEquals(
191 ImmutableList.of("(null)", "31701", "(null)"),
192 model.getAvailablePlugins().stream().map(
193 (pi) -> pi.localversion == null ? "(null)" : pi.localversion
194 ).collect(ImmutableList.toImmutableList())
195 );
196 assertEquals(
197 ImmutableList.of("6", "31772", "2"),
198 model.getAvailablePlugins().stream().map((pi) -> pi.version).collect(ImmutableList.toImmutableList())
199 );
200
201 // now we're going to choose to install baz_plugin
202 model.setPluginSelected("baz_plugin", true);
203
204 assertEquals(
205 ImmutableList.of("baz_plugin"),
206 model.getNewlyActivatedPlugins().stream().map(
207 (pi) -> pi.getName()
208 ).collect(ImmutableList.toImmutableList())
209 );
210 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
211 assertEquals(
212 ImmutableList.of("baz_plugin"),
213 model.getPluginsScheduledForUpdateOrDownload().stream().map(
214 (pi) -> pi.getName()
215 ).collect(ImmutableList.toImmutableList())
216 );
217
218 tabbedPane.savePreferences();
219
220 TestUtils.syncEDTAndWorkerThreads();
221
222 assertEquals(1, haMocker.getInvocationLog().size());
223 Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
224 assertEquals(1, (int) invocationLogEntry[0]);
225 assertEquals("Restart", invocationLogEntry[2]);
226
227 // dummy_plugin jar shouldn't have been updated
228 TestUtils.assertFileContentsEqual(this.referenceDummyJarOld, this.targetDummyJar);
229 // baz_plugin jar should have been installed
230 TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
231
232 // neither of these .jar.new files should have been left hanging round
233 assertFalse(targetDummyJarNew.exists());
234 assertFalse(targetBazJarNew.exists());
235
236 // the advertized version of dummy_plugin shouldn't have been fetched
237 this.pluginServerRule.verify(0, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
238 // but the advertized version of baz_plugin *should* have
239 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.v6.jar")));
240
241 // pluginmanager.version has been set to the current version
242 // questionably correct
243 assertEquals(10000, Config.getPref().getInt("pluginmanager.version", 111));
244 // however pluginmanager.lastupdate hasn't been updated
245 // questionably correct
246 assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
247
248 // baz_plugin should have been added to the plugins list
249 assertEquals(
250 ImmutableList.of("baz_plugin", "dummy_plugin"),
251 Config.getPref().getList("plugins", null).stream().sorted().collect(ImmutableList.toImmutableList())
252 );
253 }
254
255 /**
256 * Tests a plugin being disabled without applying available upgrades
257 * @throws Exception never
258 */
259 @Test
260 public void testDisablePluginWithUpdatesAvailable() throws Exception {
261 final PluginServer pluginServer = new PluginServer(
262 new PluginServer.RemotePlugin(this.referenceDummyJarNew),
263 new PluginServer.RemotePlugin(this.referenceBazJarNew),
264 new PluginServer.RemotePlugin(null, null, "irrelevant_plugin")
265 );
266 pluginServer.applyToWireMockServer(this.pluginServerRule);
267 Config.getPref().putList("plugins", ImmutableList.of("baz_plugin", "dummy_plugin"));
268
269 final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
270 ImmutableMap.<String, Object>of(
271 "<html>You have to restart JOSM for some settings to take effect."
272 + "<br/><br/>Would you like to restart now?</html>",
273 "Cancel"
274 )
275 );
276
277 Files.copy(this.referenceDummyJarOld.toPath(), this.targetDummyJar.toPath());
278 Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
279
280 final PreferenceTabbedPane tabbedPane = new PreferenceTabbedPane();
281
282 tabbedPane.buildGui();
283 // PluginPreference is already added to PreferenceTabbedPane by default
284 tabbedPane.selectTabByPref(PluginPreference.class);
285
286 GuiHelper.runInEDTAndWait(
287 () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "downloadListButton")).doClick()
288 );
289
290 Awaitility.await().atMost(2000, MILLISECONDS).until(() -> Config.getPref().getInt("pluginmanager.version", 999) != 999);
291
292 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
293 WireMock.resetAllRequests();
294
295 final PluginPreferencesModel model = (PluginPreferencesModel) TestUtils.getPrivateField(
296 tabbedPane.getPluginPreference(),
297 "model"
298 );
299
300 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
301 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
302 // questionably correct
303 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
304 assertEquals(model.getDisplayedPlugins(), model.getAvailablePlugins());
305
306 assertEquals(
307 ImmutableList.of("baz_plugin", "dummy_plugin", "irrelevant_plugin"),
308 model.getAvailablePlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
309 );
310 assertEquals(
311 ImmutableList.of("baz_plugin", "dummy_plugin"),
312 model.getSelectedPlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
313 );
314 assertEquals(
315 ImmutableList.of("6", "31701", "(null)"),
316 model.getAvailablePlugins().stream().map(
317 (pi) -> pi.localversion == null ? "(null)" : pi.localversion
318 ).collect(ImmutableList.toImmutableList())
319 );
320 assertEquals(
321 ImmutableList.of("7", "31772", "(null)"),
322 model.getAvailablePlugins().stream().map(
323 (pi) -> pi.version == null ? "(null)" : pi.version
324 ).collect(ImmutableList.toImmutableList())
325 );
326
327 // now we're going to choose to disable baz_plugin
328 model.setPluginSelected("baz_plugin", false);
329
330 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
331 assertEquals(
332 ImmutableList.of("baz_plugin"),
333 model.getNewlyDeactivatedPlugins().stream().map(
334 (pi) -> pi.getName()
335 ).collect(ImmutableList.toImmutableList())
336 );
337 // questionably correct
338 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
339
340 tabbedPane.savePreferences();
341
342 TestUtils.syncEDTAndWorkerThreads();
343
344 assertEquals(1, haMocker.getInvocationLog().size());
345 Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
346 assertEquals(1, (int) invocationLogEntry[0]);
347 assertEquals("Restart", invocationLogEntry[2]);
348
349 // dummy_plugin jar shouldn't have been updated
350 TestUtils.assertFileContentsEqual(this.referenceDummyJarOld, this.targetDummyJar);
351 // baz_plugin jar shouldn't have been deleted
352 TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
353
354 // neither of these .jar.new files have a reason to be here
355 assertFalse(targetDummyJarNew.exists());
356 assertFalse(targetBazJarNew.exists());
357
358 // neither of the new jars have been fetched
359 this.pluginServerRule.verify(0, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
360 this.pluginServerRule.verify(0, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.v6.jar")));
361
362 // pluginmanager.version has been set to the current version
363 // questionably correct
364 assertEquals(10000, Config.getPref().getInt("pluginmanager.version", 111));
365 // however pluginmanager.lastupdate hasn't been updated
366 // questionably correct
367 assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
368
369 // baz_plugin should have been removed from the installed plugins list
370 assertEquals(
371 ImmutableList.of("dummy_plugin"),
372 Config.getPref().getList("plugins", null).stream().sorted().collect(ImmutableList.toImmutableList())
373 );
374 }
375
376 /**
377 * Demonstrates behaviour exhibited when attempting to update a single plugin when multiple updates
378 * are available by deselecting it before clicking the update button then reselecting it.
379 *
380 * This is probably NOT desirable and should be fixed, however this test documents the behaviour.
381 * @throws Exception never
382 */
383 @Test
384 public void testUpdateOnlySelectedPlugin() throws Exception {
385 TestUtils.assumeWorkingJMockit();
386 final PluginServer pluginServer = new PluginServer(
387 new PluginServer.RemotePlugin(this.referenceDummyJarNew),
388 new PluginServer.RemotePlugin(this.referenceBazJarNew)
389 );
390 pluginServer.applyToWireMockServer(this.pluginServerRule);
391 Config.getPref().putList("plugins", ImmutableList.of("baz_plugin", "dummy_plugin"));
392
393 final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker();
394 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker();
395
396 Files.copy(this.referenceDummyJarOld.toPath(), this.targetDummyJar.toPath());
397 Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
398
399 final PreferenceTabbedPane tabbedPane = new PreferenceTabbedPane();
400
401 tabbedPane.buildGui();
402 // PluginPreference is already added to PreferenceTabbedPane by default
403 tabbedPane.selectTabByPref(PluginPreference.class);
404
405 GuiHelper.runInEDTAndWait(
406 () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "downloadListButton")).doClick()
407 );
408
409 TestUtils.syncEDTAndWorkerThreads();
410
411 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
412 WireMock.resetAllRequests();
413
414 final PluginPreferencesModel model = (PluginPreferencesModel) TestUtils.getPrivateField(
415 tabbedPane.getPluginPreference(),
416 "model"
417 );
418
419 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
420 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
421 // questionably correct
422 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
423 assertEquals(model.getDisplayedPlugins(), model.getAvailablePlugins());
424
425 assertEquals(
426 ImmutableList.of("baz_plugin", "dummy_plugin"),
427 model.getAvailablePlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
428 );
429 assertEquals(
430 ImmutableList.of("baz_plugin", "dummy_plugin"),
431 model.getSelectedPlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
432 );
433 assertEquals(
434 ImmutableList.of("6", "31701"),
435 model.getAvailablePlugins().stream().map(
436 (pi) -> pi.localversion == null ? "(null)" : pi.localversion
437 ).collect(ImmutableList.toImmutableList())
438 );
439 assertEquals(
440 ImmutableList.of("7", "31772"),
441 model.getAvailablePlugins().stream().map((pi) -> pi.version).collect(ImmutableList.toImmutableList())
442 );
443
444 // now we're going to choose not to update baz_plugin
445 model.setPluginSelected("baz_plugin", false);
446
447 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
448 assertEquals(
449 ImmutableList.of("baz_plugin"),
450 model.getNewlyDeactivatedPlugins().stream().map(
451 pi -> pi.getName()
452 ).collect(ImmutableList.toImmutableList())
453 );
454 // questionably correct
455 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
456
457 // prepare haMocker to handle this message
458 haMocker.getMockResultMap().put(
459 "<html>The following plugin has been downloaded <strong>successfully</strong>:"
460 + "<ul><li>dummy_plugin (31772)</li></ul>Please restart JOSM to activate the "
461 + "downloaded plugins.</html>",
462 "OK"
463 );
464
465 GuiHelper.runInEDTAndWait(
466 () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "updatePluginsButton")).doClick()
467 );
468
469 TestUtils.syncEDTAndWorkerThreads();
470
471 assertTrue(jopsMocker.getInvocationLog().isEmpty());
472 assertEquals(1, haMocker.getInvocationLog().size());
473 Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
474 assertEquals(0, (int) invocationLogEntry[0]);
475 assertEquals("Update plugins", invocationLogEntry[2]);
476
477 // dummy_plugin jar should have been updated
478 TestUtils.assertFileContentsEqual(this.referenceDummyJarNew, this.targetDummyJar);
479 // but baz_plugin jar shouldn't have been
480 TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
481
482 // any .jar.new files should have been removed
483 assertFalse(targetDummyJarNew.exists());
484 assertFalse(targetBazJarNew.exists());
485
486 // the plugin list was rechecked
487 // questionably necessary
488 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
489 // dummy_plugin has been fetched
490 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
491 // baz_plugin has not
492 this.pluginServerRule.verify(0, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.v7.jar")));
493 WireMock.resetAllRequests();
494
495 // pluginmanager.version has been set to the current version
496 // questionably correct
497 assertEquals(10000, Config.getPref().getInt("pluginmanager.version", 111));
498 // however pluginmanager.lastupdate hasn't been updated
499 // questionably correct
500 assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
501
502 // plugins list shouldn't have been altered, we haven't hit save yet
503 assertEquals(
504 ImmutableList.of("baz_plugin", "dummy_plugin"),
505 Config.getPref().getList("plugins", null).stream().sorted().collect(ImmutableList.toImmutableList())
506 );
507
508 // the model's selection state should be largely as before
509 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
510 assertEquals(
511 ImmutableList.of("baz_plugin"),
512 model.getNewlyDeactivatedPlugins().stream().map(
513 (pi) -> pi.getName()
514 ).collect(ImmutableList.toImmutableList())
515 );
516 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
517
518 // but now we re-select baz_plugin so that it isn't removed/disabled
519 model.setPluginSelected("baz_plugin", true);
520
521 // this has caused baz_plugin to be interpreted as a plugin "for download"
522 // questionably correct
523 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
524 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
525 assertEquals(
526 ImmutableList.of("baz_plugin"),
527 model.getPluginsScheduledForUpdateOrDownload().stream().map(
528 (pi) -> pi.getName()
529 ).collect(ImmutableList.toImmutableList())
530 );
531
532 // prepare jopsMocker to handle this message
533 jopsMocker.getMockResultMap().put(
534 "<html>The following plugin has been downloaded <strong>successfully</strong>:"
535 + "<ul><li>baz_plugin (7)</li></ul></html>",
536 JOptionPane.OK_OPTION
537 );
538
539 tabbedPane.savePreferences();
540
541 TestUtils.syncEDTAndWorkerThreads();
542
543 // from previous haMocker invocation
544 assertEquals(1, haMocker.getInvocationLog().size());
545 // we've been alerted that (the new version of) baz_plugin was installed
546 // questionably correct
547 assertEquals(1, jopsMocker.getInvocationLog().size());
548 invocationLogEntry = jopsMocker.getInvocationLog().get(0);
549 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
550 assertEquals("Warning", invocationLogEntry[2]);
551
552 // dummy_plugin jar is still the updated version
553 TestUtils.assertFileContentsEqual(this.referenceDummyJarNew, this.targetDummyJar);
554 // but now the baz_plugin jar has been too
555 // questionably correct
556 TestUtils.assertFileContentsEqual(this.referenceBazJarNew, this.targetBazJar);
557
558 // all .jar.new files have been deleted
559 assertFalse(targetDummyJarNew.exists());
560 assertFalse(targetBazJarNew.exists());
561
562 // dummy_plugin was not fetched
563 this.pluginServerRule.verify(0, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
564 // baz_plugin however was fetched
565 // questionably correct
566 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.v7.jar")));
567
568 assertEquals(10000, Config.getPref().getInt("pluginmanager.version", 111));
569 // questionably correct
570 assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
571 }
572
573 /**
574 * Tests the effect of requesting a "plugin update" when everything is up to date
575 * @throws Exception never
576 */
577 @Test
578 public void testUpdateWithNoAvailableUpdates() throws Exception {
579 TestUtils.assumeWorkingJMockit();
580 final PluginServer pluginServer = new PluginServer(
581 new PluginServer.RemotePlugin(this.referenceDummyJarOld),
582 new PluginServer.RemotePlugin(this.referenceBazJarOld),
583 new PluginServer.RemotePlugin(null, ImmutableMap.of("Plugin-Version", "123"), "irrelevant_plugin")
584 );
585 pluginServer.applyToWireMockServer(this.pluginServerRule);
586 Config.getPref().putList("plugins", ImmutableList.of("baz_plugin", "dummy_plugin"));
587
588 final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(
589 ImmutableMap.<String, Object>of(
590 "All installed plugins are up to date. JOSM does not have to download newer versions.",
591 "OK"
592 )
593 );
594 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker();
595
596 Files.copy(this.referenceDummyJarOld.toPath(), this.targetDummyJar.toPath());
597 Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
598
599 final PreferenceTabbedPane tabbedPane = new PreferenceTabbedPane();
600
601 tabbedPane.buildGui();
602 // PluginPreference is already added to PreferenceTabbedPane by default
603 tabbedPane.selectTabByPref(PluginPreference.class);
604
605 GuiHelper.runInEDTAndWait(
606 () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "downloadListButton")).doClick()
607 );
608
609 TestUtils.syncEDTAndWorkerThreads();
610
611 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
612 WireMock.resetAllRequests();
613
614 final PluginPreferencesModel model = (PluginPreferencesModel) TestUtils.getPrivateField(
615 tabbedPane.getPluginPreference(),
616 "model"
617 );
618
619 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
620 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
621 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
622 assertEquals(model.getDisplayedPlugins(), model.getAvailablePlugins());
623
624 assertEquals(
625 ImmutableList.of("baz_plugin", "dummy_plugin", "irrelevant_plugin"),
626 model.getAvailablePlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
627 );
628 assertEquals(
629 ImmutableList.of("baz_plugin", "dummy_plugin"),
630 model.getSelectedPlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
631 );
632 assertEquals(
633 ImmutableList.of("6", "31701", "(null)"),
634 model.getAvailablePlugins().stream().map(
635 (pi) -> pi.localversion == null ? "(null)" : pi.localversion
636 ).collect(ImmutableList.toImmutableList())
637 );
638 assertEquals(
639 ImmutableList.of("6", "31701", "123"),
640 model.getAvailablePlugins().stream().map((pi) -> pi.version).collect(ImmutableList.toImmutableList())
641 );
642
643 GuiHelper.runInEDTAndWait(
644 () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "updatePluginsButton")).doClick()
645 );
646
647 TestUtils.syncEDTAndWorkerThreads();
648
649 assertTrue(jopsMocker.getInvocationLog().isEmpty());
650 assertEquals(1, haMocker.getInvocationLog().size());
651 Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
652 assertEquals(0, (int) invocationLogEntry[0]);
653 assertEquals("Plugins up to date", invocationLogEntry[2]);
654
655 // neither jar should have changed
656 TestUtils.assertFileContentsEqual(this.referenceDummyJarOld, this.targetDummyJar);
657 TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
658
659 // no reason for any .jar.new files
660 assertFalse(targetDummyJarNew.exists());
661 assertFalse(targetBazJarNew.exists());
662
663 // the plugin list was rechecked
664 // questionably necessary
665 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
666 // that should have been the only request to our PluginServer
667 assertEquals(1, this.pluginServerRule.getAllServeEvents().size());
668 WireMock.resetAllRequests();
669
670 // pluginmanager.version has been set to the current version
671 assertEquals(10000, Config.getPref().getInt("pluginmanager.version", 111));
672 // pluginmanager.lastupdate hasn't been updated
673 // questionably correct
674 assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
675
676 // plugins list shouldn't have been altered
677 assertEquals(
678 ImmutableList.of("baz_plugin", "dummy_plugin"),
679 Config.getPref().getList("plugins", null).stream().sorted().collect(ImmutableList.toImmutableList())
680 );
681
682 // the model's selection state should be largely as before
683 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
684 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
685 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
686
687 tabbedPane.savePreferences();
688
689 TestUtils.syncEDTAndWorkerThreads();
690
691 assertTrue(jopsMocker.getInvocationLog().isEmpty());
692 assertEquals(1, haMocker.getInvocationLog().size());
693
694 // both jars are still the original version
695 TestUtils.assertFileContentsEqual(this.referenceDummyJarOld, this.targetDummyJar);
696 TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
697
698 // no reason for any .jar.new files
699 assertFalse(targetDummyJarNew.exists());
700 assertFalse(targetBazJarNew.exists());
701
702 // none of PluginServer's URLs should have been touched
703 assertEquals(0, this.pluginServerRule.getAllServeEvents().size());
704
705 // pluginmanager.version has been set to the current version
706 assertEquals(10000, Config.getPref().getInt("pluginmanager.version", 111));
707 // pluginmanager.lastupdate hasn't been updated
708 // questionably correct
709 assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
710 }
711
712 /**
713 * Tests installing a single plugin which is marked as "Canloadatruntime"
714 * @throws Exception never
715 */
716 @Test
717 public void testInstallWithoutRestartRequired() throws Exception {
718 TestUtils.assumeWorkingJMockit();
719 final boolean[] loadPluginsCalled = new boolean[] {false};
720 new MockUp<PluginHandler>() {
721 @mockit.Mock
722 private void loadPlugins(
723 final Component parent,
724 final Collection<org.openstreetmap.josm.plugins.PluginInformation> plugins,
725 final org.openstreetmap.josm.gui.progress.ProgressMonitor monitor
726 ) {
727 assertEquals(1, plugins.size());
728 assertEquals("dummy_plugin", plugins.iterator().next().name);
729 assertEquals("31772", plugins.iterator().next().localversion);
730 loadPluginsCalled[0] = true;
731 }
732 };
733
734 final PluginServer pluginServer = new PluginServer(
735 new PluginServer.RemotePlugin(this.referenceDummyJarNew),
736 new PluginServer.RemotePlugin(this.referenceBazJarNew)
737 );
738 pluginServer.applyToWireMockServer(this.pluginServerRule);
739 Config.getPref().putList("plugins", ImmutableList.of());
740
741 final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker();
742 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(ImmutableMap.<String, Object>of(
743 "<html>The following plugin has been downloaded <strong>successfully</strong>:"
744 + "<ul><li>dummy_plugin (31772)</li></ul></html>",
745 JOptionPane.OK_OPTION
746 ));
747
748 final PreferenceTabbedPane tabbedPane = new PreferenceTabbedPane();
749
750 tabbedPane.buildGui();
751 // PluginPreference is already added to PreferenceTabbedPane by default
752 tabbedPane.selectTabByPref(PluginPreference.class);
753
754 GuiHelper.runInEDTAndWait(
755 () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "downloadListButton")).doClick()
756 );
757
758 TestUtils.syncEDTAndWorkerThreads();
759
760 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
761 WireMock.resetAllRequests();
762
763 final PluginPreferencesModel model = (PluginPreferencesModel) TestUtils.getPrivateField(
764 tabbedPane.getPluginPreference(),
765 "model"
766 );
767
768 assertTrue(model.getNewlyActivatedPlugins().isEmpty());
769 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
770 assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
771 assertEquals(model.getDisplayedPlugins(), model.getAvailablePlugins());
772
773 assertEquals(
774 ImmutableList.of("baz_plugin", "dummy_plugin"),
775 model.getAvailablePlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
776 );
777 assertTrue(model.getSelectedPlugins().isEmpty());
778 assertEquals(
779 ImmutableList.of("(null)", "(null)"),
780 model.getAvailablePlugins().stream().map(
781 (pi) -> pi.localversion == null ? "(null)" : pi.localversion
782 ).collect(ImmutableList.toImmutableList())
783 );
784 assertEquals(
785 ImmutableList.of("7", "31772"),
786 model.getAvailablePlugins().stream().map((pi) -> pi.version).collect(ImmutableList.toImmutableList())
787 );
788
789 // now we select dummy_plugin
790 model.setPluginSelected("dummy_plugin", true);
791
792 // model should now reflect this
793 assertEquals(
794 ImmutableList.of("dummy_plugin"),
795 model.getNewlyActivatedPlugins().stream().map(
796 pi -> pi.getName()
797 ).collect(ImmutableList.toImmutableList())
798 );
799 assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
800
801 tabbedPane.savePreferences();
802
803 TestUtils.syncEDTAndWorkerThreads();
804
805 assertEquals(1, jopsMocker.getInvocationLog().size());
806 org.openstreetmap.josm.tools.Logging.error(jopsMocker.getInvocationLog().get(0)[0].toString());
807 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
808 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
809 assertEquals("Warning", invocationLogEntry[2]);
810
811 assertTrue(haMocker.getInvocationLog().isEmpty());
812
813 // any .jar.new files should have been deleted
814 assertFalse(targetDummyJarNew.exists());
815 assertFalse(targetBazJarNew.exists());
816
817 // dummy_plugin was fetched
818 this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
819
820 // loadPlugins(...) was called (with expected parameters)
821 assertTrue(loadPluginsCalled[0]);
822
823 // pluginmanager.version has been set to the current version
824 assertEquals(10000, Config.getPref().getInt("pluginmanager.version", 111));
825 // pluginmanager.lastupdate hasn't been updated
826 // questionably correct
827 assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
828 }
829}
Note: See TracBrowser for help on using the repository browser.