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

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

see #15102 - speedup MainTest by avoiding useless slow http requests to wiki/StartupPage and /maps

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