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.assertTrue;
|
---|
8 |
|
---|
9 | import java.net.MalformedURLException;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import org.junit.Rule;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.Main.InitStatusListener;
|
---|
16 | import org.openstreetmap.josm.Main.InitializationTask;
|
---|
17 | import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager;
|
---|
18 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
19 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
20 |
|
---|
21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Unit tests of {@link Main} class.
|
---|
25 | */
|
---|
26 | public class MainTest {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Setup test.
|
---|
30 | */
|
---|
31 | @Rule
|
---|
32 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
33 | public JOSMTestRules test = new JOSMTestRules().platform().https().devAPI().main().projection();
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Unit test of {@link Main#preConstructorInit}.
|
---|
37 | */
|
---|
38 | @Test
|
---|
39 | public void testPreConstructorInit() {
|
---|
40 | Main.preConstructorInit();
|
---|
41 | assertNotNull(CoordinateFormatManager.getDefaultFormat());
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Unit test of {@link Main#addNetworkError}, {@link Main#getNetworkErrors} and {@link Main#clearNetworkErrors}.
|
---|
46 | * @throws MalformedURLException if any error occurs
|
---|
47 | */
|
---|
48 | @Test
|
---|
49 | public void testNetworkErrors() throws MalformedURLException {
|
---|
50 | Main.clearNetworkErrors();
|
---|
51 | assertTrue(Main.getNetworkErrors().isEmpty());
|
---|
52 | Main.addNetworkError("http://url1", new Exception("exception_1"));
|
---|
53 | Main.addNetworkError(new URL("http://url2"), new Exception("exception_2"));
|
---|
54 | Map<String, Throwable> errors = Main.getNetworkErrors();
|
---|
55 | assertEquals(2, errors.size());
|
---|
56 | assertEquals("exception_1", errors.get("http://url1").getMessage());
|
---|
57 | assertEquals("exception_2", errors.get("http://url2").getMessage());
|
---|
58 | Main.clearNetworkErrors();
|
---|
59 | assertTrue(Main.getNetworkErrors().isEmpty());
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Unit test of {@link Main#setOffline} and {@link Main#getOfflineResources}.
|
---|
64 | */
|
---|
65 | @Test
|
---|
66 | public void testOfflineRessources() {
|
---|
67 | Main.setOnline(OnlineResource.ALL);
|
---|
68 | assertTrue(Main.getOfflineResources().isEmpty());
|
---|
69 | assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
70 | Main.setOffline(OnlineResource.JOSM_WEBSITE);
|
---|
71 | assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
72 | Main.setOnline(OnlineResource.JOSM_WEBSITE);
|
---|
73 | assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
74 | Main.setOffline(OnlineResource.ALL);
|
---|
75 | assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
76 | assertTrue(Main.isOffline(OnlineResource.OSM_API));
|
---|
77 | Main.setOnline(OnlineResource.ALL);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static class InitStatusListenerStub implements InitStatusListener {
|
---|
81 |
|
---|
82 | boolean updated;
|
---|
83 | boolean finished;
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public Object updateStatus(String event) {
|
---|
87 | updated = true;
|
---|
88 | return null;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public void finish(Object status) {
|
---|
93 | finished = true;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Unit test of {@link Main#setInitStatusListener}.
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | public void testSetInitStatusListener() {
|
---|
102 | InitStatusListenerStub listener = new InitStatusListenerStub();
|
---|
103 | Main.setInitStatusListener(listener);
|
---|
104 | assertFalse(listener.updated);
|
---|
105 | assertFalse(listener.finished);
|
---|
106 | new InitializationTask("", () -> { }).call();
|
---|
107 | assertTrue(listener.updated);
|
---|
108 | assertTrue(listener.finished);
|
---|
109 | }
|
---|
110 | }
|
---|