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

Last change on this file since 18602 was 18602, checked in by taylor.smock, 3 years ago

See r18598, fix tests under Java 18/19

This was caused by a race condition where the bug report thread finished prior
to the not null check in Java 8, but almost always does not under Java 18/19.

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