| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui;
|
|---|
| 3 |
|
|---|
| 4 | import static org.junit.Assert.assertEquals;
|
|---|
| 5 | import static org.junit.Assert.assertNotNull;
|
|---|
| 6 | import static org.junit.Assert.assertNull;
|
|---|
| 7 |
|
|---|
| 8 | import java.io.ByteArrayOutputStream;
|
|---|
| 9 | import java.io.IOException;
|
|---|
| 10 | import java.io.PrintStream;
|
|---|
| 11 | import java.util.Arrays;
|
|---|
| 12 | import java.util.Collection;
|
|---|
| 13 |
|
|---|
| 14 | import javax.swing.event.ChangeEvent;
|
|---|
| 15 | import javax.swing.event.ChangeListener;
|
|---|
| 16 |
|
|---|
| 17 | import org.junit.BeforeClass;
|
|---|
| 18 | import org.junit.Test;
|
|---|
| 19 | import org.openstreetmap.josm.JOSMFixture;
|
|---|
| 20 | import org.openstreetmap.josm.data.Version;
|
|---|
| 21 | import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
|
|---|
| 22 | import org.openstreetmap.josm.plugins.PluginHandler;
|
|---|
| 23 | import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
|
|---|
| 24 | import org.openstreetmap.josm.plugins.PluginInformation;
|
|---|
| 25 | import org.openstreetmap.josm.plugins.PluginListParseException;
|
|---|
| 26 | import org.openstreetmap.josm.plugins.PluginListParser;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Unit tests of {@link MainApplication} class.
|
|---|
| 30 | */
|
|---|
| 31 | public class MainApplicationTest {
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Setup test.
|
|---|
| 35 | */
|
|---|
| 36 | @BeforeClass
|
|---|
| 37 | public static void setUp() {
|
|---|
| 38 | JOSMFixture.createUnitTestFixture().init(true);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | private void testShow(final String arg, String expected) throws InterruptedException, IOException {
|
|---|
| 42 | PrintStream old = System.out;
|
|---|
| 43 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|---|
| 44 | System.setOut(new PrintStream(baos));
|
|---|
| 45 | Thread t = new Thread() {
|
|---|
| 46 | @Override
|
|---|
| 47 | public void run() {
|
|---|
| 48 | MainApplication.main(new String[] {arg});
|
|---|
| 49 | }
|
|---|
| 50 | };
|
|---|
| 51 | t.run();
|
|---|
| 52 | t.join();
|
|---|
| 53 | System.out.flush();
|
|---|
| 54 | assertEquals(expected, baos.toString().trim());
|
|---|
| 55 | } finally {
|
|---|
| 56 | System.setOut(old);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Test of {@link MainApplication#main} with argument {@code --version}.
|
|---|
| 62 | * @throws Exception in case of error
|
|---|
| 63 | */
|
|---|
| 64 | @Test
|
|---|
| 65 | public void testShowVersion() throws Exception {
|
|---|
| 66 | testShow("--version", Version.getInstance().getAgentString());
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Test of {@link MainApplication#main} with argument {@code --help}.
|
|---|
| 71 | * @throws Exception in case of error
|
|---|
| 72 | */
|
|---|
| 73 | @Test
|
|---|
| 74 | public void testShowHelp() throws Exception {
|
|---|
| 75 | testShow("--help", MainApplication.getHelp().trim());
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
|
|---|
| 80 | * @throws PluginListParseException if an error occurs
|
|---|
| 81 | */
|
|---|
| 82 | @Test
|
|---|
| 83 | public void testUpdateAndLoadPlugins() throws PluginListParseException {
|
|---|
| 84 | final String old = System.getProperty("josm.plugins");
|
|---|
| 85 | try {
|
|---|
| 86 | System.setProperty("josm.plugins", "buildings_tools,plastic_laf");
|
|---|
| 87 | SplashProgressMonitor monitor = new SplashProgressMonitor("foo", new ChangeListener() {
|
|---|
| 88 | @Override
|
|---|
| 89 | public void stateChanged(ChangeEvent e) {
|
|---|
| 90 | // Do nothing
|
|---|
| 91 | }
|
|---|
| 92 | });
|
|---|
| 93 | Collection<PluginInformation> plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
|---|
| 94 | if (plugins.isEmpty()) {
|
|---|
| 95 | PluginHandlerTestIT.downloadPlugins(Arrays.asList(
|
|---|
| 96 | newPluginInformation("buildings_tools"),
|
|---|
| 97 | newPluginInformation("plastic_laf")));
|
|---|
| 98 | plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
|---|
| 99 | }
|
|---|
| 100 | assertEquals(2, plugins.size());
|
|---|
| 101 | assertNotNull(PluginHandler.getPlugin("plastic_laf"));
|
|---|
| 102 | assertNull(PluginHandler.getPlugin("buildings_tools"));
|
|---|
| 103 | MainApplication.loadLatePlugins(null, monitor, plugins);
|
|---|
| 104 | assertNotNull(PluginHandler.getPlugin("buildings_tools"));
|
|---|
| 105 | } finally {
|
|---|
| 106 | if (old != null) {
|
|---|
| 107 | System.setProperty("josm.plugins", old);
|
|---|
| 108 | } else {
|
|---|
| 109 | System.clearProperty("josm.plugins");
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
|
|---|
| 115 | return PluginListParser.createInfo(plugin+".jar", "https://svn.openstreetmap.org/applications/editors/josm/dist/"+plugin+".jar",
|
|---|
| 116 | "");
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|