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

Last change on this file since 10983 was 10983, checked in by Don-vip, 8 years ago

fix #13609 - --version and --help are too verbose

  • 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.util.Arrays;
12import java.util.Collection;
13
14import javax.swing.event.ChangeEvent;
15import javax.swing.event.ChangeListener;
16
17import org.junit.BeforeClass;
18import org.junit.Test;
19import org.openstreetmap.josm.JOSMFixture;
20import org.openstreetmap.josm.data.Version;
21import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
22import org.openstreetmap.josm.plugins.PluginHandler;
23import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
24import org.openstreetmap.josm.plugins.PluginInformation;
25import org.openstreetmap.josm.plugins.PluginListParseException;
26import org.openstreetmap.josm.plugins.PluginListParser;
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 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}
Note: See TracBrowser for help on using the repository browser.