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