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

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

see #15229 - deprecate Main.platform and related methods - new class PlatformManager

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