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.nio.charset.StandardCharsets;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 |
|
---|
15 | import org.junit.BeforeClass;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.JOSMFixture;
|
---|
18 | import org.openstreetmap.josm.data.Version;
|
---|
19 | import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
21 | import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
|
---|
22 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
23 | import org.openstreetmap.josm.plugins.PluginListParseException;
|
---|
24 | import org.openstreetmap.josm.plugins.PluginListParser;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
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 | @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING")
|
---|
42 | private void testShow(final String arg, String expected) throws InterruptedException, IOException {
|
---|
43 | PrintStream old = System.out;
|
---|
44 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
---|
45 | System.setOut(new PrintStream(baos));
|
---|
46 | Thread t = new Thread() {
|
---|
47 | @Override
|
---|
48 | public void run() {
|
---|
49 | MainApplication.main(new String[] {arg});
|
---|
50 | }
|
---|
51 | };
|
---|
52 | t.start();
|
---|
53 | t.join();
|
---|
54 | System.out.flush();
|
---|
55 | assertEquals(expected, baos.toString(StandardCharsets.UTF_8.name()).trim());
|
---|
56 | } finally {
|
---|
57 | System.setOut(old);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Test of {@link MainApplication#main} with argument {@code --version}.
|
---|
63 | * @throws Exception in case of error
|
---|
64 | */
|
---|
65 | @Test
|
---|
66 | public void testShowVersion() throws Exception {
|
---|
67 | testShow("--version", Version.getInstance().getAgentString());
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Test of {@link MainApplication#main} with argument {@code --help}.
|
---|
72 | * @throws Exception in case of error
|
---|
73 | */
|
---|
74 | @Test
|
---|
75 | public void testShowHelp() throws Exception {
|
---|
76 | testShow("--help", MainApplication.getHelp().trim());
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
|
---|
81 | * @throws PluginListParseException if an error occurs
|
---|
82 | */
|
---|
83 | @Test
|
---|
84 | public void testUpdateAndLoadPlugins() throws PluginListParseException {
|
---|
85 | final String old = System.getProperty("josm.plugins");
|
---|
86 | try {
|
---|
87 | System.setProperty("josm.plugins", "buildings_tools,plastic_laf");
|
---|
88 | SplashProgressMonitor monitor = new SplashProgressMonitor("foo", e -> {
|
---|
89 | // Do nothing
|
---|
90 | });
|
---|
91 | Collection<PluginInformation> plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
---|
92 | if (plugins.isEmpty()) {
|
---|
93 | PluginHandlerTestIT.downloadPlugins(Arrays.asList(
|
---|
94 | newPluginInformation("buildings_tools"),
|
---|
95 | newPluginInformation("plastic_laf")));
|
---|
96 | plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
---|
97 | }
|
---|
98 | assertEquals(2, plugins.size());
|
---|
99 | assertNotNull(PluginHandler.getPlugin("plastic_laf"));
|
---|
100 | assertNull(PluginHandler.getPlugin("buildings_tools"));
|
---|
101 | MainApplication.loadLatePlugins(null, monitor, plugins);
|
---|
102 | assertNotNull(PluginHandler.getPlugin("buildings_tools"));
|
---|
103 | } finally {
|
---|
104 | if (old != null) {
|
---|
105 | System.setProperty("josm.plugins", old);
|
---|
106 | } else {
|
---|
107 | System.clearProperty("josm.plugins");
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
|
---|
113 | return PluginListParser.createInfo(plugin+".jar", "https://svn.openstreetmap.org/applications/editors/josm/dist/"+plugin+".jar",
|
---|
114 | "");
|
---|
115 | }
|
---|
116 | }
|
---|