Changeset 11738 in josm for trunk/test
- Timestamp:
- 2017-03-17T19:00:17+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java
r11726 r11738 3 3 4 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertTrue; 5 6 7 import java.io.FileInputStream; 6 8 import java.io.IOException; 7 9 import java.io.InputStream; 10 import java.util.Arrays; 8 11 import java.util.Collection; 12 import java.util.HashSet; 13 import java.util.Objects; 14 import java.util.Set; 9 15 10 16 import org.junit.Rule; … … 14 20 import org.openstreetmap.josm.actions.search.SearchAction; 15 21 import org.openstreetmap.josm.data.osm.DataSet; 22 import org.openstreetmap.josm.data.osm.Node; 16 23 import org.openstreetmap.josm.data.osm.OsmPrimitive; 24 import org.openstreetmap.josm.data.osm.Relation; 25 import org.openstreetmap.josm.data.osm.RelationMember; 17 26 import org.openstreetmap.josm.data.osm.Way; 18 27 import org.openstreetmap.josm.gui.layer.Layer; 19 28 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 29 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 20 30 import org.openstreetmap.josm.io.IllegalDataException; 21 31 import org.openstreetmap.josm.io.OsmReader; 22 32 import org.openstreetmap.josm.testutils.JOSMTestRules; 33 import org.openstreetmap.josm.tools.MultiMap; 23 34 import org.openstreetmap.josm.tools.Utils; 24 35 … … 82 93 } 83 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 } 84 187 }
Note:
See TracChangeset
for help on using the changeset viewer.