1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.util.Collections;
|
---|
13 |
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.Preferences;
|
---|
17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.plugins.PluginDownloadTask;
|
---|
19 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
20 | import org.openstreetmap.josm.testutils.annotations.AssumeRevision;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Unit tests for class {@link PluginDownloadTask}.
|
---|
24 | */
|
---|
25 | @AssumeRevision("Revision: 8000\n")
|
---|
26 | class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent {
|
---|
27 | protected String pluginPath;
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | protected String getRemoteContentType() {
|
---|
31 | return "application/java-archive";
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | protected String getRemoteFile() {
|
---|
36 | return this.pluginPath;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Test download task when updating a plugin that we already have in our plugins directory
|
---|
41 | * and the downloaded file is valid
|
---|
42 | * @throws Exception if an error occurs
|
---|
43 | */
|
---|
44 | @Test
|
---|
45 | void testUpdatePluginValid() throws Exception {
|
---|
46 | this.pluginPath = "plugin/dummy_plugin.v31772.jar";
|
---|
47 | this.mockHttp();
|
---|
48 |
|
---|
49 | final File srcPluginFile = new File(
|
---|
50 | new File(TestUtils.getTestDataRoot()), "__files/" + this.pluginPath
|
---|
51 | );
|
---|
52 | final File pluginDir = Preferences.main().getPluginsDirectory();
|
---|
53 | final File pluginFile = new File(pluginDir, "dummy_plugin.jar");
|
---|
54 | final File pluginFileNew = new File(pluginDir, "dummy_plugin.jar.new");
|
---|
55 |
|
---|
56 | // put existing "plugin file" in place
|
---|
57 | pluginFile.getParentFile().mkdirs();
|
---|
58 | final byte[] existingPluginContents = "Existing plugin contents 123".getBytes(StandardCharsets.UTF_8);
|
---|
59 | try (FileOutputStream existingPluginOutputStream = new FileOutputStream(pluginFile)) {
|
---|
60 | existingPluginOutputStream.write(existingPluginContents);
|
---|
61 | }
|
---|
62 |
|
---|
63 | // get PluginInformation from jar file
|
---|
64 | final PluginInformation pluginInformation = new PluginInformation(srcPluginFile, "dummy_plugin");
|
---|
65 | // ...and grafting on the downloadlink
|
---|
66 | pluginInformation.downloadlink = this.getRemoteFileUrl();
|
---|
67 |
|
---|
68 | final PluginDownloadTask pluginDownloadTask = new PluginDownloadTask(
|
---|
69 | NullProgressMonitor.INSTANCE,
|
---|
70 | Collections.singletonList(pluginInformation),
|
---|
71 | null
|
---|
72 | );
|
---|
73 | pluginDownloadTask.run();
|
---|
74 |
|
---|
75 | // the ".jar.new" file should have been deleted
|
---|
76 | assertFalse(pluginFileNew.exists());
|
---|
77 | // the ".jar" file should still exist and its contents should equal those that were served to the task
|
---|
78 | TestUtils.assertFileContentsEqual(pluginFile, srcPluginFile);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Test download task when updating a plugin that we already have in our plugins directory
|
---|
83 | * and the downloaded file is not a valid jar file.
|
---|
84 | * @throws Exception if an error occurs
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testUpdatePluginCorrupt() throws Exception {
|
---|
88 | this.pluginPath = "plugin/corrupted_plugin.jar";
|
---|
89 | this.mockHttp();
|
---|
90 |
|
---|
91 | final File srcPluginFile = new File(
|
---|
92 | new File(TestUtils.getTestDataRoot()), "__files/" + this.pluginPath
|
---|
93 | );
|
---|
94 | final File pluginDir = Preferences.main().getPluginsDirectory();
|
---|
95 | final File pluginFile = new File(pluginDir, "corrupted_plugin.jar");
|
---|
96 | final File pluginFileNew = new File(pluginDir, "corrupted_plugin.jar.new");
|
---|
97 | // have to store this manifest externally as it clearly can't be read from our corrupted plugin
|
---|
98 | final File pluginManifest = new File(TestUtils.getTestDataRoot(), "plugin/corrupted_plugin.MANIFEST.MF");
|
---|
99 |
|
---|
100 | pluginFile.getParentFile().mkdirs();
|
---|
101 | final byte[] existingPluginContents = "Existing plugin contents 123".getBytes(StandardCharsets.UTF_8);
|
---|
102 | try (FileOutputStream existingPluginOutputStream = new FileOutputStream(pluginFile)) {
|
---|
103 | existingPluginOutputStream.write(existingPluginContents);
|
---|
104 | }
|
---|
105 |
|
---|
106 | try (FileInputStream manifestInputStream = new FileInputStream(pluginManifest)) {
|
---|
107 | final PluginInformation pluginInformation = new PluginInformation(
|
---|
108 | manifestInputStream,
|
---|
109 | "corrupted_plugin",
|
---|
110 | this.getRemoteFileUrl()
|
---|
111 | );
|
---|
112 | final PluginDownloadTask pluginDownloadTask = new PluginDownloadTask(
|
---|
113 | NullProgressMonitor.INSTANCE,
|
---|
114 | Collections.singletonList(pluginInformation),
|
---|
115 | null
|
---|
116 | );
|
---|
117 | pluginDownloadTask.run();
|
---|
118 | }
|
---|
119 |
|
---|
120 | // assert that the "corrupt" jar file made it through in tact
|
---|
121 | TestUtils.assertFileContentsEqual(pluginFileNew, srcPluginFile);
|
---|
122 | // the ".jar" file should still exist
|
---|
123 | assertTrue(pluginFile.exists());
|
---|
124 | try (
|
---|
125 | FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
|
---|
126 | ) {
|
---|
127 | // the ".jar" file's contents should be as before
|
---|
128 | assertArrayEquals(
|
---|
129 | existingPluginContents,
|
---|
130 | pluginDirPluginStream.readAllBytes()
|
---|
131 | );
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|