Changeset 9732 in josm for trunk


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

remote control: add more unit tests, robustness

Location:
trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    r9214 r9732  
    273273                //
    274274                final OsmDataLayer layer = createNewLayer(newLayerName);
    275                 Main.main.addLayer(layer, computeBbox(bounds));
     275                if (Main.main != null)
     276                    Main.main.addLayer(layer, computeBbox(bounds));
    276277                return layer;
    277278            }
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r9353 r9732  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.IOException;
    67import java.io.InputStream;
    78import java.net.HttpURLConnection;
     
    122123            }
    123124
     125            if ("file".equals(url.getProtocol())) {
     126                try {
     127                    return url.openStream();
     128                } catch (IOException e) {
     129                    throw new OsmTransferException(e);
     130                }
     131            }
     132
    124133            final HttpClient client = HttpClient.create(url);
    125134            activeConnection = client;
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java

    r8846 r9732  
    115115    protected void validateRequest() throws RequestHandlerBadRequestException {
    116116        try {
    117             lat = Double.parseDouble(args.get("lat"));
    118             lon = Double.parseDouble(args.get("lon"));
     117            lat = Double.parseDouble(args != null ? args.get("lat") : "");
     118            lon = Double.parseDouble(args != null ? args.get("lon") : "");
    119119        } catch (NumberFormatException e) {
    120120            throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+')', e);
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandler.java

    r8444 r9732  
    9898    @Override
    9999    protected void validateRequest() throws RequestHandlerBadRequestException {
    100         String urlString = args.get("url");
     100        String urlString = args != null ? args.get("url") : null;
    101101        if (Main.pref.getBoolean("remotecontrol.importhandler.fix_url_query", true)) {
    102102            urlString = Utils.fixURLQuery(urlString);
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java

    r8929 r9732  
    223223        List<String> missingKeys = new LinkedList<>();
    224224        boolean error = false;
    225         if (mandatory != null) for (String key : mandatory) {
    226             String value = args.get(key);
    227             if (value == null || value.isEmpty()) {
    228                 error = true;
    229                 Main.warn('\'' + myCommand + "' remote control request must have '" + key + "' parameter");
    230                 missingKeys.add(key);
     225        if (mandatory != null && args != null) {
     226            for (String key : mandatory) {
     227                String value = args.get(key);
     228                if (value == null || value.isEmpty()) {
     229                    error = true;
     230                    Main.warn('\'' + myCommand + "' remote control request must have '" + key + "' parameter");
     231                    missingKeys.add(key);
     232                }
    231233            }
    232234        }
    233235        Set<String> knownParams = new HashSet<>();
    234         if (mandatory != null) Collections.addAll(knownParams, mandatory);
    235         if (optional != null) Collections.addAll(knownParams, optional);
    236         for (String par: args.keySet()) {
    237             if (!knownParams.contains(par)) {
    238                 Main.warn("Unknown remote control parameter {0}, skipping it", par);
     236        if (mandatory != null)
     237            Collections.addAll(knownParams, mandatory);
     238        if (optional != null)
     239            Collections.addAll(knownParams, optional);
     240        if (args != null) {
     241            for (String par: args.keySet()) {
     242                if (!knownParams.contains(par)) {
     243                    Main.warn("Unknown remote control parameter {0}, skipping it", par);
     244                }
    239245            }
    240246        }
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r9720 r9732  
    12211221     */
    12221222    public static String fixURLQuery(String url) {
    1223         if (url.indexOf('?') == -1)
     1223        if (url == null || url.indexOf('?') == -1)
    12241224            return url;
    12251225
  • 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.