Ticket #16700: v1-0001-JOSMTestRules-give-JOSMTestRules-a-mechanism-to-a.patch

File v1-0001-JOSMTestRules-give-JOSMTestRules-a-mechanism-to-a.patch, 3.1 KB (added by ris, 3 weeks ago)
  • test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    From a441a3d5445b926972ddb025796db8f55e8ef6bd Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Wed, 29 Aug 2018 20:06:40 +0100
    Subject: [PATCH v1 1/2] JOSMTestRules: give JOSMTestRules a mechanism to allow
     per-method overrides of rule options
    
    the caveats here are that it will only ever be possible to "add" additional
    options to the rule in the same way options can be appended to a call chain
    already (because they're processed using the same mechanism). annotations
    can also only ever take primitive types as arguments (so no specifying new
    .fakeImagery(TileSourceRule ...) arguments)
    ---
     .../josm/testutils/JOSMTestRules.java              | 30 ++++++++++++++++++++++
     1 file changed, 30 insertions(+)
    
    diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
    index a425ec7f1..9fb529ba6 100644
    a b import java.awt.event.WindowEvent; 
    77import java.io.ByteArrayInputStream;
    88import java.io.File;
    99import java.io.IOException;
     10import java.lang.annotation.Documented;
     11import java.lang.annotation.ElementType;
     12import java.lang.annotation.Retention;
     13import java.lang.annotation.RetentionPolicy;
     14import java.lang.annotation.Target;
    1015import java.nio.charset.StandardCharsets;
    1116import java.security.GeneralSecurityException;
    1217import java.text.MessageFormat;
    public class JOSMTestRules implements TestRule { 
    367372
    368373    @Override
    369374    public Statement apply(Statement base, Description description) {
     375        // First process any Override* annotations for per-test overrides.
     376        // The following only work because "option" methods modify JOSMTestRules in-place
     377        final OverrideAssumeRevision overrideAssumeRevision = description.getAnnotation(OverrideAssumeRevision.class);
     378        if (overrideAssumeRevision != null) {
     379            this.assumeRevision(overrideAssumeRevision.value());
     380        }
     381        final OverrideTimeout overrideTimeout = description.getAnnotation(OverrideTimeout.class);
     382        if (overrideTimeout != null) {
     383            this.timeout(overrideTimeout.value());
     384        }
     385
    370386        Statement statement = base;
    371387        // counter-intuitively, Statements which need to have their setup routines performed *after* another one need to
    372388        // be added into the chain *before* that one, so that it ends up on the "inside".
    public class JOSMTestRules implements TestRule { 
    694710        return java.lang.management.ManagementFactory.getRuntimeMXBean().
    695711                getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
    696712    }
     713
     714    @Documented
     715    @Retention(RetentionPolicy.RUNTIME)
     716    @Target(ElementType.METHOD)
     717    public @interface OverrideAssumeRevision {
     718        String value();
     719    }
     720
     721    @Documented
     722    @Retention(RetentionPolicy.RUNTIME)
     723    @Target(ElementType.METHOD)
     724    public @interface OverrideTimeout {
     725        int value();
     726    }
    697727}