Ticket #15742: v1-0002-JOSMTestRules-add-assumeRevision-method-to-allow-.patch

File v1-0002-JOSMTestRules-add-assumeRevision-method-to-allow-.patch, 5.0 KB (added by ris, 7 years ago)
  • test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    From 772560bdd9611c15fa6975d2be132f176949b1bc Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Sun, 7 Jan 2018 22:10:28 +0000
    Subject: [PATCH v1 2/4] JOSMTestRules: add assumeRevision method to allow
     JOSM's apparent version to be mocked
    
    ---
     .../josm/testutils/JOSMTestRules.java              | 39 ++++++++++++++++++++--
     1 file changed, 37 insertions(+), 2 deletions(-)
    
    diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
    index cec968231..8cf19f9aa 100644
    a b  
    22package org.openstreetmap.josm.testutils;
    33
    44import java.awt.Color;
     5import java.io.ByteArrayInputStream;
    56import java.io.File;
    67import java.io.IOException;
    78import java.security.GeneralSecurityException;
    import org.junit.runners.model.InitializationError;  
    1516import org.junit.runners.model.Statement;
    1617import org.openstreetmap.josm.JOSMFixture;
    1718import org.openstreetmap.josm.Main;
     19import org.openstreetmap.josm.TestUtils;
    1820import org.openstreetmap.josm.actions.DeleteAction;
    1921import org.openstreetmap.josm.command.DeleteCommand;
    2022import org.openstreetmap.josm.data.UserIdentityManager;
    import org.openstreetmap.josm.data.osm.User;  
    2224import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    2325import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
    2426import org.openstreetmap.josm.data.projection.Projections;
     27import org.openstreetmap.josm.data.Version;
    2528import org.openstreetmap.josm.gui.MainApplication;
    2629import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    2730import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
    public class JOSMTestRules implements TestRule {  
    5861    private APIType useAPI = APIType.NONE;
    5962    private String i18n = null;
    6063    private TileSourceRule tileSourceRule;
     64    private String assumeRevisionString;
     65    private Version originalVersion;
    6166    private boolean platform;
    6267    private boolean useProjection;
    6368    private boolean useProjectionNadGrids;
    public class JOSMTestRules implements TestRule {  
    136141    }
    137142
    138143    /**
     144     * Mock this test's assumed JOSM version (as reported by
     145     * {@link Version}).
     146     * @param revisionProperties mock contents of JOSM's {@code REVISION} properties file
     147     * @return this instance, for easy chaining
     148     */
     149    public JOSMTestRules assumeRevision(final String revisionProperties) {
     150        this.assumeRevisionString = revisionProperties;
     151        return this;
     152    }
     153
     154    /**
    139155     * Enable the dev.openstreetmap.org API for this test.
    140156     * @return this instance, for easy chaining
    141157     */
    public class JOSMTestRules implements TestRule {  
    286302        return this;
    287303    }
    288304
     305    private static class MockVersion extends Version {
     306        public MockVersion(final String propertiesString) {
     307            super.initFromRevisionInfo(
     308                new ByteArrayInputStream(propertiesString.getBytes())
     309            );
     310        }
     311    }
     312
    289313    @Override
    290314    public Statement apply(Statement base, Description description) {
    291315        Statement statement = base;
    public class JOSMTestRules implements TestRule {  
    318342     * Set up before running a test
    319343     * @throws InitializationError If an error occured while creating the required environment.
    320344     */
    321     protected void before() throws InitializationError {
     345    protected void before() throws InitializationError, ReflectiveOperationException {
    322346        // Tests are running headless by default.
    323347        System.setProperty("java.awt.headless", "true");
    324348
    325349        cleanUpFromJosmFixture();
    326350
     351        if (this.assumeRevisionString != null) {
     352            this.originalVersion = Version.getInstance();
     353            final Version replacementVersion = new MockVersion(this.assumeRevisionString);
     354            TestUtils.setPrivateStaticField(Version.class, "instance", replacementVersion);
     355        }
     356
    327357        Config.setPreferencesInstance(Main.pref);
    328358        Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
    329359        // All tests use the same timezone.
    public class JOSMTestRules implements TestRule {  
    466496     * Clean up after running a test
    467497     */
    468498    @SuppressFBWarnings("DM_GC")
    469     protected void after() {
     499    protected void after() throws ReflectiveOperationException {
    470500        // Sync AWT Thread
    471501        GuiHelper.runInEDTAndWait(new Runnable() {
    472502            @Override
    public class JOSMTestRules implements TestRule {  
    480510        // TODO: Remove global listeners and other global state.
    481511        Main.pref.resetToInitialState();
    482512        Main.platform = null;
     513
     514        if (this.assumeRevisionString != null && this.originalVersion != null) {
     515            TestUtils.setPrivateStaticField(Version.class, "instance", this.originalVersion);
     516        }
     517
    483518        // Parts of JOSM uses weak references - destroy them.
    484519        System.gc();
    485520    }