Changeset 8965 in josm for trunk/test/unit


Ignore:
Timestamp:
2015-10-29T12:52:09+01:00 (9 years ago)
Author:
simon04
Message:

see #10730 - Way splitting: fix and test (route) relation adaption

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/actions/SplitWayActionTest.java

    r8876 r8965  
    22package org.openstreetmap.josm.actions;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.assertSame;
    56import static org.junit.Assert.assertTrue;
    67
     8import java.util.ArrayList;
    79import java.util.Arrays;
     10import java.util.Iterator;
    811
    912import org.junit.BeforeClass;
     
    1215import org.openstreetmap.josm.Main;
    1316import org.openstreetmap.josm.data.coor.EastNorth;
     17import org.openstreetmap.josm.data.coor.LatLon;
    1418import org.openstreetmap.josm.data.osm.DataSet;
    1519import org.openstreetmap.josm.data.osm.Node;
     20import org.openstreetmap.josm.data.osm.OsmPrimitive;
     21import org.openstreetmap.josm.data.osm.Relation;
     22import org.openstreetmap.josm.data.osm.RelationMember;
    1623import org.openstreetmap.josm.data.osm.Way;
    1724import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    94101        }
    95102    }
     103
     104    @Test
     105    public void testRouteRelation() {
     106        doTestRouteRelation(false, 0);
     107        doTestRouteRelation(false, 1);
     108        doTestRouteRelation(false, 2);
     109        doTestRouteRelation(false, 3);
     110        doTestRouteRelation(true, 0);
     111        doTestRouteRelation(true, 1);
     112        doTestRouteRelation(true, 2);
     113        doTestRouteRelation(true, 3);
     114    }
     115
     116    void doTestRouteRelation(final boolean wayIsReversed, final int indexOfWayToKeep) {
     117        final DataSet dataSet = new DataSet();
     118        final OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
     119        final Node n1 = new Node(new LatLon(1, 0));
     120        final Node n2 = new Node(new LatLon(2, 0));
     121        final Node n3 = new Node(new LatLon(3, 0));
     122        final Node n4 = new Node(new LatLon(4, 0));
     123        final Node n5 = new Node(new LatLon(5, 0));
     124        final Node n6 = new Node(new LatLon(6, 0));
     125        final Node n7 = new Node(new LatLon(7, 0));
     126        final Way w1 = new Way();
     127        final Way w2 = new Way();
     128        final Way w3 = new Way();
     129        final Relation route = new Relation();
     130        for (OsmPrimitive p : Arrays.asList(n1, n2, n3, n4, n5, n6, n7, w1, w2, w3, route)) {
     131            dataSet.addPrimitive(p);
     132        }
     133        w1.setNodes(Arrays.asList(n1, n2));
     134        w2.setNodes(wayIsReversed
     135                ? Arrays.asList(n6, n5, n4, n3, n2)
     136                : Arrays.asList(n2, n3, n4, n5, n6)
     137        );
     138        w3.setNodes(Arrays.asList(n6, n7));
     139        route.put("type", "route");
     140        route.addMember(new RelationMember("", w1));
     141        route.addMember(new RelationMember("", w2));
     142        route.addMember(new RelationMember("", w3));
     143        dataSet.setSelected(Arrays.asList(w2, n3, n4, n5));
     144
     145
     146        final SplitWayAction.Strategy strategy = new SplitWayAction.Strategy() {
     147
     148            @Override
     149            public Way determineWayToKeep(Iterable<Way> wayChunks) {
     150                final Iterator<Way> it = wayChunks.iterator();
     151                for (int i = 0; i < indexOfWayToKeep; i++) {
     152                    it.next();
     153                }
     154                return it.next();
     155            }
     156        };
     157        final SplitWayAction.SplitWayResult result = SplitWayAction.splitWay(
     158                layer, w2, SplitWayAction.buildSplitChunks(w2, Arrays.asList(n3, n4, n5)), new ArrayList<OsmPrimitive>(), strategy);
     159        Main.main.undoRedo.add(result.getCommand());
     160
     161        assertEquals(6, route.getMembersCount());
     162        assertEquals(w1, route.getMemberPrimitivesList().get(0));
     163        assertEquals(w3, route.getMemberPrimitivesList().get(5));
     164        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(0)), n1);
     165        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(0)), n2);
     166        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(1)), n2);
     167        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(1)), n3);
     168        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(2)), n3);
     169        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(2)), n4);
     170        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(3)), n4);
     171        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(3)), n5);
     172        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(4)), n5);
     173        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(4)), n6);
     174        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(5)), n6);
     175        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(5)), n7);
     176
     177    }
     178
     179    static void assertFirstLastNodeIs(Way way, Node node) {
     180        assertTrue("First/last node of " + way + " should be " + node, node.equals(way.firstNode()) || node.equals(way.lastNode()));
     181    }
    96182}
Note: See TracChangeset for help on using the changeset viewer.