source: josm/trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandlerTest.java@ 10973

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

see #13564 - proper management of invalid URLs in remote control handlers + add unit test

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