From 3519931dd4381beb80da80111bdaf421f4b8f268 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 24 Feb 2018 11:48:11 +0000
Subject: [PATCH v1 5/5] tests: add PluginHandlerUpdatePluginsTest

---
 test/data/__files/plugin/MANIFEST.MF               |  12 +++
 test/data/__files/plugin/baz_plugin.jar            | Bin 0 -> 2999 bytes
 test/data/plugin/remotePluginsList                 |  25 +++++
 .../plugins/PluginHandlerUpdatePluginsTest.java    | 114 +++++++++++++++++++++
 4 files changed, 151 insertions(+)
 create mode 100644 test/data/__files/plugin/MANIFEST.MF
 create mode 100644 test/data/__files/plugin/baz_plugin.jar
 create mode 100644 test/data/plugin/remotePluginsList
 create mode 100644 test/unit/org/openstreetmap/josm/plugins/PluginHandlerUpdatePluginsTest.java

diff --git a/test/data/__files/plugin/MANIFEST.MF b/test/data/__files/plugin/MANIFEST.MF
new file mode 100644
index 000000000..fd0ed67e3
--- /dev/null
+++ b/test/data/__files/plugin/MANIFEST.MF
@@ -0,0 +1,12 @@
+Manifest-Version: 1.0
+Ant-Version: Apache Ant 1.9.6
+Created-By: 1.8.0_141-15 (Oracle Corporation)
+Plugin-Mainversion: 8001
+Plugin-Version: 7
+Plugin-Class: org.openstreetmap.josm.plugins.baz.BazPlugin
+Plugin-Description: A functionless example plugin
+Plugin-Date: 2017-12-31T00:58:30.642
+Author: Author A
+Plugin-Link: https://example.com/author-a/josm-baz
+Plugin-Icon: images/icons/24x24/baz.png
+
diff --git a/test/data/__files/plugin/baz_plugin.jar b/test/data/__files/plugin/baz_plugin.jar
new file mode 100644
index 0000000000000000000000000000000000000000..e00bd97b820bfbc096eff5ab30b3b32cc98e1d02
GIT binary patch
literal 2999
zcmWIWW@h1HVBlb22xgz}&42_r8CV#6T|*poJ^kGD|D9rB2mmS-Vc_84VBp>7<%6V>
zK?KNwEA@5s^K^3!4$<><^9?xdci2E+kNYY69~|uaFMVmu&)umIc#-*0*R&13yq&p~
zu_3>{TI6!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-aK`2Ge0O5+>E&YYZ<T_3ugwU0!Ah-2HZ^uPzpi-cq;+YpkQPY0X80BO$y{164azX
z0Bs->t`(_)fowT?xeKaE5ugm{TzI{Ta51_jP|b<}vk{tL^(=0!pqdr|j-hLX)w>7-
z&~pu_=0$+-Km#E4Fm4^7niv5jfq4=$*C7WHx{aWU3jsW_>W9|TxE%qisS%(Kw;`~s
zkIzM*ni~Oj6E+M|k0a6%C_<6zWz<yg2WTL)UI)1XmiR!LkV`gDm5l%%z*LUA0!KCn
o6u!uL15^$ofHkl-$BZ9BrDA|LE2tH~zzc*4K$kcIo9YY<00j=C$p8QV

literal 0
HcmV?d00001

diff --git a/test/data/plugin/remotePluginsList b/test/data/plugin/remotePluginsList
new file mode 100644
index 000000000..c2c2df93b
--- /dev/null
+++ b/test/data/plugin/remotePluginsList
@@ -0,0 +1,25 @@
+dummy_plugin.jar;http://localhost:%1$s/plugin/dummy_plugin.jar
+	Manifest-Version: 1.0
+	Ant-Version: Apache Ant 1.9.6
+	Author: Don-vip
+	Created-By: 1.7.0_91-b02 (Oracle Corporation)
+	Plugin-Canloadatruntime: true
+	Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin
+	Plugin-Date: 2015-11-19T08:21:07.645033Z
+	Plugin-Description: Handling of French EPCIs (boundary=local_authority)
+	Plugin-Early: true
+	Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr
+	Plugin-Mainversion: 7001
+	Plugin-Version: 31772
+baz_plugin.jar;http://localhost:%1$s/plugin/baz_plugin.jar
+	Manifest-Version: 1.0
+	Ant-Version: Apache Ant 1.9.6
+	Created-By: 1.8.0_141-15 (Oracle Corporation)
+	Plugin-Mainversion: 8001
+	Plugin-Version: 7
+	Plugin-Class: org.openstreetmap.josm.plugins.baz.BazPlugin
+	Plugin-Description: A functionless example plugin
+	Plugin-Date: 2017-12-31T00:58:30.642
+	Author: Author A
+	Plugin-Link: https://example.com/author-a/josm-baz
+	Plugin-Icon: images/icons/24x24/baz.png
diff --git a/test/unit/org/openstreetmap/josm/plugins/PluginHandlerUpdatePluginsTest.java b/test/unit/org/openstreetmap/josm/plugins/PluginHandlerUpdatePluginsTest.java
new file mode 100644
index 000000000..92455363e
--- /dev/null
+++ b/test/unit/org/openstreetmap/josm/plugins/PluginHandlerUpdatePluginsTest.java
@@ -0,0 +1,114 @@
+// License: GPL. For details, see LICENSE file.
+package org.openstreetmap.josm.plugins;
+
+import static org.junit.Assert.assertEquals;
+import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
+
+import java.io.File;
+import java.io.FileReader;
+import java.util.ArrayList;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.TestUtils;
+import org.openstreetmap.josm.spi.preferences.Config;
+import org.openstreetmap.josm.testutils.ExtendedDialogMocker;
+import org.openstreetmap.josm.testutils.JOSMTestRules;
+
+import com.google.common.io.CharStreams;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+
+/**
+ * Further unit tests of {@link PluginHandler} class.
+ */
+public class PluginHandlerUpdatePluginsTest {
+    /**
+     * Setup test.
+     */
+    @Rule
+    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
+    public JOSMTestRules test = new JOSMTestRules().preferences().main().assumeRevision(
+        "Revision: 6000\n"
+    );
+
+    /**
+     * HTTP mock.
+     */
+    @Rule
+    public WireMockRule wireMockRule = new WireMockRule(
+        options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot())
+    );
+
+    @Before
+    public void setUp() throws Exception {
+        try (
+            FileReader pluginListReader = new FileReader(
+                new File(TestUtils.getTestDataRoot(), "plugin/remotePluginsList")
+            );
+        ) {
+            final String pluginList = String.format(
+                CharStreams.toString(pluginListReader),
+                this.wireMockRule.port()
+            );
+
+            this.wireMockRule.stubFor(
+                WireMock.get(WireMock.urlEqualTo("/plugins")).willReturn(
+                    WireMock.aResponse()
+                    .withStatus(200)
+                    .withHeader("Content-Type", "text/plain")
+                    .withBody(pluginList)
+                )
+            );
+            ImmutableList.of("dummy_plugin", "baz_plugin").forEach(plugin_name -> this.wireMockRule.stubFor(
+                WireMock.get(WireMock.urlEqualTo("/plugin/" + plugin_name + ".jar")).willReturn(
+                    WireMock.aResponse()
+                    .withStatus(200)
+                    .withHeader("Content-Type", "application/java-archive")
+                    .withBodyFile("plugin/" + plugin_name + ".jar")
+                )
+            ));
+        }
+    }
+
+    /**
+     * test update of plugins when those plugins turn out to require a higher JOSM version, but the
+     * user chooses to update them anyway.
+     */
+    @Test
+    public void testUpdatePlugins() {
+        new ExtendedDialogMocker(ImmutableMap.<String, Object>builder()
+            .put("JOSM version 8,001 required for plugin baz_plugin.", "Download Plugin")
+            .put("JOSM version 7,001 required for plugin dummy_plugin.", "Download Plugin")
+            .build()
+        );
+
+        Config.getPref().putList("plugins", ImmutableList.of("dummy_plugin", "baz_plugin"));
+        Config.getPref().putList("pluginmanager.sites",
+            ImmutableList.of(String.format("http://localhost:%s/plugins", this.wireMockRule.port()))
+        );
+
+        final ArrayList<PluginInformation> updatedPlugins = new ArrayList<>(PluginHandler.updatePlugins(Main.parent, null, null, false));
+
+        updatedPlugins.sort((a, b) -> a.name.compareTo(b.name));
+
+        assertEquals(2, updatedPlugins.size());
+
+        assertEquals(updatedPlugins.get(0).name, "baz_plugin");
+        assertEquals("7", updatedPlugins.get(0).localversion);
+
+        assertEquals(updatedPlugins.get(1).name, "dummy_plugin");
+        assertEquals("31772", updatedPlugins.get(1).localversion);
+
+        this.wireMockRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugins")));
+        this.wireMockRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/dummy_plugin.jar")));
+        this.wireMockRule.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/plugin/baz_plugin.jar")));
+    }
+}
-- 
2.11.0

