Changeset 14213 in josm


Ignore:
Timestamp:
2018-09-01T00:06:40+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #16700 - add new tests exercising handling of plugins that advertise multiple versions for compatibility (patch by ris)

Location:
trunk/test
Files:
5 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/plugin/PluginPreferenceHighLevelTest.java

    r14153 r14213  
    804804
    805805        assertEquals(1, jopsMocker.getInvocationLog().size());
    806         org.openstreetmap.josm.tools.Logging.error(jopsMocker.getInvocationLog().get(0)[0].toString());
    807806        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
    808807        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
     
    818817        this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.v31772.jar")));
    819818
     819        // the dummy_plugin jar has been installed
     820        TestUtils.assertFileContentsEqual(this.referenceDummyJarNew, this.targetDummyJar);
     821        // the baz_plugin jar has not
     822        assertFalse(this.targetBazJar.exists());
     823
    820824        // loadPlugins(...) was called (with expected parameters)
    821825        assertTrue(loadPluginsCalled[0]);
     
    827831        assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
    828832    }
     833
     834    /**
     835     * Tests installing a single plugin which has multiple versions advertised, with our JOSM version
     836     * preventing us from using the latest version
     837     * @throws Exception on failure
     838     */
     839    @JOSMTestRules.OverrideAssumeRevision("Revision: 7000\n")
     840    @Test
     841    public void testInstallMultiVersion() throws Exception {
     842        TestUtils.assumeWorkingJMockit();
     843
     844        final String bazOldServePath = "/baz/old.jar";
     845        final PluginServer pluginServer = new PluginServer(
     846            new PluginServer.RemotePlugin(this.referenceDummyJarNew),
     847            new PluginServer.RemotePlugin(this.referenceBazJarNew, ImmutableMap.of(
     848                "6800_Plugin-Url", "6;http://localhost:" + this.pluginServerRule.port() + bazOldServePath
     849            ))
     850        );
     851        pluginServer.applyToWireMockServer(this.pluginServerRule);
     852        // need to actually serve this older jar from somewhere
     853        this.pluginServerRule.stubFor(
     854            WireMock.get(WireMock.urlEqualTo(bazOldServePath)).willReturn(
     855                WireMock.aResponse().withStatus(200).withHeader("Content-Type", "application/java-archive").withBodyFile(
     856                    "plugin/baz_plugin.v6.jar"
     857                )
     858            )
     859        );
     860        Config.getPref().putList("plugins", ImmutableList.of());
     861
     862        final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker(ImmutableMap.of(
     863            "<html>The following plugin has been downloaded <strong>successfully</strong>:"
     864            + "<ul><li>baz_plugin (6)</li></ul>"
     865            + "You have to restart JOSM for some settings to take effect.<br/><br/>"
     866            + "Would you like to restart now?</html>",
     867            "Cancel"
     868        ));
     869        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker();
     870
     871        final PreferenceTabbedPane tabbedPane = new PreferenceTabbedPane();
     872
     873        tabbedPane.buildGui();
     874        // PluginPreference is already added to PreferenceTabbedPane by default
     875        tabbedPane.selectTabByPref(PluginPreference.class);
     876
     877        GuiHelper.runInEDTAndWait(
     878            () -> ((javax.swing.JButton) TestUtils.getComponentByName(tabbedPane, "downloadListButton")).doClick()
     879        );
     880
     881        TestUtils.syncEDTAndWorkerThreads();
     882
     883        this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
     884        WireMock.resetAllRequests();
     885
     886        final PluginPreferencesModel model = (PluginPreferencesModel) TestUtils.getPrivateField(
     887            tabbedPane.getPluginPreference(),
     888            "model"
     889        );
     890
     891        assertTrue(model.getNewlyActivatedPlugins().isEmpty());
     892        assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
     893        assertTrue(model.getPluginsScheduledForUpdateOrDownload().isEmpty());
     894        assertEquals(model.getDisplayedPlugins(), model.getAvailablePlugins());
     895
     896        assertEquals(
     897            ImmutableList.of("baz_plugin", "dummy_plugin"),
     898            model.getAvailablePlugins().stream().map((pi) -> pi.getName()).collect(ImmutableList.toImmutableList())
     899        );
     900        assertTrue(model.getSelectedPlugins().isEmpty());
     901        assertEquals(
     902            ImmutableList.of("(null)", "(null)"),
     903            model.getAvailablePlugins().stream().map(
     904                (pi) -> pi.localversion == null ? "(null)" : pi.localversion
     905            ).collect(ImmutableList.toImmutableList())
     906        );
     907        assertEquals(
     908            ImmutableList.of("6", "31772"),
     909            model.getAvailablePlugins().stream().map((pi) -> pi.version).collect(ImmutableList.toImmutableList())
     910        );
     911
     912        // now we select dummy_plugin
     913        model.setPluginSelected("baz_plugin", true);
     914
     915        // model should now reflect this
     916        assertEquals(
     917            ImmutableList.of("baz_plugin"),
     918            model.getNewlyActivatedPlugins().stream().map(
     919                pi -> pi.getName()
     920            ).collect(ImmutableList.toImmutableList())
     921        );
     922        assertTrue(model.getNewlyDeactivatedPlugins().isEmpty());
     923
     924        tabbedPane.savePreferences();
     925
     926        TestUtils.syncEDTAndWorkerThreads();
     927
     928        assertEquals(1, haMocker.getInvocationLog().size());
     929        Object[] invocationLogEntry = haMocker.getInvocationLog().get(0);
     930        assertEquals(1, (int) invocationLogEntry[0]);
     931        assertEquals("Restart", invocationLogEntry[2]);
     932
     933        assertTrue(jopsMocker.getInvocationLog().isEmpty());
     934
     935        // any .jar.new files should have been deleted
     936        assertFalse(targetDummyJarNew.exists());
     937        assertFalse(targetBazJarNew.exists());
     938
     939        // dummy_plugin was fetched
     940        this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo(bazOldServePath)));
     941
     942        // the "old" baz_plugin jar has been installed
     943        TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
     944        // the dummy_plugin jar has not
     945        assertFalse(this.targetDummyJar.exists());
     946
     947        // pluginmanager.version has been set to the current version
     948        assertEquals(7000, Config.getPref().getInt("pluginmanager.version", 111));
     949        // pluginmanager.lastupdate hasn't been updated
     950        // questionably correct
     951        assertEquals("999", Config.getPref().get("pluginmanager.lastupdate", "111"));
     952    }
    829953}
  • trunk/test/unit/org/openstreetmap/josm/plugins/PluginHandlerJOSMTooOldTest.java

    r14153 r14213  
    6767        this.referenceBazJarOld = new File(TestUtils.getTestDataRoot(), "__files/plugin/baz_plugin.v6.jar");
    6868        this.referenceBazJarNew = new File(TestUtils.getTestDataRoot(), "__files/plugin/baz_plugin.v7.jar");
     69        this.referenceQuxJarOld = new File(TestUtils.getTestDataRoot(), "__files/" + referencePathQuxJarOld);
     70        this.referenceQuxJarNewer = new File(TestUtils.getTestDataRoot(), "__files/" + referencePathQuxJarNewer);
    6971        this.pluginDir = Preferences.main().getPluginsDirectory();
    7072        this.targetDummyJar = new File(this.pluginDir, "dummy_plugin.jar");
     
    7274        this.targetBazJar = new File(this.pluginDir, "baz_plugin.jar");
    7375        this.targetBazJarNew = new File(this.pluginDir, "baz_plugin.jar.new");
     76        this.targetQuxJar = new File(this.pluginDir, "qux_plugin.jar");
     77        this.targetQuxJarNew = new File(this.pluginDir, "qux_plugin.jar.new");
    7478        this.pluginDir.mkdirs();
    7579    }
     80
     81    private static final String referencePathQuxJarOld = "plugin/qux_plugin.v345.jar";
     82    private static final String referencePathQuxJarNewer = "plugin/qux_plugin.v432.jar";
    7683
    7784    private File pluginDir;
     
    8087    private File referenceBazJarOld;
    8188    private File referenceBazJarNew;
     89    private File referenceQuxJarOld;
     90    private File referenceQuxJarNewer;
    8291    private File targetDummyJar;
    8392    private File targetDummyJarNew;
    8493    private File targetBazJar;
    8594    private File targetBazJarNew;
     95    private File targetQuxJar;
     96    private File targetQuxJarNew;
    8697
    8798    private final String bazPluginVersionReqString = "JOSM version 8,001 required for plugin baz_plugin.";
     
    283294        assertNotEquals(Config.getPref().get("pluginmanager.lastupdate", "999"), "999");
    284295    }
     296
     297    /**
     298     * When a plugin advertises several versions for compatibility with older JOSMs, but even the
     299     * oldest of those is newer than our JOSM version, the user is prompted to upgrade to the newest
     300     * version anyway.
     301     *
     302     * While this behaviour is not incorrect, it's probably less helpful than it could be - the
     303     * version that's most likely to work best in this case will be the "oldest" still-available
     304     * version, however this test documents the behaviour.
     305     * @throws IOException never
     306     */
     307    @Test
     308    @JOSMTestRules.OverrideAssumeRevision("Revision: 7200\n")
     309    public void testUpdatePluginsMultiVersionInsufficient() throws IOException {
     310        TestUtils.assumeWorkingJMockit();
     311
     312        final PluginServer pluginServer = new PluginServer(
     313            new PluginServer.RemotePlugin(this.referenceBazJarOld),
     314            new PluginServer.RemotePlugin(this.referenceQuxJarNewer, ImmutableMap.of(
     315                "7499_Plugin-Url", "346;http://localhost:" + this.pluginServerRule.port() + "/dont/bother.jar"
     316            ))
     317        );
     318        pluginServer.applyToWireMockServer(this.pluginServerRule);
     319        Config.getPref().putList("plugins", ImmutableList.of("qux_plugin", "baz_plugin"));
     320
     321        new ExtendedDialogMocker(ImmutableMap.of("JOSM version 7,500 required for plugin qux_plugin.", "Download Plugin"));
     322
     323        Files.copy(this.referenceQuxJarOld.toPath(), this.targetQuxJar.toPath());
     324        Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
     325
     326        final List<PluginInformation> updatedPlugins = PluginHandler.updatePlugins(
     327            MainApplication.getMainFrame(),
     328            null,
     329            null,
     330            false
     331        ).stream().sorted((a, b) -> a.name.compareTo(b.name)).collect(ImmutableList.toImmutableList());
     332
     333        assertEquals(2, updatedPlugins.size());
     334
     335        assertEquals("baz_plugin", updatedPlugins.get(0).name);
     336        assertEquals("6", updatedPlugins.get(0).localversion);
     337
     338        assertEquals("qux_plugin", updatedPlugins.get(1).name);
     339        // questionably correct
     340        assertEquals("432", updatedPlugins.get(1).localversion);
     341
     342        assertFalse(targetQuxJarNew.exists());
     343
     344        TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
     345        // questionably correct
     346        TestUtils.assertFileContentsEqual(this.referenceQuxJarNewer, this.targetQuxJar);
     347
     348        assertEquals(2, WireMock.getAllServeEvents().size());
     349        this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
     350        // questionably correct
     351        this.pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/qux_plugin.v432.jar")));
     352
     353        assertEquals(7200, Config.getPref().getInt("pluginmanager.version", 111));
     354        // not mocking the time so just check it's not its original value
     355        assertNotEquals("999", Config.getPref().get("pluginmanager.lastupdate", "999"));
     356    }
    285357}
Note: See TracChangeset for help on using the changeset viewer.