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

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

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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