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.assertNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 |
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.Point;
|
---|
11 | import java.util.Collection;
|
---|
12 |
|
---|
13 | import org.junit.BeforeClass;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.Main.DownloadParamType;
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
18 |
|
---|
19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link Main} class.
|
---|
23 | */
|
---|
24 | public class MainTest {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Setup test.
|
---|
28 | */
|
---|
29 | @BeforeClass
|
---|
30 | public static void setUp() {
|
---|
31 | JOSMFixture.createUnitTestFixture().init();
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unit test of {@link DownloadParamType#paramType} method.
|
---|
36 | */
|
---|
37 | @Test
|
---|
38 | public void testParamType() {
|
---|
39 | assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
|
---|
40 | assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
|
---|
41 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
|
---|
42 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
|
---|
43 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
|
---|
44 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Unit test of {@code Main#preConstructorInit}.
|
---|
49 | */
|
---|
50 | @Test
|
---|
51 | public void testPreConstructorInit() {
|
---|
52 | Main.preConstructorInit(MainApplication.buildCommandLineArgumentMap(new String[0]));
|
---|
53 | Main.preConstructorInit(MainApplication.buildCommandLineArgumentMap(new String[]{"--geometry=400x300+10+5", "--no-maximize"}));
|
---|
54 | assertEquals(new WindowGeometry(new Point(10, 5), new Dimension(400, 300)), Main.geometry);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Unit tests on log messages.
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
|
---|
62 | public void testLogs() {
|
---|
63 |
|
---|
64 | assertNull(Main.getErrorMessage(null));
|
---|
65 |
|
---|
66 | // Correct behaviour with errors
|
---|
67 | Main.error(new Exception("exception_error"));
|
---|
68 | Main.error("Error message on one line");
|
---|
69 | Main.error("First line of error message on several lines\nline2\nline3\nline4");
|
---|
70 | Collection<String> errors = Main.getLastErrorAndWarnings();
|
---|
71 | assertTrue(errors.contains("E: java.lang.Exception: exception_error"));
|
---|
72 | assertTrue(errors.contains("E: Error message on one line"));
|
---|
73 | assertTrue(errors.contains("E: First line of error message on several lines"));
|
---|
74 |
|
---|
75 | // Correct behaviour with warnings
|
---|
76 | Main.warn(new Exception("exception_warn", new Exception("root_cause")));
|
---|
77 | Main.warn("Warning message on one line");
|
---|
78 | Main.warn("First line of warning message on several lines\nline2\nline3\nline4");
|
---|
79 | Collection<String> warnings = Main.getLastErrorAndWarnings();
|
---|
80 | assertTrue(warnings.contains("W: java.lang.Exception: exception_warn. Cause: java.lang.Exception: root_cause"));
|
---|
81 | assertTrue(warnings.contains("W: Warning message on one line"));
|
---|
82 | assertTrue(warnings.contains("W: First line of warning message on several lines"));
|
---|
83 |
|
---|
84 | int defaultLevel = Main.logLevel;
|
---|
85 |
|
---|
86 | // Check levels
|
---|
87 | Main.logLevel = 5;
|
---|
88 | assertTrue(Main.isTraceEnabled());
|
---|
89 | assertTrue(Main.isDebugEnabled());
|
---|
90 |
|
---|
91 | Main.logLevel = 4;
|
---|
92 | assertFalse(Main.isTraceEnabled());
|
---|
93 | assertTrue(Main.isDebugEnabled());
|
---|
94 |
|
---|
95 | Main.logLevel = 3;
|
---|
96 | assertFalse(Main.isTraceEnabled());
|
---|
97 | assertFalse(Main.isDebugEnabled());
|
---|
98 |
|
---|
99 | Main.logLevel = defaultLevel;
|
---|
100 | }
|
---|
101 | }
|
---|