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
|
|
| 2 | 2 | package org.openstreetmap.josm.testutils; |
| 3 | 3 | |
| 4 | 4 | import java.awt.Color; |
| | 5 | import java.io.ByteArrayInputStream; |
| 5 | 6 | import java.io.File; |
| 6 | 7 | import java.io.IOException; |
| 7 | 8 | import java.security.GeneralSecurityException; |
| … |
… |
import org.junit.runners.model.InitializationError;
|
| 15 | 16 | import org.junit.runners.model.Statement; |
| 16 | 17 | import org.openstreetmap.josm.JOSMFixture; |
| 17 | 18 | import org.openstreetmap.josm.Main; |
| | 19 | import org.openstreetmap.josm.TestUtils; |
| 18 | 20 | import org.openstreetmap.josm.actions.DeleteAction; |
| 19 | 21 | import org.openstreetmap.josm.command.DeleteCommand; |
| 20 | 22 | import org.openstreetmap.josm.data.UserIdentityManager; |
| … |
… |
import org.openstreetmap.josm.data.osm.User;
|
| 22 | 24 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager; |
| 23 | 25 | import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; |
| 24 | 26 | import org.openstreetmap.josm.data.projection.Projections; |
| | 27 | import org.openstreetmap.josm.data.Version; |
| 25 | 28 | import org.openstreetmap.josm.gui.MainApplication; |
| 26 | 29 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; |
| 27 | 30 | import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard; |
| … |
… |
public class JOSMTestRules implements TestRule {
|
| 58 | 61 | private APIType useAPI = APIType.NONE; |
| 59 | 62 | private String i18n = null; |
| 60 | 63 | private TileSourceRule tileSourceRule; |
| | 64 | private String assumeRevisionString; |
| | 65 | private Version originalVersion; |
| 61 | 66 | private boolean platform; |
| 62 | 67 | private boolean useProjection; |
| 63 | 68 | private boolean useProjectionNadGrids; |
| … |
… |
public class JOSMTestRules implements TestRule {
|
| 136 | 141 | } |
| 137 | 142 | |
| 138 | 143 | /** |
| | 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 | /** |
| 139 | 155 | * Enable the dev.openstreetmap.org API for this test. |
| 140 | 156 | * @return this instance, for easy chaining |
| 141 | 157 | */ |
| … |
… |
public class JOSMTestRules implements TestRule {
|
| 286 | 302 | return this; |
| 287 | 303 | } |
| 288 | 304 | |
| | 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 | |
| 289 | 313 | @Override |
| 290 | 314 | public Statement apply(Statement base, Description description) { |
| 291 | 315 | Statement statement = base; |
| … |
… |
public class JOSMTestRules implements TestRule {
|
| 318 | 342 | * Set up before running a test |
| 319 | 343 | * @throws InitializationError If an error occured while creating the required environment. |
| 320 | 344 | */ |
| 321 | | protected void before() throws InitializationError { |
| | 345 | protected void before() throws InitializationError, ReflectiveOperationException { |
| 322 | 346 | // Tests are running headless by default. |
| 323 | 347 | System.setProperty("java.awt.headless", "true"); |
| 324 | 348 | |
| 325 | 349 | cleanUpFromJosmFixture(); |
| 326 | 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 | } |
| | 356 | |
| 327 | 357 | Config.setPreferencesInstance(Main.pref); |
| 328 | 358 | Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance()); |
| 329 | 359 | // All tests use the same timezone. |
| … |
… |
public class JOSMTestRules implements TestRule {
|
| 466 | 496 | * Clean up after running a test |
| 467 | 497 | */ |
| 468 | 498 | @SuppressFBWarnings("DM_GC") |
| 469 | | protected void after() { |
| | 499 | protected void after() throws ReflectiveOperationException { |
| 470 | 500 | // Sync AWT Thread |
| 471 | 501 | GuiHelper.runInEDTAndWait(new Runnable() { |
| 472 | 502 | @Override |
| … |
… |
public class JOSMTestRules implements TestRule {
|
| 480 | 510 | // TODO: Remove global listeners and other global state. |
| 481 | 511 | Main.pref.resetToInitialState(); |
| 482 | 512 | Main.platform = null; |
| | 513 | |
| | 514 | if (this.assumeRevisionString != null && this.originalVersion != null) { |
| | 515 | TestUtils.setPrivateStaticField(Version.class, "instance", this.originalVersion); |
| | 516 | } |
| | 517 | |
| 483 | 518 | // Parts of JOSM uses weak references - destroy them. |
| 484 | 519 | System.gc(); |
| 485 | 520 | } |