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

Last change on this file since 17275 was 17195, checked in by simon04, 4 years ago

see #15102 - see #16637 - Use WireMockServer.url()

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