Changeset 7163 in josm for trunk/test/functional


Ignore:
Timestamp:
2014-05-22T01:17:51+02:00 (5 years ago)
Author:
Don-vip
Message:

fix last unit test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/functional/org/openstreetmap/josm/io/OsmServerBackreferenceReaderTest.java

    r7150 r7163  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertNotNull;
     7import static org.junit.Assert.assertTrue;
    68import static org.junit.Assert.fail;
    79
     
    2931import org.openstreetmap.josm.data.osm.DataSet;
    3032import org.openstreetmap.josm.data.osm.Node;
     33import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3134import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    3235import org.openstreetmap.josm.data.osm.Relation;
     
    202205
    203206    @Test
    204     public void testBackrefrenceForNode() throws OsmTransferException {
     207    public void testBackreferenceForNode() throws OsmTransferException {
    205208        Node n = lookupNode(ds, 0);
    206209        assertNotNull(n);
     
    212215        DataSet referers = reader.parseOsm(NullProgressMonitor.INSTANCE);
    213216        printNumberOfPrimitives(referers);
    214         assertEquals(10, referers.getNodes().size());
    215         assertEquals(1, referers.getWays().size());
    216         assertEquals(0, referers.getRelations().size());
     217
     218        Set<Long> expectedNodeIds = new HashSet<>();
     219        Set<Long> expectedWayIds = new HashSet<>();
     220        Set<Long> expectedRelationIds = new HashSet<>();
     221
     222        for (OsmPrimitive ref : n.getReferrers()) {
     223            if (ref instanceof Way) {
     224                expectedWayIds.add(ref.getId());
     225                expectedNodeIds.addAll(getNodeIdsInWay((Way) ref));
     226            } else if (ref instanceof Relation) {
     227                expectedRelationIds.add(ref.getId());
     228                expectedWayIds.addAll(getWayIdsInRelation((Relation) ref, false));
     229                expectedNodeIds.addAll(getNodeIdsInRelation((Relation) ref, false));
     230            }
     231        }
     232
     233        assertEquals(expectedNodeIds.size(), referers.getNodes().size());
     234        assertEquals(expectedWayIds.size(), referers.getWays().size());
     235        assertEquals(expectedRelationIds.size(), referers.getRelations().size());
     236
     237        for (Node node : referers.getNodes()) {
     238            assertTrue(expectedNodeIds.contains(node.getId()));
     239            assertFalse(node.isIncomplete());
     240        }
     241
    217242        for (Way way : referers.getWays()) {
    218             assertEquals(w.getId(), way.getId());
    219             assertEquals(false, way.isIncomplete());
     243            assertTrue(expectedWayIds.contains(way.getId()));
     244            assertEquals(n.getReferrers().contains(way), !way.isIncomplete());
     245        }
     246
     247        for (Relation relation : referers.getRelations()) {
     248            assertTrue(expectedRelationIds.contains(relation.getId()));
     249            assertFalse(relation.isIncomplete());
    220250        }
    221251    }
     
    228258
    229259    @Test
    230     public void testBackrefrenceForNode_Full() throws OsmTransferException {
     260    public void testBackreferenceForNode_Full() throws OsmTransferException {
    231261        Node n = lookupNode(ds, 0);
    232262        assertNotNull(n);
    233         Way w = lookupWay(ds, 0);
    234         assertNotNull(w);
    235263
    236264        OsmServerBackreferenceReader reader = new OsmServerBackreferenceReader(n);
     
    238266        DataSet referers = reader.parseOsm(NullProgressMonitor.INSTANCE);
    239267        printNumberOfPrimitives(referers);
    240         assertEquals(10, referers.getNodes().size());
    241         assertEquals(1, referers.getWays().size());
    242         assertEquals(0, referers.getRelations().size());
     268
     269        Set<Long> expectedNodeIds = new HashSet<>();
     270        Set<Long> expectedWayIds = new HashSet<>();
     271        Set<Long> expectedRelationIds = new HashSet<>();
     272        for (OsmPrimitive ref : n.getReferrers()) {
     273            if (ref instanceof Way) {
     274                expectedWayIds.add(ref.getId());
     275                expectedNodeIds.addAll(getNodeIdsInWay((Way) ref));
     276            } else if (ref instanceof Relation) {
     277                expectedRelationIds.add(ref.getId());
     278                expectedWayIds.addAll(getWayIdsInRelation((Relation) ref, true));
     279                expectedNodeIds.addAll(getNodeIdsInRelation((Relation) ref, true));
     280            }
     281        }
     282
     283        assertEquals(expectedNodeIds.size(), referers.getNodes().size());
     284        assertEquals(expectedWayIds.size(), referers.getWays().size());
     285        assertEquals(expectedRelationIds.size(), referers.getRelations().size());
     286
     287        for (Node node : referers.getNodes()) {
     288            assertTrue(expectedNodeIds.contains(node.getId()));
     289            assertFalse(node.isIncomplete());
     290        }
     291
    243292        for (Way way : referers.getWays()) {
    244             assertEquals(w.getId(), way.getId());
    245             assertEquals(false, way.isIncomplete());
    246             assertEquals(10, w.getNodesCount());
     293            assertTrue(expectedWayIds.contains(way.getId()));
     294            assertFalse(way.isIncomplete());
     295        }
     296
     297        for (Relation relation : referers.getRelations()) {
     298            assertTrue(expectedRelationIds.contains(relation.getId()));
     299            assertFalse(relation.isIncomplete());
    247300        }
    248301    }
    249302
    250303    @Test
    251     public void testBackrefrenceForWay() throws OsmTransferException {
     304    public void testBackreferenceForWay() throws OsmTransferException {
    252305        Way w = lookupWay(ds, 1);
    253306        assertNotNull(w);
     
    259312        DataSet referers = reader.parseOsm(NullProgressMonitor.INSTANCE);
    260313        printNumberOfPrimitives(referers);
    261         assertEquals(0, referers.getNodes().size()); // no nodes loaded
    262         assertEquals(6, referers.getWays().size());  // 6 ways referred by two relations
     314
     315        Set<Long> expectedNodeIds = new HashSet<>();
     316        Set<Long> expectedWayIds = new HashSet<>();
     317        Set<Long> expectedRelationIds = new HashSet<>();
     318
     319        for (OsmPrimitive ref : w.getReferrers()) {
     320            if (ref instanceof Relation) {
     321                expectedRelationIds.add(ref.getId());
     322                expectedWayIds.addAll(getWayIdsInRelation((Relation) ref, false));
     323                expectedNodeIds.addAll(getNodeIdsInRelation((Relation) ref, false));
     324            }
     325        }
     326
     327        assertEquals(expectedNodeIds.size(), referers.getNodes().size());
     328        assertEquals(expectedWayIds.size(), referers.getWays().size());
     329        assertEquals(expectedRelationIds.size(), referers.getRelations().size());
     330
    263331        for (Way w1 : referers.getWays()) {
    264             assertEquals(true, w1.isIncomplete());
     332            assertTrue(w1.isIncomplete());
    265333        }
    266334        assertEquals(2, referers.getRelations().size());  // two relations referring to w
     
    268336        Relation r = lookupRelation(referers, 0);
    269337        assertNotNull(r);
    270         assertEquals(false, r.isIncomplete());
     338        assertFalse(r.isIncomplete());
    271339        r = lookupRelation(referers, 1);
    272         assertEquals(false, r.isIncomplete());
     340        assertFalse(r.isIncomplete());
    273341    }
    274342
    275343    @Test
    276     public void testBackrefrenceForWay_Full() throws OsmTransferException {
     344    public void testBackreferenceForWay_Full() throws OsmTransferException {
    277345        Way w = lookupWay(ds, 1);
    278346        assertNotNull(w);
     
    285353        assertEquals(6, referers.getWays().size());  // 6 ways referred by two relations
    286354        for (Way w1 : referers.getWays()) {
    287             assertEquals(false, w1.isIncomplete());
     355            assertFalse(w1.isIncomplete());
    288356        }
    289357        assertEquals(2, referers.getRelations().size());  // two relations referring to
     
    297365        assertEquals(expectedNodeIds.size(), referers.getNodes().size());
    298366        for (Node n : referers.getNodes()) {
    299             assertEquals(true, expectedNodeIds.contains(n.getId()));
     367            assertTrue(expectedNodeIds.contains(n.getId()));
    300368        }
    301369
    302370        Relation r = lookupRelation(referers, 0);
    303371        assertNotNull(r);
    304         assertEquals(false, r.isIncomplete());
     372        assertFalse(r.isIncomplete());
    305373        r = lookupRelation(referers, 1);
    306         assertEquals(false, r.isIncomplete());
     374        assertFalse(r.isIncomplete());
    307375    }
    308376
    309377    @Test
    310     public void testBackrefrenceForRelation() throws OsmTransferException {
     378    public void testBackreferenceForRelation() throws OsmTransferException {
    311379        Relation r = lookupRelation(ds, 1);
    312380        assertNotNull(r);
     
    321389
    322390        Set<Long> referringRelationsIds = new HashSet<>();
    323         r = lookupRelation(referers, 6);
    324         assertNotNull(r);
    325         assertEquals(false, r.isIncomplete());
    326         referringRelationsIds.add(r.getId());
    327         r = lookupRelation(referers, 7);
    328         assertNotNull(r);
    329         assertEquals(false, r.isIncomplete());
    330         referringRelationsIds.add(r.getId());
    331         r = lookupRelation(referers, 8);
    332         assertNotNull(r);
    333         assertEquals(false, r.isIncomplete());
    334         referringRelationsIds.add(r.getId());
    335         r = lookupRelation(referers, 9);
    336         assertNotNull(r);
    337         assertEquals(false, r.isIncomplete());
    338         referringRelationsIds.add(r.getId());
     391        Relation r6 = lookupRelation(referers, 6);
     392        assertNotNull(r6);
     393        assertFalse(r6.isIncomplete());
     394        referringRelationsIds.add(r6.getId());
     395        Relation r7 = lookupRelation(referers, 7);
     396        assertNotNull(r7);
     397        assertFalse(r7.isIncomplete());
     398        referringRelationsIds.add(r7.getId());
     399        Relation r8 = lookupRelation(referers, 8);
     400        assertNotNull(r8);
     401        assertFalse(r8.isIncomplete());
     402        referringRelationsIds.add(r8.getId());
     403        Relation r9 = lookupRelation(referers, 9);
     404        assertNotNull(r9);
     405        assertFalse(r9.isIncomplete());
     406        referringRelationsIds.add(r9.getId());
    339407
    340408        for (Relation r1 : referers.getRelations()) {
    341409            if (!referringRelationsIds.contains(r1.getId())) {
    342                 assertEquals(true, r1.isIncomplete());
     410                assertTrue(r1.isIncomplete());
    343411            }
    344412        }
     
    371439        assertEquals(expectedWayIds.size(), referers.getWays().size());
    372440        for (Way w1 : referers.getWays()) {
    373             assertEquals(true, expectedWayIds.contains(w1.getId()));
    374             assertEquals(true, w1.isIncomplete());
    375         }
    376 
    377         // make sure we didn't read any nodes
    378         //
    379         assertEquals(0, referers.getNodes().size());
    380     }
    381 
    382     protected Set<Long> getNodeIdsInWay(Way way) {
     441            assertTrue(expectedWayIds.contains(w1.getId()));
     442            assertTrue(w1.isIncomplete());
     443        }
     444
     445        // make sure we read all nodes referred to by parent relations.
     446        Set<Long> expectedNodeIds = new HashSet<>();
     447        for (OsmPrimitive ref : r.getReferrers()) {
     448            if (ref instanceof Relation) {
     449                expectedNodeIds.addAll(getNodeIdsInRelation((Relation) ref, false));
     450            }
     451        }
     452        assertEquals(expectedNodeIds.size(), referers.getNodes().size());
     453    }
     454
     455    protected static Set<Long> getNodeIdsInWay(Way way) {
    383456        HashSet<Long> ret = new HashSet<>();
    384457        if (way == null)return ret;
     
    389462    }
    390463
    391     protected Set<Long> getNodeIdsInRelation(Relation r) {
     464    protected static Set<Long> getNodeIdsInRelation(Relation r, boolean children) {
    392465        HashSet<Long> ret = new HashSet<>();
    393466        if (r == null) return ret;
     
    395468            if (m.isNode()) {
    396469                ret.add(m.getMember().getId());
    397             } else if (m.isWay()) {
     470            } else if (m.isWay() && children) {
    398471                ret.addAll(getNodeIdsInWay(m.getWay()));
    399             } else if (m.isRelation()) {
    400                 ret.addAll(getNodeIdsInRelation(m.getRelation()));
     472            } else if (m.isRelation() && children) {
     473                ret.addAll(getNodeIdsInRelation(m.getRelation(), true));
    401474            }
    402475        }
     
    404477    }
    405478
     479    protected static Set<Long> getWayIdsInRelation(Relation r, boolean children) {
     480        HashSet<Long> ret = new HashSet<>();
     481        if (r == null) return ret;
     482        for (RelationMember m: r.getMembers()) {
     483            if (m.isWay()) {
     484                ret.add(m.getMember().getId());
     485            } else if (m.isRelation() && children) {
     486                ret.addAll(getWayIdsInRelation(m.getRelation(), true));
     487            }
     488        }
     489        return ret;
     490    }
     491
    406492    @Test
    407     public void testBackrefrenceForRelation_Full() throws OsmTransferException {
     493    public void testBackreferenceForRelation_Full() throws OsmTransferException {
    408494        Relation r = lookupRelation(ds, 1);
    409495        assertNotNull(r);
     
    419505        r = lookupRelation(referers, 6);
    420506        assertNotNull(r);
    421         assertEquals(false, r.isIncomplete());
     507        assertFalse(r.isIncomplete());
    422508        referringRelationsIds.add(r.getId());
    423509        r = lookupRelation(referers, 7);
    424510        assertNotNull(r);
    425         assertEquals(false, r.isIncomplete());
     511        assertFalse(r.isIncomplete());
    426512        referringRelationsIds.add(r.getId());
    427513        r = lookupRelation(referers, 8);
    428514        assertNotNull(r);
    429         assertEquals(false, r.isIncomplete());
     515        assertFalse(r.isIncomplete());
    430516        referringRelationsIds.add(r.getId());
    431517        r = lookupRelation(referers, 9);
    432518        assertNotNull(r);
    433         assertEquals(false, r.isIncomplete());
     519        assertFalse(r.isIncomplete());
    434520        referringRelationsIds.add(r.getId());
    435521
     
    437523        //
    438524        for (Relation r1 : referers.getRelations()) {
    439             assertEquals(false, r1.isIncomplete());
     525            assertFalse(r1.isIncomplete());
    440526        }
    441527
     
    467553            Way w = (Way) referers.getPrimitiveById(id, OsmPrimitiveType.WAY);
    468554            assertNotNull(w);
    469             assertEquals(false, w.isIncomplete());
     555            assertFalse(w.isIncomplete());
    470556        }
    471557
     
    473559        for (int i = 6; i < 10; i++) {
    474560            Relation r1 = lookupRelation(ds, i);
    475             expectedNodeIds.addAll(getNodeIdsInRelation(r1));
     561            expectedNodeIds.addAll(getNodeIdsInRelation(r1, true));
    476562        }
    477563
    478564        assertEquals(expectedNodeIds.size(), referers.getNodes().size());
    479565        for (Node n : referers.getNodes()) {
    480             assertEquals(true, expectedNodeIds.contains(n.getId()));
     566            assertTrue(expectedNodeIds.contains(n.getId()));
    481567        }
    482568    }
Note: See TracChangeset for help on using the changeset viewer.