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

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

update unit test

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