source: josm/trunk/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java@ 12899

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

fix unit test for real (depends on OSM API dev data. Not ideal but these coordinates should last longer)

  • Property svn:eol-style set to native
File size: 12.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
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.awt.BorderLayout;
11import java.awt.event.KeyEvent;
12import java.io.ByteArrayOutputStream;
13import java.io.IOException;
14import java.io.PrintStream;
15import java.net.MalformedURLException;
16import java.nio.charset.StandardCharsets;
17import java.nio.file.Paths;
18import java.util.Arrays;
19import java.util.Collection;
20import java.util.List;
21import java.util.concurrent.ExecutionException;
22import java.util.concurrent.Future;
23
24import javax.swing.JComponent;
25import javax.swing.JPanel;
26import javax.swing.UIManager;
27
28import org.junit.Rule;
29import org.junit.Test;
30import org.openstreetmap.josm.Main;
31import org.openstreetmap.josm.TestUtils;
32import org.openstreetmap.josm.actions.AboutAction;
33import org.openstreetmap.josm.data.Version;
34import org.openstreetmap.josm.data.osm.DataSet;
35import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
36import org.openstreetmap.josm.gui.layer.GpxLayer;
37import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
38import org.openstreetmap.josm.plugins.PluginHandler;
39import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
40import org.openstreetmap.josm.plugins.PluginInformation;
41import org.openstreetmap.josm.plugins.PluginListParseException;
42import org.openstreetmap.josm.plugins.PluginListParser;
43import org.openstreetmap.josm.spi.preferences.Config;
44import org.openstreetmap.josm.testutils.JOSMTestRules;
45import org.openstreetmap.josm.tools.Logging;
46import org.openstreetmap.josm.tools.Shortcut;
47
48import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
49
50/**
51 * Unit tests of {@link MainApplication} class.
52 */
53public class MainApplicationTest {
54
55 /**
56 * Setup test.
57 */
58 @Rule
59 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
60 public JOSMTestRules test = new JOSMTestRules().main().projection().https().devAPI().timeout(20000);
61
62 /**
63 * Make sure {@link MainApplication#contentPanePrivate} is initialized.
64 */
65 public static void initContentPane() {
66 if (MainApplication.contentPanePrivate == null) {
67 MainApplication.contentPanePrivate = new JPanel(new BorderLayout());
68 }
69 }
70
71 /**
72 * Returns {@link MainApplication#contentPanePrivate} (not public).
73 * @return {@link MainApplication#contentPanePrivate}
74 */
75 public static JComponent getContentPane() {
76 return MainApplication.contentPanePrivate;
77 }
78
79 /**
80 * Make sure {@code MainApplication.mainPanel} is initialized.
81 * @param reAddListeners {@code true} to re-add listeners
82 */
83 @SuppressWarnings("deprecation")
84 public static void initMainPanel(boolean reAddListeners) {
85 if (MainApplication.mainPanel == null) {
86 MainApplication.mainPanel = new MainPanel(MainApplication.getLayerManager());
87 }
88 if (reAddListeners) {
89 MainApplication.mainPanel.reAddListeners();
90 }
91 if (Main.main != null) {
92 Main.main.panel = MainApplication.mainPanel;
93 }
94 }
95
96 /**
97 * Make sure {@code MainApplication.menu} is initialized.
98 */
99 @SuppressWarnings("deprecation")
100 public static void initMainMenu() {
101 MainApplication.menu = new MainMenu();
102 Main.main.menu = MainApplication.menu;
103 }
104
105 /**
106 * Make sure {@link MainApplication#toolbar} is initialized.
107 */
108 @SuppressWarnings("deprecation")
109 public static void initToolbar() {
110 if (MainApplication.toolbar == null) {
111 MainApplication.toolbar = new ToolbarPreferences();
112 }
113 if (Main.toolbar == null) {
114 Main.toolbar = MainApplication.getToolbar();
115 }
116 }
117
118 @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING")
119 private void testShow(final String arg, String expected) throws InterruptedException, IOException {
120 PrintStream old = System.out;
121 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
122 System.setOut(new PrintStream(baos));
123 Thread t = new Thread() {
124 @Override
125 public void run() {
126 MainApplication.main(new String[] {arg});
127 }
128 };
129 t.start();
130 t.join();
131 System.out.flush();
132 assertEquals(expected, baos.toString(StandardCharsets.UTF_8.name()).trim());
133 } finally {
134 System.setOut(old);
135 }
136 }
137
138 /**
139 * Test of {@link MainApplication#main} with argument {@code --version}.
140 * @throws Exception in case of error
141 */
142 @Test
143 public void testShowVersion() throws Exception {
144 testShow("--version", Version.getInstance().getAgentString());
145 }
146
147 /**
148 * Test of {@link MainApplication#main} with argument {@code --help}.
149 * @throws Exception in case of error
150 */
151 @Test
152 public void testShowHelp() throws Exception {
153 testShow("--help", MainApplication.getHelp().trim());
154 }
155
156 /**
157 * Unit test of {@link DownloadParamType#paramType} method.
158 */
159 @Test
160 public void testParamType() {
161 assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
162 assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
163 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
164 assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
165 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
166 assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
167 }
168
169 /**
170 * Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
171 * @throws PluginListParseException if an error occurs
172 */
173 @Test
174 public void testUpdateAndLoadPlugins() throws PluginListParseException {
175 final String old = System.getProperty("josm.plugins");
176 try {
177 System.setProperty("josm.plugins", "buildings_tools,plastic_laf");
178 SplashProgressMonitor monitor = new SplashProgressMonitor("foo", e -> {
179 // Do nothing
180 });
181 Collection<PluginInformation> plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
182 if (plugins.isEmpty()) {
183 PluginHandlerTestIT.downloadPlugins(Arrays.asList(
184 newPluginInformation("buildings_tools"),
185 newPluginInformation("plastic_laf")));
186 plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
187 }
188 assertEquals(2, plugins.size());
189 assertNotNull(PluginHandler.getPlugin("plastic_laf"));
190 assertNull(PluginHandler.getPlugin("buildings_tools"));
191 MainApplication.loadLatePlugins(null, monitor, plugins);
192 assertNotNull(PluginHandler.getPlugin("buildings_tools"));
193 } finally {
194 if (old != null) {
195 System.setProperty("josm.plugins", old);
196 } else {
197 System.clearProperty("josm.plugins");
198 }
199 }
200 }
201
202 /**
203 * Unit test of {@link MainApplication#setupUIManager}.
204 */
205 @Test
206 public void testSetupUIManager() {
207 MainApplication.setupUIManager();
208 assertEquals(Config.getPref().get("laf", Main.platform.getDefaultStyle()), UIManager.getLookAndFeel().getClass().getCanonicalName());
209 }
210
211 private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
212 return PluginListParser.createInfo(plugin+".jar", "https://svn.openstreetmap.org/applications/editors/josm/dist/"+plugin+".jar",
213 "");
214 }
215
216 /**
217 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - empty case.
218 */
219 @Test
220 public void testPostConstructorProcessCmdLineEmpty() {
221 // Check the method accepts no arguments
222 MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
223 }
224
225 private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) {
226 assertNull(MainApplication.getLayerManager().getEditDataSet());
227 for (Future<?> f : MainApplication.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
228 "--download=" + download,
229 "--downloadgps=" + downloadGps,
230 "--selection=type: node"}))) {
231 try {
232 f.get();
233 } catch (InterruptedException | ExecutionException e) {
234 Logging.error(e);
235 }
236 }
237 DataSet ds = MainApplication.getLayerManager().getEditDataSet();
238 assertNotNull(ds);
239 assertFalse(ds.getSelected().isEmpty());
240 MainApplication.getLayerManager().removeLayer(MainApplication.getLayerManager().getEditLayer());
241 if (gpx) {
242 List<GpxLayer> gpxLayers = MainApplication.getLayerManager().getLayersOfType(GpxLayer.class);
243 assertEquals(1, gpxLayers.size());
244 MainApplication.getLayerManager().removeLayer(gpxLayers.iterator().next());
245 }
246 }
247
248 /**
249 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with bounds.
250 * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
251 */
252 @Test
253 public void testPostConstructorProcessCmdLineBounds() {
254 doTestPostConstructorProcessCmdLine(
255 "-47.20,-126.75,-47.10,-126.65",
256 "51.35,-0.4,51.60,0.2", true);
257 }
258
259 /**
260 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with http/https URLs.
261 * This test assumes the DEV API contains nodes around -47.15, -126.7 (R'lyeh) and GPX tracks around London
262 */
263 @Test
264 public void testPostConstructorProcessCmdLineHttpUrl() {
265 doTestPostConstructorProcessCmdLine(
266 "http://api06.dev.openstreetmap.org/api/0.6/map?bbox=-126.75,-47.20,-126.65,-47.10",
267 "https://master.apis.dev.openstreetmap.org/api/0.6/trackpoints?bbox=-0.4,51.35,0.2,51.6&page=0", true);
268 }
269
270 /**
271 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file URLs.
272 * @throws MalformedURLException if an error occurs
273 */
274 @Test
275 public void testPostConstructorProcessCmdLineFileUrl() throws MalformedURLException {
276 doTestPostConstructorProcessCmdLine(
277 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toUri().toURL().toExternalForm(),
278 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toUri().toURL().toExternalForm(), false);
279 }
280
281 /**
282 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file names.
283 * @throws MalformedURLException if an error occurs
284 */
285 @Test
286 public void testPostConstructorProcessCmdLineFilename() throws MalformedURLException {
287 doTestPostConstructorProcessCmdLine(
288 Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toFile().getAbsolutePath(),
289 Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toFile().getAbsolutePath(), false);
290 }
291
292 /**
293 * Unit test of {@link MainApplication#getRegisteredActionShortcut}.
294 */
295 @Test
296 public void testGetRegisteredActionShortcut() {
297 Shortcut noKeystroke = Shortcut.registerShortcut("no", "keystroke", 0, 0);
298 assertNull(noKeystroke.getKeyStroke());
299 assertNull(MainApplication.getRegisteredActionShortcut(noKeystroke));
300 Shortcut noAction = Shortcut.registerShortcut("foo", "bar", KeyEvent.VK_AMPERSAND, Shortcut.SHIFT);
301 assertNotNull(noAction.getKeyStroke());
302 assertNull(MainApplication.getRegisteredActionShortcut(noAction));
303 AboutAction about = new AboutAction();
304 assertEquals(about, MainApplication.getRegisteredActionShortcut(about.getShortcut()));
305 }
306
307 /**
308 * Unit test of {@link MainApplication#addMapFrameListener} and {@link MainApplication#removeMapFrameListener}.
309 */
310 @Test
311 public void testMapFrameListener() {
312 MapFrameListener listener = (o, n) -> { };
313 assertTrue(MainApplication.addMapFrameListener(listener));
314 assertFalse(MainApplication.addMapFrameListener(null));
315 assertTrue(MainApplication.removeMapFrameListener(listener));
316 assertFalse(MainApplication.removeMapFrameListener(null));
317 }
318
319 /**
320 * Unit test of {@link DownloadParamType} enum.
321 */
322 @Test
323 public void testEnumDownloadParamType() {
324 TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
325 }
326}
Note: See TracBrowser for help on using the repository browser.