1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotNull;
|
---|
7 | import static org.junit.Assert.assertNull;
|
---|
8 | import static org.junit.Assert.assertTrue;
|
---|
9 |
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.net.MalformedURLException;
|
---|
12 | import java.net.URL;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.Map;
|
---|
15 |
|
---|
16 | import javax.swing.UIManager;
|
---|
17 |
|
---|
18 | import org.junit.Rule;
|
---|
19 | import org.junit.Test;
|
---|
20 | import org.openstreetmap.josm.Main.InitStatusListener;
|
---|
21 | import org.openstreetmap.josm.Main.InitializationTask;
|
---|
22 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
23 | import org.openstreetmap.josm.gui.DownloadParamType;
|
---|
24 | import org.openstreetmap.josm.gui.MapFrameListener;
|
---|
25 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
26 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
27 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
28 |
|
---|
29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link Main} class.
|
---|
33 | */
|
---|
34 | public class MainTest {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Setup test.
|
---|
38 | */
|
---|
39 | @Rule
|
---|
40 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
41 | public JOSMTestRules test = new JOSMTestRules().platform().https().devAPI().main().projection();
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Unit test of {@link DownloadParamType#paramType} method.
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testParamType() {
|
---|
48 | assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
|
---|
49 | assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
|
---|
50 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
|
---|
51 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
|
---|
52 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
|
---|
53 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Unit tests on log messages.
|
---|
58 | * @deprecated to remove end of 2017
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
|
---|
62 | @Deprecated
|
---|
63 | public void testLogs() {
|
---|
64 |
|
---|
65 | assertNull(Main.getErrorMessage(null));
|
---|
66 |
|
---|
67 | // Correct behaviour with errors
|
---|
68 | Main.error(new Exception("exception_error"));
|
---|
69 | Main.error("Error message on one line");
|
---|
70 | Main.error("Error message with {0}", "param");
|
---|
71 | Main.error("First line of error message on several lines\nline2\nline3\nline4");
|
---|
72 | Collection<String> errors = Main.getLastErrorAndWarnings();
|
---|
73 | assertTrue(errors.contains("E: java.lang.Exception: exception_error"));
|
---|
74 | assertTrue(errors.contains("E: Error message with param"));
|
---|
75 | assertTrue(errors.contains("E: Error message on one line"));
|
---|
76 | assertTrue(errors.contains("E: First line of error message on several lines"));
|
---|
77 |
|
---|
78 | // Correct behaviour with warnings
|
---|
79 | Main.warn(new Exception("exception_warn", new Exception("root_cause")));
|
---|
80 | Main.warn(new Exception("exception_warn_bool"), true);
|
---|
81 | Main.warn("Warning message on one line");
|
---|
82 | Main.warn("First line of warning message on several lines\nline2\nline3\nline4");
|
---|
83 | Collection<String> warnings = Main.getLastErrorAndWarnings();
|
---|
84 | assertTrue(warnings.contains("W: java.lang.Exception: exception_warn. Cause: java.lang.Exception: root_cause"));
|
---|
85 | assertTrue(warnings.contains("W: java.lang.Exception: exception_warn_bool"));
|
---|
86 | assertTrue(warnings.contains("W: Warning message on one line"));
|
---|
87 | assertTrue(warnings.contains("W: First line of warning message on several lines"));
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Unit test of {@link Main#preConstructorInit}.
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | public void testPreConstructorInit() {
|
---|
95 | Main.preConstructorInit();
|
---|
96 | assertNotNull(Main.getProjection());
|
---|
97 | assertEquals(Main.pref.get("laf", Main.platform.getDefaultStyle()), UIManager.getLookAndFeel().getClass().getCanonicalName());
|
---|
98 | assertNotNull(Main.toolbar);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Unit test of {@link Main#getBaseUserUrl}.
|
---|
103 | */
|
---|
104 | @Test
|
---|
105 | public void testGetBaseUserUrl() {
|
---|
106 | assertEquals("http://api06.dev.openstreetmap.org/user", Main.getBaseUserUrl());
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Unit test of {@link Main#addNetworkError}, {@link Main#getNetworkErrors} and {@link Main#clearNetworkErrors}.
|
---|
111 | * @throws MalformedURLException if any error occurs
|
---|
112 | */
|
---|
113 | @Test
|
---|
114 | public void testNetworkErrors() throws MalformedURLException {
|
---|
115 | Main.clearNetworkErrors();
|
---|
116 | assertTrue(Main.getNetworkErrors().isEmpty());
|
---|
117 | Main.addNetworkError("http://url1", new Exception("exception_1"));
|
---|
118 | Main.addNetworkError(new URL("http://url2"), new Exception("exception_2"));
|
---|
119 | Map<String, Throwable> errors = Main.getNetworkErrors();
|
---|
120 | assertEquals(2, errors.size());
|
---|
121 | assertEquals("exception_1", errors.get("http://url1").getMessage());
|
---|
122 | assertEquals("exception_2", errors.get("http://url2").getMessage());
|
---|
123 | Main.clearNetworkErrors();
|
---|
124 | assertTrue(Main.getNetworkErrors().isEmpty());
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Unit test of {@link Main#setOffline} and {@link Main#getOfflineResources}.
|
---|
129 | */
|
---|
130 | @Test
|
---|
131 | public void testOfflineRessources() {
|
---|
132 | Main.setOnline(OnlineResource.ALL);
|
---|
133 | assertTrue(Main.getOfflineResources().isEmpty());
|
---|
134 | assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
135 | Main.setOffline(OnlineResource.JOSM_WEBSITE);
|
---|
136 | assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
137 | Main.setOnline(OnlineResource.JOSM_WEBSITE);
|
---|
138 | assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
139 | Main.setOffline(OnlineResource.ALL);
|
---|
140 | assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
141 | assertTrue(Main.isOffline(OnlineResource.OSM_API));
|
---|
142 | Main.setOnline(OnlineResource.ALL);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Unit test of {@link Main#getRegisteredActionShortcut}.
|
---|
147 | */
|
---|
148 | @Test
|
---|
149 | public void testGetRegisteredActionShortcut() {
|
---|
150 | Shortcut noKeystroke = Shortcut.registerShortcut("no", "keystroke", 0, 0);
|
---|
151 | assertNull(noKeystroke.getKeyStroke());
|
---|
152 | assertNull(Main.getRegisteredActionShortcut(noKeystroke));
|
---|
153 | Shortcut noAction = Shortcut.registerShortcut("foo", "bar", KeyEvent.VK_AMPERSAND, Shortcut.SHIFT);
|
---|
154 | assertNotNull(noAction.getKeyStroke());
|
---|
155 | assertNull(Main.getRegisteredActionShortcut(noAction));
|
---|
156 | AboutAction about = new AboutAction();
|
---|
157 | assertEquals(about, Main.getRegisteredActionShortcut(about.getShortcut()));
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Unit test of {@link Main#addMapFrameListener} and {@link Main#removeMapFrameListener}.
|
---|
162 | */
|
---|
163 | @Test
|
---|
164 | public void testMapFrameListener() {
|
---|
165 | MapFrameListener listener = (o, n) -> { };
|
---|
166 | assertTrue(Main.addMapFrameListener(listener));
|
---|
167 | assertFalse(Main.addMapFrameListener(null));
|
---|
168 | assertTrue(Main.removeMapFrameListener(listener));
|
---|
169 | assertFalse(Main.removeMapFrameListener(null));
|
---|
170 | }
|
---|
171 |
|
---|
172 | private static class InitStatusListenerStub implements InitStatusListener {
|
---|
173 |
|
---|
174 | boolean updated;
|
---|
175 | boolean finished;
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | public Object updateStatus(String event) {
|
---|
179 | updated = true;
|
---|
180 | return null;
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public void finish(Object status) {
|
---|
185 | finished = true;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Unit test of {@link Main#setInitStatusListener}.
|
---|
191 | */
|
---|
192 | @Test
|
---|
193 | public void testSetInitStatusListener() {
|
---|
194 | InitStatusListenerStub listener = new InitStatusListenerStub();
|
---|
195 | Main.setInitStatusListener(listener);
|
---|
196 | assertFalse(listener.updated);
|
---|
197 | assertFalse(listener.finished);
|
---|
198 | new InitializationTask("", () -> { }).call();
|
---|
199 | assertTrue(listener.updated);
|
---|
200 | assertTrue(listener.finished);
|
---|
201 | }
|
---|
202 | }
|
---|