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.util.Collection;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.UIManager;
|
---|
14 |
|
---|
15 | import org.junit.Rule;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.Main.DownloadParamType;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.gui.ProgramArguments;
|
---|
20 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
21 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
22 |
|
---|
23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests of {@link Main} class.
|
---|
27 | */
|
---|
28 | public class MainTest {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Setup test.
|
---|
32 | */
|
---|
33 | @Rule
|
---|
34 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
35 | public JOSMTestRules test = new JOSMTestRules().platform().devAPI();
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Unit test of {@link DownloadParamType#paramType} method.
|
---|
39 | */
|
---|
40 | @Test
|
---|
41 | public void testParamType() {
|
---|
42 | assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
|
---|
43 | assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
|
---|
44 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
|
---|
45 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
|
---|
46 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
|
---|
47 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Unit tests on log messages.
|
---|
52 | */
|
---|
53 | @Test
|
---|
54 | @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
|
---|
55 | public void testLogs() {
|
---|
56 |
|
---|
57 | assertNull(Main.getErrorMessage(null));
|
---|
58 |
|
---|
59 | // Correct behaviour with errors
|
---|
60 | Main.error(new Exception("exception_error"));
|
---|
61 | Main.error("Error message on one line");
|
---|
62 | Main.error("First line of error message on several lines\nline2\nline3\nline4");
|
---|
63 | Collection<String> errors = Main.getLastErrorAndWarnings();
|
---|
64 | assertTrue(errors.contains("E: java.lang.Exception: exception_error"));
|
---|
65 | assertTrue(errors.contains("E: Error message on one line"));
|
---|
66 | assertTrue(errors.contains("E: First line of error message on several lines"));
|
---|
67 |
|
---|
68 | // Correct behaviour with warnings
|
---|
69 | Main.warn(new Exception("exception_warn", new Exception("root_cause")));
|
---|
70 | Main.warn("Warning message on one line");
|
---|
71 | Main.warn("First line of warning message on several lines\nline2\nline3\nline4");
|
---|
72 | Collection<String> warnings = Main.getLastErrorAndWarnings();
|
---|
73 | assertTrue(warnings.contains("W: java.lang.Exception: exception_warn. Cause: java.lang.Exception: root_cause"));
|
---|
74 | assertTrue(warnings.contains("W: Warning message on one line"));
|
---|
75 | assertTrue(warnings.contains("W: First line of warning message on several lines"));
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Unit test of {@link Main#preConstructorInit}.
|
---|
80 | */
|
---|
81 | @Test
|
---|
82 | public void testPreConstructorInit() {
|
---|
83 | Main.preConstructorInit();
|
---|
84 | assertNotNull(Main.getProjection());
|
---|
85 | assertEquals(Main.pref.get("laf", Main.platform.getDefaultStyle()), UIManager.getLookAndFeel().getClass().getCanonicalName());
|
---|
86 | assertNotNull(Main.toolbar);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Unit test of {@link Main#postConstructorProcessCmdLine} - empty case.
|
---|
91 | */
|
---|
92 | @Test
|
---|
93 | public void testPostConstructorProcessCmdLineEmpty() {
|
---|
94 | // Check the method accepts no arguments
|
---|
95 | Main.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal cases.
|
---|
100 | * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
|
---|
101 | */
|
---|
102 | @Test
|
---|
103 | public void testPostConstructorProcessCmdLineNominal() {
|
---|
104 | assertNull(Main.getLayerManager().getEditDataSet());
|
---|
105 | Main.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
|
---|
106 | "--download=0.01,0.01,0.05,0.05",
|
---|
107 | "--downloadgps=51.35,-0.4,51.60,0.2",
|
---|
108 | "--selection=type: node"}));
|
---|
109 | DataSet ds = Main.getLayerManager().getEditDataSet();
|
---|
110 | assertNotNull(ds);
|
---|
111 | assertFalse(ds.getSelected().isEmpty());
|
---|
112 | Main.getLayerManager().removeLayer(Main.getLayerManager().getEditLayer());
|
---|
113 | List<GpxLayer> gpxLayers = Main.getLayerManager().getLayersOfType(GpxLayer.class);
|
---|
114 | assertEquals(1, gpxLayers.size());
|
---|
115 | Main.getLayerManager().removeLayer(gpxLayers.iterator().next());
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Unit test of {@link DownloadParamType} enum.
|
---|
120 | */
|
---|
121 | @Test
|
---|
122 | public void testEnumDownloadParamType() {
|
---|
123 | TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
|
---|
124 | }
|
---|
125 | }
|
---|