1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.plugin;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.Test;
|
---|
13 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
14 | import org.openstreetmap.josm.TestUtils;
|
---|
15 | import org.openstreetmap.josm.gui.preferences.PreferencesTestUtils;
|
---|
16 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.plugins.PluginDownloadTask;
|
---|
18 | import org.openstreetmap.josm.plugins.PluginException;
|
---|
19 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 | import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker;
|
---|
22 |
|
---|
23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests of {@link PluginPreference} class.
|
---|
27 | */
|
---|
28 | public class PluginPreferenceTest {
|
---|
29 | /**
|
---|
30 | * Setup test.
|
---|
31 | */
|
---|
32 | @RegisterExtension
|
---|
33 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
34 | public JOSMTestRules test = new JOSMTestRules().preferences().assertionsInEDT();
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Unit test of {@link PluginPreference#PluginPreference}.
|
---|
38 | */
|
---|
39 | @Test
|
---|
40 | void testPluginPreference() {
|
---|
41 | assertNotNull(new PluginPreference.Factory().createPreferenceSetting());
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Returns a dummy plugin information.
|
---|
46 | * @return a dummy plugin information
|
---|
47 | * @throws PluginException if an error occurs
|
---|
48 | */
|
---|
49 | public static PluginInformation getDummyPluginInformation() throws PluginException {
|
---|
50 | return new PluginInformation(
|
---|
51 | new File(TestUtils.getTestDataRoot() + "__files/plugin/dummy_plugin.v31772.jar"), "dummy_plugin");
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Unit test of {@link PluginPreference#buildDownloadSummary}.
|
---|
56 | * @throws Exception if an error occurs
|
---|
57 | */
|
---|
58 | @Test
|
---|
59 | void testBuildDownloadSummary() throws Exception {
|
---|
60 | final PluginInformation dummy = getDummyPluginInformation();
|
---|
61 | assertEquals("", PluginPreference.buildDownloadSummary(
|
---|
62 | new PluginDownloadTask(NullProgressMonitor.INSTANCE, Collections.<PluginInformation>emptyList(), "")));
|
---|
63 | assertEquals("", PluginPreference.buildDownloadSummary(
|
---|
64 | new PluginDownloadTask(NullProgressMonitor.INSTANCE, Arrays.asList(dummy), "")));
|
---|
65 | assertEquals("The following plugin has been downloaded <strong>successfully</strong>:<ul><li>dummy_plugin (31772)</li></ul>"+
|
---|
66 | "Downloading the following plugin has <strong>failed</strong>:<ul><li>dummy_plugin</li></ul>"+
|
---|
67 | "<br>Error message(untranslated): test",
|
---|
68 | PluginPreference.buildDownloadSummary(
|
---|
69 | new PluginDownloadTask(NullProgressMonitor.INSTANCE, Arrays.asList(dummy), "") {
|
---|
70 | @Override
|
---|
71 | public Collection<PluginInformation> getFailedPlugins() {
|
---|
72 | return Collections.singleton(dummy);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public Collection<PluginInformation> getDownloadedPlugins() {
|
---|
77 | return Collections.singleton(dummy);
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public Exception getLastException() {
|
---|
82 | return new Exception("test");
|
---|
83 | }
|
---|
84 | }));
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Unit test of {@link PluginPreference#notifyDownloadResults}.
|
---|
89 | */
|
---|
90 | @Test
|
---|
91 | void testNotifyDownloadResults() {
|
---|
92 | final HelpAwareOptionPaneMocker mocker = new HelpAwareOptionPaneMocker();
|
---|
93 | mocker.getMockResultMap().put("<html></html>", "OK"); // (buildDownloadSummary() output was empty)
|
---|
94 | mocker.getMockResultMap().put("<html>Please restart JOSM to activate the downloaded plugins.</html>", "OK");
|
---|
95 |
|
---|
96 | PluginDownloadTask task = new PluginDownloadTask(NullProgressMonitor.INSTANCE, Collections.<PluginInformation>emptyList(), "");
|
---|
97 | PluginPreference.notifyDownloadResults(null, task, false);
|
---|
98 | PluginPreference.notifyDownloadResults(null, task, true);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Unit test of {@link PluginPreference#addGui}.
|
---|
103 | */
|
---|
104 | @Test
|
---|
105 | void testAddGui() {
|
---|
106 | PreferencesTestUtils.doTestPreferenceSettingAddGui(new PluginPreference.Factory(), null);
|
---|
107 | }
|
---|
108 | }
|
---|