1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.Comparator;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.stream.Collectors;
|
---|
16 | import java.util.stream.Stream;
|
---|
17 |
|
---|
18 | import org.junit.jupiter.api.BeforeEach;
|
---|
19 | import org.junit.jupiter.api.Test;
|
---|
20 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
21 | import org.openstreetmap.josm.TestUtils;
|
---|
22 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
23 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.Way;
|
---|
28 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
29 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
30 | import org.openstreetmap.josm.io.OsmReader;
|
---|
31 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
33 |
|
---|
34 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Unit tests for class {@link SimplifyWayAction}.
|
---|
38 | */
|
---|
39 | final class SimplifyWayActionTest {
|
---|
40 |
|
---|
41 | /** Class under test. */
|
---|
42 | private static SimplifyWayAction action;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Setup test.
|
---|
46 | */
|
---|
47 | @RegisterExtension
|
---|
48 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
49 | public JOSMTestRules test = new JOSMTestRules().main();
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Setup test.
|
---|
53 | */
|
---|
54 | @BeforeEach
|
---|
55 | public void setUp() {
|
---|
56 | if (action == null) {
|
---|
57 | action = MainApplication.getMenu().simplifyWay;
|
---|
58 | action.setEnabled(true);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | private DataSet getDs(String file) throws IllegalDataException, IOException {
|
---|
63 | return OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "tracks/" + file + ".osm")), null);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Tests simplification
|
---|
68 | * @throws Exception in case of error
|
---|
69 | */
|
---|
70 | @Test
|
---|
71 | void testSimplify() throws Exception {
|
---|
72 | DataSet DsSimplify = getDs("tracks");
|
---|
73 | DataSet DsExpected = getDs("tracks-simplify15");
|
---|
74 | SimplifyWayAction.simplifyWays(new ArrayList<>(DsSimplify.getWays()), 15);
|
---|
75 | DsSimplify.cleanupDeletedPrimitives();
|
---|
76 | //compare sorted Coordinates and total amount of primitives, because IDs and order will vary after reload
|
---|
77 | List<LatLon> CoorSimplify = DsSimplify.getNodes().stream()
|
---|
78 | .map(Node::getCoor)
|
---|
79 | .sorted(Comparator.comparing(LatLon::hashCode))
|
---|
80 | .collect(Collectors.toList());
|
---|
81 | List<LatLon> CoorExpected = DsExpected.getNodes().stream()
|
---|
82 | .map(Node::getCoor)
|
---|
83 | .sorted(Comparator.comparing(LatLon::hashCode))
|
---|
84 | .collect(Collectors.toList());
|
---|
85 | assertEquals(CoorExpected, CoorSimplify);
|
---|
86 | assertEquals(DsExpected.allPrimitives().size(), DsSimplify.allPrimitives().size());
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Tests that also the first node may be simplified, see #13094.
|
---|
91 | */
|
---|
92 | @Test
|
---|
93 | void testSimplifyFirstNode() {
|
---|
94 | final DataSet ds = new DataSet();
|
---|
95 | final Node n1 = new Node(new LatLon(47.26269614984, 11.34044231149));
|
---|
96 | final Node n2 = new Node(new LatLon(47.26274590831, 11.34053120859));
|
---|
97 | final Node n3 = new Node(new LatLon(47.26276562382, 11.34034715039));
|
---|
98 | final Node n4 = new Node(new LatLon(47.26264639132, 11.34035341438));
|
---|
99 | final Way w = new Way();
|
---|
100 | Stream.of(n1, n2, n3, n4, w).forEach(ds::addPrimitive);
|
---|
101 | Stream.of(n1, n2, n3, n4, n1).forEach(w::addNode);
|
---|
102 | final SequenceCommand command = SimplifyWayAction.createSimplifyCommand(w, 3);
|
---|
103 | assertNotNull(command);
|
---|
104 | assertEquals(2, command.getChildren().size());
|
---|
105 | final Collection<DeleteCommand> deleteCommands = Utils.filteredCollection(command.getChildren(), DeleteCommand.class);
|
---|
106 | assertEquals(1, deleteCommands.size());
|
---|
107 | assertEquals(Collections.singleton(n1), deleteCommands.iterator().next().getParticipatingPrimitives());
|
---|
108 | }
|
---|
109 | }
|
---|