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