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