Changeset 2443 in josm
- Timestamp:
- 2009-11-13T10:24:58+01:00 (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/DataSetMerger.java
r2433 r2443 26 26 27 27 /** the target dataset for merging */ 28 private final DataSet myDataSet;28 private final DataSet targetDataSet; 29 29 /** the source dataset where primitives are merged from */ 30 private final DataSet theirDataSet;30 private final DataSet sourceDataSet; 31 31 32 32 /** … … 38 38 * to relation members) after the first phase of merging 39 39 */ 40 private Set<Long> fixReferences; 40 private Set<Long> childrenToMerge; 41 42 private Set<OsmPrimitive> deletedObjectsToUnlink; 41 43 42 44 /** … … 45 47 * The visitor will merge <code>theirDataSet</code> onto <code>myDataSet</code> 46 48 * 47 * @param myDataSet dataset with my primitives. Must not be null.48 * @param theirDataSet dataset with their primitives. Ignored, if null.49 * @param targetDataSet dataset with my primitives. Must not be null. 50 * @param sourceDataSet dataset with their primitives. Ignored, if null. 49 51 * @throws IllegalArgumentException thrown if myDataSet is null 50 52 */ 51 public DataSetMerger(DataSet myDataSet, DataSet theirDataSet) throws IllegalArgumentException {52 if ( myDataSet == null)53 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null" ));54 this. myDataSet = myDataSet;55 this. theirDataSet = theirDataSet;53 public DataSetMerger(DataSet targetDataSet, DataSet sourceDataSet) throws IllegalArgumentException { 54 if (targetDataSet == null) 55 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "targetDataSet")); 56 this.targetDataSet = targetDataSet; 57 this.sourceDataSet = sourceDataSet; 56 58 conflicts = new ConflictCollection(); 57 59 mergedMap = new HashMap<Long, Long>(); 58 fixReferences = new HashSet<Long>(); 60 childrenToMerge = new HashSet<Long>(); 61 deletedObjectsToUnlink = new HashSet<OsmPrimitive>(); 59 62 } 60 63 … … 71 74 * 72 75 * @param <P> the type of the other primitive 73 * @param otherthe other primitive74 */ 75 protected <P extends OsmPrimitive> void mergePrimitive(P other) {76 if (! other.isNew() ) {76 * @param source the other primitive 77 */ 78 protected <P extends OsmPrimitive> void mergePrimitive(P source) { 79 if (!source.isNew() ) { 77 80 // try to merge onto a matching primitive with the same 78 81 // defined id 79 82 // 80 if (mergeById( other))83 if (mergeById(source)) 81 84 return; 82 if (! other.isVisible())85 if (!source.isVisible()) 83 86 // ignore it 84 87 return; … … 88 91 // 89 92 Collection<? extends OsmPrimitive> candidates = null; 90 switch( other.getType()) {91 case NODE: candidates = myDataSet.getNodes(); break;92 case WAY: candidates = myDataSet.getWays(); break;93 case RELATION: candidates = myDataSet.getRelations(); break;94 } 95 for (OsmPrimitive my: candidates) {96 if (! my.isNew()) {93 switch(source.getType()) { 94 case NODE: candidates = targetDataSet.getNodes(); break; 95 case WAY: candidates =targetDataSet.getWays(); break; 96 case RELATION: candidates = targetDataSet.getRelations(); break; 97 } 98 for (OsmPrimitive target : candidates) { 99 if (!target.isNew()) { 97 100 continue; 98 101 } 99 if ( my.hasEqualSemanticAttributes(other)) {100 mergedMap.put( other.getUniqueId(), my.getUniqueId());101 if ( my.isDeleted() != other.isDeleted()) {102 if (target.hasEqualSemanticAttributes(source)) { 103 mergedMap.put(source.getUniqueId(), target.getUniqueId()); 104 if (target.isDeleted() != source.isDeleted()) { 102 105 // differences in deleted state have to be merged manually 103 106 // 104 conflicts.add( my, other);107 conflicts.add(target, source); 105 108 } else { 106 109 // copy the technical attributes from other 107 110 // version 108 my.setVisible(other.isVisible());109 my.setUser(other.getUser());110 my.setTimestamp(other.getTimestamp());111 my.setModified(other.isModified());112 fixReferences.add(other.getUniqueId());111 target.setVisible(source.isVisible()); 112 target.setUser(source.getUser()); 113 target.setTimestamp(source.getTimestamp()); 114 target.setModified(source.isModified()); 115 childrenToMerge.add(source.getUniqueId()); 113 116 } 114 117 return; … … 118 121 119 122 // If we get here we didn't find a suitable primitive in 120 // my dataset. Create a clone and add it to mydataset.123 // the target dataset. Create a clone and add it to the target dataset. 121 124 // 122 OsmPrimitive my= null;123 switch( other.getType()) {124 case NODE: my = other.isNew() ? new Node() : new Node(other.getId()); break;125 case WAY: my = other.isNew() ? new Way() : new Way(other.getId()); break;126 case RELATION: my = other.isNew() ? new Relation() : new Relation(other.getId()); break;127 } 128 my.mergeFrom(other);129 myDataSet.addPrimitive(my);130 mergedMap.put( other.getUniqueId(), my.getUniqueId());131 fixReferences.add(other.getUniqueId());125 OsmPrimitive target = null; 126 switch(source.getType()) { 127 case NODE: target = source.isNew() ? new Node() : new Node(source.getId()); break; 128 case WAY: target = source.isNew() ? new Way() : new Way(source.getId()); break; 129 case RELATION: target = source.isNew() ? new Relation() : new Relation(source.getId()); break; 130 } 131 target.mergeFrom(source); 132 targetDataSet.addPrimitive(target); 133 mergedMap.put(source.getUniqueId(), target.getUniqueId()); 134 childrenToMerge.add(source.getUniqueId()); 132 135 } 133 136 … … 136 139 if (targetId == null) 137 140 throw new RuntimeException(tr("Missing merge target for way with id {0}", mergeSource.getUniqueId())); 138 return myDataSet.getPrimitiveById(targetId, mergeSource.getType());141 return targetDataSet.getPrimitiveById(targetId, mergeSource.getType()); 139 142 } 140 143 … … 156 159 */ 157 160 public void fixReferences() { 158 for (Way w : theirDataSet.getWays()) {159 if (!conflicts.hasConflictForTheir(w) && fixReferences.contains(w.getUniqueId())) {161 for (Way w : sourceDataSet.getWays()) { 162 if (!conflicts.hasConflictForTheir(w) && childrenToMerge.contains(w.getUniqueId())) { 160 163 mergeNodeList(w); 161 164 fixIncomplete(w); 162 165 } 163 166 } 164 for (Relation r : theirDataSet.getRelations()) {165 if (!conflicts.hasConflictForTheir(r) && fixReferences.contains(r.getUniqueId())) {167 for (Relation r : sourceDataSet.getRelations()) { 168 if (!conflicts.hasConflictForTheir(r) && childrenToMerge.contains(r.getUniqueId())) { 166 169 mergeRelationMembers(r); 167 170 } 168 171 } 169 } 170 171 private void mergeNodeList(Way other) { 172 Way myWay = (Way)getMergeTarget(other); 173 if (myWay == null) 174 throw new RuntimeException(tr("Missing merge target for way with id {0}", other.getUniqueId())); 175 176 List<Node> myNodes = new LinkedList<Node>(); 177 for (Node otherNode : other.getNodes()) { 178 Node myNode = (Node)getMergeTarget(otherNode); 179 if (myNode != null) { 180 if (!myNode.isDeleted()) { 181 myNodes.add(myNode); 172 for (OsmPrimitive source: deletedObjectsToUnlink) { 173 OsmPrimitive target = getMergeTarget(source); 174 if (target == null) 175 throw new RuntimeException(tr("Missing merge target for object with id {0}", source.getUniqueId())); 176 targetDataSet.unlinkReferencesToPrimitive(target); 177 } 178 } 179 180 /** 181 * Merges the node list of a source way onto its target way. 182 * 183 * @param source the source way 184 * @throws IllegalStateException thrown if no target way can be found for the source way 185 * @throws IllegalStateException thrown if there isn't a target node for one of the nodes in the source way 186 * 187 */ 188 private void mergeNodeList(Way source) throws IllegalStateException { 189 Way target = (Way)getMergeTarget(source); 190 if (target == null) 191 throw new IllegalStateException(tr("Missing merge target for way with id {0}", source.getUniqueId())); 192 193 List<Node> newNodes = new LinkedList<Node>(); 194 for (Node sourceNode : source.getNodes()) { 195 Node targetNode = (Node)getMergeTarget(sourceNode); 196 if (targetNode != null) { 197 if (!targetNode.isDeleted() && targetNode.isVisible()) { 198 newNodes.add(targetNode); 199 } else { 200 target.setModified(true); 182 201 } 183 202 } else 184 throw new RuntimeException(tr("Missing merge target for node with id {0}", otherNode.getUniqueId())); 185 } 186 187 // check whether the node list has changed. If so, set the modified flag on the way 188 // 189 if (myWay.getNodes().size() != myNodes.size()) { 190 myWay.setModified(true); 191 } else { 192 for (int i=0; i< myWay.getNodesCount();i++) { 193 Node n1 = myWay.getNode(i); 194 Node n2 = myNodes.get(i); 195 if (n1.isNew() ^ n2.isNew()) { 196 myWay.setModified(true); 197 break; 198 } else if (n1.isNew() && n1 != n2) { 199 myWay.setModified(true); 200 break; 201 } else if (! n1.isNew() && n1.getId() != n2.getId()) { 202 myWay.setModified(true); 203 break; 204 } 205 } 206 } 207 myWay.setNodes(myNodes); 208 } 209 210 private void mergeRelationMembers(Relation other) { 211 Relation myRelation = (Relation) getMergeTarget(other); 212 if (myRelation == null) 213 throw new RuntimeException(tr("Missing merge target for relation with id {0}", other.getUniqueId())); 203 throw new IllegalStateException(tr("Missing merge target for node with id {0}", sourceNode.getUniqueId())); 204 } 205 target.setNodes(newNodes); 206 } 207 208 209 /** 210 * Merges the relation members of a source relation onto the corresponding target relation. 211 * @param source the source relation 212 * @throws IllegalStateException thrown if there is no corresponding target relation 213 * @throws IllegalStateException thrown if there isn't a corresponding target object for one of the relation 214 * members in source 215 */ 216 private void mergeRelationMembers(Relation source) throws IllegalStateException { 217 Relation target = (Relation) getMergeTarget(source); 218 if (target == null) 219 throw new IllegalStateException(tr("Missing merge target for relation with id {0}", source.getUniqueId())); 214 220 LinkedList<RelationMember> newMembers = new LinkedList<RelationMember>(); 215 for (RelationMember otherMember : other.getMembers()) {216 OsmPrimitive mergedMember = getMergeTarget(otherMember.getMember());217 if ( mergedMember == null)218 throw new RuntimeException(tr("Missing merge target of type {0} with id {1}", mergedMember.getType(), mergedMember.getUniqueId()));219 if (! mergedMember.isDeleted()) {220 RelationMember newMember = new RelationMember( otherMember.getRole(), mergedMember);221 for (RelationMember sourceMember : source.getMembers()) { 222 OsmPrimitive targetMember = getMergeTarget(sourceMember.getMember()); 223 if (targetMember == null) 224 throw new IllegalStateException(tr("Missing merge target of type {0} with id {1}", targetMember.getType(), targetMember.getUniqueId())); 225 if (! targetMember.isDeleted() && targetMember.isVisible()) { 226 RelationMember newMember = new RelationMember(sourceMember.getRole(), targetMember); 221 227 newMembers.add(newMember); 222 } 223 } 224 225 // check whether the list of relation members has changed 226 // 227 if (other.getMembersCount() != newMembers.size()) { 228 myRelation.setModified(true); 229 } else { 230 for (int i=0; i<other.getMembersCount();i++) { 231 RelationMember rm1 = other.getMember(i); 232 RelationMember rm2 = newMembers.get(i); 233 if (!rm1.getRole().equals(rm2.getRole())) { 234 myRelation.setModified(true); 235 break; 236 } else if (rm1.getMember().isNew() ^ rm2.getMember().isNew()) { 237 myRelation.setModified(true); 238 break; 239 } else if (rm1.getMember().isNew() && rm1.getMember() != rm2.getMember()) { 240 myRelation.setModified(true); 241 break; 242 } else if (! rm1.getMember().isNew() && rm1.getMember().getId() != rm2.getMember().getId()) { 243 myRelation.setModified(true); 244 break; 245 } 246 } 247 } 248 myRelation.setMembers(newMembers); 249 } 250 251 /** 252 * Tries to merge a primitive <code>other</code> into an existing primitive with the same id. 253 * 254 * @param other the other primitive which is to be merged onto a primitive in my primitives 255 * @return true, if this method was able to merge <code>other</code> with an existing node; false, otherwise 256 */ 257 private <P extends OsmPrimitive> boolean mergeById(P other) { 258 OsmPrimitive my = myDataSet.getPrimitiveById(other.getId(), other.getType()); 228 } else { 229 target.setModified(true); 230 } 231 } 232 target.setMembers(newMembers); 233 } 234 235 /** 236 * Tries to merge a primitive <code>source</code> into an existing primitive with the same id. 237 * 238 * @param source the other primitive which is to be merged onto a primitive in my primitives 239 * @return true, if this method was able to merge <code>source</code> into a target object; false, otherwise 240 */ 241 private boolean mergeById(OsmPrimitive source) { 242 OsmPrimitive target = targetDataSet.getPrimitiveById(source.getId(), source.getType()); 259 243 // merge other into an existing primitive with the same id, if possible 260 244 // 261 if ( my== null)245 if (target == null) 262 246 return false; 263 mergedMap.put(other.getUniqueId(), my.getUniqueId()); 264 if (my.getVersion() > other.getVersion()) 265 // my.version > other.version => keep my version 247 // found a corresponding target, remember it 248 mergedMap.put(source.getUniqueId(), target.getUniqueId()); 249 250 if (target.getVersion() > source.getVersion()) 251 // target.version > source.version => keep target version 266 252 return true; 267 if (! my.isVisible() && other.isVisible()) {253 if (! target.isVisible() && source.isVisible()) { 268 254 // should not happen 269 255 // 270 logger.warning(tr(" My primitivewith id {0} and version {1} is visible although "271 + " their primitivewith lower version {2} is not visible. "272 + "Can' t deal with this inconsistency. Keeping my primitive. ",273 Long.toString( my.getId()),Long.toString(my.getVersion()), Long.toString(other.getVersion())256 logger.warning(tr("Target object with id {0} and version {1} is visible although " 257 + "source object with lower version {2} is not visible. " 258 + "Can''t deal with this inconsistency. Keeping target object. ", 259 Long.toString(target.getId()),Long.toString(target.getVersion()), Long.toString(source.getVersion()) 274 260 )); 275 } else if ( my.isVisible() && ! other.isVisible()) {261 } else if (target.isVisible() && ! source.isVisible()) { 276 262 // this is always a conflict because the user has to decide whether 277 // he wants to create a clone of its localprimitive or whether he278 // wants to purge myfrom the local dataset. He can't keep it unchanged263 // he wants to create a clone of its target primitive or whether he 264 // wants to purge the target from the local dataset. He can't keep it unchanged 279 265 // because it was deleted on the server. 280 266 // 281 conflicts.add( my,other);282 } else if ( my.incomplete && !other.incomplete) {283 // my is incomplete, othercompletes it284 // => merge other onto my285 // 286 my.mergeFrom(other);287 fixReferences.add(other.getUniqueId());288 } else if (! my.incomplete && other.incomplete) {289 // my is complete and the otheris incomplete290 // => keep mine, we havemore information already291 // 292 } else if ( my.incomplete && other.incomplete) {293 // my and otherare incomplete. Doesn't matter which one to294 // take. We take mine.295 // 296 } else if ( my.isDeleted() && ! other.isDeleted() && my.getVersion() == other.getVersion()) {297 // same version, but my is deleted. Assume minetakes precedence267 conflicts.add(target,source); 268 } else if (target.incomplete && !source.incomplete) { 269 // target is incomplete, source completes it 270 // => merge source into target 271 // 272 target.mergeFrom(source); 273 childrenToMerge.add(source.getUniqueId()); 274 } else if (!target.incomplete && source.incomplete) { 275 // target is complete and source is incomplete 276 // => keep target, it has more information already 277 // 278 } else if (target.incomplete && source.incomplete) { 279 // target and source are incomplete. Doesn't matter which one to 280 // take. We take target. 281 // 282 } else if (target.isDeleted() && ! source.isDeleted() && target.getVersion() == source.getVersion()) { 283 // same version, but target is deleted. Assume target takes precedence 298 284 // otherwise too many conflicts when refreshing from the server 299 } else if ( my.isDeleted() != other.isDeleted()) {285 } else if (target.isDeleted() != source.isDeleted()) { 300 286 // differences in deleted state have to be resolved manually 301 287 // 302 conflicts.add( my,other);303 } else if (! my.isModified() && other.isModified()) {304 // my not modified. We can assume that otheris the most recent version.305 // clone it onto my. But check first, whether otheris deleted. if so,306 // make sure that my is not referencesanymore in myDataSet.307 // 308 if ( other.isDeleted()) {309 myDataSet.unlinkReferencesToPrimitive(my);310 } 311 my.mergeFrom(other);312 fixReferences.add(other.getUniqueId());313 } else if (! my.isModified() && !other.isModified() && my.getVersion() == other.getVersion()) {288 conflicts.add(target,source); 289 } else if (! target.isModified() && source.isModified()) { 290 // target not modified. We can assume that source is the most recent version. 291 // clone it into target. But check first, whether source is deleted. if so, 292 // make sure that target is not referenced anymore in myDataSet. 293 // 294 if (source.isDeleted()) { 295 deletedObjectsToUnlink.add(source); 296 } 297 target.mergeFrom(source); 298 childrenToMerge.add(source.getUniqueId()); 299 } else if (! target.isModified() && !source.isModified() && target.getVersion() == source.getVersion()) { 314 300 // both not modified. Keep mine 315 301 // 316 } else if (! my.isModified() && !other.isModified() && my.getVersion() < other.getVersion()) {302 } else if (! target.isModified() && !source.isModified() && target.getVersion() < source.getVersion()) { 317 303 // my not modified but other is newer. clone other onto mine. 318 304 // 319 my.mergeFrom(other);320 fixReferences.add(other.getUniqueId());321 } else if ( my.isModified() && ! other.isModified() && my.getVersion() == other.getVersion()) {305 target.mergeFrom(source); 306 childrenToMerge.add(source.getUniqueId()); 307 } else if (target.isModified() && ! source.isModified() && target.getVersion() == source.getVersion()) { 322 308 // my is same as other but mine is modified 323 309 // => keep mine 324 } else if (! my.hasEqualSemanticAttributes(other)) {310 } else if (! target.hasEqualSemanticAttributes(source)) { 325 311 // my is modified and is not semantically equal with other. Can't automatically 326 312 // resolve the differences 327 313 // => create a conflict 328 conflicts.add( my,other);314 conflicts.add(target,source); 329 315 } else { 330 316 // clone from other, but keep the modified flag. mergeFrom will mainly copy … … 332 318 // attributes should already be equal if we get here. 333 319 // 334 my.mergeFrom(other);335 my.setModified(true);336 fixReferences.add(other.getUniqueId());320 target.mergeFrom(source); 321 target.setModified(true); 322 childrenToMerge.add(source.getUniqueId()); 337 323 } 338 324 return true; … … 346 332 */ 347 333 public void merge() { 348 if ( theirDataSet == null)334 if (sourceDataSet == null) 349 335 return; 350 for (Node node: theirDataSet.getNodes()) {336 for (Node node: sourceDataSet.getNodes()) { 351 337 mergePrimitive(node); 352 338 } 353 for (Way way: theirDataSet.getWays()) {339 for (Way way: sourceDataSet.getWays()) { 354 340 mergePrimitive(way); 355 341 } 356 for (Relation relation: theirDataSet.getRelations()) {342 for (Relation relation: sourceDataSet.getRelations()) { 357 343 mergePrimitive(relation); 358 344 } … … 365 351 * @return 366 352 */ 367 public DataSet get MyDataSet() {368 return myDataSet;353 public DataSet getTargetDataSet() { 354 return targetDataSet; 369 355 } 370 356 -
trunk/src/org/openstreetmap/josm/io/OsmServerBackreferenceReader.java
r2433 r2443 269 269 DataSetMerger visitor = new DataSetMerger(ret,ds); 270 270 visitor.merge(); 271 ret = visitor.get MyDataSet();271 ret = visitor.getTargetDataSet(); 272 272 } 273 273 DataSet ds = getReferringRelations(progressMonitor.createSubTaskMonitor(1, false)); 274 274 DataSetMerger visitor = new DataSetMerger(ret,ds); 275 275 visitor.merge(); 276 ret = visitor.get MyDataSet();276 ret = visitor.getTargetDataSet(); 277 277 readIncompletePrimitives(ret, progressMonitor.createSubTaskMonitor(1, false)); 278 278 return ret; -
trunk/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java
r2435 r2443 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.data.osm .visitor;2 package org.openstreetmap.josm.data.osm; 3 3 4 4 import static org.junit.Assert.assertEquals; … … 21 21 import org.openstreetmap.josm.Main; 22 22 import org.openstreetmap.josm.data.coor.LatLon; 23 import org.openstreetmap.josm.data.osm.DataSet;24 import org.openstreetmap.josm.data.osm.DataSetMerger;25 import org.openstreetmap.josm.data.osm.Node;26 import org.openstreetmap.josm.data.osm.OsmPrimitive;27 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;28 import org.openstreetmap.josm.data.osm.Relation;29 import org.openstreetmap.josm.data.osm.RelationMember;30 import org.openstreetmap.josm.data.osm.User;31 import org.openstreetmap.josm.data.osm.Way;32 23 import org.openstreetmap.josm.data.projection.Mercator; 33 24 34 public class MergeVisitorTest {35 private static Logger logger = Logger.getLogger( MergeVisitorTest.class.getName());25 public class DataSetMergerTest { 26 private static Logger logger = Logger.getLogger(DataSetMergerTest.class.getName()); 36 27 37 28 static Properties testProperties; … … 46 37 // 47 38 try { 48 testProperties.load( MergeVisitorTest.class.getResourceAsStream("/test-unit-env.properties"));39 testProperties.load(DataSetMergerTest.class.getResourceAsStream("/test-unit-env.properties")); 49 40 } catch(Exception e){ 50 41 logger.log(Level.SEVERE, MessageFormat.format("failed to load property file ''{0}''", "/test-unit-env.properties")); … … 106 97 assertEquals(false, n2.isModified()); 107 98 assertEquals("value1", n2.get("key1")); 99 100 // merge target not modified after merging 101 assertTrue(!n2.isModified()); 108 102 } 109 103 … … 145 139 assertEquals("value1-new", n2.get("key1")); 146 140 assertEquals("value2", n2.get("key2")); 141 142 // the merge target should not be modified 143 assertTrue(!n2.isModified()); 147 144 } 148 145 … … 479 476 public void waySimple_IdenicalNodesDifferentTags() { 480 477 481 482 DataSet my= new DataSet();483 my.setVersion("0.6");478 // -- the target dataset 479 DataSet target = new DataSet(); 480 target.setVersion("0.6"); 484 481 485 482 Node n1 = new Node(); 486 483 n1.setCoor(new LatLon(0,0)); 487 484 n1.setOsmId(1,1); 488 my.addPrimitive(n1); 489 485 target.addPrimitive(n1); 490 486 491 487 Node n2 = new Node(); … … 493 489 n2.setOsmId(2,1); 494 490 495 my.addPrimitive(n2);491 target.addPrimitive(n2); 496 492 497 493 Way myWay = new Way(); … … 500 496 myWay.addNode(n1); 501 497 myWay.addNode(n2); 502 my.addPrimitive(myWay); 503 504 DataSet their = new DataSet(); 505 their.setVersion("0.6"); 498 target.addPrimitive(myWay); 499 500 // -- the source data set 501 DataSet source = new DataSet(); 502 source.setVersion("0.6"); 506 503 507 504 Node n3 = new Node(new LatLon(0,0)); 508 505 n3.setOsmId(1,1); 509 their.addPrimitive(n3);506 source.addPrimitive(n3); 510 507 511 508 Node n4 = new Node(new LatLon(1,1)); 512 509 n4.setOsmId(2,1); 513 their.addPrimitive(n4);510 source.addPrimitive(n4); 514 511 515 512 Way theirWay = new Way(); … … 519 516 theirWay.addNode(n3); 520 517 theirWay.addNode(n4); 521 their.addPrimitive(theirWay); 522 523 524 DataSetMerger visitor = new DataSetMerger(my,their); 525 visitor.merge(); 526 527 Way merged = (Way)my.getPrimitiveById(3, OsmPrimitiveType.WAY); 518 source.addPrimitive(theirWay); 519 520 521 DataSetMerger visitor = new DataSetMerger(target,source); 522 visitor.merge(); 523 524 // -- tests 525 Way merged = (Way)target.getPrimitiveById(3, OsmPrimitiveType.WAY); 528 526 assertEquals(0,visitor.getConflicts().size()); 529 527 assertEquals("value1",merged.get("key1")); … … 535 533 assertEquals(2,merged.getNode(1).getId()); 536 534 assertTrue(merged == myWay); 537 assertTrue(merged.getDataSet() == my);538 539 Node mergedNode = (Node) my.getPrimitiveById(1, OsmPrimitiveType.NODE);535 assertTrue(merged.getDataSet() == target); 536 537 Node mergedNode = (Node)target.getPrimitiveById(1, OsmPrimitiveType.NODE); 540 538 assertTrue(mergedNode == n1); 541 mergedNode = (Node) my.getPrimitiveById(2, OsmPrimitiveType.NODE);539 mergedNode = (Node)target.getPrimitiveById(2, OsmPrimitiveType.NODE); 542 540 assertTrue(mergedNode == n2); 541 542 assertTrue(!merged.isModified()); 543 543 } 544 544 … … 572 572 573 573 // --- their data set 574 575 574 DataSet their = new DataSet(); 576 575 their.setVersion("0.6"); … … 601 600 visitor.merge(); 602 601 602 // -- tests 603 603 Way merged = (Way)my.getPrimitiveById(3, OsmPrimitiveType.WAY); 604 604 assertEquals(0,visitor.getConflicts().size()); … … 614 614 assertTrue(merged.getNode(1) != n5); // must be clone of the original node in their 615 615 assertTrue(merged.getNode(2) == n2); 616 617 assertTrue(!merged.isModified()); // the target wasn't modified before merging, it 618 // mustn't be after merging 616 619 } 617 620 … … 625 628 public void waySimple_DifferentNodesAndMyIsModified() { 626 629 627 DataSet my = new DataSet(); 628 my.setVersion("0.6"); 630 // -- the target dataset 631 DataSet target = new DataSet(); 632 target.setVersion("0.6"); 629 633 630 634 Node n1 = new Node(new LatLon(0,0)); 631 635 n1.setOsmId(1,1); 632 my.addPrimitive(n1);636 target.addPrimitive(n1); 633 637 634 638 Node n2 = new Node(new LatLon(1,1)); 635 639 n2.setOsmId(2,1); 636 my.addPrimitive(n2);640 target.addPrimitive(n2); 637 641 638 642 Way myWay = new Way(); … … 643 647 myWay.setModified(true); 644 648 myWay.put("key1", "value1"); 645 my.addPrimitive(myWay); 646 647 DataSet their = new DataSet(); 648 their.setVersion("0.6"); 649 target.addPrimitive(myWay); 650 651 // -- the source dataset 652 DataSet source = new DataSet(); 653 source.setVersion("0.6"); 649 654 650 655 Node n3 = new Node(new LatLon(0,0)); 651 656 n3.setOsmId(1,1); 652 their.addPrimitive(n3);657 source.addPrimitive(n3); 653 658 654 659 Node n5 = new Node(new LatLon(1,1)); 655 660 n5.setOsmId(4,1); 656 their.addPrimitive(n5);661 source.addPrimitive(n5); 657 662 658 663 Node n4 = new Node(new LatLon(2,2)); 659 664 n4.setOsmId(2,1); 660 665 n4.put("key1", "value1"); 661 their.addPrimitive(n4);666 source.addPrimitive(n4); 662 667 663 668 … … 668 673 theirWay.addNode(n5); // insert a node 669 674 theirWay.addNode(n4); // this one is updated 670 their.addPrimitive(theirWay);671 672 673 DataSetMerger visitor = new DataSetMerger( my,their);674 visitor.merge(); 675 676 Way merged = (Way) my.getPrimitiveById(3, OsmPrimitiveType.WAY);675 source.addPrimitive(theirWay); 676 677 678 DataSetMerger visitor = new DataSetMerger(target,source); 679 visitor.merge(); 680 681 Way merged = (Way)target.getPrimitiveById(3, OsmPrimitiveType.WAY); 677 682 assertEquals(1,visitor.getConflicts().size()); 678 683 assertEquals(3,merged.getId()); … … 786 791 assertTrue(merged.getNode(0) == n1); 787 792 assertTrue(merged.getNode(1) == n2); 793 794 assertTrue(!merged.isModified()); 788 795 } 789 796 … … 842 849 assertTrue(merged.getNode(0) == n1); 843 850 assertTrue(merged.getNode(1) == n2); 851 852 assertTrue(!merged.isModified()); 844 853 } 845 854 … … 901 910 assertTrue(!myWay.getNodes().contains(n)); 902 911 assertTrue(n != null); 912 913 //a node was removed from the way,it should thus be modified 914 assertTrue(myWay.isModified()); 903 915 } 904 916 … … 956 968 Node n = (Node)my.getPrimitiveById(1,OsmPrimitiveType.NODE); 957 969 assertTrue(n != null); 970 971 assertTrue(r.isModified()); 958 972 } 959 973
Note:
See TracChangeset
for help on using the changeset viewer.