Ignore:
Timestamp:
2018-01-08T02:45:59+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #15742 - check downloaded plugin is valid *before* we delete any existing one (patch by ris)

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/TestUtils.java

    r13079 r13300  
    188188
    189189    /**
     190     * Sets a private static field value.
     191     * @param cls object class
     192     * @param fieldName private field name
     193     * @param value replacement value
     194     * @throws ReflectiveOperationException if a reflection operation error occurs
     195     */
     196    public static void setPrivateStaticField(Class<?> cls, String fieldName, final Object value) throws ReflectiveOperationException {
     197        Field f = cls.getDeclaredField(fieldName);
     198        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
     199            f.setAccessible(true);
     200            return null;
     201        });
     202        f.set(null, value);
     203    }
     204
     205    /**
    190206     * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
    191207     * but does not show the progress.
  • trunk/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java

    r12558 r13300  
    4040
    4141    /**
     42     * Returns the {@code Content-Type} with which to serve the file referenced
     43     * by {@link #getRemoteFile()}
     44     * @return the {@code Content-Type} string for file {@link #getRemoteFile()}
     45     */
     46    protected String getRemoteContentType() {
     47        return "text/xml";
     48    }
     49
     50    /**
    4251     * Returns the http URL to remote test file to download.
    4352     * @return the http URL to remote test file to download
     
    5463                .willReturn(aResponse()
    5564                    .withStatus(200)
    56                     .withHeader("Content-Type", "text/xml")
     65                    .withHeader("Content-Type", getRemoteContentType())
    5766                    .withBodyFile(getRemoteFile())));
    5867    }
  • trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    r13078 r13300  
    33
    44import java.awt.Color;
     5import java.io.ByteArrayInputStream;
    56import java.io.File;
    67import java.io.IOException;
     
    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;
     23import org.openstreetmap.josm.data.Version;
    2124import org.openstreetmap.josm.data.osm.User;
    2225import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
     
    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;
     
    133138    public JOSMTestRules platform() {
    134139        platform = true;
     140        return this;
     141    }
     142
     143    /**
     144     * Mock this test's assumed JOSM version (as reported by {@link Version}).
     145     * @param revisionProperties mock contents of JOSM's {@code REVISION} properties file
     146     * @return this instance, for easy chaining
     147     */
     148    public JOSMTestRules assumeRevision(final String revisionProperties) {
     149        this.assumeRevisionString = revisionProperties;
    135150        return this;
    136151    }
     
    277292    /**
    278293     * Use the {@link Main#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel},
    279      *         {@link Main#menu}, {@link Main#toolbar} global variables in this test.
     294     *         global variables in this test.
    280295     * @return this instance, for easy chaining
    281296     * @since 12557
     
    285300        main = true;
    286301        return this;
     302    }
     303
     304    private static class MockVersion extends Version {
     305        MockVersion(final String propertiesString) {
     306            super.initFromRevisionInfo(
     307                new ByteArrayInputStream(propertiesString.getBytes())
     308            );
     309        }
    287310    }
    288311
     
    318341     * Set up before running a test
    319342     * @throws InitializationError If an error occured while creating the required environment.
    320      */
    321     protected void before() throws InitializationError {
     343     * @throws ReflectiveOperationException if a reflective access error occurs
     344     */
     345    protected void before() throws InitializationError, ReflectiveOperationException {
    322346        // Tests are running headless by default.
    323347        System.setProperty("java.awt.headless", "true");
    324348
    325349        cleanUpFromJosmFixture();
     350
     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        }
    326356
    327357        Config.setPreferencesInstance(Main.pref);
     
    465495    /**
    466496     * Clean up after running a test
     497     * @throws ReflectiveOperationException if a reflective access error occurs
    467498     */
    468499    @SuppressFBWarnings("DM_GC")
    469     protected void after() {
     500    protected void after() throws ReflectiveOperationException {
    470501        // Sync AWT Thread
    471502        GuiHelper.runInEDTAndWait(new Runnable() {
     
    481512        Main.pref.resetToInitialState();
    482513        Main.platform = null;
     514
     515        if (this.assumeRevisionString != null && this.originalVersion != null) {
     516            TestUtils.setPrivateStaticField(Version.class, "instance", this.originalVersion);
     517        }
     518
    483519        // Parts of JOSM uses weak references - destroy them.
    484520        System.gc();
Note: See TracChangeset for help on using the changeset viewer.