Ticket #18731: 18731.1.patch

File 18731.1.patch, 3.4 KB (added by taylor.smock, 3 months ago)

Add tests and fix an issue with a possible NPE (if bbox is null, there is an error in searchNodes) this time, with the correct file

  • src/org/openstreetmap/josm/data/osm/DataSet.java

     
    429429        }
    430430    }
    431431
     432    /**
     433     * Searches for all primitives in the given bounding box
     434     *
     435     * @param bbox the bounding box
     436     * @return List of primitives in the given bbox. Can be empty but not null
     437     * @since xxx
     438     */
     439    public List<OsmPrimitive> searchPrimitives(BBox bbox) {
     440        List<OsmPrimitive> primitiveList = new ArrayList<>();
     441        primitiveList.addAll(searchNodes(bbox));
     442        primitiveList.addAll(searchWays(bbox));
     443        primitiveList.addAll(searchRelations(bbox));
     444        return primitiveList;
     445    }
     446
    432447    @Override
    433448    public Collection<Relation> getRelations() {
    434449        return getPrimitives(Relation.class::isInstance);
  • src/org/openstreetmap/josm/data/osm/QuadBuckets.java

     
    585585     */
    586586    public List<T> search(BBox searchBbox) {
    587587        List<T> ret = new ArrayList<>();
    588         if (!searchBbox.isValid()) {
     588        if (searchBbox == null || !searchBbox.isValid()) {
    589589            return ret;
    590590        }
    591591
  • test/unit/org/openstreetmap/josm/data/osm/DataSetTest.java

     
    1313import org.junit.Assert;
    1414import org.junit.Rule;
    1515import org.junit.Test;
     16import org.openstreetmap.josm.TestUtils;
    1617import org.openstreetmap.josm.data.Bounds;
    1718import org.openstreetmap.josm.data.DataSource;
    1819import org.openstreetmap.josm.data.coor.LatLon;
     
    6768    }
    6869
    6970    /**
     71     * Unit test for {@link DataSet#searchPrimitives}
     72     */
     73    @Test
     74    public void testSearchPrimitives() {
     75        final DataSet ds = new DataSet();
     76        // null bbox => empty list
     77        Assert.assertTrue("Empty data set should produce an empty list.", ds.searchPrimitives(null).isEmpty());
     78
     79        // empty data set, any bbox => empty list
     80        BBox bbox = new BBox(new LatLon(-180, -90), new LatLon(180, 90));
     81        Assert.assertTrue("Empty data set should produce an empty list.", ds.searchPrimitives(bbox).isEmpty());
     82        // data set with elements in the given bbox => these elements
     83        Node node = new Node(LatLon.ZERO);
     84        Node node2 = new Node(new LatLon(-0.01, -0.01));
     85        Way way = TestUtils.newWay("", node, node2);
     86        Relation r = new Relation(1);
     87        RelationMember rm = new RelationMember("role", node);
     88        r.addMember(rm);
     89        way.getNodes().forEach(ds::addPrimitive);
     90        ds.addPrimitive(way);
     91        ds.addPrimitive(r);
     92        bbox = new BBox(new LatLon(-1.0, -1.0), new LatLon(1.0, 1.0));
     93        List<OsmPrimitive> result = ds.searchPrimitives(bbox);
     94        Assert.assertEquals("We should have found four items.", 4, result.size());
     95    }
     96
     97    /**
    7098     * Unit test of methods {@link DataSet#addChangeSetTag} / {@link DataSet#getChangeSetTags}.
    7199     */
    72100    @Test