Changeset 9817 in josm for trunk


Ignore:
Timestamp:
2016-02-17T22:05:02+01:00 (9 years ago)
Author:
Don-vip
Message:

fix coverity defect 1351474 (division by zero), add unit test

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/DownloadAlongTrackAction.java

    r9804 r9817  
    8181                latcnt++;
    8282            }
     83        }
     84        if (latcnt == 0) {
     85            return null;
    8386        }
    8487        double avglat = latsum / latcnt;
     
    190193    @Override
    191194    public void actionPerformed(ActionEvent e) {
    192         Main.worker.submit(createTask());
     195        PleaseWaitRunnable task = createTask();
     196        if (task != null) {
     197            Main.worker.submit(task);
     198        }
    193199    }
    194200}
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadAlongTrackActionTest.java

    r9816 r9817  
    33
    44import static org.junit.Assert.assertNotNull;
     5import static org.junit.Assert.assertNull;
    56
    67import org.junit.BeforeClass;
     
    2829    }
    2930
    30     /**
    31      * Test action.
    32      * @throws Exception if an error occurs
    33      */
    34     @Test
    35     public void testDownload() throws Exception {
    36         final OsmDataLayer layer = new OsmDataLayer(new DataSet(), getClass().getName(), null);
     31    private static PleaseWaitRunnable createTask(String file) throws Exception {
     32        final OsmDataLayer layer = new OsmDataLayer(new DataSet(), DownloadAlongTrackActionTest.class.getName(), null);
    3733        try {
    3834            Main.main.addLayer(layer);
    3935            // Perform action
    40             final GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "minimal.gpx");
    41             PleaseWaitRunnable task = new DownloadAlongTrackAction(gpx).createTask();
    42             assertNotNull(task);
     36            final GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + file);
     37            return new DownloadAlongTrackAction(gpx).createTask();
    4338        } finally {
    4439            // Ensure we clean the place before leaving, even if test fails.
     
    4641        }
    4742    }
     43
     44    /**
     45     * Test action with nominal data set.
     46     * @throws Exception if an error occurs
     47     */
     48    @Test
     49    public void testDownload() throws Exception {
     50        assertNotNull(createTask("minimal.gpx"));
     51    }
     52
     53    /**
     54     * Test action with empty data set.
     55     * @throws Exception if an error occurs
     56     */
     57    @Test
     58    public void testDownloadEmpty() throws Exception {
     59        assertNull(createTask("empty.gpx"));
     60    }
    4861}
Note: See TracChangeset for help on using the changeset viewer.