1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 |
|
---|
8 | import org.junit.BeforeClass;
|
---|
9 | import org.junit.Rule;
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.junit.rules.ExpectedException;
|
---|
12 | import org.openstreetmap.josm.JOSMFixture;
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.TestUtils;
|
---|
15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
16 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Unit tests of {@link ImportHandler} class.
|
---|
21 | */
|
---|
22 | public 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.map.mapView.getLayersOfType(OsmDataLayer.class)) {
|
---|
98 | Main.main.removeLayer(layer);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|