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

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

see #15182 - move GUI program arguments management from Main to gui.MainApplication

  • Property svn:eol-style set to native
File size: 8.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.assertFalse;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertNull;
8
9import java.io.ByteArrayOutputStream;
10import java.io.IOException;
11import java.io.PrintStream;
12import java.net.MalformedURLException;
13import java.nio.charset.StandardCharsets;
14import java.nio.file.Paths;
15import java.util.Arrays;
16import java.util.Collection;
17import java.util.List;
18import java.util.concurrent.ExecutionException;
19import java.util.concurrent.Future;
20
21import org.junit.Rule;
22import org.junit.Test;
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.TestUtils;
25import org.openstreetmap.josm.data.Version;
26import org.openstreetmap.josm.data.osm.DataSet;
27import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
28import org.openstreetmap.josm.gui.layer.GpxLayer;
29import org.openstreetmap.josm.plugins.PluginHandler;
30import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
31import org.openstreetmap.josm.plugins.PluginInformation;
32import org.openstreetmap.josm.plugins.PluginListParseException;
33import org.openstreetmap.josm.plugins.PluginListParser;
34import org.openstreetmap.josm.testutils.JOSMTestRules;
35import org.openstreetmap.josm.tools.Logging;
36
37import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
38
39/**
40 * Unit tests of {@link MainApplication} class.
41 */
42public class MainApplicationTest {
43
44 /**
45 * Setup test.
46 */
47 @Rule
48 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
49 public JOSMTestRules test = new JOSMTestRules().platform().main();
50
51 @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING")
52 private void testShow(final String arg, String expected) throws InterruptedException, IOException {
53 PrintStream old = System.out;
54 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
55 System.setOut(new PrintStream(baos));
56 Thread t = new Thread() {
57 @Override
58 public void run() {
59 MainApplication.main(new String[] {arg});
60 }
61 };
62 t.start();
63 t.join();
64 System.out.flush();
65 assertEquals(expected, baos.toString(StandardCharsets.UTF_8.name()).trim());
66 } finally {
67 System.setOut(old);
68 }
69 }
70
71 /**
72 * Test of {@link MainApplication#main} with argument {@code --version}.
73 * @throws Exception in case of error
74 */
75 @Test
76 public void testShowVersion() throws Exception {
77 testShow("--version", Version.getInstance().getAgentString());
78 }
79
80 /**
81 * Test of {@link MainApplication#main} with argument {@code --help}.
82 * @throws Exception in case of error
83 */
84 @Test
85 public void testShowHelp() throws Exception {
86 testShow("--help", MainApplication.getHelp().trim());
87 }
88
89 /**
90 * Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
91 * @throws PluginListParseException if an error occurs
92 */
93 @Test
94 public void testUpdateAndLoadPlugins() throws PluginListParseException {
95 final String old = System.getProperty("josm.plugins");
96 try {
97 System.setProperty("josm.plugins", "buildings_tools,plastic_laf");
98 SplashProgressMonitor monitor = new SplashProgressMonitor("foo", e -> {
99 // Do nothing
100 });
101 Collection<PluginInformation> plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
102 if (plugins.isEmpty()) {
103 PluginHandlerTestIT.downloadPlugins(Arrays.asList(
104 newPluginInformation("buildings_tools"),
105 newPluginInformation("plastic_laf")));
106 plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
107 }
108 assertEquals(2, plugins.size());
109 assertNotNull(PluginHandler.getPlugin("plastic_laf"));
110 assertNull(PluginHandler.getPlugin("buildings_tools"));
111 MainApplication.loadLatePlugins(null, monitor, plugins);
112 assertNotNull(PluginHandler.getPlugin("buildings_tools"));
113 } finally {
114 if (old != null) {
115 System.setProperty("josm.plugins", old);
116 } else {
117 System.clearProperty("josm.plugins");
118 }
119 }
120 }
121
122 private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
123 return PluginListParser.createInfo(plugin+".jar", "https://svn.openstreetmap.org/applications/editors/josm/dist/"+plugin+".jar",
124 "");
125 }
126
127 /**
128 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - empty case.
129 */
130 @Test
131 public void testPostConstructorProcessCmdLineEmpty() {
132 // Check the method accepts no arguments
133 MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
134 }
135
136 private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) {
137 assertNull(Main.getLayerManager().getEditDataSet());
138 for (Future<?> f : MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
139 "--download=" + download,
140 "--downloadgps=" + downloadGps,
141 "--selection=type: node"}))) {
142 try {
143 f.get();
144 } catch (InterruptedException | ExecutionException e) {
145 Logging.error(e);
146 }
147 }
148 DataSet ds = Main.getLayerManager().getEditDataSet();
149 assertNotNull(ds);
150 assertFalse(ds.getSelected().isEmpty());
151 Main.getLayerManager().removeLayer(Main.getLayerManager().getEditLayer());
152 if (gpx) {
153 List<GpxLayer> gpxLayers = Main.getLayerManager().getLayersOfType(GpxLayer.class);
154 assertEquals(1, gpxLayers.size());
155 Main.getLayerManager().removeLayer(gpxLayers.iterator().next());
156 }
157 }
158
159 /**
160 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with bounds.
161 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
162 */
163 @Test
164 public void testPostConstructorProcessCmdLineBounds() {
165 doTestPostConstructorProcessCmdLine(
166 "0.01,0.01,0.05,0.05",
167 "51.35,-0.4,51.60,0.2", true);
168 }
169
170 /**
171 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with http/https URLs.
172 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
173 */
174 @Test
175 public void testPostConstructorProcessCmdLineHttpUrl() {
176 doTestPostConstructorProcessCmdLine(
177 "http://api06.dev.openstreetmap.org/api/0.6/map?bbox=0.01,0.01,0.05,0.05",
178 "https://master.apis.dev.openstreetmap.org/api/0.6/trackpoints?bbox=-0.4,51.35,0.2,51.6&page=0", true);
179 }
180
181 /**
182 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file URLs.
183 * @throws MalformedURLException if an error occurs
184 */
185 @Test
186 public void testPostConstructorProcessCmdLineFileUrl() throws MalformedURLException {
187 doTestPostConstructorProcessCmdLine(
188 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toUri().toURL().toExternalForm(),
189 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toUri().toURL().toExternalForm(), false);
190 }
191
192 /**
193 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file names.
194 * @throws MalformedURLException if an error occurs
195 */
196 @Test
197 public void testPostConstructorProcessCmdLineFilename() throws MalformedURLException {
198 doTestPostConstructorProcessCmdLine(
199 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toFile().getAbsolutePath(),
200 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toFile().getAbsolutePath(), false);
201 }
202
203 /**
204 * Unit test of {@link DownloadParamType} enum.
205 */
206 @Test
207 public void testEnumDownloadParamType() {
208 TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
209 }
210}
Note: See TracBrowser for help on using the repository browser.