Changeset 12166 in josm


Ignore:
Timestamp:
2017-05-15T16:53:55+02:00 (8 years ago)
Author:
michael2402
Message:

Implement GpxData tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/gpx/GpxDataTest.java

    r12164 r12166  
    44import static org.junit.Assert.assertEquals;
    55import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertNotNull;
     7import static org.junit.Assert.assertNull;
    68import static org.junit.Assert.assertTrue;
    7 import static org.junit.Assert.fail;
    8 
     9
     10import java.util.ArrayList;
     11import java.util.Arrays;
    912import java.util.Collections;
     13import java.util.Date;
     14import java.util.List;
     15import java.util.stream.Collectors;
     16import java.util.stream.Stream;
    1017
    1118import org.junit.Before;
    1219import org.junit.Rule;
    1320import org.junit.Test;
     21import org.openstreetmap.josm.Main;
     22import org.openstreetmap.josm.data.Bounds;
     23import org.openstreetmap.josm.data.DataSource;
     24import org.openstreetmap.josm.data.coor.EastNorth;
    1425import org.openstreetmap.josm.data.coor.LatLon;
     26import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeEvent;
     27import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeListener;
    1528import org.openstreetmap.josm.testutils.JOSMTestRules;
     29import org.openstreetmap.josm.tools.ListenerList;
    1630
    1731import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    2842    @Rule
    2943    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    30     public JOSMTestRules test = new JOSMTestRules();
     44    public JOSMTestRules test = new JOSMTestRules().projection();
    3145
    3246    private GpxData data;
     
    4660    @Test
    4761    public void testMergeFrom() {
    48         fail("Not yet implemented");
     62        ImmutableGpxTrack track = singleWaypointGpxTrack();
     63        GpxRoute route = singleWaypointRoute();
     64        WayPoint newWP = new WayPoint(LatLon.NORTH_POLE);
     65        WayPoint existingWP = new WayPoint(LatLon.SOUTH_POLE);
     66
     67        GpxData dataToMerge = new GpxData();
     68        dataToMerge.addTrack(track);
     69        dataToMerge.addRoute(route);
     70        dataToMerge.addWaypoint(newWP);
     71
     72        data.addWaypoint(existingWP);
     73        data.mergeFrom(dataToMerge);
     74
     75        assertEquals(1, data.getTracks().size());
     76        assertEquals(1, data.getRoutes().size());
     77        assertEquals(2, data.getWaypoints().size());
     78
     79        assertTrue(data.getTracks().contains(track));
     80        assertTrue(data.getRoutes().contains(route));
     81        assertTrue(data.getWaypoints().contains(newWP));
     82        assertTrue(data.getWaypoints().contains(existingWP));
    4983    }
    5084
     
    5791
    5892        ImmutableGpxTrack track1 = emptyGpxTrack();
    59         ImmutableGpxTrack track2 = emptyGpxTrack();
     93        ImmutableGpxTrack track2 = singleWaypointGpxTrack();
    6094        data.addTrack(track1);
    6195        assertEquals(1, data.getTracks().size());
     
    101135        GpxRoute route1 = new GpxRoute();
    102136        GpxRoute route2 = new GpxRoute();
     137        route2.routePoints.add(new WayPoint(LatLon.NORTH_POLE));
    103138        data.addRoute(route1);
    104139        assertEquals(1, data.getRoutes().size());
     
    143178
    144179        WayPoint waypoint1 = new WayPoint(LatLon.ZERO);
    145         WayPoint waypoint2 = new WayPoint(LatLon.ZERO);
     180        WayPoint waypoint2 = new WayPoint(LatLon.NORTH_POLE);
    146181        data.addWaypoint(waypoint1);
    147182        assertEquals(1, data.getWaypoints().size());
     
    211246    @Test
    212247    public void testHasRoutePoints() {
    213         fail("Not yet implemented");
     248
    214249    }
    215250
     
    219254    @Test
    220255    public void testIsEmpty() {
    221         fail("Not yet implemented");
    222     }
    223 
    224     /**
    225      * Test method for {@link GpxData#getMetaBounds()}.
    226      */
    227     @Test
    228     public void testGetMetaBounds() {
    229         fail("Not yet implemented");
    230     }
    231 
    232     /**
    233      * Test method for {@link GpxData#recalculateBounds()}.
    234      */
    235     @Test
    236     public void testRecalculateBounds() {
    237         fail("Not yet implemented");
     256        ImmutableGpxTrack track1 = singleWaypointGpxTrack();
     257        WayPoint waypoint = new WayPoint(LatLon.ZERO);
     258        GpxRoute route = singleWaypointRoute();
     259
     260        assertTrue(data.isEmpty());
     261
     262        data.addTrack(track1);
     263        assertFalse(data.isEmpty());
     264        data.removeTrack(track1);
     265        assertTrue(data.isEmpty());
     266
     267        data.addWaypoint(waypoint);
     268        assertFalse(data.isEmpty());
     269        data.removeWaypoint(waypoint);
     270        assertTrue(data.isEmpty());
     271
     272        data.addRoute(route);
     273        assertFalse(data.isEmpty());
     274        data.removeRoute(route);
     275        assertTrue(data.isEmpty());
    238276    }
    239277
     
    243281    @Test
    244282    public void testLength() {
    245         fail("Not yet implemented");
    246     }
    247 
    248     /**
    249      * Test method for {@link GpxData#getMinMaxTimeForTrack(GpxTrack)}.
    250      */
    251     @Test
    252     public void testGetMinMaxTimeForTrack() {
    253         fail("Not yet implemented");
     283        ImmutableGpxTrack track1 = waypointGpxTrack(new WayPoint(new LatLon(0, 0)), new WayPoint(new LatLon(1, 1)), new WayPoint(new LatLon(0, 2)));
     284        ImmutableGpxTrack track2 = waypointGpxTrack(new WayPoint(new LatLon(0, 0)), new WayPoint(new LatLon(-1, 1)));
     285        data.addTrack(track1);
     286        data.addTrack(track2);
     287        assertEquals(3 * new LatLon(0, 0).greatCircleDistance(new LatLon(1, 1)), data.length(), 1);
     288
    254289    }
    255290
     
    259294    @Test
    260295    public void testGetMinMaxTimeForAllTracks() {
    261         fail("Not yet implemented");
     296        assertEquals(0, data.getMinMaxTimeForAllTracks().length);
     297
     298        WayPoint p1 = new WayPoint(LatLon.NORTH_POLE);
     299        WayPoint p2 = new WayPoint(LatLon.NORTH_POLE);
     300        WayPoint p3 = new WayPoint(LatLon.NORTH_POLE);
     301        WayPoint p4 = new WayPoint(LatLon.NORTH_POLE);
     302        WayPoint p5 = new WayPoint(LatLon.NORTH_POLE);
     303        p1.setTime(new Date(200020));
     304        p2.setTime(new Date(100020));
     305        p4.setTime(new Date(500020));
     306        data.addTrack(new ImmutableGpxTrack(Arrays.asList(Arrays.asList(p1, p2)), Collections.emptyMap()));
     307        data.addTrack(new ImmutableGpxTrack(Arrays.asList(Arrays.asList(p3, p4, p5)), Collections.emptyMap()));
     308
     309        Date[] times = data.getMinMaxTimeForAllTracks();
     310        assertEquals(times.length, 2);
     311        assertEquals(new Date(100020), times[0]);
     312        assertEquals(new Date(500020), times[1]);
    262313    }
    263314
     
    267318    @Test
    268319    public void testNearestPointOnTrack() {
    269         fail("Not yet implemented");
    270     }
    271 
    272     /**
    273      * Test method for {@link GpxData#getLinesIterable(boolean[])}.
    274      */
    275     @Test
    276     public void testGetLinesIterable() {
    277         fail("Not yet implemented");
    278     }
    279 
    280     /**
    281      * Test method for {@link GpxData#resetEastNorthCache()}.
    282      */
    283     @Test
    284     public void testResetEastNorthCache() {
    285         fail("Not yet implemented");
     320        List<WayPoint> points = Stream
     321                .of(new EastNorth(10, 10), new EastNorth(10, 0), new EastNorth(-1, 0))
     322                .map(Main.getProjection()::eastNorth2latlon)
     323                .map(WayPoint::new)
     324                .collect(Collectors.toList());
     325        data.addTrack(new ImmutableGpxTrack(Arrays.asList(points), Collections.emptyMap()));
     326
     327        assertEquals(points.get(1), data.nearestPointOnTrack(new EastNorth(10, 0), 10));
     328
     329        WayPoint close = data.nearestPointOnTrack(new EastNorth(5, 5), 10);
     330        assertEquals(10, close.getEastNorth().east(), .01);
     331        assertEquals(5, close.getEastNorth().north(), .01);
     332
     333        assertNull(data.nearestPointOnTrack(new EastNorth(5, 5), 1));
    286334    }
    287335
     
    291339    @Test
    292340    public void testGetDataSources() {
    293         fail("Not yet implemented");
     341        DataSource ds = new DataSource(new Bounds(0, 0, 1, 1), "test");
     342        data.dataSources.add(ds);
     343        assertEquals(new ArrayList<>(Arrays.asList(ds)), new ArrayList<>(data.getDataSources()));
    294344    }
    295345
     
    299349    @Test
    300350    public void testGetDataSourceArea() {
    301         fail("Not yet implemented");
     351        DataSource ds = new DataSource(new Bounds(0, 0, 1, 1), "test");
     352        data.dataSources.add(ds);
     353        assertNotNull(data.getDataSourceArea());
     354        assertTrue(data.getDataSourceArea().contains(0.5, 0.5));
     355        assertFalse(data.getDataSourceArea().contains(0.5, 1.5));
    302356    }
    303357
     
    307361    @Test
    308362    public void testGetDataSourceBounds() {
    309         fail("Not yet implemented");
    310     }
    311 
    312     /**
    313      * Test method for {@link GpxData#addChangeListener(GpxData.GpxDataChangeListener)}.
    314      */
    315     @Test
    316     public void testAddChangeListener() {
    317         fail("Not yet implemented");
    318     }
    319 
    320     /**
    321      * Test method for {@link GpxData#addWeakChangeListener(GpxData.GpxDataChangeListener)}.
    322      */
    323     @Test
    324     public void testAddWeakChangeListener() {
    325         fail("Not yet implemented");
    326     }
    327 
    328     /**
    329      * Test method for {@link GpxData#removeChangeListener(GpxData.GpxDataChangeListener)}.
    330      */
    331     @Test
    332     public void testRemoveChangeListener() {
    333         fail("Not yet implemented");
     363        Bounds bounds = new Bounds(0, 0, 1, 1);
     364        DataSource ds = new DataSource(bounds, "test");
     365        data.dataSources.add(ds);
     366        assertEquals(Arrays.asList(bounds), data.getDataSourceBounds());
     367    }
     368
     369    /**
     370     * Test method for {@link GpxData#addChangeListener(GpxData.GpxDataChangeListener)},
     371     * {@link GpxData#addWeakChangeListener(GpxData.GpxDataChangeListener)},
     372     * {@link GpxData#removeChangeListener(GpxData.GpxDataChangeListener)}.
     373     */
     374    @Test
     375    public void testChangeListener() {
     376        TestChangeListener cl1 = new TestChangeListener();
     377        TestChangeListener cl2 = new TestChangeListener();
     378
     379        data.addChangeListener(cl1);
     380        data.addWeakChangeListener(cl2);
     381        assertNull(cl1.lastEvent);
     382        assertNull(cl2.lastEvent);
     383
     384        data.addTrack(singleWaypointGpxTrack());
     385        assertEquals(data, cl1.lastEvent.getSource());
     386        assertEquals(data, cl2.lastEvent.getSource());
     387        cl1.lastEvent = null;
     388        cl2.lastEvent = null;
     389
     390        data.addRoute(singleWaypointRoute());
     391        assertEquals(data, cl1.lastEvent.getSource());
     392        assertEquals(data, cl2.lastEvent.getSource());
     393        cl1.lastEvent = null;
     394        cl2.lastEvent = null;
     395
     396        data.removeChangeListener(cl1);
     397        data.removeChangeListener(cl2);
     398        data.addTrack(singleWaypointGpxTrack());
     399        assertNull(cl1.lastEvent);
     400        assertNull(cl2.lastEvent);
     401    }
     402
     403    private static class TestChangeListener implements GpxDataChangeListener {
     404
     405        private GpxDataChangeEvent lastEvent;
     406
     407        @Override
     408        public void gpxDataChanged(GpxDataChangeEvent e) {
     409            lastEvent = e;
     410        }
     411
    334412    }
    335413
     
    342420    }
    343421
     422    private static ImmutableGpxTrack waypointGpxTrack(WayPoint... wps) {
     423        return new ImmutableGpxTrack(Collections.singleton(Arrays.asList(wps)), Collections.emptyMap());
     424    }
     425
     426    private static GpxRoute singleWaypointRoute() {
     427        GpxRoute route = new GpxRoute();
     428        route.routePoints.add(new WayPoint(LatLon.ZERO));
     429        return route;
     430    }
     431
    344432    /**
    345433     * Unit test of methods {@link GpxData#equals} and {@link GpxData#hashCode}.
     
    348436    public void testEqualsContract() {
    349437        EqualsVerifier.forClass(GpxData.class).usingGetClass()
    350             .withIgnoredFields("attr", "creator", "fromServer", "storageFile", "listeners")
     438            .withIgnoredFields("attr", "creator", "fromServer", "storageFile", "listeners", "tracks", "routes", "waypoints", "proxy")
    351439            .withPrefabValues(WayPoint.class, new WayPoint(LatLon.NORTH_POLE), new WayPoint(LatLon.SOUTH_POLE))
     440            .withPrefabValues(ListenerList.class, ListenerList.create(), ListenerList.create())
    352441            .verify();
    353442    }
Note: See TracChangeset for help on using the changeset viewer.