Changeset 2443 in josm


Ignore:
Timestamp:
2009-11-13T10:24:58+01:00 (15 years ago)
Author:
Gubaer
Message:

Fixes problems with merging

Location:
trunk
Files:
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSetMerger.java

    r2433 r2443  
    2626
    2727    /** the target dataset for merging */
    28     private final DataSet myDataSet;
     28    private final DataSet targetDataSet;
    2929    /** the source dataset where primitives are merged from */
    30     private final DataSet theirDataSet;
     30    private final DataSet sourceDataSet;
    3131
    3232    /**
     
    3838     * to relation members) after the first phase of merging
    3939     */
    40     private Set<Long> fixReferences;
     40    private Set<Long> childrenToMerge;
     41
     42    private Set<OsmPrimitive> deletedObjectsToUnlink;
    4143
    4244    /**
     
    4547     * The visitor will merge <code>theirDataSet</code> onto <code>myDataSet</code>
    4648     *
    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.
    4951     * @throws IllegalArgumentException thrown if myDataSet is null
    5052     */
    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;
    5658        conflicts = new ConflictCollection();
    5759        mergedMap = new HashMap<Long, Long>();
    58         fixReferences = new HashSet<Long>();
     60        childrenToMerge = new HashSet<Long>();
     61        deletedObjectsToUnlink = new HashSet<OsmPrimitive>();
    5962    }
    6063
     
    7174     *
    7275     * @param <P>  the type of the other primitive
    73      * @param other  the other primitive
    74      */
    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() ) {
    7780            // try to merge onto a matching primitive with the same
    7881            // defined id
    7982            //
    80             if (mergeById(other))
     83            if (mergeById(source))
    8184                return;
    82             if (!other.isVisible())
     85            if (!source.isVisible())
    8386                // ignore it
    8487                return;
     
    8891            //
    8992            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()) {
    97100                    continue;
    98101                }
    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()) {
    102105                        // differences in deleted state have to be merged manually
    103106                        //
    104                         conflicts.add(my, other);
     107                        conflicts.add(target, source);
    105108                    } else {
    106109                        // copy the technical attributes from other
    107110                        // 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());
    113116                    }
    114117                    return;
     
    118121
    119122        // If we get here we didn't find a suitable primitive in
    120         // my dataset. Create a clone and add it to my dataset.
     123        // the target dataset. Create a clone and add it to the target dataset.
    121124        //
    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());
    132135    }
    133136
     
    136139        if (targetId == null)
    137140            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());
    139142    }
    140143
     
    156159     */
    157160    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())) {
    160163                mergeNodeList(w);
    161164                fixIncomplete(w);
    162165            }
    163166        }
    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())) {
    166169                mergeRelationMembers(r);
    167170            }
    168171        }
    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);
    182201                }
    183202            } 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()));
    214220        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);
    221227                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());
    259243        // merge other into an existing primitive with the same id, if possible
    260244        //
    261         if (my == null)
     245        if (target == null)
    262246            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
    266252            return true;
    267         if (! my.isVisible() && other.isVisible()) {
     253        if (! target.isVisible() && source.isVisible()) {
    268254            // should not happen
    269255            //
    270             logger.warning(tr("My primitive with id {0} and version {1} is visible although "
    271                     + "their primitive with 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())
    274260            ));
    275         } else if (my.isVisible() && ! other.isVisible()) {
     261        } else if (target.isVisible() && ! source.isVisible()) {
    276262            // this is always a conflict because the user has to decide whether
    277             // he wants to create a clone of its local primitive or whether he
    278             // wants to purge my from the local dataset. He can't keep it unchanged
     263            // 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
    279265            // because it was deleted on the server.
    280266            //
    281             conflicts.add(my,other);
    282         } else if (my.incomplete && !other.incomplete) {
    283             // my is incomplete, other completes it
    284             // => merge other onto my
    285             //
    286             my.mergeFrom(other);
    287             fixReferences.add(other.getUniqueId());
    288         } else if (!my.incomplete && other.incomplete) {
    289             // my is complete and the other is incomplete
    290             // => keep mine, we have more information already
    291             //
    292         } else if (my.incomplete && other.incomplete) {
    293             // my and other are incomplete. Doesn't matter which one to
    294             // take. We take mine.
    295             //
    296         } else if (my.isDeleted() && ! other.isDeleted() && my.getVersion() == other.getVersion()) {
    297             // same version, but my is deleted. Assume mine takes precedence
     267            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
    298284            // otherwise too many conflicts when refreshing from the server
    299         } else if (my.isDeleted() != other.isDeleted()) {
     285        } else if (target.isDeleted() != source.isDeleted()) {
    300286            // differences in deleted state have to be resolved manually
    301287            //
    302             conflicts.add(my,other);
    303         } else if (! my.isModified() && other.isModified()) {
    304             // my not modified. We can assume that other is the most recent version.
    305             // clone it onto my. But check first, whether other is deleted. if so,
    306             // make sure that my is not references anymore 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()) {
    314300            // both not modified. Keep mine
    315301            //
    316         } else if (! my.isModified() && !other.isModified() && my.getVersion() < other.getVersion()) {
     302        } else if (! target.isModified() && !source.isModified() && target.getVersion() < source.getVersion()) {
    317303            // my not modified but other is newer. clone other onto mine.
    318304            //
    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()) {
    322308            // my is same as other but mine is modified
    323309            // => keep mine
    324         } else if (! my.hasEqualSemanticAttributes(other)) {
     310        } else if (! target.hasEqualSemanticAttributes(source)) {
    325311            // my is modified and is not semantically equal with other. Can't automatically
    326312            // resolve the differences
    327313            // =>  create a conflict
    328             conflicts.add(my,other);
     314            conflicts.add(target,source);
    329315        } else {
    330316            // clone from other, but keep the modified flag. mergeFrom will mainly copy
     
    332318            // attributes should already be equal if we get here.
    333319            //
    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());
    337323        }
    338324        return true;
     
    346332     */
    347333    public void merge() {
    348         if (theirDataSet == null)
     334        if (sourceDataSet == null)
    349335            return;
    350         for (Node node: theirDataSet.getNodes()) {
     336        for (Node node: sourceDataSet.getNodes()) {
    351337            mergePrimitive(node);
    352338        }
    353         for (Way way: theirDataSet.getWays()) {
     339        for (Way way: sourceDataSet.getWays()) {
    354340            mergePrimitive(way);
    355341        }
    356         for (Relation relation: theirDataSet.getRelations()) {
     342        for (Relation relation: sourceDataSet.getRelations()) {
    357343            mergePrimitive(relation);
    358344        }
     
    365351     * @return
    366352     */
    367     public DataSet getMyDataSet() {
    368         return myDataSet;
     353    public DataSet getTargetDataSet() {
     354        return targetDataSet;
    369355    }
    370356
  • trunk/src/org/openstreetmap/josm/io/OsmServerBackreferenceReader.java

    r2433 r2443  
    269269                DataSetMerger visitor = new DataSetMerger(ret,ds);
    270270                visitor.merge();
    271                 ret = visitor.getMyDataSet();
     271                ret = visitor.getTargetDataSet();
    272272            }
    273273            DataSet ds = getReferringRelations(progressMonitor.createSubTaskMonitor(1, false));
    274274            DataSetMerger visitor = new DataSetMerger(ret,ds);
    275275            visitor.merge();
    276             ret = visitor.getMyDataSet();
     276            ret = visitor.getTargetDataSet();
    277277            readIncompletePrimitives(ret, progressMonitor.createSubTaskMonitor(1, false));
    278278            return ret;
  • trunk/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java

    r2435 r2443  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.data.osm.visitor;
     2package org.openstreetmap.josm.data.osm;
    33
    44import static org.junit.Assert.assertEquals;
     
    2121import org.openstreetmap.josm.Main;
    2222import 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;
    3223import org.openstreetmap.josm.data.projection.Mercator;
    3324
    34 public class MergeVisitorTest {
    35     private static Logger logger = Logger.getLogger(MergeVisitorTest.class.getName());
     25public class DataSetMergerTest {
     26    private static Logger logger = Logger.getLogger(DataSetMergerTest.class.getName());
    3627
    3728    static Properties testProperties;
     
    4637            //
    4738            try {
    48                 testProperties.load(MergeVisitorTest.class.getResourceAsStream("/test-unit-env.properties"));
     39                testProperties.load(DataSetMergerTest.class.getResourceAsStream("/test-unit-env.properties"));
    4940            } catch(Exception e){
    5041                logger.log(Level.SEVERE, MessageFormat.format("failed to load property file ''{0}''", "/test-unit-env.properties"));
     
    10697        assertEquals(false, n2.isModified());
    10798        assertEquals("value1", n2.get("key1"));
     99
     100        // merge target not modified after merging
     101        assertTrue(!n2.isModified());
    108102    }
    109103
     
    145139        assertEquals("value1-new", n2.get("key1"));
    146140        assertEquals("value2", n2.get("key2"));
     141
     142        // the merge target should not be modified
     143        assertTrue(!n2.isModified());
    147144    }
    148145
     
    479476    public void waySimple_IdenicalNodesDifferentTags() {
    480477
    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");
    484481
    485482        Node n1 = new Node();
    486483        n1.setCoor(new LatLon(0,0));
    487484        n1.setOsmId(1,1);
    488         my.addPrimitive(n1);
    489 
     485        target.addPrimitive(n1);
    490486
    491487        Node n2 = new Node();
     
    493489        n2.setOsmId(2,1);
    494490
    495         my.addPrimitive(n2);
     491        target.addPrimitive(n2);
    496492
    497493        Way myWay = new Way();
     
    500496        myWay.addNode(n1);
    501497        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");
    506503
    507504        Node n3 = new Node(new LatLon(0,0));
    508505        n3.setOsmId(1,1);
    509         their.addPrimitive(n3);
     506        source.addPrimitive(n3);
    510507
    511508        Node n4 = new Node(new LatLon(1,1));
    512509        n4.setOsmId(2,1);
    513         their.addPrimitive(n4);
     510        source.addPrimitive(n4);
    514511
    515512        Way theirWay = new Way();
     
    519516        theirWay.addNode(n3);
    520517        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);
    528526        assertEquals(0,visitor.getConflicts().size());
    529527        assertEquals("value1",merged.get("key1"));
     
    535533        assertEquals(2,merged.getNode(1).getId());
    536534        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);
    540538        assertTrue(mergedNode == n1);
    541         mergedNode = (Node)my.getPrimitiveById(2, OsmPrimitiveType.NODE);
     539        mergedNode = (Node)target.getPrimitiveById(2, OsmPrimitiveType.NODE);
    542540        assertTrue(mergedNode == n2);
     541
     542        assertTrue(!merged.isModified());
    543543    }
    544544
     
    572572
    573573        // --- their data set
    574 
    575574        DataSet their = new DataSet();
    576575        their.setVersion("0.6");
     
    601600        visitor.merge();
    602601
     602        // -- tests
    603603        Way merged = (Way)my.getPrimitiveById(3, OsmPrimitiveType.WAY);
    604604        assertEquals(0,visitor.getConflicts().size());
     
    614614        assertTrue(merged.getNode(1) != n5); // must be clone of the original node in their
    615615        assertTrue(merged.getNode(2) == n2);
     616
     617        assertTrue(!merged.isModified());  // the target wasn't modified before merging, it
     618        // mustn't be after merging
    616619    }
    617620
     
    625628    public void waySimple_DifferentNodesAndMyIsModified() {
    626629
    627         DataSet my = new DataSet();
    628         my.setVersion("0.6");
     630        // -- the target dataset
     631        DataSet target = new DataSet();
     632        target.setVersion("0.6");
    629633
    630634        Node n1 = new Node(new LatLon(0,0));
    631635        n1.setOsmId(1,1);
    632         my.addPrimitive(n1);
     636        target.addPrimitive(n1);
    633637
    634638        Node n2 = new Node(new LatLon(1,1));
    635639        n2.setOsmId(2,1);
    636         my.addPrimitive(n2);
     640        target.addPrimitive(n2);
    637641
    638642        Way myWay = new Way();
     
    643647        myWay.setModified(true);
    644648        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");
    649654
    650655        Node n3 = new Node(new LatLon(0,0));
    651656        n3.setOsmId(1,1);
    652         their.addPrimitive(n3);
     657        source.addPrimitive(n3);
    653658
    654659        Node n5 = new Node(new LatLon(1,1));
    655660        n5.setOsmId(4,1);
    656         their.addPrimitive(n5);
     661        source.addPrimitive(n5);
    657662
    658663        Node n4 = new Node(new LatLon(2,2));
    659664        n4.setOsmId(2,1);
    660665        n4.put("key1", "value1");
    661         their.addPrimitive(n4);
     666        source.addPrimitive(n4);
    662667
    663668
     
    668673        theirWay.addNode(n5); // insert a node
    669674        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);
    677682        assertEquals(1,visitor.getConflicts().size());
    678683        assertEquals(3,merged.getId());
     
    786791        assertTrue(merged.getNode(0) == n1);
    787792        assertTrue(merged.getNode(1) == n2);
     793
     794        assertTrue(!merged.isModified());
    788795    }
    789796
     
    842849        assertTrue(merged.getNode(0) == n1);
    843850        assertTrue(merged.getNode(1) == n2);
     851
     852        assertTrue(!merged.isModified());
    844853    }
    845854
     
    901910        assertTrue(!myWay.getNodes().contains(n));
    902911        assertTrue(n != null);
     912
     913        //a node was removed from the way,it should thus be modified
     914        assertTrue(myWay.isModified());
    903915    }
    904916
     
    956968        Node n = (Node)my.getPrimitiveById(1,OsmPrimitiveType.NODE);
    957969        assertTrue(n != null);
     970
     971        assertTrue(r.isModified());
    958972    }
    959973
Note: See TracChangeset for help on using the changeset viewer.