source: josm/trunk/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java@ 11647

Last change on this file since 11647 was 11324, checked in by Don-vip, 7 years ago

findbugs

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertNull;
7
8import java.io.ByteArrayOutputStream;
9import java.io.IOException;
10import java.io.PrintStream;
11import java.nio.charset.StandardCharsets;
12import java.util.Arrays;
13import java.util.Collection;
14
15import org.junit.BeforeClass;
16import org.junit.Test;
17import org.openstreetmap.josm.JOSMFixture;
18import org.openstreetmap.josm.data.Version;
19import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
20import org.openstreetmap.josm.plugins.PluginHandler;
21import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
22import org.openstreetmap.josm.plugins.PluginInformation;
23import org.openstreetmap.josm.plugins.PluginListParseException;
24import org.openstreetmap.josm.plugins.PluginListParser;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests of {@link MainApplication} class.
30 */
31public 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}
Note: See TracBrowser for help on using the repository browser.