source: josm/trunk/test/unit/org/openstreetmap/josm/command/SplitWayCommandTest.java @ 14134

Last change on this file since 14134 was 14134, checked in by Don-vip, 2 months ago

see #15229 - deprecate Main*.undoRedo - make UndoRedoHandler a singleton

File size: 6.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNull;
6import static org.junit.Assert.assertTrue;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Iterator;
11
12import org.junit.Rule;
13import org.junit.Test;
14import org.openstreetmap.josm.command.SplitWayCommand.Strategy;
15import org.openstreetmap.josm.data.UndoRedoHandler;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Relation;
21import org.openstreetmap.josm.data.osm.RelationMember;
22import org.openstreetmap.josm.data.osm.Way;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24
25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26
27/**
28 * Unit tests for class {@link SplitWayCommand}.
29 */
30public final class SplitWayCommandTest {
31
32    /**
33     * Setup test.
34     */
35    @Rule
36    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37    public JOSMTestRules test = new JOSMTestRules().main().projection();
38
39    /**
40     * Unit test of {@link SplitWayCommand#findVia}.
41     */
42    @Test
43    public void testFindVia() {
44        // empty relation
45        assertNull(SplitWayCommand.findVia(new Relation(), null));
46        // restriction relation without via member
47        Relation r = new Relation();
48        r.addMember(new RelationMember("", new Node()));
49        assertNull(SplitWayCommand.findVia(r, "restriction"));
50        // restriction relation with via member
51        r = new Relation();
52        OsmPrimitive via = new Node();
53        r.addMember(new RelationMember("via", via));
54        assertEquals(via, SplitWayCommand.findVia(r, "restriction"));
55        // destination_sign relation without sign nor intersection
56        r = new Relation();
57        r.addMember(new RelationMember("", new Node()));
58        assertNull(SplitWayCommand.findVia(r, "destination_sign"));
59        // destination_sign with sign
60        r = new Relation();
61        via = new Node();
62        r.addMember(new RelationMember("sign", via));
63        assertEquals(via, SplitWayCommand.findVia(r, "destination_sign"));
64        // destination_sign with intersection
65        r = new Relation();
66        via = new Node();
67        r.addMember(new RelationMember("intersection", via));
68        assertEquals(via, SplitWayCommand.findVia(r, "destination_sign"));
69    }
70
71    /**
72     * Unit tests of route relations.
73     */
74    @Test
75    public void testRouteRelation() {
76        doTestRouteRelation(false, 0);
77        doTestRouteRelation(false, 1);
78        doTestRouteRelation(false, 2);
79        doTestRouteRelation(false, 3);
80        doTestRouteRelation(true, 0);
81        doTestRouteRelation(true, 1);
82        doTestRouteRelation(true, 2);
83        doTestRouteRelation(true, 3);
84    }
85
86    void doTestRouteRelation(final boolean wayIsReversed, final int indexOfWayToKeep) {
87        final DataSet dataSet = new DataSet();
88        final Node n1 = new Node(new LatLon(1, 0));
89        final Node n2 = new Node(new LatLon(2, 0));
90        final Node n3 = new Node(new LatLon(3, 0));
91        final Node n4 = new Node(new LatLon(4, 0));
92        final Node n5 = new Node(new LatLon(5, 0));
93        final Node n6 = new Node(new LatLon(6, 0));
94        final Node n7 = new Node(new LatLon(7, 0));
95        final Way w1 = new Way();
96        final Way w2 = new Way();
97        final Way w3 = new Way();
98        final Relation route = new Relation();
99        for (OsmPrimitive p : Arrays.asList(n1, n2, n3, n4, n5, n6, n7, w1, w2, w3, route)) {
100            dataSet.addPrimitive(p);
101        }
102        w1.setNodes(Arrays.asList(n1, n2));
103        w2.setNodes(wayIsReversed
104                ? Arrays.asList(n6, n5, n4, n3, n2)
105                : Arrays.asList(n2, n3, n4, n5, n6)
106        );
107        w3.setNodes(Arrays.asList(n6, n7));
108        route.put("type", "route");
109        route.addMember(new RelationMember("", w1));
110        route.addMember(new RelationMember("", w2));
111        route.addMember(new RelationMember("", w3));
112        dataSet.setSelected(Arrays.asList(w2, n3, n4, n5));
113
114        final Strategy strategy = wayChunks -> {
115                final Iterator<Way> it = wayChunks.iterator();
116                for (int i = 0; i < indexOfWayToKeep; i++) {
117                    it.next();
118                }
119                return it.next();
120            };
121        final SplitWayCommand result = SplitWayCommand.splitWay(
122                w2, SplitWayCommand.buildSplitChunks(w2, Arrays.asList(n3, n4, n5)), new ArrayList<OsmPrimitive>(), strategy);
123        UndoRedoHandler.getInstance().add(result);
124
125        assertEquals(6, route.getMembersCount());
126        assertEquals(w1, route.getMemberPrimitivesList().get(0));
127        assertEquals(w3, route.getMemberPrimitivesList().get(5));
128        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(0)), n1);
129        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(0)), n2);
130        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(1)), n2);
131        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(1)), n3);
132        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(2)), n3);
133        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(2)), n4);
134        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(3)), n4);
135        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(3)), n5);
136        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(4)), n5);
137        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(4)), n6);
138        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(5)), n6);
139        assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(5)), n7);
140    }
141
142    static void assertFirstLastNodeIs(Way way, Node node) {
143        assertTrue("First/last node of " + way + " should be " + node, node.equals(way.firstNode()) || node.equals(way.lastNode()));
144    }
145}
Note: See TracBrowser for help on using the repository browser.