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

Last change on this file since 17571 was 17571, checked in by simon04, 4 years ago

fix #20563 - PluginListParser.parse amounts to 80% of allocations during startup

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