Changeset 9732 in josm for trunk/test/unit/org/openstreetmap


Ignore:
Timestamp:
2016-02-04T00:48:38+01:00 (9 years ago)
Author:
Don-vip
Message:

remote control: add more unit tests, robustness

Location:
trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/handler
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandlerTest.java

    r8876 r9732  
    44import static org.junit.Assert.assertEquals;
    55
     6import java.io.File;
     7
     8import org.junit.BeforeClass;
     9import org.junit.Rule;
    610import 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;
    718
    819/**
     
    1223
    1324    /**
     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    /**
    1446     * Non-regression test for bug #7434.
    1547     */
    1648    @Test
    1749    public void testTicket7434() {
    18         final ImportHandler req = new ImportHandler();
    19         req.setUrl("http://localhost:8111/import?url=http://localhost:8888/relations?relations=19711&mode=recursive");
     50        ImportHandler req = newHandler("http://localhost:8111/import?url=http://localhost:8888/relations?relations=19711&mode=recursive");
    2051        assertEquals("http://localhost:8888/relations?relations=19711&mode=recursive", req.args.get("url"));
    2152    }
     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    }
    22102}
Note: See TracChangeset for help on using the changeset viewer.