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