Changeset 2433 in josm for trunk/test/unit


Ignore:
Timestamp:
2009-11-10T21:25:40+01:00 (10 years ago)
Author:
Gubaer
Message:

Improved test cases for MergeVisitor.
Moved MergeVisitor and removed Visitor-pattern. Double-dispatching isn't necessary and only slows down the merge process.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/osm/visitor/MergeVisitorTest.java

    r2417 r2433  
    2222import org.openstreetmap.josm.data.coor.LatLon;
    2323import org.openstreetmap.josm.data.osm.DataSet;
     24import org.openstreetmap.josm.data.osm.DataSetMerger;
    2425import org.openstreetmap.josm.data.osm.Node;
    2526import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    9596
    9697
    97         MergeVisitor visitor = new MergeVisitor(my,their);
     98        DataSetMerger visitor = new DataSetMerger(my,their);
    9899        visitor.merge();
    99100
    100101        Node n2 = (Node)my.getPrimitiveById(1, OsmPrimitiveType.NODE);
    101102        assertTrue(visitor.getConflicts().isEmpty());
     103        assertTrue(n1 != n2); // make sure we have a clone
    102104        assertEquals(1, n2.getId());
    103105        assertEquals(1, n2.getVersion());
     
    131133
    132134
    133         MergeVisitor visitor = new MergeVisitor(my,their);
     135        DataSetMerger visitor = new DataSetMerger(my,their);
    134136        visitor.merge();
    135137
    136138        Node n2 = (Node)my.getPrimitiveById(1, OsmPrimitiveType.NODE);
    137139        assertTrue(visitor.getConflicts().isEmpty());
     140        assertTrue(n == n2); // make sure the merged node is still the original node
     141        assertTrue(n2.getDataSet() == my);
    138142        assertEquals(1, n2.getId());
    139143        assertEquals(2, n2.getVersion());
     
    144148
    145149    /**
    146      * node with same id, my is modified, their has a higher version
     150     * Node with same id, my is modified, their has a higher version
    147151     * => results in a conflict
    148152     *
     
    171175
    172176
    173         MergeVisitor visitor = new MergeVisitor(my,their);
     177        DataSetMerger visitor = new DataSetMerger(my,their);
    174178        visitor.merge();
    175179
    176180        Node n2 = (Node)my.getPrimitiveById(1, OsmPrimitiveType.NODE);
    177181        assertEquals(1,visitor.getConflicts().size());
    178         assertEquals(n, n2);
     182        assertTrue(n == n2);
     183        assertTrue(n1 != n2);
     184        assertTrue(n1.getDataSet() == their);
    179185    }
    180186
     
    207213
    208214
    209         MergeVisitor visitor = new MergeVisitor(my,their);
     215        DataSetMerger visitor = new DataSetMerger(my,their);
    210216        visitor.merge();
    211217
    212218        Node n2 = (Node)my.getPrimitiveById(1, OsmPrimitiveType.NODE);
    213219        assertEquals(1,visitor.getConflicts().size());
    214         assertEquals(n, n2);
     220        assertTrue(n == n2);
     221        assertTrue(n1 != n2);
     222        assertTrue(n1.getDataSet() == their);
    215223    }
    216224
     
    240248
    241249
    242         MergeVisitor visitor = new MergeVisitor(my,their);
     250        DataSetMerger visitor = new DataSetMerger(my,their);
    243251        visitor.merge();
    244252
     
    246254        assertEquals(1,visitor.getConflicts().size());
    247255        assertEquals(true, n2.isVisible());
     256        assertTrue(n == n2);
     257        assertTrue(n1 != n2);
     258        assertTrue(n1.getDataSet() == their);
    248259    }
    249260
     
    269280
    270281
    271         MergeVisitor visitor = new MergeVisitor(my,their);
     282        DataSetMerger visitor = new DataSetMerger(my,their);
    272283        visitor.merge();
    273284
     
    275286        assertEquals(0,visitor.getConflicts().size());
    276287        assertEquals(true, n2.isVisible());
     288        assertTrue(n == n2);
     289        assertTrue(n.getDataSet() == my);
     290        assertTrue(n1.getDataSet() == their);
    277291    }
    278292
     
    297311
    298312
    299         MergeVisitor visitor = new MergeVisitor(my,their);
     313        DataSetMerger visitor = new DataSetMerger(my,their);
    300314        visitor.merge();
    301315
    302316        assertEquals(1,visitor.getConflicts().size());
     317        assertTrue(n.getDataSet() == my);
     318        assertTrue(n1.getDataSet() == their);
    303319    }
    304320
     
    324340
    325341
    326         MergeVisitor visitor = new MergeVisitor(my,their);
    327         visitor.merge();
    328 
    329         assertEquals(0,visitor.getConflicts().size());
     342        DataSetMerger visitor = new DataSetMerger(my,their);
     343        visitor.merge();
     344
     345        assertEquals(0,visitor.getConflicts().size());
     346        Node n2 = (Node)my.getNodes().toArray()[0];
     347        assertTrue(n2 == n);
     348        assertTrue(n2.isDeleted());
    330349    }
    331350
     
    352371
    353372
    354         MergeVisitor visitor = new MergeVisitor(my,their);
     373        DataSetMerger visitor = new DataSetMerger(my,their);
    355374        visitor.merge();
    356375
    357376        Node n2 = (Node)my.getPrimitiveById(1,OsmPrimitiveType.NODE);
    358377        assertEquals(0,visitor.getConflicts().size());
    359         assertEquals(2, my.getNodes().size());
     378        assertEquals(1, my.getNodes().size());
    360379        assertEquals(n,n2);
    361380    }
     
    398417
    399418
    400         MergeVisitor visitor = new MergeVisitor(my,their);
     419        DataSetMerger visitor = new DataSetMerger(my,their);
    401420        visitor.merge();
    402421
     
    406425        assertEquals(true, n1.getTimestamp().equals(n2.getTimestamp()));
    407426        assertEquals(theirUser,n2.getUser());
     427        assertTrue(n2 == n);
     428        assertTrue(n2 != n1);
     429        assertTrue(n2.getDataSet() == my);
    408430    }
    409431
     
    436458
    437459
    438         MergeVisitor visitor = new MergeVisitor(my,their);
     460        DataSetMerger visitor = new DataSetMerger(my,their);
    439461        visitor.merge();
    440462
     
    444466        assertEquals(true, n1.getTimestamp().equals(n2.getTimestamp()));
    445467        assertEquals(false, n2.incomplete);
     468        assertTrue(n2 == n);
    446469    }
    447470
     
    499522
    500523
    501         MergeVisitor visitor = new MergeVisitor(my,their);
     524        DataSetMerger visitor = new DataSetMerger(my,their);
    502525        visitor.merge();
    503526
     
    511534        assertEquals(1,merged.getNode(0).getId());
    512535        assertEquals(2,merged.getNode(1).getId());
    513 
     536        assertTrue(merged == myWay);
     537        assertTrue(merged.getDataSet() == my);
     538
     539        Node mergedNode = (Node)my.getPrimitiveById(1, OsmPrimitiveType.NODE);
     540        assertTrue(mergedNode == n1);
     541        mergedNode = (Node)my.getPrimitiveById(2, OsmPrimitiveType.NODE);
     542        assertTrue(mergedNode == n2);
    514543    }
    515544
     
    524553    public void waySimple_AdditionalNodesAndChangedNodes() {
    525554
     555        // -- my data set
    526556        DataSet my = new DataSet();
    527557        my.setVersion("0.6");
     
    540570        myWay.addNode(n2);
    541571        my.addPrimitive(myWay);
     572
     573        // --- their data set
    542574
    543575        DataSet their = new DataSet();
     
    566598        their.addPrimitive(theirWay);
    567599
    568 
    569         MergeVisitor visitor = new MergeVisitor(my,their);
     600        DataSetMerger visitor = new DataSetMerger(my,their);
    570601        visitor.merge();
    571602
     
    579610        assertEquals(2,merged.getNode(2).getId());
    580611        assertEquals("value1",merged.getNode(2).get("key1"));
     612
     613        assertTrue(merged.getNode(0) == n1);
     614        assertTrue(merged.getNode(1) != n5); // must be clone of the original node in their
     615        assertTrue(merged.getNode(2) == n2);
    581616    }
    582617
     
    636671
    637672
    638         MergeVisitor visitor = new MergeVisitor(my,their);
     673        DataSetMerger visitor = new DataSetMerger(my,their);
    639674        visitor.merge();
    640675
     
    684719        their.addPrimitive(theirWay);
    685720
    686         MergeVisitor visitor = new MergeVisitor(my,their);
     721        DataSetMerger visitor = new DataSetMerger(my,their);
    687722        visitor.merge();
    688723
     
    699734     * their way can be merged on my way. No conflict.
    700735     *
    701      *
    702      *
    703736     */
    704737    @Test
    705738    public void waySimple_twoWaysWithNoId_NodesWithId() {
    706739
     740        // -- my data set
    707741        DataSet my = new DataSet();
    708742        my.setVersion("0.6");
     
    721755        my.addPrimitive(myWay);
    722756
     757        // -- their data set
    723758        DataSet their = new DataSet();
    724759        their.setVersion("0.6");
     
    739774        their.addPrimitive(theirWay);
    740775
    741         MergeVisitor visitor = new MergeVisitor(my,their);
    742         visitor.merge();
    743 
    744         assertEquals(0,visitor.getConflicts().size());
    745         assertEquals("their", myWay.getUser().getName());
    746         assertEquals(1111, myWay.getUser().getId());
    747         assertEquals(1111, myWay.getUser().getId());
    748         assertEquals(theirWay.getTimestamp(), myWay.getTimestamp());
     776        DataSetMerger visitor = new DataSetMerger(my,their);
     777        visitor.merge();
     778
     779        // -- tests
     780        Way merged = (Way)my.getWays().toArray()[0];
     781        assertEquals(0,visitor.getConflicts().size());
     782        assertEquals("their", merged.getUser().getName());
     783        assertEquals(1111, merged.getUser().getId());
     784        assertEquals(theirWay.getTimestamp(), merged.getTimestamp());
     785        assertTrue(merged == myWay);
     786        assertTrue(merged.getNode(0) == n1);
     787        assertTrue(merged.getNode(1) == n2);
    749788    }
    750789
     
    758797    public void waySimple_twoWaysWithNoId_NodesWithoutId() {
    759798
     799        // -- my data set
    760800        DataSet my = new DataSet();
    761801        my.setVersion("0.6");
     
    772812        my.addPrimitive(myWay);
    773813
     814        // -- their data set
    774815        DataSet their = new DataSet();
    775816        their.setVersion("0.6");
     
    789830        their.addPrimitive(theirWay);
    790831
    791         MergeVisitor visitor = new MergeVisitor(my,their);
    792         visitor.merge();
    793 
    794         assertEquals(0,visitor.getConflicts().size());
    795         assertEquals("their", myWay.getUser().getName());
    796         assertEquals(1111, myWay.getUser().getId());
    797         assertEquals(theirWay.getTimestamp(), myWay.getTimestamp());
     832        DataSetMerger visitor = new DataSetMerger(my,their);
     833        visitor.merge();
     834
     835        // -- tests
     836        Way merged = (Way)my.getWays().toArray()[0];
     837        assertEquals(0,visitor.getConflicts().size());
     838        assertEquals("their", merged.getUser().getName());
     839        assertEquals(1111, merged.getUser().getId());
     840        assertEquals(theirWay.getTimestamp(), merged.getTimestamp());
     841        assertTrue(merged == myWay);
     842        assertTrue(merged.getNode(0) == n1);
     843        assertTrue(merged.getNode(1) == n2);
    798844    }
    799845
     
    810856    public void wayComplex_mergingADeletedNode() {
    811857
     858        // -- my dataset
    812859        DataSet my = new DataSet();
    813860        my.setVersion("0.6");
     
    833880        their.addPrimitive(n5);
    834881
    835 
     882        // -- their data set
    836883        Way theirWay = new Way();
    837884        theirWay.setOsmId(4,1);
     
    843890        their.addPrimitive(theirWay);
    844891
    845         MergeVisitor visitor = new MergeVisitor(my,their);
     892        DataSetMerger visitor = new DataSetMerger(my,their);
    846893        visitor.merge();
    847894
     
    852899
    853900        Node n = (Node)my.getPrimitiveById(1,OsmPrimitiveType.NODE);
     901        assertTrue(!myWay.getNodes().contains(n));
    854902        assertTrue(n != null);
    855903    }
     
    857905    /**
    858906     * My dataset includes a deleted node.
    859      * Their dataset includes a relation with thre nodes, the first one being my node.
     907     * Their dataset includes a relation with three nodes, the first one being my node.
    860908     *
    861909     * => the merged relation should include two nodes only. the deleted node should still be
     
    898946        their.addPrimitive(theirRelation);
    899947
    900         MergeVisitor visitor = new MergeVisitor(my,their);
     948        DataSetMerger visitor = new DataSetMerger(my,their);
    901949        visitor.merge();
    902950
     
    934982        their.setVersion("0.6");
    935983
    936         MergeVisitor visitor = new MergeVisitor(my,their);
     984        DataSetMerger visitor = new DataSetMerger(my,their);
    937985        visitor.merge();
    938986
     
    9971045        my.addPrimitive(w6);
    9981046
    999         MergeVisitor visitor = new MergeVisitor(my,their);
     1047        DataSetMerger visitor = new DataSetMerger(my,their);
    10001048        visitor.merge();
    10011049
Note: See TracChangeset for help on using the changeset viewer.