source: josm/trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java@ 11738

Last change on this file since 11738 was 11738, checked in by bastiK, 7 years ago

add unit test for join areas action

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertTrue;
6
7import java.io.FileInputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.HashSet;
13import java.util.Objects;
14import java.util.Set;
15
16import org.junit.Rule;
17import org.junit.Test;
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.TestUtils;
20import org.openstreetmap.josm.actions.search.SearchAction;
21import org.openstreetmap.josm.data.osm.DataSet;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.Relation;
25import org.openstreetmap.josm.data.osm.RelationMember;
26import org.openstreetmap.josm.data.osm.Way;
27import org.openstreetmap.josm.gui.layer.Layer;
28import org.openstreetmap.josm.gui.layer.OsmDataLayer;
29import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
30import org.openstreetmap.josm.io.IllegalDataException;
31import org.openstreetmap.josm.io.OsmReader;
32import org.openstreetmap.josm.testutils.JOSMTestRules;
33import org.openstreetmap.josm.tools.MultiMap;
34import org.openstreetmap.josm.tools.Utils;
35
36import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37
38/**
39 * Unit tests of {@link JoinAreasAction} class.
40 */
41public class JoinAreasActionTest {
42
43 /**
44 * Setup test.
45 */
46 @Rule
47 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
48 public JOSMTestRules test = new JOSMTestRules().commands();
49
50 /**
51 * Non-regression test for bug #10511.
52 * @throws IOException if any I/O error occurs
53 * @throws IllegalDataException if OSM parsing fails
54 */
55 @Test
56 public void testTicket10511() throws IOException, IllegalDataException {
57 try (InputStream is = TestUtils.getRegressionDataStream(10511, "10511_mini.osm")) {
58 DataSet ds = OsmReader.parseDataSet(is, null);
59 Layer layer = new OsmDataLayer(ds, null, null);
60 Main.getLayerManager().addLayer(layer);
61 try {
62 new JoinAreasAction().join(ds.getWays());
63 } finally {
64 // Ensure we clean the place before leaving, even if test fails.
65 Main.getLayerManager().removeLayer(layer);
66 }
67 }
68 }
69
70 /**
71 * Non-regression test for bug #11992.
72 * @throws IOException if any I/O error occurs
73 * @throws IllegalDataException if OSM parsing fails
74 */
75 @Test
76 public void testTicket11992() throws IOException, IllegalDataException {
77 try (InputStream is = TestUtils.getRegressionDataStream(11992, "shapes.osm")) {
78 DataSet ds = OsmReader.parseDataSet(is, null);
79 assertEquals(10, ds.getWays().size());
80 Layer layer = new OsmDataLayer(ds, null, null);
81 Main.getLayerManager().addLayer(layer);
82 for (String ref : new String[]{"A", "B", "C", "D", "E"}) {
83 System.out.print("Joining ways " + ref);
84 Collection<OsmPrimitive> found = SearchAction.searchAndReturn("type:way ref="+ref, SearchAction.SearchMode.replace);
85 assertEquals(2, found.size());
86
87 Main.main.menu.joinAreas.join(Utils.filteredCollection(found, Way.class));
88
89 Collection<OsmPrimitive> found2 = SearchAction.searchAndReturn("type:relation ref="+ref, SearchAction.SearchMode.replace);
90 assertEquals(1, found2.size());
91 System.out.println(" ==> OK");
92 }
93 }
94 }
95
96 @Test
97 public void testExamples() throws Exception {
98 DataSet dsToJoin, dsExpected;
99 try (InputStream is = new FileInputStream("data_nodist/Join_Areas_Tests.osm")) {
100 dsToJoin = OsmReader.parseDataSet(is, NullProgressMonitor.INSTANCE);
101 }
102 try (InputStream is = new FileInputStream("data_nodist/Join_Areas_Tests_joined.osm")) {
103 dsExpected = OsmReader.parseDataSet(is, NullProgressMonitor.INSTANCE);
104 }
105 Collection<OsmPrimitive> testPrims = dsToJoin.getPrimitives(osm -> osm.get("test") != null);
106 MultiMap<String, OsmPrimitive> tests = new MultiMap<>();
107 for (OsmPrimitive testPrim : testPrims) {
108 tests.put(testPrim.get("test"), testPrim);
109 }
110 for (String test : tests.keySet()) {
111 Collection<OsmPrimitive> primitives = tests.get(test);
112 for (OsmPrimitive osm : primitives) {
113 assertTrue(test + "; expected way, but got: " + osm, osm instanceof Way);
114 }
115 new JoinAreasAction().join((Collection) primitives);
116 Collection<OsmPrimitive> joinedCol = dsToJoin.getPrimitives(osm -> !osm.isDeleted() && Objects.equals(osm.get("test"), test));
117 assertEquals("in test " + test + ":", 1, joinedCol.size());
118 Collection<OsmPrimitive> expectedCol = dsExpected.getPrimitives(osm -> !osm.isDeleted() && Objects.equals(osm.get("test"), test));
119 assertEquals("in test " + test + ":", 1, expectedCol.size());
120 OsmPrimitive osmJoined = joinedCol.iterator().next();
121 OsmPrimitive osmExpected = expectedCol.iterator().next();
122 assertTrue("difference in test " + test, isSemanticallyEqual(osmExpected, osmJoined));
123 }
124 }
125
126 /**
127 * Check if 2 primitives are semantically equal as result of a join areas
128 * operation.
129 * @param osm1 first primitive
130 * @param osm2 second primitive
131 * @return true if both primitives are semantically equal
132 */
133 private boolean isSemanticallyEqual(OsmPrimitive osm1, OsmPrimitive osm2) {
134 if (osm1 instanceof Node && osm2 instanceof Node)
135 return isSemanticallyEqualNode((Node) osm1, (Node) osm2);
136 if (osm1 instanceof Way && osm2 instanceof Way)
137 return isSemanticallyEqualWay((Way) osm1, (Way) osm2);
138 if (osm1 instanceof Relation && osm2 instanceof Relation)
139 return isSemanticallyEqualRelation((Relation) osm1, (Relation) osm2);
140 return false;
141 }
142
143 private boolean isSemanticallyEqualRelation(Relation r1, Relation r2) {
144 if (!r1.getKeys().equals(r2.getKeys())) return false;
145 if (r1.getMembersCount() != r2.getMembersCount()) return false;
146 Set<RelationMember> matchCandidates = new HashSet<>(r2.getMembers());
147 for (RelationMember rm : r1.getMembers()) {
148 RelationMember matched = null;
149 for (RelationMember cand : matchCandidates) {
150 if (!rm.getRole().equals(cand.getRole())) continue;
151 if (!isSemanticallyEqual(rm.getMember(), cand.getMember())) continue;
152 matched = cand;
153 break;
154 }
155 if (matched == null) return false;
156 matchCandidates.remove(matched);
157 }
158 return true;
159 }
160
161 private boolean isSemanticallyEqualWay(Way w1, Way w2) {
162 if (!w1.isClosed() || !w2.isClosed()) throw new UnsupportedOperationException();
163 if (!w1.getKeys().equals(w2.getKeys())) return false;
164 if (w1.getNodesCount() != w2.getNodesCount()) return false;
165 int n = w1.getNodesCount() - 1;
166 for (int dir : Arrays.asList(1, -1)) {
167 for (int i = 0; i < n; i++) {
168 boolean different = false;
169 for (int j = 0; j < n; j++) {
170 Node n1 = w1.getNode(j);
171 Node n2 = w2.getNode(Utils.mod(i + dir*j, n));
172 if (!isSemanticallyEqualNode(n1, n2)) {
173 different = true;
174 break;
175 }
176 }
177 if (!different)
178 return true;
179 }
180 }
181 return false;
182 }
183
184 private boolean isSemanticallyEqualNode(Node n1, Node n2) {
185 return n1.hasEqualSemanticAttributes(n2);
186 }
187}
Note: See TracBrowser for help on using the repository browser.