1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
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 |
|
---|
9 | import java.io.ByteArrayOutputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.PrintStream;
|
---|
12 | import java.net.MalformedURLException;
|
---|
13 | import java.nio.charset.StandardCharsets;
|
---|
14 | import java.nio.file.Paths;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.concurrent.ExecutionException;
|
---|
19 | import java.util.concurrent.Future;
|
---|
20 |
|
---|
21 | import javax.swing.UIManager;
|
---|
22 |
|
---|
23 | import org.junit.Rule;
|
---|
24 | import org.junit.Test;
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.TestUtils;
|
---|
27 | import org.openstreetmap.josm.data.Version;
|
---|
28 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
29 | import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
|
---|
30 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
31 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
32 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
33 | import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
|
---|
34 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
35 | import org.openstreetmap.josm.plugins.PluginListParseException;
|
---|
36 | import org.openstreetmap.josm.plugins.PluginListParser;
|
---|
37 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
38 | import org.openstreetmap.josm.tools.Logging;
|
---|
39 |
|
---|
40 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Unit tests of {@link MainApplication} class.
|
---|
44 | */
|
---|
45 | public class MainApplicationTest {
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Setup test.
|
---|
49 | */
|
---|
50 | @Rule
|
---|
51 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
52 | public JOSMTestRules test = new JOSMTestRules().main().https().devAPI().timeout(20000);
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Make sure {@link MainApplication#toolbar} is initialized.
|
---|
56 | */
|
---|
57 | public static void initToolbar() {
|
---|
58 | if (MainApplication.toolbar == null) {
|
---|
59 | MainApplication.toolbar = new ToolbarPreferences();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING")
|
---|
64 | private void testShow(final String arg, String expected) throws InterruptedException, IOException {
|
---|
65 | PrintStream old = System.out;
|
---|
66 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
---|
67 | System.setOut(new PrintStream(baos));
|
---|
68 | Thread t = new Thread() {
|
---|
69 | @Override
|
---|
70 | public void run() {
|
---|
71 | MainApplication.main(new String[] {arg});
|
---|
72 | }
|
---|
73 | };
|
---|
74 | t.start();
|
---|
75 | t.join();
|
---|
76 | System.out.flush();
|
---|
77 | assertEquals(expected, baos.toString(StandardCharsets.UTF_8.name()).trim());
|
---|
78 | } finally {
|
---|
79 | System.setOut(old);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Test of {@link MainApplication#main} with argument {@code --version}.
|
---|
85 | * @throws Exception in case of error
|
---|
86 | */
|
---|
87 | @Test
|
---|
88 | public void testShowVersion() throws Exception {
|
---|
89 | testShow("--version", Version.getInstance().getAgentString());
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Test of {@link MainApplication#main} with argument {@code --help}.
|
---|
94 | * @throws Exception in case of error
|
---|
95 | */
|
---|
96 | @Test
|
---|
97 | public void testShowHelp() throws Exception {
|
---|
98 | testShow("--help", MainApplication.getHelp().trim());
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Unit test of {@link DownloadParamType#paramType} method.
|
---|
103 | */
|
---|
104 | @Test
|
---|
105 | public void testParamType() {
|
---|
106 | assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
|
---|
107 | assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
|
---|
108 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
|
---|
109 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
|
---|
110 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
|
---|
111 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
|
---|
116 | * @throws PluginListParseException if an error occurs
|
---|
117 | */
|
---|
118 | @Test
|
---|
119 | public void testUpdateAndLoadPlugins() throws PluginListParseException {
|
---|
120 | final String old = System.getProperty("josm.plugins");
|
---|
121 | try {
|
---|
122 | System.setProperty("josm.plugins", "buildings_tools,plastic_laf");
|
---|
123 | SplashProgressMonitor monitor = new SplashProgressMonitor("foo", e -> {
|
---|
124 | // Do nothing
|
---|
125 | });
|
---|
126 | Collection<PluginInformation> plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
---|
127 | if (plugins.isEmpty()) {
|
---|
128 | PluginHandlerTestIT.downloadPlugins(Arrays.asList(
|
---|
129 | newPluginInformation("buildings_tools"),
|
---|
130 | newPluginInformation("plastic_laf")));
|
---|
131 | plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
---|
132 | }
|
---|
133 | assertEquals(2, plugins.size());
|
---|
134 | assertNotNull(PluginHandler.getPlugin("plastic_laf"));
|
---|
135 | assertNull(PluginHandler.getPlugin("buildings_tools"));
|
---|
136 | MainApplication.loadLatePlugins(null, monitor, plugins);
|
---|
137 | assertNotNull(PluginHandler.getPlugin("buildings_tools"));
|
---|
138 | } finally {
|
---|
139 | if (old != null) {
|
---|
140 | System.setProperty("josm.plugins", old);
|
---|
141 | } else {
|
---|
142 | System.clearProperty("josm.plugins");
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Unit test of {@link MainApplication#setupUIManager}.
|
---|
149 | */
|
---|
150 | @Test
|
---|
151 | public void testSetupUIManager() {
|
---|
152 | MainApplication.setupUIManager();
|
---|
153 | assertEquals(Main.pref.get("laf", Main.platform.getDefaultStyle()), UIManager.getLookAndFeel().getClass().getCanonicalName());
|
---|
154 | }
|
---|
155 |
|
---|
156 | private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
|
---|
157 | return PluginListParser.createInfo(plugin+".jar", "https://svn.openstreetmap.org/applications/editors/josm/dist/"+plugin+".jar",
|
---|
158 | "");
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - empty case.
|
---|
163 | */
|
---|
164 | @Test
|
---|
165 | public void testPostConstructorProcessCmdLineEmpty() {
|
---|
166 | // Check the method accepts no arguments
|
---|
167 | MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
|
---|
168 | }
|
---|
169 |
|
---|
170 | private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) {
|
---|
171 | assertNull(MainApplication.getLayerManager().getEditDataSet());
|
---|
172 | for (Future<?> f : MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
|
---|
173 | "--download=" + download,
|
---|
174 | "--downloadgps=" + downloadGps,
|
---|
175 | "--selection=type: node"}))) {
|
---|
176 | try {
|
---|
177 | f.get();
|
---|
178 | } catch (InterruptedException | ExecutionException e) {
|
---|
179 | Logging.error(e);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
---|
183 | assertNotNull(ds);
|
---|
184 | assertFalse(ds.getSelected().isEmpty());
|
---|
185 | MainApplication.getLayerManager().removeLayer(MainApplication.getLayerManager().getEditLayer());
|
---|
186 | if (gpx) {
|
---|
187 | List<GpxLayer> gpxLayers = MainApplication.getLayerManager().getLayersOfType(GpxLayer.class);
|
---|
188 | assertEquals(1, gpxLayers.size());
|
---|
189 | MainApplication.getLayerManager().removeLayer(gpxLayers.iterator().next());
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with bounds.
|
---|
195 | * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
|
---|
196 | */
|
---|
197 | @Test
|
---|
198 | public void testPostConstructorProcessCmdLineBounds() {
|
---|
199 | doTestPostConstructorProcessCmdLine(
|
---|
200 | "0.01,0.01,0.05,0.05",
|
---|
201 | "51.35,-0.4,51.60,0.2", true);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with http/https URLs.
|
---|
206 | * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
|
---|
207 | */
|
---|
208 | @Test
|
---|
209 | public void testPostConstructorProcessCmdLineHttpUrl() {
|
---|
210 | doTestPostConstructorProcessCmdLine(
|
---|
211 | "http://api06.dev.openstreetmap.org/api/0.6/map?bbox=0.01,0.01,0.05,0.05",
|
---|
212 | "https://master.apis.dev.openstreetmap.org/api/0.6/trackpoints?bbox=-0.4,51.35,0.2,51.6&page=0", true);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file URLs.
|
---|
217 | * @throws MalformedURLException if an error occurs
|
---|
218 | */
|
---|
219 | @Test
|
---|
220 | public void testPostConstructorProcessCmdLineFileUrl() throws MalformedURLException {
|
---|
221 | doTestPostConstructorProcessCmdLine(
|
---|
222 | Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toUri().toURL().toExternalForm(),
|
---|
223 | Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toUri().toURL().toExternalForm(), false);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file names.
|
---|
228 | * @throws MalformedURLException if an error occurs
|
---|
229 | */
|
---|
230 | @Test
|
---|
231 | public void testPostConstructorProcessCmdLineFilename() throws MalformedURLException {
|
---|
232 | doTestPostConstructorProcessCmdLine(
|
---|
233 | Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toFile().getAbsolutePath(),
|
---|
234 | Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toFile().getAbsolutePath(), false);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Unit test of {@link DownloadParamType} enum.
|
---|
239 | */
|
---|
240 | @Test
|
---|
241 | public void testEnumDownloadParamType() {
|
---|
242 | TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
|
---|
243 | }
|
---|
244 | }
|
---|