source: josm/trunk/test/unit/org/openstreetmap/josm/MainTest.java@ 11988

Last change on this file since 11988 was 11988, checked in by Don-vip, 7 years ago

improve unit test for Main.postConstructorProcessCmdLine

File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertNull;
8import static org.junit.Assert.assertTrue;
9
10import java.net.MalformedURLException;
11import java.nio.file.Paths;
12import java.util.Collection;
13import java.util.List;
14
15import javax.swing.UIManager;
16
17import org.junit.Rule;
18import org.junit.Test;
19import org.openstreetmap.josm.Main.DownloadParamType;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.gui.ProgramArguments;
22import org.openstreetmap.josm.gui.layer.GpxLayer;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24
25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26
27/**
28 * Unit tests of {@link Main} class.
29 */
30public class MainTest {
31
32 /**
33 * Setup test.
34 */
35 @Rule
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules().platform().devAPI();
38
39 /**
40 * Unit test of {@link DownloadParamType#paramType} method.
41 */
42 @Test
43 public void testParamType() {
44 assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
45 assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
46 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
47 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
48 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
49 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
50 }
51
52 /**
53 * Unit tests on log messages.
54 */
55 @Test
56 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
57 public void testLogs() {
58
59 assertNull(Main.getErrorMessage(null));
60
61 // Correct behaviour with errors
62 Main.error(new Exception("exception_error"));
63 Main.error("Error message on one line");
64 Main.error("First line of error message on several lines\nline2\nline3\nline4");
65 Collection<String> errors = Main.getLastErrorAndWarnings();
66 assertTrue(errors.contains("E: java.lang.Exception: exception_error"));
67 assertTrue(errors.contains("E: Error message on one line"));
68 assertTrue(errors.contains("E: First line of error message on several lines"));
69
70 // Correct behaviour with warnings
71 Main.warn(new Exception("exception_warn", new Exception("root_cause")));
72 Main.warn("Warning message on one line");
73 Main.warn("First line of warning message on several lines\nline2\nline3\nline4");
74 Collection<String> warnings = Main.getLastErrorAndWarnings();
75 assertTrue(warnings.contains("W: java.lang.Exception: exception_warn. Cause: java.lang.Exception: root_cause"));
76 assertTrue(warnings.contains("W: Warning message on one line"));
77 assertTrue(warnings.contains("W: First line of warning message on several lines"));
78 }
79
80 /**
81 * Unit test of {@link Main#preConstructorInit}.
82 */
83 @Test
84 public void testPreConstructorInit() {
85 Main.preConstructorInit();
86 assertNotNull(Main.getProjection());
87 assertEquals(Main.pref.get("laf", Main.platform.getDefaultStyle()), UIManager.getLookAndFeel().getClass().getCanonicalName());
88 assertNotNull(Main.toolbar);
89 }
90
91 /**
92 * Unit test of {@link Main#postConstructorProcessCmdLine} - empty case.
93 */
94 @Test
95 public void testPostConstructorProcessCmdLineEmpty() {
96 // Check the method accepts no arguments
97 Main.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
98 }
99
100 private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) {
101 assertNull(Main.getLayerManager().getEditDataSet());
102 Main.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
103 "--download=" + download,
104 "--downloadgps=" + downloadGps,
105 "--selection=type: node"}));
106 DataSet ds = Main.getLayerManager().getEditDataSet();
107 assertNotNull(ds);
108 assertFalse(ds.getSelected().isEmpty());
109 Main.getLayerManager().removeLayer(Main.getLayerManager().getEditLayer());
110 if (gpx) {
111 List<GpxLayer> gpxLayers = Main.getLayerManager().getLayersOfType(GpxLayer.class);
112 assertEquals(1, gpxLayers.size());
113 Main.getLayerManager().removeLayer(gpxLayers.iterator().next());
114 }
115 }
116
117 /**
118 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with bounds.
119 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
120 */
121 @Test
122 public void testPostConstructorProcessCmdLineBounds() {
123 doTestPostConstructorProcessCmdLine(
124 "0.01,0.01,0.05,0.05",
125 "51.35,-0.4,51.60,0.2", true);
126 }
127
128 /**
129 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with http/https URLs.
130 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
131 */
132 @Test
133 public void testPostConstructorProcessCmdLineHttpUrl() {
134 doTestPostConstructorProcessCmdLine(
135 "http://api06.dev.openstreetmap.org/api/0.6/map?bbox=0.01,0.01,0.05,0.05",
136 "https://master.apis.dev.openstreetmap.org/api/0.6/trackpoints?bbox=-0.4,51.35,0.2,51.6&page=0", true);
137 }
138
139 /**
140 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with file URLs.
141 * @throws MalformedURLException if an error occurs
142 */
143 @Test
144 public void testPostConstructorProcessCmdLineFileUrl() throws MalformedURLException {
145 doTestPostConstructorProcessCmdLine(
146 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toUri().toURL().toExternalForm(),
147 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toUri().toURL().toExternalForm(), false);
148 }
149
150 /**
151 * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal case with file names.
152 * @throws MalformedURLException if an error occurs
153 */
154 @Test
155 public void testPostConstructorProcessCmdLineFilename() throws MalformedURLException {
156 doTestPostConstructorProcessCmdLine(
157 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toFile().getAbsolutePath(),
158 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toFile().getAbsolutePath(), false);
159 }
160
161 /**
162 * Unit test of {@link DownloadParamType} enum.
163 */
164 @Test
165 public void testEnumDownloadParamType() {
166 TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
167 }
168}
Note: See TracBrowser for help on using the repository browser.