source: josm/trunk/test/unit/org/openstreetmap/josm/plugins/PluginHandlerMultiVersionTest.java@ 14213

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

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

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