source: josm/trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandlerTest.java@ 9732

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

remote control: add more unit tests, robustness

  • 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 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 AddNodeHandler} class.
16 */
17public class AddNodeHandlerTest {
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 AddNodeHandler newHandler(String url) {
34 AddNodeHandler req = new AddNodeHandler();
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 node");
48 newHandler("https://localhost?lat=0&lon=0").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("NumberFormatException (empty String)");
59 OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null);
60 try {
61 Main.main.addLayer(layer);
62 newHandler(null).handle();
63 } finally {
64 Main.main.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: lat, lon");
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: lat, lon");
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.main.addLayer(layer);
99 newHandler("https://localhost?lat=0&lon=0").handle();
100 } finally {
101 Main.main.removeLayer(layer);
102 }
103 }
104}
Note: See TracBrowser for help on using the repository browser.