Changeset 11215 in josm


Ignore:
Timestamp:
2016-11-04T01:16:28+01:00 (3 years ago)
Author:
bastiK
Message:

see #13907 - add unit test for problem fixed in [11212]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/functional/org/openstreetmap/josm/gui/mappaint/StyleCacheTest.java

    r9770 r11215  
    99import java.io.File;
    1010import java.io.InputStream;
     11import java.util.IdentityHashMap;
    1112
     13import org.junit.Assert;
    1214import org.junit.BeforeClass;
    1315import org.junit.Test;
     
    1618import org.openstreetmap.josm.data.Bounds;
    1719import org.openstreetmap.josm.data.osm.DataSet;
     20import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1821import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
    1922import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
     
    2225import org.openstreetmap.josm.io.Compression;
    2326import org.openstreetmap.josm.io.OsmReader;
     27import org.openstreetmap.josm.tools.Pair;
    2428
    2529/**
    2630 * Test {@link StyleCache}.
    27  *
    28  * Verifies, that the intern pool is not growing when repeatedly rendering the
    29  * same set of primitives (and clearing the calculated styles each time).
    30  *
    31  * If it grows, this is an indication that the {@code equals} and {@code hashCode}
    32  * implementation is broken and two identical objects are not recognized as equal
    33  * or produce different hash codes.
    34  *
    35  * The opposite problem (different objects are mistaken as equal) has more visible
    36  * consequences for the user (wrong rendering on the map) and is not recognized by
    37  * this test.
    3831 */
    3932public class StyleCacheTest {
     
    4639    private static NavigatableComponent nc;
    4740    private static DataSet dsCity;
     41    private static DataSet dsCity2;
    4842
    4943    @BeforeClass
     
    6458            dsCity = OsmReader.parseDataSet(fisC, NullProgressMonitor.INSTANCE);
    6559        }
     60        try (
     61            InputStream fisC = Compression.getUncompressedFileInputStream(new File("data_nodist/neubrandenburg.osm.bz2"));
     62        ) {
     63            dsCity2 = OsmReader.parseDataSet(fisC, NullProgressMonitor.INSTANCE);
     64        }
    6665    }
    6766
     67    /**
     68     * Verifies, that the intern pool is not growing when repeatedly rendering the
     69     * same set of primitives (and clearing the calculated styles each time).
     70     *
     71     * If it grows, this is an indication that the {@code equals} and {@code hashCode}
     72     * implementation is broken and two identical objects are not recognized as equal
     73     * or produce different hash codes.
     74     *
     75     * The opposite problem (different objects are mistaken as equal) has more visible
     76     * consequences for the user (wrong rendering on the map) and is not recognized by
     77     * this test.
     78     */
    6879    @Test
    69     public void testStyleCacheInternPool() throws Exception {
     80    public void testStyleCacheInternPool() {
    7081        Bounds bounds = new Bounds(53.56, 13.25, 53.57, 13.26);
    7182        Rendering visitor = new StyledMapRenderer(g, nc, false);
     
    8394        }
    8495    }
     96
     97    /**
     98     * Verifies, that the number of {@code StyleElementList} instances stored
     99     * for all the rendered primitives is actually low (as intended).
     100     *
     101     * Two primitives with the same style should share one {@code StyleElementList}
     102     * instance for the cached style elements. This is verified by counting all
     103     * the instances using {@code A == B} identity.
     104     */
     105    @Test
     106    public void testStyleCacheInternPool2() {
     107        Bounds bounds = new Bounds(53.56, 13.25, 53.57, 13.26);
     108        Rendering visitor = new StyledMapRenderer(g, nc, false);
     109        nc.zoomTo(bounds);
     110        visitor.render(dsCity2, true, bounds);
     111
     112        IdentityHashMap<StyleElementList, Integer> counter = new IdentityHashMap<>();
     113        int noPrimitives = 0;
     114        for (OsmPrimitive osm : dsCity2.allPrimitives()) {
     115            // primitives, that have been rendered, should have the cache populated
     116            if (osm.mappaintStyle != null) {
     117                noPrimitives++;
     118                Pair<StyleElementList, Range> p = osm.mappaintStyle.getWithRange(nc.getDist100Pixel(), false);
     119                StyleElementList sel = p.a;
     120                Assert.assertNotNull(sel);
     121                Integer k = counter.get(sel);
     122                if (k == null) {
     123                    k = 0;
     124                }
     125                counter.put(sel, k + 1);
     126            }
     127        }
     128        int EXPECTED_NO_PRIMITIVES = 4298; // needs to be updated if data file or bbox changes
     129        Assert.assertEquals(
     130                "The number of rendered primitives should be " + EXPECTED_NO_PRIMITIVES,
     131                EXPECTED_NO_PRIMITIVES, noPrimitives);
     132        Assert.assertTrue(
     133                "Too many StyleElementList instances, they should be shared using the StyleCache",
     134                counter.size() < 100);
     135    }
    85136}
Note: See TracChangeset for help on using the changeset viewer.