source: josm/trunk/test/unit/org/openstreetmap/josm/MainTest.java@ 12620

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

File size: 11.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
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.event.KeyEvent;
11import java.net.MalformedURLException;
12import java.net.URL;
13import java.nio.file.Paths;
14import java.util.Collection;
15import java.util.List;
16import java.util.Map;
17import java.util.concurrent.ExecutionException;
18import java.util.concurrent.Future;
19
20import javax.swing.UIManager;
21
22import org.junit.Rule;
23import org.junit.Test;
24import org.openstreetmap.josm.Main.DownloadParamType;
25import org.openstreetmap.josm.Main.InitStatusListener;
26import org.openstreetmap.josm.Main.InitializationTask;
27import org.openstreetmap.josm.actions.AboutAction;
28import org.openstreetmap.josm.data.osm.DataSet;
29import org.openstreetmap.josm.gui.MapFrameListener;
30import org.openstreetmap.josm.gui.ProgramArguments;
31import org.openstreetmap.josm.gui.layer.GpxLayer;
32import org.openstreetmap.josm.io.OnlineResource;
33import org.openstreetmap.josm.testutils.JOSMTestRules;
34import org.openstreetmap.josm.tools.Logging;
35import org.openstreetmap.josm.tools.Shortcut;
36
37import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
38
39/**
40 * Unit tests of {@link Main} class.
41 */
42public class MainTest {
43
44 /**
45 * Setup test.
46 */
47 @Rule
48 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
49 public JOSMTestRules test = new JOSMTestRules().platform().https().devAPI().mainMenu().projection();
50
51 /**
52 * Unit test of {@link DownloadParamType#paramType} method.
53 */
54 @Test
55 public void testParamType() {
56 assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
57 assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
58 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
59 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
60 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
61 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
62 }
63
64 /**
65 * Unit tests on log messages.
66 * @deprecated to remove end of 2017
67 */
68 @Test
69 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
70 @Deprecated
71 public void testLogs() {
72
73 assertNull(Main.getErrorMessage(null));
74
75 // Correct behaviour with errors
76 Main.error(new Exception("exception_error"));
77 Main.error("Error message on one line");
78 Main.error("Error message with {0}", "param");
79 Main.error("First line of error message on several lines\nline2\nline3\nline4");
80 Collection<String> errors = Main.getLastErrorAndWarnings();
81 assertTrue(errors.contains("E: java.lang.Exception: exception_error"));
82 assertTrue(errors.contains("E: Error message with param"));
83 assertTrue(errors.contains("E: Error message on one line"));
84 assertTrue(errors.contains("E: First line of error message on several lines"));
85
86 // Correct behaviour with warnings
87 Main.warn(new Exception("exception_warn", new Exception("root_cause")));
88 Main.warn(new Exception("exception_warn_bool"), true);
89 Main.warn("Warning message on one line");
90 Main.warn("First line of warning message on several lines\nline2\nline3\nline4");
91 Collection<String> warnings = Main.getLastErrorAndWarnings();
92 assertTrue(warnings.contains("W: java.lang.Exception: exception_warn. Cause: java.lang.Exception: root_cause"));
93 assertTrue(warnings.contains("W: java.lang.Exception: exception_warn_bool"));
94 assertTrue(warnings.contains("W: Warning message on one line"));
95 assertTrue(warnings.contains("W: First line of warning message on several lines"));
96 }
97
98 /**
99 * Unit test of {@link Main#preConstructorInit}.
100 */
101 @Test
102 public void testPreConstructorInit() {
103 Main.preConstructorInit();
104 assertNotNull(Main.getProjection());
105 assertEquals(Main.pref.get("laf", Main.platform.getDefaultStyle()), UIManager.getLookAndFeel().getClass().getCanonicalName());
106 assertNotNull(Main.toolbar);
107 }
108
109 /**
110 * Unit test of {@link Main#postConstructorProcessCmdLine} - empty case.
111 */
112 @Test
113 public void testPostConstructorProcessCmdLineEmpty() {
114 // Check the method accepts no arguments
115 Main.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
116 }
117
118 private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) {
119 assertNull(Main.getLayerManager().getEditDataSet());
120 for (Future<?> f : Main.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
121 "--download=" + download,
122 "--downloadgps=" + downloadGps,
123 "--selection=type: node"}))) {
124 try {
125 f.get();
126 } catch (InterruptedException | ExecutionException e) {
127 Logging.error(e);
128 }
129 }
130 DataSet ds = Main.getLayerManager().getEditDataSet();
131 assertNotNull(ds);
132 assertFalse(ds.getSelected().isEmpty());
133 Main.getLayerManager().removeLayer(Main.getLayerManager().getEditLayer());
134 if (gpx) {
135 List<GpxLayer> gpxLayers = Main.getLayerManager().getLayersOfType(GpxLayer.class);
136 assertEquals(1, gpxLayers.size());
137 Main.getLayerManager().removeLayer(gpxLayers.iterator().next());
138 }
139 }
140
141 /**
142 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with bounds.
143 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
144 */
145 @Test
146 public void testPostConstructorProcessCmdLineBounds() {
147 doTestPostConstructorProcessCmdLine(
148 "0.01,0.01,0.05,0.05",
149 "51.35,-0.4,51.60,0.2", true);
150 }
151
152 /**
153 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with http/https URLs.
154 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
155 */
156 @Test
157 public void testPostConstructorProcessCmdLineHttpUrl() {
158 doTestPostConstructorProcessCmdLine(
159 "http://api06.dev.openstreetmap.org/api/0.6/map?bbox=0.01,0.01,0.05,0.05",
160 "https://master.apis.dev.openstreetmap.org/api/0.6/trackpoints?bbox=-0.4,51.35,0.2,51.6&page=0", true);
161 }
162
163 /**
164 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with file URLs.
165 * @throws MalformedURLException if an error occurs
166 */
167 @Test
168 public void testPostConstructorProcessCmdLineFileUrl() throws MalformedURLException {
169 doTestPostConstructorProcessCmdLine(
170 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toUri().toURL().toExternalForm(),
171 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toUri().toURL().toExternalForm(), false);
172 }
173
174 /**
175 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with file names.
176 * @throws MalformedURLException if an error occurs
177 */
178 @Test
179 public void testPostConstructorProcessCmdLineFilename() throws MalformedURLException {
180 doTestPostConstructorProcessCmdLine(
181 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toFile().getAbsolutePath(),
182 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toFile().getAbsolutePath(), false);
183 }
184
185 /**
186 * Unit test of {@link DownloadParamType} enum.
187 */
188 @Test
189 public void testEnumDownloadParamType() {
190 TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
191 }
192
193 /**
194 * Unit test of {@link Main#getBaseUserUrl}.
195 */
196 @Test
197 public void testGetBaseUserUrl() {
198 assertEquals("http://api06.dev.openstreetmap.org/user", Main.getBaseUserUrl());
199 }
200
201 /**
202 * Unit test of {@link Main#addNetworkError}, {@link Main#getNetworkErrors} and {@link Main#clearNetworkErrors}.
203 * @throws MalformedURLException if any error occurs
204 */
205 @Test
206 public void testNetworkErrors() throws MalformedURLException {
207 Main.clearNetworkErrors();
208 assertTrue(Main.getNetworkErrors().isEmpty());
209 Main.addNetworkError("http://url1", new Exception("exception_1"));
210 Main.addNetworkError(new URL("http://url2"), new Exception("exception_2"));
211 Map<String, Throwable> errors = Main.getNetworkErrors();
212 assertEquals(2, errors.size());
213 assertEquals("exception_1", errors.get("http://url1").getMessage());
214 assertEquals("exception_2", errors.get("http://url2").getMessage());
215 Main.clearNetworkErrors();
216 assertTrue(Main.getNetworkErrors().isEmpty());
217 }
218
219 /**
220 * Unit test of {@link Main#setOffline} and {@link Main#getOfflineResources}.
221 */
222 @Test
223 public void testOfflineRessources() {
224 Main.setOnline(OnlineResource.ALL);
225 assertTrue(Main.getOfflineResources().isEmpty());
226 assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
227 Main.setOffline(OnlineResource.JOSM_WEBSITE);
228 assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
229 Main.setOnline(OnlineResource.JOSM_WEBSITE);
230 assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
231 Main.setOffline(OnlineResource.ALL);
232 assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
233 assertTrue(Main.isOffline(OnlineResource.OSM_API));
234 Main.setOnline(OnlineResource.ALL);
235 }
236
237 /**
238 * Unit test of {@link Main#getRegisteredActionShortcut}.
239 */
240 @Test
241 public void testGetRegisteredActionShortcut() {
242 Shortcut noKeystroke = Shortcut.registerShortcut("no", "keystroke", 0, 0);
243 assertNull(noKeystroke.getKeyStroke());
244 assertNull(Main.getRegisteredActionShortcut(noKeystroke));
245 Shortcut noAction = Shortcut.registerShortcut("foo", "bar", KeyEvent.VK_AMPERSAND, Shortcut.SHIFT);
246 assertNotNull(noAction.getKeyStroke());
247 assertNull(Main.getRegisteredActionShortcut(noAction));
248 AboutAction about = new AboutAction();
249 assertEquals(about, Main.getRegisteredActionShortcut(about.getShortcut()));
250 }
251
252 /**
253 * Unit test of {@link Main#addMapFrameListener} and {@link Main#removeMapFrameListener}.
254 */
255 @Test
256 public void testMapFrameListener() {
257 MapFrameListener listener = (o, n) -> { };
258 assertTrue(Main.addMapFrameListener(listener));
259 assertFalse(Main.addMapFrameListener(null));
260 assertTrue(Main.removeMapFrameListener(listener));
261 assertFalse(Main.removeMapFrameListener(null));
262 }
263
264 private static class InitStatusListenerStub implements InitStatusListener {
265
266 boolean updated;
267 boolean finished;
268
269 @Override
270 public Object updateStatus(String event) {
271 updated = true;
272 return null;
273 }
274
275 @Override
276 public void finish(Object status) {
277 finished = true;
278 }
279 }
280
281 /**
282 * Unit test of {@link Main#setInitStatusListener}.
283 */
284 @Test
285 public void testSetInitStatusListener() {
286 InitStatusListenerStub listener = new InitStatusListenerStub();
287 Main.setInitStatusListener(listener);
288 assertFalse(listener.updated);
289 assertFalse(listener.finished);
290 new InitializationTask("", () -> { }).call();
291 assertTrue(listener.updated);
292 assertTrue(listener.finished);
293 }
294}
Note: See TracBrowser for help on using the repository browser.