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

Last change on this file since 13435 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

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