source: josm/trunk/test/unit/org/openstreetmap/josm/actions/SimplifyWayActionTest.java@ 17275

Last change on this file since 17275 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertNotNull;
6
7import java.io.IOException;
8import java.nio.file.Files;
9import java.nio.file.Paths;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.Comparator;
14import java.util.List;
15import java.util.stream.Collectors;
16import java.util.stream.Stream;
17
18import org.junit.jupiter.api.BeforeEach;
19import org.junit.jupiter.api.Test;
20import org.junit.jupiter.api.extension.RegisterExtension;
21import org.openstreetmap.josm.TestUtils;
22import org.openstreetmap.josm.command.DeleteCommand;
23import org.openstreetmap.josm.command.SequenceCommand;
24import org.openstreetmap.josm.data.coor.LatLon;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.Node;
27import org.openstreetmap.josm.data.osm.Way;
28import org.openstreetmap.josm.gui.MainApplication;
29import org.openstreetmap.josm.io.IllegalDataException;
30import org.openstreetmap.josm.io.OsmReader;
31import org.openstreetmap.josm.testutils.JOSMTestRules;
32import org.openstreetmap.josm.tools.Utils;
33
34import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35
36/**
37 * Unit tests for class {@link SimplifyWayAction}.
38 */
39final 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}
Note: See TracBrowser for help on using the repository browser.