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.assertFalse;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Set;
|
---|
16 |
|
---|
17 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
18 | import org.junit.jupiter.api.Test;
|
---|
19 | import org.openstreetmap.josm.TestUtils;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.NodePair;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
25 | import org.openstreetmap.josm.io.OsmReader;
|
---|
26 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
27 |
|
---|
28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
29 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests for class {@link CombineWayAction}.
|
---|
33 | */
|
---|
34 | class CombineWayActionTest {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Setup test.
|
---|
38 | */
|
---|
39 | @RegisterExtension
|
---|
40 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
41 | public JOSMTestRules test = new JOSMTestRules();
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Non-regression test for bug #11957.
|
---|
45 | * @throws IOException if any I/O error occurs
|
---|
46 | * @throws IllegalDataException if OSM parsing fails
|
---|
47 | */
|
---|
48 | @Test
|
---|
49 | void testTicket11957() throws IOException, IllegalDataException {
|
---|
50 | try (InputStream is = TestUtils.getRegressionDataStream(11957, "data.osm")) {
|
---|
51 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
52 | List<Node> path = CombineWayAction.tryJoin(ds.getWays());
|
---|
53 | assertEquals(10, path.size());
|
---|
54 | Set<Long> firstAndLastObtained = new HashSet<>();
|
---|
55 | firstAndLastObtained.add(path.get(0).getId());
|
---|
56 | firstAndLastObtained.add(path.get(path.size()-1).getId());
|
---|
57 | Set<Long> firstAndLastExpected = new HashSet<>();
|
---|
58 | firstAndLastExpected.add(1618969016L);
|
---|
59 | firstAndLastExpected.add(35213705L);
|
---|
60 | assertEquals(firstAndLastExpected, firstAndLastObtained);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Non-regression test for bug #18385 (combine way with overlapping ways)
|
---|
66 | * @throws IOException if any I/O error occurs
|
---|
67 | * @throws IllegalDataException if OSM parsing fails
|
---|
68 | */
|
---|
69 | @Test
|
---|
70 | void testTicket18385() throws IOException, IllegalDataException {
|
---|
71 | try (InputStream is = TestUtils.getRegressionDataStream(18385, "data.osm")) {
|
---|
72 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
73 | List<Node> path = CombineWayAction.tryJoin(ds.getWays());
|
---|
74 | assertFalse(path.isEmpty());
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Non-regression test for bug #18387 (combine new way with oneway)
|
---|
80 | * @throws IOException if any I/O error occurs
|
---|
81 | * @throws IllegalDataException if OSM parsing fails
|
---|
82 | */
|
---|
83 | @Test
|
---|
84 | void testTicket18387() throws IOException, IllegalDataException {
|
---|
85 | try (InputStream is = TestUtils.getRegressionDataStream(18387, "data.osm")) {
|
---|
86 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
87 | ArrayList<Way> selection = new ArrayList<>(ds.getWays());
|
---|
88 | assertEquals(2, selection.size());
|
---|
89 | if (!selection.get(0).isNew())
|
---|
90 | Collections.reverse(selection);
|
---|
91 | double expectedLen = getOriginalLength(selection);
|
---|
92 | List<Node> path = CombineWayAction.tryJoin(selection);
|
---|
93 | assertFalse(path.isEmpty());
|
---|
94 | Way combined = new Way(0);
|
---|
95 | combined.setNodes(path);
|
---|
96 | assertEquals(expectedLen, combined.getLength(), 0.01);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Non-regression test for bug #18367 (Lines cannot be combined when they share an overlapping segment)
|
---|
102 | * @throws IOException if any I/O error occurs
|
---|
103 | * @throws IllegalDataException if OSM parsing fails
|
---|
104 | */
|
---|
105 | @Test
|
---|
106 | void testTicket18367() throws IOException, IllegalDataException {
|
---|
107 | try (InputStream is = TestUtils.getRegressionDataStream(18367, "nocombine.osm")) {
|
---|
108 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
109 | ArrayList<Way> selection = new ArrayList<>(ds.getWays());
|
---|
110 | assertEquals(2, selection.size());
|
---|
111 | double expectedLen = getOriginalLength(selection);
|
---|
112 | List<Node> path = CombineWayAction.tryJoin(selection);
|
---|
113 | assertFalse(path.isEmpty());
|
---|
114 | Way combined = new Way(0);
|
---|
115 | combined.setNodes(path);
|
---|
116 | assertEquals(expectedLen, combined.getLength(), 1e-7);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Non-regression test for bug #18367
|
---|
123 | * @throws IOException if any I/O error occurs
|
---|
124 | * @throws IllegalDataException if OSM parsing fails
|
---|
125 | */
|
---|
126 | @Test
|
---|
127 | void testTicket18367NeedsSplit() throws IOException, IllegalDataException {
|
---|
128 | try (InputStream is = TestUtils.getRegressionDataStream(18367, "split-and-reverse.osm")) {
|
---|
129 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
130 | ArrayList<Way> selection = new ArrayList<>(ds.getWays());
|
---|
131 | double expectedLen = getOriginalLength(selection);
|
---|
132 | List<Node> path = CombineWayAction.tryJoin(selection);
|
---|
133 | assertFalse(path.isEmpty());
|
---|
134 | Way combined = new Way(0);
|
---|
135 | combined.setNodes(path);
|
---|
136 | assertEquals(expectedLen, combined.getLength(), 1e-7);
|
---|
137 | List<Way> reversedWays = new LinkedList<>();
|
---|
138 | List<Way> unreversedWays = new LinkedList<>();
|
---|
139 | CombineWayAction.detectReversedWays(selection, path, reversedWays, unreversedWays);
|
---|
140 | assertFalse(reversedWays.isEmpty());
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Test for bad reverse way detection. See #18367
|
---|
147 | * @throws IOException if any I/O error occurs
|
---|
148 | * @throws IllegalDataException if OSM parsing fails
|
---|
149 | */
|
---|
150 | @Test
|
---|
151 | void testDetectReversedWays() throws IOException, IllegalDataException {
|
---|
152 | try (InputStream is = TestUtils.getRegressionDataStream(18367, "silent-revert.osm")) {
|
---|
153 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
154 | ArrayList<Way> selection = new ArrayList<>(ds.getWays());
|
---|
155 | assertEquals(2, selection.size());
|
---|
156 | // make sure that short way is first
|
---|
157 | if (selection.get(0).getNodesCount() != 2)
|
---|
158 | Collections.reverse(selection);
|
---|
159 | double expectedLen = getOriginalLength(selection);
|
---|
160 | List<Node> path = CombineWayAction.tryJoin(selection);
|
---|
161 | assertFalse(path.isEmpty());
|
---|
162 | Way combined = new Way(0);
|
---|
163 | combined.setNodes(path);
|
---|
164 | assertEquals(expectedLen, combined.getLength(), 1e-7);
|
---|
165 | List<Way> reversedWays = new LinkedList<>();
|
---|
166 | List<Way> unreversedWays = new LinkedList<>();
|
---|
167 | CombineWayAction.detectReversedWays(selection, path, reversedWays, unreversedWays);
|
---|
168 | assertFalse(reversedWays.contains(selection.get(0)));
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Unit test of methods {@link NodePair#equals} and {@link NodePair#hashCode}.
|
---|
175 | */
|
---|
176 | @Test
|
---|
177 | void testEqualsContract() {
|
---|
178 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
179 | EqualsVerifier.forClass(NodePair.class).usingGetClass()
|
---|
180 | .withPrefabValues(Node.class, new Node(1), new Node(2))
|
---|
181 | .verify();
|
---|
182 | }
|
---|
183 |
|
---|
184 | private static double getOriginalLength(Collection<Way> ways) {
|
---|
185 | return ways.stream().mapToDouble(Way::getLength).sum();
|
---|
186 | }
|
---|
187 |
|
---|
188 | }
|
---|