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

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 12.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertNull;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9import static org.junit.Assume.assumeFalse;
10
11import java.awt.BorderLayout;
12import java.awt.event.KeyEvent;
13import java.io.ByteArrayOutputStream;
14import java.io.IOException;
15import java.io.PrintStream;
16import java.net.MalformedURLException;
17import java.nio.charset.StandardCharsets;
18import java.nio.file.Paths;
19import java.util.Arrays;
20import java.util.Collection;
21import java.util.List;
22import java.util.concurrent.ExecutionException;
23import java.util.concurrent.Future;
24
25import javax.swing.JComponent;
26import javax.swing.JPanel;
27import javax.swing.UIManager;
28
29import org.junit.jupiter.api.Test;
30import org.junit.jupiter.api.extension.RegisterExtension;
31import org.openstreetmap.josm.TestUtils;
32import org.openstreetmap.josm.actions.JosmAction;
33import org.openstreetmap.josm.actions.OpenLocationAction;
34import org.openstreetmap.josm.data.Version;
35import org.openstreetmap.josm.data.osm.DataSet;
36import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
37import org.openstreetmap.josm.gui.layer.GpxLayer;
38import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
39import org.openstreetmap.josm.plugins.PluginHandler;
40import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
41import org.openstreetmap.josm.plugins.PluginInformation;
42import org.openstreetmap.josm.plugins.PluginListParseException;
43import org.openstreetmap.josm.plugins.PluginListParser;
44import org.openstreetmap.josm.spi.preferences.Config;
45import org.openstreetmap.josm.testutils.JOSMTestRules;
46import org.openstreetmap.josm.tools.Logging;
47import org.openstreetmap.josm.tools.PlatformManager;
48import org.openstreetmap.josm.tools.Shortcut;
49
50import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
51
52/**
53 * Unit tests of {@link MainApplication} class.
54 */
55public class MainApplicationTest {
56
57 /**
58 * Setup test.
59 */
60 @RegisterExtension
61 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
62 public JOSMTestRules test = new JOSMTestRules().main().projection().https().devAPI().timeout(20000);
63
64 /**
65 * Make sure {@link MainApplication#contentPanePrivate} is initialized.
66 */
67 public static void initContentPane() {
68 if (MainApplication.contentPanePrivate == null) {
69 MainApplication.contentPanePrivate = new JPanel(new BorderLayout());
70 }
71 }
72
73 /**
74 * Returns {@link MainApplication#contentPanePrivate} (not public).
75 * @return {@link MainApplication#contentPanePrivate}
76 */
77 public static JComponent getContentPane() {
78 return MainApplication.contentPanePrivate;
79 }
80
81 /**
82 * Make sure {@code MainApplication.mainPanel} is initialized.
83 * @param reAddListeners {@code true} to re-add listeners
84 */
85 public static void initMainPanel(boolean reAddListeners) {
86 if (MainApplication.mainPanel == null) {
87 MainApplication.mainPanel = new MainPanel(MainApplication.getLayerManager());
88 }
89 if (reAddListeners) {
90 MainApplication.mainPanel.reAddListeners();
91 }
92 }
93
94 /**
95 * Make sure {@code MainApplication.menu} is initialized.
96 */
97 public static void initMainMenu() {
98 MainApplication.menu = new MainMenu();
99 }
100
101 /**
102 * Make sure {@link MainApplication#toolbar} is initialized.
103 */
104 public static void initToolbar() {
105 if (MainApplication.toolbar == null) {
106 MainApplication.toolbar = new ToolbarPreferences();
107 }
108 }
109
110 @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING")
111 private void testShow(final String arg, String expected) throws InterruptedException, IOException {
112 PrintStream old = System.out;
113 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
114 System.setOut(new PrintStream(baos));
115 Thread t = new Thread() {
116 @Override
117 public void run() {
118 MainApplication.main(new String[] {arg});
119 }
120 };
121 t.start();
122 t.join();
123 System.out.flush();
124 assertEquals(expected, baos.toString(StandardCharsets.UTF_8.name()).trim());
125 } finally {
126 System.setOut(old);
127 }
128 }
129
130 /**
131 * Test of {@link MainApplication#main} with argument {@code --version}.
132 * @throws Exception in case of error
133 */
134 @Test
135 void testShowVersion() throws Exception {
136 testShow("--version", Version.getInstance().getAgentString());
137 }
138
139 /**
140 * Test of {@link MainApplication#main} with argument {@code --help}.
141 * @throws Exception in case of error
142 */
143 @Test
144 void testShowHelp() throws Exception {
145 testShow("--help", MainApplication.getHelp().trim());
146 }
147
148 /**
149 * Unit test of {@link DownloadParamType#paramType} method.
150 */
151 @Test
152 void testParamType() {
153 assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
154 assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
155 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
156 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
157 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
158 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
159 }
160
161 /**
162 * Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
163 * @throws PluginListParseException if an error occurs
164 */
165 @Test
166 void testUpdateAndLoadPlugins() throws PluginListParseException {
167 final String old = System.getProperty("josm.plugins");
168 try {
169 System.setProperty("josm.plugins", "buildings_tools,log4j");
170 SplashProgressMonitor monitor = new SplashProgressMonitor("foo", e -> {
171 // Do nothing
172 });
173 Collection<PluginInformation> plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
174 if (plugins.isEmpty()) {
175 PluginHandlerTestIT.downloadPlugins(Arrays.asList(
176 newPluginInformation("buildings_tools"),
177 newPluginInformation("log4j")));
178 plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
179 }
180 assertEquals(2, plugins.size());
181 assertNotNull(PluginHandler.getPlugin("log4j"));
182 assertNull(PluginHandler.getPlugin("buildings_tools"));
183 MainApplication.loadLatePlugins(null, monitor, plugins);
184 assertNotNull(PluginHandler.getPlugin("buildings_tools"));
185 } finally {
186 if (old != null) {
187 System.setProperty("josm.plugins", old);
188 } else {
189 System.clearProperty("josm.plugins");
190 }
191 }
192 }
193
194 /**
195 * Unit test of {@link MainApplication#setupUIManager}.
196 */
197 @Test
198 void testSetupUIManager() {
199 assumeFalse(PlatformManager.isPlatformWindows() && "True".equals(System.getenv("APPVEYOR")));
200 MainApplication.setupUIManager();
201 assertEquals(Config.getPref().get("laf", PlatformManager.getPlatform().getDefaultStyle()),
202 UIManager.getLookAndFeel().getClass().getCanonicalName());
203 }
204
205 private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
206 return PluginListParser.createInfo(plugin+".jar", "https://josm.openstreetmap.de/osmsvn/applications/editors/josm/dist/"+plugin+".jar",
207 "");
208 }
209
210 /**
211 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - empty case.
212 */
213 @Test
214 void testPostConstructorProcessCmdLineEmpty() {
215 // Check the method accepts no arguments
216 MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
217 }
218
219 private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) {
220 assertNull(MainApplication.getLayerManager().getEditDataSet());
221 for (Future<?> f : MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
222 "--download=" + download,
223 "--downloadgps=" + downloadGps,
224 "--selection=type: node"}))) {
225 try {
226 f.get();
227 } catch (InterruptedException | ExecutionException e) {
228 Logging.error(e);
229 }
230 }
231 DataSet ds = MainApplication.getLayerManager().getEditDataSet();
232 assertNotNull(ds);
233 assertFalse(ds.getSelected().isEmpty());
234 MainApplication.getLayerManager().removeLayer(MainApplication.getLayerManager().getEditLayer());
235 if (gpx) {
236 List<GpxLayer> gpxLayers = MainApplication.getLayerManager().getLayersOfType(GpxLayer.class);
237 assertEquals(1, gpxLayers.size());
238 MainApplication.getLayerManager().removeLayer(gpxLayers.iterator().next());
239 }
240 }
241
242 /**
243 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with bounds.
244 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
245 */
246 @Test
247 void testPostConstructorProcessCmdLineBounds() {
248 doTestPostConstructorProcessCmdLine(
249 "-47.20,-126.75,-47.10,-126.65",
250 "51.35,-0.4,51.60,0.2", true);
251 }
252
253 /**
254 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with http/https URLs.
255 * This test assumes the DEV API contains nodes around -47.15, -126.7 (R'lyeh) and GPX tracks around London
256 */
257 @Test
258 void testPostConstructorProcessCmdLineHttpUrl() {
259 doTestPostConstructorProcessCmdLine(
260 "https://api06.dev.openstreetmap.org/api/0.6/map?bbox=-126.75,-47.20,-126.65,-47.10",
261 "https://master.apis.dev.openstreetmap.org/api/0.6/trackpoints?bbox=-0.4,51.35,0.2,51.6&page=0", true);
262 }
263
264 /**
265 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file URLs.
266 * @throws MalformedURLException if an error occurs
267 */
268 @Test
269 void testPostConstructorProcessCmdLineFileUrl() throws MalformedURLException {
270 doTestPostConstructorProcessCmdLine(
271 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toUri().toURL().toExternalForm(),
272 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toUri().toURL().toExternalForm(), false);
273 }
274
275 /**
276 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file names.
277 * @throws MalformedURLException if an error occurs
278 */
279 @Test
280 void testPostConstructorProcessCmdLineFilename() throws MalformedURLException {
281 doTestPostConstructorProcessCmdLine(
282 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toFile().getAbsolutePath(),
283 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toFile().getAbsolutePath(), false);
284 }
285
286 /**
287 * Unit test of {@link MainApplication#getRegisteredActionShortcut}.
288 */
289 @Test
290 void testGetRegisteredActionShortcut() {
291 Shortcut noKeystroke = Shortcut.registerShortcut("no", "keystroke", 0, 0);
292 assertNull(noKeystroke.getKeyStroke());
293 assertNull(MainApplication.getRegisteredActionShortcut(noKeystroke));
294 Shortcut noAction = Shortcut.registerShortcut("foo", "bar", KeyEvent.VK_AMPERSAND, Shortcut.SHIFT);
295 assertNotNull(noAction.getKeyStroke());
296 assertNull(MainApplication.getRegisteredActionShortcut(noAction));
297 JosmAction action = new OpenLocationAction();
298 assertEquals(action, MainApplication.getRegisteredActionShortcut(action.getShortcut()));
299 }
300
301 /**
302 * Unit test of {@link MainApplication#addMapFrameListener} and {@link MainApplication#removeMapFrameListener}.
303 */
304 @Test
305 void testMapFrameListener() {
306 MapFrameListener listener = (o, n) -> { };
307 assertTrue(MainApplication.addMapFrameListener(listener));
308 assertFalse(MainApplication.addMapFrameListener(null));
309 assertTrue(MainApplication.removeMapFrameListener(listener));
310 assertFalse(MainApplication.removeMapFrameListener(null));
311 }
312
313 /**
314 * Unit test of {@link DownloadParamType} enum.
315 */
316 @Test
317 void testEnumDownloadParamType() {
318 TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
319 }
320}
Note: See TracBrowser for help on using the repository browser.