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