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

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

see #15182 - deprecate Main.main.menu. Replacement: gui.MainApplication.getMenu()

  • Property svn:eol-style set to native
File size: 8.2 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.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.MainApplication;
28import org.openstreetmap.josm.gui.layer.Layer;
29import org.openstreetmap.josm.gui.layer.OsmDataLayer;
30import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
31import org.openstreetmap.josm.io.IllegalDataException;
32import org.openstreetmap.josm.io.OsmReader;
33import org.openstreetmap.josm.testutils.JOSMTestRules;
34import org.openstreetmap.josm.tools.MultiMap;
35import org.openstreetmap.josm.tools.Utils;
36
37import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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().platform().main().projection();
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 MainApplication.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 MainApplication.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 MainApplication.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 MainApplication.getMenu().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
113 // set current edit layer
114 MainApplication.getLayerManager().addLayer(new OsmDataLayer(dsToJoin, "join", null));
115
116 Collection<OsmPrimitive> testPrims = dsToJoin.getPrimitives(osm -> osm.get("test") != null);
117 MultiMap<String, OsmPrimitive> tests = new MultiMap<>();
118 for (OsmPrimitive testPrim : testPrims) {
119 tests.put(testPrim.get("test"), testPrim);
120 }
121 for (String test : tests.keySet()) {
122 Collection<OsmPrimitive> primitives = tests.get(test);
123 for (OsmPrimitive osm : primitives) {
124 assertTrue(test + "; expected way, but got: " + osm, osm instanceof Way);
125 }
126 new JoinAreasAction().join((Collection) primitives);
127 Collection<OsmPrimitive> joinedCol = dsToJoin.getPrimitives(osm -> !osm.isDeleted() && Objects.equals(osm.get("test"), test));
128 assertEquals("in test " + test + ":", 1, joinedCol.size());
129 Collection<OsmPrimitive> expectedCol = dsExpected.getPrimitives(osm -> !osm.isDeleted() && Objects.equals(osm.get("test"), test));
130 assertEquals("in test " + test + ":", 1, expectedCol.size());
131 OsmPrimitive osmJoined = joinedCol.iterator().next();
132 OsmPrimitive osmExpected = expectedCol.iterator().next();
133 assertTrue("difference in test " + test, isSemanticallyEqual(osmExpected, osmJoined));
134 }
135 }
136
137 /**
138 * Check if 2 primitives are semantically equal as result of a join areas
139 * operation.
140 * @param osm1 first primitive
141 * @param osm2 second primitive
142 * @return true if both primitives are semantically equal
143 */
144 private boolean isSemanticallyEqual(OsmPrimitive osm1, OsmPrimitive osm2) {
145 if (osm1 instanceof Node && osm2 instanceof Node)
146 return isSemanticallyEqualNode((Node) osm1, (Node) osm2);
147 if (osm1 instanceof Way && osm2 instanceof Way)
148 return isSemanticallyEqualWay((Way) osm1, (Way) osm2);
149 if (osm1 instanceof Relation && osm2 instanceof Relation)
150 return isSemanticallyEqualRelation((Relation) osm1, (Relation) osm2);
151 return false;
152 }
153
154 private boolean isSemanticallyEqualRelation(Relation r1, Relation r2) {
155 if (!r1.getKeys().equals(r2.getKeys())) return false;
156 if (r1.getMembersCount() != r2.getMembersCount()) return false;
157 Set<RelationMember> matchCandidates = new HashSet<>(r2.getMembers());
158 for (RelationMember rm : r1.getMembers()) {
159 RelationMember matched = null;
160 for (RelationMember cand : matchCandidates) {
161 if (!rm.getRole().equals(cand.getRole())) continue;
162 if (!isSemanticallyEqual(rm.getMember(), cand.getMember())) continue;
163 matched = cand;
164 break;
165 }
166 if (matched == null) return false;
167 matchCandidates.remove(matched);
168 }
169 return true;
170 }
171
172 private boolean isSemanticallyEqualWay(Way w1, Way w2) {
173 if (!w1.isClosed() || !w2.isClosed()) throw new UnsupportedOperationException();
174 if (!w1.getKeys().equals(w2.getKeys())) return false;
175 if (w1.getNodesCount() != w2.getNodesCount()) return false;
176 int n = w1.getNodesCount() - 1;
177 for (int dir : Arrays.asList(1, -1)) {
178 for (int i = 0; i < n; i++) {
179 boolean different = false;
180 for (int j = 0; j < n; j++) {
181 Node n1 = w1.getNode(j);
182 Node n2 = w2.getNode(Utils.mod(i + dir*j, n));
183 if (!isSemanticallyEqualNode(n1, n2)) {
184 different = true;
185 break;
186 }
187 }
188 if (!different)
189 return true;
190 }
191 }
192 return false;
193 }
194
195 private boolean isSemanticallyEqualNode(Node n1, Node n2) {
196 return n1.hasEqualSemanticAttributes(n2);
197 }
198}
Note: See TracBrowser for help on using the repository browser.