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

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

add missing https rule in unit test

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