| 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.jupiter.api.Assertions.assertEquals;
|
|---|
| 6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
|---|
| 7 | import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|---|
| 8 |
|
|---|
| 9 | import java.io.File;
|
|---|
| 10 | import java.nio.file.Files;
|
|---|
| 11 | import java.util.Arrays;
|
|---|
| 12 | import java.util.Collections;
|
|---|
| 13 | import java.util.Comparator;
|
|---|
| 14 | import java.util.HashMap;
|
|---|
| 15 | import java.util.List;
|
|---|
| 16 | import java.util.Map;
|
|---|
| 17 | import java.util.stream.Collectors;
|
|---|
| 18 |
|
|---|
| 19 | import org.junit.jupiter.api.BeforeEach;
|
|---|
| 20 | import org.junit.jupiter.api.Test;
|
|---|
| 21 | import org.junit.jupiter.api.extension.RegisterExtension;
|
|---|
| 22 | import org.openstreetmap.josm.TestUtils;
|
|---|
| 23 | import org.openstreetmap.josm.data.Preferences;
|
|---|
| 24 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 25 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 26 | import org.openstreetmap.josm.testutils.PluginServer;
|
|---|
| 27 | import org.openstreetmap.josm.testutils.annotations.AssumeRevision;
|
|---|
| 28 | import org.openstreetmap.josm.testutils.annotations.FullPreferences;
|
|---|
| 29 | import org.openstreetmap.josm.testutils.annotations.Main;
|
|---|
| 30 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
|
|---|
| 31 |
|
|---|
| 32 | import com.github.tomakehurst.wiremock.client.WireMock;
|
|---|
| 33 | import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
|
|---|
| 34 | import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Test parts of {@link PluginHandler} class with plugins that advertise multiple versions for compatibility.
|
|---|
| 38 | */
|
|---|
| 39 | @FullPreferences
|
|---|
| 40 | @Main
|
|---|
| 41 | class PluginHandlerMultiVersionTest {
|
|---|
| 42 | /**
|
|---|
| 43 | * Plugin server mock.
|
|---|
| 44 | */
|
|---|
| 45 | @RegisterExtension
|
|---|
| 46 | static WireMockExtension pluginServerRule = WireMockExtension.newInstance()
|
|---|
| 47 | .options(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot())).build();
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Setup test.
|
|---|
| 51 | */
|
|---|
| 52 | @BeforeEach
|
|---|
| 53 | void setUp() {
|
|---|
| 54 | Config.getPref().putInt("pluginmanager.version", 999);
|
|---|
| 55 | Config.getPref().put("pluginmanager.lastupdate", "999");
|
|---|
| 56 | Config.getPref().putList("pluginmanager.sites",
|
|---|
| 57 | Collections.singletonList(pluginServerRule.url("/plugins"))
|
|---|
| 58 | );
|
|---|
| 59 |
|
|---|
| 60 | this.referenceBazJarOld = new File(TestUtils.getTestDataRoot(), "__files/plugin/baz_plugin.v6.jar");
|
|---|
| 61 | this.referenceQuxJarOld = new File(TestUtils.getTestDataRoot(), "__files/" + referencePathQuxJarOld);
|
|---|
| 62 | this.referenceQuxJarNewer = new File(TestUtils.getTestDataRoot(), "__files/" + referencePathQuxJarNewer);
|
|---|
| 63 | this.referenceQuxJarNewest = new File(TestUtils.getTestDataRoot(), "__files/" + referencePathQuxJarNewest);
|
|---|
| 64 | this.pluginDir = Preferences.main().getPluginsDirectory();
|
|---|
| 65 | this.targetBazJar = new File(this.pluginDir, "baz_plugin.jar");
|
|---|
| 66 | this.targetBazJarNew = new File(this.pluginDir, "baz_plugin.jar.new");
|
|---|
| 67 | this.targetQuxJar = new File(this.pluginDir, "qux_plugin.jar");
|
|---|
| 68 | this.targetQuxJarNew = new File(this.pluginDir, "qux_plugin.jar.new");
|
|---|
| 69 | this.pluginDir.mkdirs();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | private static final String referencePathQuxJarOld = "plugin/qux_plugin.v345.jar";
|
|---|
| 73 | private static final String referencePathQuxJarNewer = "plugin/qux_plugin.v432.jar";
|
|---|
| 74 | private static final String referencePathQuxJarNewest = "plugin/qux_plugin.v435.jar";
|
|---|
| 75 |
|
|---|
| 76 | private File pluginDir;
|
|---|
| 77 | private File referenceBazJarOld;
|
|---|
| 78 | private File referenceQuxJarOld;
|
|---|
| 79 | private File referenceQuxJarNewer;
|
|---|
| 80 | private File referenceQuxJarNewest;
|
|---|
| 81 | private File targetBazJar;
|
|---|
| 82 | private File targetBazJarNew;
|
|---|
| 83 | private File targetQuxJar;
|
|---|
| 84 | private File targetQuxJarNew;
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * test update of plugins when our current JOSM version prevents us from using the latest version,
|
|---|
| 88 | * but an additional version is listed which *does* support our version
|
|---|
| 89 | * @throws Exception on failure
|
|---|
| 90 | */
|
|---|
| 91 | @AssumeRevision("Revision: 7501\n")
|
|---|
| 92 | @Test
|
|---|
| 93 | void testUpdatePluginsOneMultiVersion() throws Exception {
|
|---|
| 94 | TestUtils.assumeWorkingJMockit();
|
|---|
| 95 |
|
|---|
| 96 | final String quxNewerServePath = "/qux/newer.jar";
|
|---|
| 97 | final Map<String, String> attrOverrides = new HashMap<>() {{
|
|---|
| 98 | put("7500_Plugin-Url", "432;" + pluginServerRule.url(quxNewerServePath));
|
|---|
| 99 | put("7499_Plugin-Url", "346;" + pluginServerRule.url("/not/served.jar"));
|
|---|
| 100 | put("6999_Plugin-Url", "345;" + pluginServerRule.url("/not/served/eithejar"));
|
|---|
| 101 | }};
|
|---|
| 102 | final PluginServer pluginServer = new PluginServer(
|
|---|
| 103 | new PluginServer.RemotePlugin(this.referenceBazJarOld),
|
|---|
| 104 | new PluginServer.RemotePlugin(this.referenceQuxJarNewest, attrOverrides)
|
|---|
| 105 | );
|
|---|
| 106 | final WireMockRuntimeInfo wireMockRuntimeInfo = pluginServerRule.getRuntimeInfo();
|
|---|
| 107 | pluginServer.applyToWireMockServer(wireMockRuntimeInfo);
|
|---|
| 108 | // need to actually serve this older jar from somewhere
|
|---|
| 109 | wireMockRuntimeInfo.getWireMock().register(
|
|---|
| 110 | WireMock.get(WireMock.urlEqualTo(quxNewerServePath)).willReturn(
|
|---|
| 111 | WireMock.aResponse().withStatus(200).withHeader("Content-Type", "application/java-archive").withBodyFile(
|
|---|
| 112 | referencePathQuxJarNewer
|
|---|
| 113 | )
|
|---|
| 114 | )
|
|---|
| 115 | );
|
|---|
| 116 | Config.getPref().putList("plugins", Arrays.asList("qux_plugin", "baz_plugin"));
|
|---|
| 117 |
|
|---|
| 118 | // catch any (unexpected) attempts to show us an ExtendedDialog
|
|---|
| 119 | new ExtendedDialogMocker();
|
|---|
| 120 |
|
|---|
| 121 | Files.copy(this.referenceQuxJarOld.toPath(), this.targetQuxJar.toPath());
|
|---|
| 122 | Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
|
|---|
| 123 |
|
|---|
| 124 | final List<PluginInformation> updatedPlugins = PluginHandler.updatePlugins(
|
|---|
| 125 | MainApplication.getMainFrame(),
|
|---|
| 126 | null,
|
|---|
| 127 | null,
|
|---|
| 128 | false
|
|---|
| 129 | ).stream().sorted(Comparator.comparing(a -> a.name)).collect(Collectors.toList());
|
|---|
| 130 |
|
|---|
| 131 | assertEquals(2, updatedPlugins.size());
|
|---|
| 132 |
|
|---|
| 133 | assertEquals("baz_plugin", updatedPlugins.get(0).name);
|
|---|
| 134 | assertEquals("6", updatedPlugins.get(0).localversion);
|
|---|
| 135 |
|
|---|
| 136 | assertEquals("qux_plugin", updatedPlugins.get(1).name);
|
|---|
| 137 | assertEquals("432", updatedPlugins.get(1).localversion);
|
|---|
| 138 |
|
|---|
| 139 | assertFalse(targetBazJarNew.exists());
|
|---|
| 140 | assertFalse(targetQuxJarNew.exists());
|
|---|
| 141 |
|
|---|
| 142 | TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
|
|---|
| 143 | TestUtils.assertFileContentsEqual(this.referenceQuxJarNewer, this.targetQuxJar);
|
|---|
| 144 |
|
|---|
| 145 | assertEquals(2, pluginServerRule.getAllServeEvents().size());
|
|---|
| 146 | pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
|
|---|
| 147 | pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo(quxNewerServePath)));
|
|---|
| 148 |
|
|---|
| 149 | assertEquals(7501, Config.getPref().getInt("pluginmanager.version", 111));
|
|---|
| 150 | // not mocking the time so just check it's not its original value
|
|---|
| 151 | assertNotEquals("999", Config.getPref().get("pluginmanager.lastupdate", "999"));
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * test update of plugins when our current JOSM version prevents us from using all but the version
|
|---|
| 156 | * we already have, which is still listed.
|
|---|
| 157 | * @throws Exception on failure
|
|---|
| 158 | */
|
|---|
| 159 | @AssumeRevision("Revision: 7000\n")
|
|---|
| 160 | @Test
|
|---|
| 161 | void testUpdatePluginsExistingVersionLatestPossible() throws Exception {
|
|---|
| 162 | TestUtils.assumeWorkingJMockit();
|
|---|
| 163 |
|
|---|
| 164 | final Map<String, String> attrOverrides = new HashMap<>() {{
|
|---|
| 165 | put("7500_Plugin-Url", "432;" + pluginServerRule.url("/dont.jar"));
|
|---|
| 166 | put("7499_Plugin-Url", "346;" + pluginServerRule.url("/even.jar"));
|
|---|
| 167 | put("6999_Plugin-Url", "345;" + pluginServerRule.url("/bother.jar"));
|
|---|
| 168 | }};
|
|---|
| 169 | final PluginServer pluginServer = new PluginServer(
|
|---|
| 170 | new PluginServer.RemotePlugin(this.referenceBazJarOld),
|
|---|
| 171 | new PluginServer.RemotePlugin(this.referenceQuxJarNewest, attrOverrides)
|
|---|
| 172 | );
|
|---|
| 173 | pluginServer.applyToWireMockServer(pluginServerRule.getRuntimeInfo());
|
|---|
| 174 | Config.getPref().putList("plugins", Arrays.asList("qux_plugin", "baz_plugin"));
|
|---|
| 175 |
|
|---|
| 176 | // catch any (unexpected) attempts to show us an ExtendedDialog
|
|---|
| 177 | new ExtendedDialogMocker();
|
|---|
| 178 |
|
|---|
| 179 | Files.copy(this.referenceQuxJarOld.toPath(), this.targetQuxJar.toPath());
|
|---|
| 180 | Files.copy(this.referenceBazJarOld.toPath(), this.targetBazJar.toPath());
|
|---|
| 181 |
|
|---|
| 182 | final List<PluginInformation> updatedPlugins = PluginHandler.updatePlugins(
|
|---|
| 183 | MainApplication.getMainFrame(),
|
|---|
| 184 | null,
|
|---|
| 185 | null,
|
|---|
| 186 | false
|
|---|
| 187 | ).stream().sorted(Comparator.comparing(a -> a.name)).collect(Collectors.toList());
|
|---|
| 188 |
|
|---|
| 189 | assertEquals(2, updatedPlugins.size());
|
|---|
| 190 |
|
|---|
| 191 | assertEquals("baz_plugin", updatedPlugins.get(0).name);
|
|---|
| 192 | assertEquals("6", updatedPlugins.get(0).localversion);
|
|---|
| 193 |
|
|---|
| 194 | assertEquals("qux_plugin", updatedPlugins.get(1).name);
|
|---|
| 195 | assertEquals("345", updatedPlugins.get(1).localversion);
|
|---|
| 196 |
|
|---|
| 197 | assertFalse(targetBazJarNew.exists());
|
|---|
| 198 | assertFalse(targetQuxJarNew.exists());
|
|---|
| 199 |
|
|---|
| 200 | // should be as before
|
|---|
| 201 | TestUtils.assertFileContentsEqual(this.referenceBazJarOld, this.targetBazJar);
|
|---|
| 202 | TestUtils.assertFileContentsEqual(this.referenceQuxJarOld, this.targetQuxJar);
|
|---|
| 203 |
|
|---|
| 204 | // only the plugins list should have been downloaded
|
|---|
| 205 | assertEquals(1, pluginServerRule.getAllServeEvents().size());
|
|---|
| 206 | pluginServerRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
|
|---|
| 207 |
|
|---|
| 208 | assertEquals(7000, Config.getPref().getInt("pluginmanager.version", 111));
|
|---|
| 209 | // not mocking the time so just check it's not its original value
|
|---|
| 210 | assertNotEquals("999", Config.getPref().get("pluginmanager.lastupdate", "999"));
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|