source: josm/trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandlerTest.java@ 10436

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

see #13001 - replace calls to Main.main.[add|remove]Layer by Main.getLayerManager().[add|remove]Layer

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol.handler;
3
4import static org.junit.Assert.assertEquals;
5
6import java.io.File;
7
8import org.junit.BeforeClass;
9import org.junit.Rule;
10import org.junit.Test;
11import org.junit.rules.ExpectedException;
12import org.openstreetmap.josm.JOSMFixture;
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.TestUtils;
15import org.openstreetmap.josm.gui.layer.OsmDataLayer;
16import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
17import org.openstreetmap.josm.tools.Utils;
18
19/**
20 * Unit tests of {@link ImportHandler} class.
21 */
22public class ImportHandlerTest {
23
24 /**
25 * Rule used for tests throwing exceptions.
26 */
27 @Rule
28 public ExpectedException thrown = ExpectedException.none();
29
30 /**
31 * Setup test.
32 */
33 @BeforeClass
34 public static void setUpBeforeClass() {
35 JOSMFixture.createUnitTestFixture().init(true);
36 }
37
38 private static ImportHandler newHandler(String url) {
39 ImportHandler req = new ImportHandler();
40 if (url != null)
41 req.setUrl(url);
42 return req;
43 }
44
45 /**
46 * Non-regression test for bug #7434.
47 */
48 @Test
49 public void testTicket7434() {
50 ImportHandler req = newHandler("http://localhost:8111/import?url=http://localhost:8888/relations?relations=19711&mode=recursive");
51 assertEquals("http://localhost:8888/relations?relations=19711&mode=recursive", req.args.get("url"));
52 }
53
54 /**
55 * Unit test for bad request - no param.
56 * @throws Exception if any error occurs
57 */
58 @Test
59 public void testBadRequestNoParam() throws Exception {
60 thrown.expect(RequestHandlerBadRequestException.class);
61 thrown.expectMessage("MalformedURLException: null");
62 newHandler(null).handle();
63 }
64
65 /**
66 * Unit test for bad request - invalid URL.
67 * @throws Exception if any error occurs
68 */
69 @Test
70 public void testBadRequestInvalidUrl() throws Exception {
71 thrown.expect(RequestHandlerBadRequestException.class);
72 thrown.expectMessage("MalformedURLException: no protocol: invalid_url");
73 newHandler("https://localhost?url=invalid_url").handle();
74 }
75
76 /**
77 * Unit test for bad request - incomplete URL.
78 * @throws Exception if any error occurs
79 */
80 @Test
81 public void testBadRequestIncompleteUrl() throws Exception {
82 thrown.expect(RequestHandlerBadRequestException.class);
83 thrown.expectMessage("The following keys are mandatory, but have not been provided: url");
84 newHandler("https://localhost").handle();
85 }
86
87 /**
88 * Unit test for nominal request - local data file.
89 * @throws Exception if any error occurs
90 */
91 @Test
92 public void testNominalRequest() throws Exception {
93 String url = new File(TestUtils.getRegressionDataFile(11957, "data.osm")).toURI().toURL().toExternalForm();
94 try {
95 newHandler("https://localhost?url=" + Utils.encodeUrl(url)).handle();
96 } finally {
97 for (OsmDataLayer layer : Main.getLayerManager().getLayersOfType(OsmDataLayer.class)) {
98 Main.getLayerManager().removeLayer(layer);
99 }
100 }
101 }
102}
Note: See TracBrowser for help on using the repository browser.