Ticket #16567: 16567.initial_extensions.1.patch

File 16567.initial_extensions.1.patch, 37.5 KB (added by taylor.smock, 5 years ago)

Modify two tests to use the annotations, add i18n, Main, ApiType, Presets, Projection, ProjectionNadGrids, and Territories. Territories will probably be modified so that there can be different initialization types (e.g., full initialization or partial initialization).

  • test/unit/org/openstreetmap/josm/gui/bbox/SizeButtonTest.java

     
    55import static org.junit.jupiter.api.Assertions.assertFalse;
    66import static org.junit.jupiter.api.Assertions.assertTrue;
    77
    8 import org.junit.jupiter.api.extension.RegisterExtension;
    98import org.junit.jupiter.api.Test;
    109import org.openstreetmap.josm.TestUtils;
    1110import org.openstreetmap.josm.gui.bbox.SizeButton.AccessibleSizeButton;
    12 import org.openstreetmap.josm.testutils.JOSMTestRules;
     11import org.openstreetmap.josm.testutils.annotations.FullPreferences;
    1312
    14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    15 
    1613/**
    1714 * Unit tests of {@link SizeButton} class.
    1815 */
     16@FullPreferences
    1917class SizeButtonTest {
    2018
    21     /**
    22      * Setup tests
    23      */
    24     @RegisterExtension
    25     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    26     public JOSMTestRules test = new JOSMTestRules().preferences();
    2719
    2820    /**
    2921     * Unit test of {@link SizeButton#SizeButton}.
  • test/unit/org/openstreetmap/josm/gui/datatransfer/OsmTransferHandlerTest.java

     
    66
    77import java.util.Collections;
    88
    9 import org.junit.jupiter.api.extension.RegisterExtension;
    109import org.junit.jupiter.api.Test;
    1110import org.openstreetmap.josm.actions.CopyAction;
    1211import org.openstreetmap.josm.data.coor.LatLon;
     
    1514import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    1615import org.openstreetmap.josm.gui.MainApplication;
    1716import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    18 import org.openstreetmap.josm.testutils.JOSMTestRules;
     17import org.openstreetmap.josm.testutils.annotations.FullPreferences;
     18import org.openstreetmap.josm.testutils.annotations.Main;
     19import org.openstreetmap.josm.testutils.annotations.Projection;
    1920
    20 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    21 
    2221/**
    2322 * Unit tests of {@link OsmTransferHandler} class.
    2423 */
     24@FullPreferences
     25@Projection
     26@Main
    2527class OsmTransferHandlerTest {
    26     /**
    27      * Prefs to use OSM primitives
    28      */
    29     @RegisterExtension
    30     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    31     public JOSMTestRules test = new JOSMTestRules().preferences().projection().main();
    32 
    3328    private final OsmTransferHandler transferHandler = new OsmTransferHandler();
    3429
    3530    /**
  • test/unit/org/openstreetmap/josm/io/auth/CredentialsAgentExceptionTest.java

     
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
    55
    6 import org.junit.jupiter.api.extension.RegisterExtension;
    76import org.junit.jupiter.api.Test;
    8 import org.openstreetmap.josm.testutils.JOSMTestRules;
    97
    10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    11 
    128/**
    139 * Unit tests of {@link CredentialsAgentException} class.
    1410 */
    1511class CredentialsAgentExceptionTest {
    16 
    1712    /**
    18      * Setup test
    19      */
    20     @RegisterExtension
    21     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    22     public JOSMTestRules test = new JOSMTestRules();
    23 
    24     /**
    2513     * Unit test of {@code CredentialsAgentException#CredentialsAgentException}
    2614     */
    2715    @Test
  • test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

     
    6060import org.openstreetmap.josm.io.OsmTransferCanceledException;
    6161import org.openstreetmap.josm.spi.preferences.Config;
    6262import org.openstreetmap.josm.spi.preferences.Setting;
     63import org.openstreetmap.josm.testutils.annotations.FullPreferences;
     64import org.openstreetmap.josm.testutils.annotations.JosmHome;
     65import org.openstreetmap.josm.testutils.annotations.Main;
     66import org.openstreetmap.josm.testutils.annotations.OsmApiType;
     67import org.openstreetmap.josm.testutils.annotations.Presets;
     68import org.openstreetmap.josm.testutils.annotations.Projection;
     69import org.openstreetmap.josm.testutils.annotations.ProjectionNadGrids;
    6370import org.openstreetmap.josm.testutils.mockers.EDTAssertionMocker;
    6471import org.openstreetmap.josm.testutils.mockers.WindowlessMapViewStateMocker;
    6572import org.openstreetmap.josm.testutils.mockers.WindowlessNavigatableComponentMocker;
     
    134141    /**
    135142     * Enable the use of default preferences.
    136143     * @return this instance, for easy chaining
     144     * @deprecated Use {@link FullPreferences} instead.
    137145     */
     146    @Deprecated
    138147    public JOSMTestRules preferences() {
    139148        josmHome();
    140149        usePreferences = true;
     
    144153    /**
    145154     * Set JOSM home to a valid, empty directory.
    146155     * @return this instance, for easy chaining
     156     * @deprecated Use {@link JosmHome} instead.
    147157     */
     158    @Deprecated
    148159    private JOSMTestRules josmHome() {
    149160        josmHome = new TemporaryFolder();
    150161        return this;
     
    153164    /**
    154165     * Enables the i18n module for this test in english.
    155166     * @return this instance, for easy chaining
     167     * @deprecated Use {org.openstreetmap.josm.testutils.annotations.I18n} instead.
    156168     */
     169    @Deprecated
    157170    public JOSMTestRules i18n() {
    158171        return i18n("en");
    159172    }
     
    162175     * Enables the i18n module for this test.
    163176     * @param language The language to use.
    164177     * @return this instance, for easy chaining
     178     * @deprecated Use {org.openstreetmap.josm.testutils.annotations.I18n} instead.
    165179     */
     180    @Deprecated
    166181    public JOSMTestRules i18n(String language) {
    167182        i18n = language;
    168183        return this;
     
    172187     * Mock this test's assumed JOSM version (as reported by {@link Version}).
    173188     * @param revisionProperties mock contents of JOSM's {@code REVISION} properties file
    174189     * @return this instance, for easy chaining
     190     * @deprecated Use {@link OverrideAssumeRevision} instead.
    175191     */
     192    @Deprecated
    176193    public JOSMTestRules assumeRevision(final String revisionProperties) {
    177194        this.assumeRevisionString = revisionProperties;
    178195        return this;
     
    181198    /**
    182199     * Enable the dev.openstreetmap.org API for this test.
    183200     * @return this instance, for easy chaining
     201     * @deprecated Use {@link OsmApiType} instead.
    184202     */
     203    @Deprecated
    185204    public JOSMTestRules devAPI() {
    186205        preferences();
    187206        useAPI = APIType.DEV;
     
    191210    /**
    192211     * Use the {@link FakeOsmApi} for testing.
    193212     * @return this instance, for easy chaining
     213     * @deprecated Use {@link OsmApiType} instead.
    194214     */
     215    @Deprecated
    195216    public JOSMTestRules fakeAPI() {
    196217        useAPI = APIType.FAKE;
    197218        return this;
     
    200221    /**
    201222     * Set up default projection (Mercator)
    202223     * @return this instance, for easy chaining
     224     * @deprecated Use {@link Projection} instead.
    203225     */
     226    @Deprecated
    204227    public JOSMTestRules projection() {
    205228        useProjection = true;
    206229        return this;
     
    209232    /**
    210233     * Set up loading of NTV2 grit shift files to support projections that need them.
    211234     * @return this instance, for easy chaining
     235     * @deprecated Use {@link ProjectionNadGrids} instead.
    212236     */
     237    @Deprecated
    213238    public JOSMTestRules projectionNadGrids() {
    214239        useProjectionNadGrids = true;
    215240        return this;
     
    257282     * Use presets in this test.
    258283     * @return this instance, for easy chaining
    259284     * @since 12568
     285     * @deprecated Use {@link Presets} instead.
    260286     */
     287    @Deprecated
    261288    public JOSMTestRules presets() {
    262289        preferences();
    263290        usePresets = true;
     
    268295     * Use boundaries dataset in this test.
    269296     * @return this instance, for easy chaining
    270297     * @since 12545
     298     * @deprecated Use {@link org.openstreetmap.josm.testutils.annotations.Territories} instead.
    271299     */
     300    @Deprecated
    272301    public JOSMTestRules territories() {
    273302        territories = true;
    274303        return this;
     
    351380     *         global variables in this test.
    352381     * @return this instance, for easy chaining
    353382     * @since 12557
     383     * @deprecated Use {@link Main} instead
    354384     */
     385    @Deprecated
    355386    public JOSMTestRules main() {
    356387        return this.main(
    357388            WindowlessMapViewStateMocker::new,
     
    368399     *        of {@link org.openstreetmap.josm.gui.NavigatableComponent}, null to skip.
    369400     *
    370401     * @return this instance, for easy chaining
     402     * @deprecated Use {@link Main} instead
    371403     */
     404    @Deprecated
    372405    public JOSMTestRules main(
    373406        final Runnable mapViewStateMockingRunnable,
    374407        final Runnable navigableComponentMockingRunnable
     
    397430        return this;
    398431    }
    399432
    400     private static class MockVersion extends Version {
    401         MockVersion(final String propertiesString) {
     433    /*
     434     * Mock the JOSM version. This should only be used in JOSM Core test extensions.
     435     */
     436    public static class MockVersion extends Version {
     437        public MockVersion(final String propertiesString) {
    402438            super.initFromRevisionInfo(
    403439                new ByteArrayInputStream(propertiesString.getBytes(StandardCharsets.UTF_8))
    404440            );
     
    409445    public Statement apply(Statement base, Description description) {
    410446        // First process any Override* annotations for per-test overrides.
    411447        // The following only work because "option" methods modify JOSMTestRules in-place
    412         final OverrideAssumeRevision overrideAssumeRevision = description.getAnnotation(OverrideAssumeRevision.class);
     448        OverrideAssumeRevision overrideAssumeRevision = description.getAnnotation(OverrideAssumeRevision.class);
    413449        if (overrideAssumeRevision != null) {
    414450            this.assumeRevision(overrideAssumeRevision.value());
    415451        }
     
    790826    /**
    791827     * Override this test's assumed JOSM version (as reported by {@link Version}).
    792828     * @see JOSMTestRules#assumeRevision(String)
     829     * @deprecated Use {@link org.openstreetmap.josm.testutils.annotations.AssumeRevision}
     830     *             when using JUnit 5 Extensions.
    793831     */
    794832    @Documented
    795833    @Retention(RetentionPolicy.RUNTIME)
    796834    @Target(ElementType.METHOD)
     835    @Deprecated
    797836    public @interface OverrideAssumeRevision {
    798837        /**
    799838         * Returns overridden assumed JOSM version.
  • test/unit/org/openstreetmap/josm/testutils/annotations/AssumeRevision.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.lang.annotation.Documented;
     5import java.lang.annotation.ElementType;
     6import java.lang.annotation.Retention;
     7import java.lang.annotation.RetentionPolicy;
     8import java.lang.annotation.Target;
     9import java.util.Optional;
     10
     11import org.junit.jupiter.api.extension.AfterEachCallback;
     12import org.junit.jupiter.api.extension.BeforeEachCallback;
     13import org.junit.jupiter.api.extension.ExtendWith;
     14import org.junit.jupiter.api.extension.ExtensionContext;
     15import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
     16import org.junit.jupiter.api.extension.ExtensionContext.Store;
     17import org.junit.platform.commons.support.AnnotationSupport;
     18import org.openstreetmap.josm.TestUtils;
     19import org.openstreetmap.josm.data.Version;
     20import org.openstreetmap.josm.testutils.JOSMTestRules;
     21
     22
     23/**
     24 * Override this test's assumed JOSM version (as reported by {@link Version}).
     25 * @author Taylor Smock
     26 * @see JOSMTestRules#assumeRevision(String)
     27 * @since xxx
     28 */
     29@Documented
     30@Retention(RetentionPolicy.RUNTIME)
     31@Target({ElementType.METHOD, ElementType.TYPE})
     32@ExtendWith(AssumeRevision.AssumeRevisionExtension.class)
     33public @interface AssumeRevision {
     34    /**
     35     * Returns overridden assumed JOSM version.
     36     * @return overridden assumed JOSM version
     37     */
     38    String value();
     39
     40    /**
     41     * Override the JOSM revision information. Use {@link AssumeRevision} instead of directly using this extension.
     42     * @author Taylor Smock
     43     * @since xxx
     44     */
     45    class AssumeRevisionExtension implements BeforeEachCallback, AfterEachCallback {
     46
     47        @Override
     48        public void afterEach(ExtensionContext context) throws Exception {
     49            Store store = context.getStore(Namespace.create(AssumeRevisionExtension.class));
     50            String originalVersion = store.getOrDefault(store, String.class, null);
     51            if (originalVersion != null) {
     52                TestUtils.setPrivateStaticField(Version.class, "instance", originalVersion);
     53            }
     54        }
     55
     56        @Override
     57        public void beforeEach(ExtensionContext context) throws Exception {
     58            Optional<AssumeRevision> annotation = AnnotationSupport.findAnnotation(context.getElement(), AssumeRevision.class);
     59            if (annotation.isPresent()) {
     60                Store store = context.getStore(Namespace.create(AssumeRevisionExtension.class));
     61                store.put("originalVersion", Version.getInstance());
     62                final Version replacementVersion = new JOSMTestRules.MockVersion(annotation.get().value());
     63                TestUtils.setPrivateStaticField(Version.class, "instance", replacementVersion);
     64            }
     65        }
     66    }
     67}
  • test/unit/org/openstreetmap/josm/testutils/annotations/BasicPreferences.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.lang.annotation.Documented;
     5import java.lang.annotation.ElementType;
     6import java.lang.annotation.Retention;
     7import java.lang.annotation.RetentionPolicy;
     8import java.lang.annotation.Target;
     9
     10import org.junit.jupiter.api.extension.BeforeAllCallback;
     11import org.junit.jupiter.api.extension.ExtendWith;
     12import org.junit.jupiter.api.extension.ExtensionContext;
     13import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
     14import org.openstreetmap.josm.data.Preferences;
     15import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
     16import org.openstreetmap.josm.data.preferences.JosmUrls;
     17import org.openstreetmap.josm.spi.preferences.Config;
     18import org.openstreetmap.josm.testutils.JOSMTestRules;
     19
     20/**
     21 * Allow tests to use JOSM preferences (see {@link JOSMTestRules#preferences()})
     22 * @author Taylor Smock
     23 * @see FullPreferences
     24 * @since xxx
     25 */
     26@Documented
     27@Retention(RetentionPolicy.RUNTIME)
     28@Target(ElementType.TYPE)
     29@ExtendWith(BasicPreferences.BasicPreferencesExtension.class)
     30public @interface BasicPreferences {
     31
     32    /**
     33     * Initialize basic preferences. This is often more than enough for basic tests.
     34     * @author Taylor Smock
     35     *
     36     */
     37    class BasicPreferencesExtension implements BeforeAllCallback {
     38
     39        @Override
     40        public void beforeAll(ExtensionContext context) throws Exception {
     41            Preferences pref = Preferences.main();
     42            Config.setPreferencesInstance(pref);
     43            Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
     44            Config.setUrlsProvider(JosmUrls.getInstance());
     45            context.getStore(Namespace.create(BasicPreferencesExtension.class)).put("preferences", pref);
     46        }
     47
     48    }
     49}
  • test/unit/org/openstreetmap/josm/testutils/annotations/FullPreferences.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.lang.annotation.Documented;
     5import java.lang.annotation.ElementType;
     6import java.lang.annotation.Retention;
     7import java.lang.annotation.RetentionPolicy;
     8import java.lang.annotation.Target;
     9import java.util.Map;
     10
     11import org.junit.jupiter.api.extension.BeforeEachCallback;
     12import org.junit.jupiter.api.extension.ExtendWith;
     13import org.junit.jupiter.api.extension.ExtensionContext;
     14import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
     15import org.openstreetmap.josm.TestUtils;
     16import org.openstreetmap.josm.data.Preferences;
     17import org.openstreetmap.josm.spi.preferences.Config;
     18import org.openstreetmap.josm.spi.preferences.Setting;
     19import org.openstreetmap.josm.testutils.JOSMTestRules;
     20import org.openstreetmap.josm.testutils.annotations.BasicPreferences.BasicPreferencesExtension;
     21
     22/**
     23 * Allow tests to use JOSM preferences (see {@link JOSMTestRules#preferences()})
     24 * @author Taylor Smock
     25 * @see BasicPreferences (often enough for simple tests).
     26 * @since xxx
     27 */
     28@Documented
     29@Retention(RetentionPolicy.RUNTIME)
     30@Target(ElementType.TYPE)
     31@JosmHome
     32@BasicPreferences
     33@ExtendWith(FullPreferences.UsePreferencesExtension.class)
     34public @interface FullPreferences {
     35    /**
     36     * Initialize preferences.
     37     */
     38    class UsePreferencesExtension implements BeforeEachCallback {
     39        @Override
     40        public void beforeEach(ExtensionContext context) throws Exception {
     41            Preferences pref = context.getStore(Namespace.create(BasicPreferencesExtension.class)).get("preferences", Preferences.class);
     42            @SuppressWarnings("unchecked")
     43            final Map<String, Setting<?>> defaultsMap = (Map<String, Setting<?>>) TestUtils.getPrivateField(pref, "defaultsMap");
     44            defaultsMap.clear();
     45            pref.resetToInitialState();
     46            pref.enableSaveOnPut(false);
     47            // No pref init -> that would only create the preferences file.
     48            // We force the use of a wrong API server, just in case anyone attempts an upload
     49            Config.getPref().put("osm-server.url", "http://invalid");
     50        }
     51
     52    }
     53}
  • test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.lang.annotation.Documented;
     5import java.lang.annotation.ElementType;
     6import java.lang.annotation.Retention;
     7import java.lang.annotation.RetentionPolicy;
     8import java.lang.annotation.Target;
     9import java.util.Optional;
     10
     11import org.junit.jupiter.api.extension.BeforeAllCallback;
     12import org.junit.jupiter.api.extension.ExtendWith;
     13import org.junit.jupiter.api.extension.ExtensionContext;
     14import org.junit.platform.commons.support.AnnotationSupport;
     15import org.openstreetmap.josm.testutils.JOSMTestRules;
     16
     17/**
     18 * Enables the i18n module for this test.
     19 * @author Taylor Smock
     20 * @see JOSMTestRules#i18n(String)
     21 *
     22 */
     23@Documented
     24@Retention(RetentionPolicy.RUNTIME)
     25@Target(ElementType.TYPE)
     26@ExtendWith(I18n.I18nExtension.class)
     27public @interface I18n {
     28    /**
     29     * Get the language to use for i18n
     30     * @return The language (default "en").
     31     */
     32    String language() default "en";
     33
     34    /**
     35     * Enables the i18n module for this test.
     36     * @author Taylor Smock
     37     * @see JOSMTestRules#i18n(String)
     38     *
     39     */
     40    class I18nExtension implements BeforeAllCallback {
     41        @Override
     42        public void beforeAll(ExtensionContext context) throws Exception {
     43            Optional<I18n> annotation = AnnotationSupport.findAnnotation(context.getElement(), I18n.class);
     44            String language = "en";
     45            if (annotation.isPresent()) {
     46                language = annotation.get().language();
     47            }
     48            org.openstreetmap.josm.tools.I18n.set(language);
     49        }
     50    }
     51}
  • test/unit/org/openstreetmap/josm/testutils/annotations/JosmHome.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.io.File;
     5import java.io.IOException;
     6import java.lang.annotation.Documented;
     7import java.lang.annotation.ElementType;
     8import java.lang.annotation.Retention;
     9import java.lang.annotation.RetentionPolicy;
     10import java.lang.annotation.Target;
     11import java.nio.file.FileVisitResult;
     12import java.nio.file.Files;
     13import java.nio.file.Path;
     14import java.nio.file.SimpleFileVisitor;
     15import java.nio.file.attribute.BasicFileAttributes;
     16import java.util.UUID;
     17
     18import org.junit.jupiter.api.extension.AfterAllCallback;
     19import org.junit.jupiter.api.extension.BeforeAllCallback;
     20import org.junit.jupiter.api.extension.ExtendWith;
     21import org.junit.jupiter.api.extension.ExtensionContext;
     22import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
     23import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
     24import org.openstreetmap.josm.testutils.JOSMTestRules;
     25
     26/**
     27 * Use the JOSM home directory. See {@link JOSMTestRules}.
     28 * Typically only used by {@link FullPreferences}.
     29 *
     30 * @author Taylor Smock
     31 *
     32 */
     33@Documented
     34@Retention(RetentionPolicy.RUNTIME)
     35@Target(ElementType.TYPE)
     36@ExtendWith(JosmHome.JosmHomeExtension.class)
     37public @interface JosmHome {
     38    /**
     39     * Create a JOSM home directory. Prefer using {@link JosmHome}.
     40     * @author Taylor Smock
     41     */
     42    class JosmHomeExtension implements BeforeAllCallback, AfterAllCallback {
     43        @Override
     44        public void afterAll(ExtensionContext context) throws Exception {
     45            Path tempDir = context.getStore(Namespace.create(JosmHome.class)).get("home", Path.class);
     46            Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
     47                @Override
     48                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
     49                    Files.delete(dir);
     50                    return FileVisitResult.CONTINUE;
     51                }
     52
     53                @Override
     54                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
     55                    Files.delete(file);
     56                    return FileVisitResult.CONTINUE;
     57                }
     58            });
     59        }
     60
     61        @Override
     62        public void beforeAll(ExtensionContext context) throws Exception {
     63            Path tempDir = Files.createTempDirectory(UUID.randomUUID().toString());
     64            context.getStore(Namespace.create(JosmHome.class)).put("home", tempDir);
     65            File home = tempDir.toFile();
     66            System.setProperty("josm.home", home.getAbsolutePath());
     67            JosmBaseDirectories.getInstance().clearMemos();
     68        }
     69    }
     70}
  • test/unit/org/openstreetmap/josm/testutils/annotations/Main.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import static java.lang.annotation.ElementType.TYPE;
     5import static java.lang.annotation.RetentionPolicy.RUNTIME;
     6
     7import java.lang.annotation.Documented;
     8import java.lang.annotation.Retention;
     9import java.lang.annotation.Target;
     10import java.util.Optional;
     11
     12import org.junit.jupiter.api.extension.BeforeAllCallback;
     13import org.junit.jupiter.api.extension.ExtendWith;
     14import org.junit.jupiter.api.extension.ExtensionContext;
     15import org.junit.platform.commons.support.AnnotationSupport;
     16import org.openstreetmap.josm.JOSMFixture;
     17import org.openstreetmap.josm.gui.MainApplication;
     18import org.openstreetmap.josm.testutils.JOSMTestRules;
     19import org.openstreetmap.josm.testutils.mockers.WindowlessMapViewStateMocker;
     20import org.openstreetmap.josm.testutils.mockers.WindowlessNavigatableComponentMocker;
     21
     22/**
     23 * Use the {@link MainApplication#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel}, global variables in this test.
     24 * @author Taylor Smock
     25 * @see JOSMTestRules#main()
     26 * @since xxx
     27 */
     28@Documented
     29@Retention(RUNTIME)
     30@Target(TYPE)
     31@ExtendWith(Main.MainExtension.class)
     32public @interface Main {
     33    /**
     34     * Get the class to use as the mocker for the map view
     35     * @return The mocker class for the map view
     36     */
     37    Class<?> mapViewStateMocker() default WindowlessMapViewStateMocker.class;
     38    /**
     39     * Get the class to use for the navigable component
     40     * @return The class to use for the navigable component.
     41     */
     42    Class<?> navigableComponentMocker() default WindowlessNavigatableComponentMocker.class;
     43
     44    /**
     45     * Initialize the MainApplication
     46     * @author Taylor Smock
     47     */
     48    class MainExtension implements BeforeAllCallback {
     49        @Override
     50        public void beforeAll(ExtensionContext context) throws Exception {
     51            Optional<Main> annotation = AnnotationSupport.findAnnotation(context.getElement(), Main.class);
     52            Class<?> mapViewStateMocker = null;
     53            Class<?> navigableComponentMocker = null;
     54            if (annotation.isPresent()) {
     55                mapViewStateMocker = annotation.get().mapViewStateMocker();
     56                navigableComponentMocker = annotation.get().navigableComponentMocker();
     57            }
     58
     59            // apply mockers to MapViewState and NavigableComponent whether we're headless or not
     60            // as we generally don't create the josm main window even in non-headless mode.
     61            if (mapViewStateMocker != null) {
     62                mapViewStateMocker.getConstructor().newInstance();
     63            }
     64            if (navigableComponentMocker != null) {
     65                navigableComponentMocker.getConstructor().newInstance();
     66            }
     67
     68            new MainApplication();
     69            JOSMFixture.initContentPane();
     70            JOSMFixture.initMainPanel(true);
     71            JOSMFixture.initToolbar();
     72            JOSMFixture.initMainMenu();
     73        }
     74    }
     75}
  • test/unit/org/openstreetmap/josm/testutils/annotations/OsmApiType.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import static java.lang.annotation.ElementType.METHOD;
     5import static java.lang.annotation.ElementType.TYPE;
     6import static java.lang.annotation.RetentionPolicy.RUNTIME;
     7
     8import java.lang.annotation.Documented;
     9import java.lang.annotation.Retention;
     10import java.lang.annotation.Target;
     11import java.util.Optional;
     12
     13import org.junit.jupiter.api.extension.BeforeAllCallback;
     14import org.junit.jupiter.api.extension.BeforeEachCallback;
     15import org.junit.jupiter.api.extension.ExtendWith;
     16import org.junit.jupiter.api.extension.ExtensionContext;
     17import org.junit.platform.commons.support.AnnotationSupport;
     18import org.openstreetmap.josm.io.OsmApi;
     19import org.openstreetmap.josm.spi.preferences.Config;
     20import org.openstreetmap.josm.testutils.FakeOsmApi;
     21import org.openstreetmap.josm.testutils.JOSMTestRules;
     22
     23/**
     24 * Specify the OSM API to use for the test. {@link APIType#NONE} has no effect.
     25 *
     26 * @author Taylor Smock
     27 * @see JOSMTestRules#devAPI()
     28 * @see JOSMTestRules#fakeAPI()
     29 * @since xxx
     30 */
     31@Documented
     32@Retention(RUNTIME)
     33@Target({ TYPE, METHOD })
     34@FullPreferences
     35@ExtendWith(OsmApiType.OsmApiTypeExtension.class)
     36public @interface OsmApiType {
     37    /**
     38     * API types to initialize
     39     * @author Taylor Smock
     40     *
     41     */
     42    enum APIType {
     43        NONE, FAKE, DEV;
     44    }
     45
     46    /**
     47     * The API type to use
     48     * @return The API type to use (default NONE)
     49     */
     50    APIType apiType() default APIType.NONE;
     51
     52    /**
     53     * Initialize the OSM api
     54     * @author Taylor Smock
     55     *
     56     */
     57    class OsmApiTypeExtension implements BeforeAllCallback, BeforeEachCallback {
     58
     59        @Override
     60        public void beforeEach(ExtensionContext context) throws Exception {
     61            Optional<OsmApiType> annotation = AnnotationSupport.findAnnotation(context.getElement(), OsmApiType.class);
     62            APIType useAPI = APIType.NONE;
     63            if (annotation.isPresent()) {
     64                useAPI = annotation.get().apiType();
     65            }
     66            // Set API
     67            if (useAPI == APIType.DEV) {
     68                Config.getPref().put("osm-server.url", "https://api06.dev.openstreetmap.org/api");
     69            } else if (useAPI == APIType.FAKE) {
     70                FakeOsmApi api = FakeOsmApi.getInstance();
     71                Config.getPref().put("osm-server.url", api.getServerUrl());
     72            }
     73
     74            // Initialize API
     75            if (useAPI != APIType.NONE) {
     76                OsmApi.getOsmApi().initialize(null);
     77            }
     78        }
     79
     80        @Override
     81        public void beforeAll(ExtensionContext context) throws Exception {
     82            beforeEach(context);
     83        }
     84
     85    }
     86}
  • test/unit/org/openstreetmap/josm/testutils/annotations/Presets.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import static java.lang.annotation.ElementType.METHOD;
     5import static java.lang.annotation.ElementType.TYPE;
     6import static java.lang.annotation.RetentionPolicy.RUNTIME;
     7
     8import java.lang.annotation.Documented;
     9import java.lang.annotation.Retention;
     10import java.lang.annotation.Target;
     11
     12import org.junit.jupiter.api.extension.BeforeAllCallback;
     13import org.junit.jupiter.api.extension.BeforeEachCallback;
     14import org.junit.jupiter.api.extension.ExtendWith;
     15import org.junit.jupiter.api.extension.ExtensionContext;
     16import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
     17import org.openstreetmap.josm.testutils.JOSMTestRules;
     18
     19/**
     20 * Use presets in this test.
     21 *
     22 * @author Taylor Smock
     23 * @see JOSMTestRules#presets()
     24 * @since xxx
     25 */
     26@Documented
     27@Retention(RUNTIME)
     28@Target({ TYPE, METHOD })
     29@ExtendWith(Presets.PresetsExtension.class)
     30public @interface Presets {
     31    /**
     32     * Initialize the presets
     33     * @author Taylor Smock
     34     *
     35     */
     36    class PresetsExtension implements BeforeAllCallback, BeforeEachCallback {
     37        @Override
     38        public void beforeAll(ExtensionContext context) throws Exception {
     39            TaggingPresets.readFromPreferences();
     40        }
     41
     42        @Override
     43        public void beforeEach(ExtensionContext context) throws Exception {
     44            // TODO only run if method level
     45            beforeAll(context);
     46        }
     47    }
     48}
  • test/unit/org/openstreetmap/josm/testutils/annotations/Projection.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.lang.annotation.Documented;
     5import java.lang.annotation.ElementType;
     6import java.lang.annotation.Retention;
     7import java.lang.annotation.RetentionPolicy;
     8import java.lang.annotation.Target;
     9import java.util.Optional;
     10
     11import org.junit.jupiter.api.extension.BeforeEachCallback;
     12import org.junit.jupiter.api.extension.ExtendWith;
     13import org.junit.jupiter.api.extension.ExtensionContext;
     14import org.junit.platform.commons.support.AnnotationSupport;
     15import org.openstreetmap.josm.data.projection.ProjectionRegistry;
     16import org.openstreetmap.josm.data.projection.Projections;
     17import org.openstreetmap.josm.testutils.JOSMTestRules;
     18
     19/**
     20 * Use projections in tests (Mercator).
     21 * @author Taylor Smock
     22 * @see JOSMTestRules#projection()
     23 *
     24 */
     25@Documented
     26@Retention(RetentionPolicy.RUNTIME)
     27@Target({ElementType.METHOD, ElementType.TYPE})
     28@ExtendWith(Projection.ProjectionExtension.class)
     29public @interface Projection {
     30    /**
     31     * The value to use for the projection. Defaults to EPSG:3857 (Mercator).
     32     * @return The value to use to get the projection from {@link Projections#getProjectionByCode}.
     33     */
     34    String projectionCode() default "EPSG:3857";
     35
     36    /**
     37     * Use projections in tests. Use {@link Projection} preferentially.
     38     * @author Taylor Smock
     39     *
     40     */
     41    class ProjectionExtension implements BeforeEachCallback {
     42        @Override
     43        public void beforeEach(ExtensionContext context) throws Exception {
     44            Optional<Projection> annotation = AnnotationSupport.findAnnotation(context.getElement(), Projection.class);
     45            if (annotation.isPresent()) {
     46                ProjectionRegistry.setProjection(Projections.getProjectionByCode(annotation.get().projectionCode()));
     47            } else {
     48                ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
     49            }
     50        }
     51
     52    }
     53}
  • test/unit/org/openstreetmap/josm/testutils/annotations/ProjectionNadGrids.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.lang.annotation.Documented;
     5import java.lang.annotation.ElementType;
     6import java.lang.annotation.Retention;
     7import java.lang.annotation.RetentionPolicy;
     8import java.lang.annotation.Target;
     9
     10import org.junit.jupiter.api.extension.BeforeAllCallback;
     11import org.junit.jupiter.api.extension.ExtendWith;
     12import org.junit.jupiter.api.extension.ExtensionContext;
     13import org.openstreetmap.josm.gui.MainApplication;
     14import org.openstreetmap.josm.testutils.JOSMTestRules;
     15
     16/**
     17 * Set up loading of NTV2 grit shift files to support projections that need them.
     18 * @author Taylor Smock
     19 * @see JOSMTestRules#projectionNadGrids()
     20 * @since xxx
     21 */
     22@Documented
     23@Retention(RetentionPolicy.RUNTIME)
     24@Target(ElementType.TYPE)
     25@ExtendWith(ProjectionNadGrids.ProjectionNadGridsExtension.class)
     26public @interface ProjectionNadGrids {
     27    /**
     28     * Set up loading of NTV2 grit shift files to support projections that need them.
     29     * Use {@link ProjectionNadGrids} instead.
     30     * @author Taylor Smock
     31     * @see JOSMTestRules#projectionNadGrids()
     32     */
     33    class ProjectionNadGridsExtension implements BeforeAllCallback {
     34        @Override
     35        public void beforeAll(ExtensionContext context) throws Exception {
     36            MainApplication.setupNadGridSources();
     37        }
     38    }
     39}
  • test/unit/org/openstreetmap/josm/testutils/annotations/Territories.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.testutils.annotations;
     3
     4import java.lang.annotation.Documented;
     5import java.lang.annotation.ElementType;
     6import java.lang.annotation.Retention;
     7import java.lang.annotation.RetentionPolicy;
     8import java.lang.annotation.Target;
     9
     10import org.junit.jupiter.api.extension.BeforeAllCallback;
     11import org.junit.jupiter.api.extension.ExtendWith;
     12import org.junit.jupiter.api.extension.ExtensionContext;
     13import org.openstreetmap.josm.testutils.JOSMTestRules;
     14
     15/**
     16 * Use boundaries dataset in this test.
     17 * @see JOSMTestRules#territories()
     18 * @author Taylor Smock
     19 * @since xxx
     20 */
     21@Documented
     22@Retention(RetentionPolicy.RUNTIME)
     23@Target(ElementType.TYPE)
     24@ExtendWith(Territories.TerritoriesExtension.class)
     25public @interface Territories {
     26    /**
     27     * Initialize boundaries prior to use
     28     * @author Taylor Smock
     29     *
     30     */
     31    class TerritoriesExtension implements BeforeAllCallback {
     32        @Override
     33        public void beforeAll(ExtensionContext arg0) throws Exception {
     34            org.openstreetmap.josm.tools.Territories.initializeInternalData();
     35        }
     36
     37    }
     38}