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.assertInstanceOf;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.nio.file.Files;
|
---|
11 | import java.nio.file.Paths;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.Objects;
|
---|
16 | import java.util.Set;
|
---|
17 |
|
---|
18 | import org.junit.jupiter.api.Test;
|
---|
19 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
20 | import org.openstreetmap.josm.TestUtils;
|
---|
21 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
22 | import org.openstreetmap.josm.data.Bounds;
|
---|
23 | import org.openstreetmap.josm.data.DataSource;
|
---|
24 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
25 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
29 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
30 | import org.openstreetmap.josm.data.osm.Way;
|
---|
31 | import org.openstreetmap.josm.data.osm.search.SearchMode;
|
---|
32 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
33 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
34 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
35 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
36 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
37 | import org.openstreetmap.josm.io.OsmReader;
|
---|
38 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
39 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
40 | import org.openstreetmap.josm.tools.Utils;
|
---|
41 |
|
---|
42 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Unit tests of {@link JoinAreasAction} class.
|
---|
46 | */
|
---|
47 | class JoinAreasActionTest {
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Setup test.
|
---|
51 | */
|
---|
52 | @RegisterExtension
|
---|
53 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
54 | public JOSMTestRules test = new JOSMTestRules().main().projection().preferences();
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Non-regression test for bug #9599.
|
---|
58 | * @throws IOException if any I/O error occurs
|
---|
59 | * @throws IllegalDataException if OSM parsing fails
|
---|
60 | */
|
---|
61 | @Test
|
---|
62 | void testTicket9599() throws IOException, IllegalDataException {
|
---|
63 | try (InputStream is = TestUtils.getRegressionDataStream(9599, "ex5.osm")) {
|
---|
64 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
65 | Layer layer = new OsmDataLayer(ds, null, null);
|
---|
66 | MainApplication.getLayerManager().addLayer(layer);
|
---|
67 | try {
|
---|
68 | new JoinAreasAction(false).join(ds.getWays());
|
---|
69 | Collection<IPrimitive> found = SearchAction.searchAndReturn("type:way", SearchMode.replace);
|
---|
70 | assertEquals(1, found.size());
|
---|
71 | assertEquals(257786939, found.iterator().next().getUniqueId());
|
---|
72 | } finally {
|
---|
73 | // Ensure we clean the place before leaving, even if test fails.
|
---|
74 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Non-regression test for bug #9599.
|
---|
81 | * @throws IOException if any I/O error occurs
|
---|
82 | * @throws IllegalDataException if OSM parsing fails
|
---|
83 | */
|
---|
84 | @Test
|
---|
85 | void testTicket9599Simple() throws IOException, IllegalDataException {
|
---|
86 | try (InputStream is = TestUtils.getRegressionDataStream(9599, "three_old.osm")) {
|
---|
87 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
88 | ds.addDataSource(new DataSource(new Bounds(-90, -180, 90, 180), "Everywhere"));
|
---|
89 | Layer layer = new OsmDataLayer(ds, null, null);
|
---|
90 | MainApplication.getLayerManager().addLayer(layer);
|
---|
91 | try {
|
---|
92 | new JoinAreasAction(false).join(ds.getWays());
|
---|
93 | Collection<IPrimitive> found = SearchAction.searchAndReturn("type:way", SearchMode.replace);
|
---|
94 | assertEquals(1, found.size());
|
---|
95 | assertEquals(31567319, found.iterator().next().getUniqueId());
|
---|
96 | } finally {
|
---|
97 | // Ensure we clean the place before leaving, even if test fails.
|
---|
98 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Non-regression test for bug #10511.
|
---|
105 | * @throws IOException if any I/O error occurs
|
---|
106 | * @throws IllegalDataException if OSM parsing fails
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | void testTicket10511() throws IOException, IllegalDataException {
|
---|
110 | try (InputStream is = TestUtils.getRegressionDataStream(10511, "10511_mini.osm")) {
|
---|
111 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
112 | Layer layer = new OsmDataLayer(ds, null, null);
|
---|
113 | MainApplication.getLayerManager().addLayer(layer);
|
---|
114 | try {
|
---|
115 | new JoinAreasAction(false).join(ds.getWays());
|
---|
116 | Collection<IPrimitive> found = SearchAction.searchAndReturn("type:way", SearchMode.replace);
|
---|
117 | assertEquals(1, found.size());
|
---|
118 | } finally {
|
---|
119 | // Ensure we clean the place before leaving, even if test fails.
|
---|
120 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Non-regression test for bug #11992.
|
---|
127 | * @throws IOException if any I/O error occurs
|
---|
128 | * @throws IllegalDataException if OSM parsing fails
|
---|
129 | */
|
---|
130 | @Test
|
---|
131 | void testTicket11992() throws IOException, IllegalDataException {
|
---|
132 | try (InputStream is = TestUtils.getRegressionDataStream(11992, "shapes.osm")) {
|
---|
133 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
134 | assertEquals(10, ds.getWays().size());
|
---|
135 | Layer layer = new OsmDataLayer(ds, null, null);
|
---|
136 | MainApplication.getLayerManager().addLayer(layer);
|
---|
137 | for (String ref : new String[]{"A", "B", "C", "D", "E"}) {
|
---|
138 | System.out.print("Joining ways " + ref);
|
---|
139 | Collection<IPrimitive> found = SearchAction.searchAndReturn("type:way ref="+ref, SearchMode.replace);
|
---|
140 | assertEquals(2, found.size());
|
---|
141 |
|
---|
142 | MainApplication.getMenu().joinAreas.join(Utils.filteredCollection(found, Way.class));
|
---|
143 |
|
---|
144 | Collection<IPrimitive> found2 = SearchAction.searchAndReturn("type:way ref="+ref, SearchMode.replace);
|
---|
145 | assertEquals(1, found2.size());
|
---|
146 | System.out.println(" ==> OK");
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Non-regression test for bug #18744.
|
---|
153 | * @throws IOException if any I/O error occurs
|
---|
154 | * @throws IllegalDataException if OSM parsing fails
|
---|
155 | */
|
---|
156 | @Test
|
---|
157 | void testTicket18744() throws IOException, IllegalDataException {
|
---|
158 | try (InputStream is = TestUtils.getRegressionDataStream(18744, "18744-sample.osm")) {
|
---|
159 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
160 | ds.addDataSource(new DataSource(new Bounds(-90, -180, 90, 180), "Everywhere"));
|
---|
161 | Layer layer = new OsmDataLayer(ds, null, null);
|
---|
162 | MainApplication.getLayerManager().addLayer(layer);
|
---|
163 | try {
|
---|
164 | assertEquals(3, ds.getWays().size());
|
---|
165 | new JoinAreasAction(false).join(ds.getWays());
|
---|
166 | // join should not have changed anything
|
---|
167 | assertEquals(3, ds.getWays().size());
|
---|
168 | } finally {
|
---|
169 | // Ensure we clean the place before leaving, even if test fails.
|
---|
170 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Non-regression test which checks example files in nodist/data
|
---|
178 | * @throws Exception if an error occurs
|
---|
179 | */
|
---|
180 | @Test
|
---|
181 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
---|
182 | void testExamples() throws Exception {
|
---|
183 | DataSet dsToJoin, dsExpected;
|
---|
184 | try (InputStream is = Files.newInputStream(Paths.get("nodist/data/Join_Areas_Tests.osm"))) {
|
---|
185 | dsToJoin = OsmReader.parseDataSet(is, NullProgressMonitor.INSTANCE);
|
---|
186 | }
|
---|
187 | try (InputStream is = Files.newInputStream(Paths.get("nodist/data/Join_Areas_Tests_joined.osm"))) {
|
---|
188 | dsExpected = OsmReader.parseDataSet(is, NullProgressMonitor.INSTANCE);
|
---|
189 | }
|
---|
190 |
|
---|
191 | // set current edit layer
|
---|
192 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(dsToJoin, "join", null));
|
---|
193 |
|
---|
194 | Collection<OsmPrimitive> testPrims = dsToJoin.getPrimitives(osm -> osm.get("test") != null);
|
---|
195 | MultiMap<String, OsmPrimitive> tests = new MultiMap<>();
|
---|
196 | for (OsmPrimitive testPrim : testPrims) {
|
---|
197 | tests.put(testPrim.get("test"), testPrim);
|
---|
198 | }
|
---|
199 | for (String test : tests.keySet()) {
|
---|
200 | Collection<OsmPrimitive> primitives = tests.get(test);
|
---|
201 | for (OsmPrimitive osm : primitives) {
|
---|
202 | assertInstanceOf(Way.class, osm, test + "; expected way, but got: " + osm);
|
---|
203 | }
|
---|
204 | new JoinAreasAction(false).join((Collection) primitives);
|
---|
205 | Collection<OsmPrimitive> joinedCol = dsToJoin.getPrimitives(osm -> !osm.isDeleted() && Objects.equals(osm.get("test"), test));
|
---|
206 | assertEquals(1, joinedCol.size(), "in test " + test + ":");
|
---|
207 | Collection<OsmPrimitive> expectedCol = dsExpected.getPrimitives(osm -> !osm.isDeleted() && Objects.equals(osm.get("test"), test));
|
---|
208 | assertEquals(1, expectedCol.size(), "in test " + test + ":");
|
---|
209 | OsmPrimitive osmJoined = joinedCol.iterator().next();
|
---|
210 | OsmPrimitive osmExpected = expectedCol.iterator().next();
|
---|
211 | assertTrue(isSemanticallyEqual(osmExpected, osmJoined), "difference in test " + test);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Check if 2 primitives are semantically equal as result of a join areas
|
---|
217 | * operation.
|
---|
218 | * @param osm1 first primitive
|
---|
219 | * @param osm2 second primitive
|
---|
220 | * @return true if both primitives are semantically equal
|
---|
221 | */
|
---|
222 | private boolean isSemanticallyEqual(OsmPrimitive osm1, OsmPrimitive osm2) {
|
---|
223 | if (osm1 instanceof Node && osm2 instanceof Node)
|
---|
224 | return isSemanticallyEqualNode((Node) osm1, (Node) osm2);
|
---|
225 | if (osm1 instanceof Way && osm2 instanceof Way)
|
---|
226 | return isSemanticallyEqualWay((Way) osm1, (Way) osm2);
|
---|
227 | if (osm1 instanceof Relation && osm2 instanceof Relation)
|
---|
228 | return isSemanticallyEqualRelation((Relation) osm1, (Relation) osm2);
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 |
|
---|
232 | private boolean isSemanticallyEqualRelation(Relation r1, Relation r2) {
|
---|
233 | if (!r1.getKeys().equals(r2.getKeys())) return false;
|
---|
234 | if (r1.getMembersCount() != r2.getMembersCount()) return false;
|
---|
235 | Set<RelationMember> matchCandidates = new HashSet<>(r2.getMembers());
|
---|
236 | for (RelationMember rm : r1.getMembers()) {
|
---|
237 | RelationMember matched = matchCandidates.stream()
|
---|
238 | .filter(m -> rm.getRole().equals(m.getRole()))
|
---|
239 | .filter(m -> isSemanticallyEqual(rm.getMember(), m.getMember()))
|
---|
240 | .findFirst().orElse(null);
|
---|
241 | if (matched == null) return false;
|
---|
242 | matchCandidates.remove(matched);
|
---|
243 | }
|
---|
244 | return true;
|
---|
245 | }
|
---|
246 |
|
---|
247 | private boolean isSemanticallyEqualWay(Way w1, Way w2) {
|
---|
248 | if (!w1.isClosed() || !w2.isClosed()) throw new UnsupportedOperationException();
|
---|
249 | if (!w1.getKeys().equals(w2.getKeys())) return false;
|
---|
250 | if (w1.getNodesCount() != w2.getNodesCount()) return false;
|
---|
251 | int n = w1.getNodesCount() - 1;
|
---|
252 | for (int dir : Arrays.asList(1, -1)) {
|
---|
253 | for (int i = 0; i < n; i++) {
|
---|
254 | boolean different = false;
|
---|
255 | for (int j = 0; j < n; j++) {
|
---|
256 | Node n1 = w1.getNode(j);
|
---|
257 | Node n2 = w2.getNode(Utils.mod(i + dir*j, n));
|
---|
258 | if (!isSemanticallyEqualNode(n1, n2)) {
|
---|
259 | different = true;
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | if (!different)
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | return false;
|
---|
268 | }
|
---|
269 |
|
---|
270 | private boolean isSemanticallyEqualNode(Node n1, Node n2) {
|
---|
271 | return n1.hasEqualSemanticAttributes(n2);
|
---|
272 | }
|
---|
273 | }
|
---|