Ignore:
Timestamp:
2023-03-16T22:09:01+01:00 (2 years ago)
Author:
taylor.smock
Message:

Fix #22381: Try to automatically install newly required plugins on plugin update

This additionally updates the plugin tests to JUnit 5 (see #16567). Some tests
required fixing (for example, MinimapDialogTest fails if the system of
measurement is metric).

Location:
trunk/test/unit/org/openstreetmap/josm/testutils
Files:
7 added
5 edited

Legend:

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

    r18690 r18694  
    1616import java.util.stream.StreamSupport;
    1717
    18 import org.junit.runner.Description;
    19 import org.junit.runners.model.Statement;
    20 import org.openstreetmap.josm.TestUtils;
    21 import org.openstreetmap.josm.tools.Logging;
    22 
    23 import com.github.tomakehurst.wiremock.WireMockServer;
    2418import com.github.tomakehurst.wiremock.client.MappingBuilder;
    2519import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
    2620import com.github.tomakehurst.wiremock.client.WireMock;
    2721import com.github.tomakehurst.wiremock.core.Options;
    28 import com.github.tomakehurst.wiremock.junit.WireMockRule;
     22import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
     23import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
     24import org.openstreetmap.josm.TestUtils;
     25import org.openstreetmap.josm.tools.Logging;
    2926
    3027public class PluginServer {
     
    138135        }
    139136
    140         public String getPluginURL(WireMockServer wireMockServer) {
     137        public String getPluginURL(WireMockRuntimeInfo wireMock) {
    141138            if (this.pluginURL != null) {
    142139                return this.pluginURL;
    143             } else if (wireMockServer != null && this.getJarPathBeneathFilesDir() != null) {
    144                 return wireMockServer.url(this.getPluginURLPath());
     140            } else if (wireMock != null && this.getJarPathBeneathFilesDir() != null) {
     141                return wireMock.getHttpBaseUrl() + this.getPluginURLPath();
    145142            }
    146143            return "http://example.com" + this.getPluginURLPath();
     
    156153        }
    157154
    158         public String getRemotePluginsListSection(WireMockServer wireMockServer) {
     155        public String getRemotePluginsListSection(WireMockRuntimeInfo wireMock) {
    159156            return String.format(
    160157                "%s.jar;%s\n%s",
    161158                this.getName(),
    162                 this.getPluginURL(wireMockServer),
     159                this.getPluginURL(wireMock),
    163160                this.getRemotePluginsListManifestSection()
    164161            );
     
    192189    }
    193190
    194     public void applyToWireMockServer(WireMockServer wireMockServer) {
     191    public void applyToWireMockServer(WireMockRuntimeInfo wireMock) {
     192        final WireMock wireMockServer = wireMock.getWireMock();
    195193        // first add the plugins list
    196         wireMockServer.stubFor(
     194        wireMockServer.register(
    197195            WireMock.get(WireMock.urlEqualTo("/plugins")).willReturn(
    198196                WireMock.aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody(
    199197                    this.pluginList.stream().map(
    200                         remotePlugin -> remotePlugin.getRemotePluginsListSection(wireMockServer)
     198                        remotePlugin -> remotePlugin.getRemotePluginsListSection(wireMock)
    201199                    ).collect(Collectors.joining())
    202200                )
     
    210208
    211209            if (mappingBuilder != null && responseDefinitionBuilder != null) {
    212                 wireMockServer.stubFor(
     210                wireMockServer.register(
    213211                    remotePlugin.getMappingBuilder().willReturn(remotePlugin.getResponseDefinitionBuilder())
    214212                );
     
    228226    }
    229227
    230     public class PluginServerRule extends WireMockRule {
     228    public class PluginServerRule extends WireMockExtension {
    231229        public PluginServerRule(Options ruleOptions, boolean failOnUnmatchedRequests) {
    232             super(ruleOptions, failOnUnmatchedRequests);
     230            super(extensionOptions().options(ruleOptions).failOnUnmatchedRequests(failOnUnmatchedRequests));
    233231        }
    234232
     
    238236
    239237        @Override
    240         public Statement apply(Statement base, Description description) {
    241             return super.apply(new Statement() {
    242                 @Override
    243                 public void evaluate() throws Throwable {
    244                     PluginServer.this.applyToWireMockServer(PluginServerRule.this);
    245                     base.evaluate();
    246                 }
    247             }, description);
     238        protected void onBeforeEach(WireMockRuntimeInfo wireMockRuntimeInfo) {
     239            PluginServer.this.applyToWireMockServer(wireMockRuntimeInfo);
    248240        }
    249241    }
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/BasicPreferences.java

    r18491 r18694  
    44import java.lang.annotation.Documented;
    55import java.lang.annotation.ElementType;
     6import java.lang.annotation.Inherited;
    67import java.lang.annotation.Retention;
    78import java.lang.annotation.RetentionPolicy;
    89import java.lang.annotation.Target;
     10import java.util.Map;
    911
    1012import org.junit.jupiter.api.extension.AfterAllCallback;
     
    1517import org.junit.jupiter.api.extension.ExtensionContext;
    1618import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
     19import org.junit.platform.commons.support.AnnotationSupport;
     20import org.openstreetmap.josm.TestUtils;
    1721import org.openstreetmap.josm.data.Preferences;
    1822import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
    1923import org.openstreetmap.josm.data.preferences.JosmUrls;
    2024import org.openstreetmap.josm.spi.preferences.Config;
     25import org.openstreetmap.josm.spi.preferences.Setting;
    2126import org.openstreetmap.josm.testutils.JOSMTestRules;
    2227
     
    3237@Retention(RetentionPolicy.RUNTIME)
    3338@Target({ElementType.TYPE, ElementType.METHOD})
     39@Inherited
    3440@ExtendWith(BasicPreferences.BasicPreferencesExtension.class)
    3541public @interface BasicPreferences {
     
    4753        @Override
    4854        public void afterEach(ExtensionContext context) throws Exception {
    49             if (context.getElement().isPresent() && context.getElement().get().isAnnotationPresent(BasicPreferences.class)) {
     55            if (AnnotationSupport.isAnnotated(context.getElement(), BasicPreferences.class)) {
    5056                this.afterAll(context);
    5157            }
     
    5763            // Disable saving on put, just to avoid overwriting pref files
    5864            pref.enableSaveOnPut(false);
    59             pref.resetToDefault();
     65            pref.resetToInitialState();
     66            pref.enableSaveOnPut(false);
     67            @SuppressWarnings("unchecked")
     68            final Map<String, Setting<?>> defaultsMap = (Map<String, Setting<?>>) TestUtils.getPrivateField(pref, "defaultsMap");
     69            defaultsMap.clear();
    6070            Config.setPreferencesInstance(pref);
    6171            Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
     
    7080        @Override
    7181        public void beforeEach(ExtensionContext context) throws Exception {
    72             if (AnnotationUtils.elementIsAnnotated(context.getElement(), BasicPreferences.class) || Config.getPref() == null) {
     82            if (AnnotationSupport.isAnnotated(context.getElement(), BasicPreferences.class) || Config.getPref() == null) {
    7383                this.beforeAll(context);
    7484            }
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/FullPreferences.java

    r18037 r18694  
    77import java.lang.annotation.RetentionPolicy;
    88import java.lang.annotation.Target;
    9 import java.util.Map;
    109
    11 import org.junit.jupiter.api.extension.BeforeAllCallback;
    1210import org.junit.jupiter.api.extension.BeforeEachCallback;
    1311import org.junit.jupiter.api.extension.ExtendWith;
    1412import org.junit.jupiter.api.extension.ExtensionContext;
    1513import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
    16 import org.openstreetmap.josm.TestUtils;
    1714import org.openstreetmap.josm.data.Preferences;
     15import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
    1816import org.openstreetmap.josm.spi.preferences.Config;
    19 import org.openstreetmap.josm.spi.preferences.Setting;
    2017import org.openstreetmap.josm.testutils.JOSMTestRules;
    2118import org.openstreetmap.josm.testutils.annotations.BasicPreferences.BasicPreferencesExtension;
     
    3734     * Initialize preferences.
    3835     */
    39     class UsePreferencesExtension implements BeforeAllCallback, BeforeEachCallback {
     36    class UsePreferencesExtension implements BeforeEachCallback {
    4037        @Override
    41         public void beforeAll(ExtensionContext context) throws Exception {
     38        public void beforeEach(ExtensionContext context) throws Exception {
    4239            Preferences pref = context.getStore(Namespace.create(BasicPreferencesExtension.class)).get("preferences", Preferences.class);
    43             @SuppressWarnings("unchecked")
    44             final Map<String, Setting<?>> defaultsMap = (Map<String, Setting<?>>) TestUtils.getPrivateField(pref, "defaultsMap");
    45             defaultsMap.clear();
     40            if (pref.getDirs() instanceof JosmBaseDirectories) {
     41                ((JosmBaseDirectories) pref.getDirs()).clearMemos();
     42            }
     43            pref.enableSaveOnPut(false);
    4644            pref.resetToInitialState();
    4745            pref.enableSaveOnPut(false);
     
    5048            Config.getPref().put("osm-server.url", "http://invalid");
    5149        }
    52 
    53         @Override
    54         public void beforeEach(ExtensionContext context) throws Exception {
    55             if (AnnotationUtils.elementIsAnnotated(context.getElement(), FullPreferences.class)) {
    56                 this.beforeAll(context);
    57             }
    58         }
    5950    }
    6051}
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java

    r18037 r18694  
    77import java.lang.annotation.RetentionPolicy;
    88import java.lang.annotation.Target;
     9import java.util.Locale;
    910
    1011import org.junit.jupiter.api.extension.AfterEachCallback;
     
    1314import org.junit.jupiter.api.extension.ExtensionContext;
    1415import org.junit.platform.commons.support.AnnotationSupport;
     16import org.openstreetmap.josm.tools.LanguageInfo;
    1517
    1618/**
     
    4143        public void beforeEach(ExtensionContext context) {
    4244            String language = AnnotationSupport.findAnnotation(context.getElement(), I18n.class).map(I18n::value).orElse("en");
    43             org.openstreetmap.josm.tools.I18n.set(language);
     45            if (!Locale.getDefault().equals(LanguageInfo.getLocale(language, false))) {
     46                org.openstreetmap.josm.tools.I18n.set(language);
     47            }
    4448        }
    4549
    4650        @Override
    4751        public void afterEach(ExtensionContext context) {
    48             org.openstreetmap.josm.tools.I18n.set("en");
    49             org.openstreetmap.josm.tools.I18n.set(org.openstreetmap.josm.tools.I18n.getOriginalLocale().getLanguage());
     52            if (!Locale.ENGLISH.equals(Locale.getDefault())) {
     53                org.openstreetmap.josm.tools.I18n.set("en");
     54                org.openstreetmap.josm.tools.I18n.set(org.openstreetmap.josm.tools.I18n.getOriginalLocale().getLanguage());
     55                Locale.setDefault(Locale.ENGLISH);
     56            }
    5057        }
    5158    }
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/JosmHome.java

    r18037 r18694  
    1616import java.util.UUID;
    1717
    18 import org.junit.jupiter.api.extension.AfterAllCallback;
    19 import org.junit.jupiter.api.extension.BeforeAllCallback;
     18import org.junit.jupiter.api.extension.AfterEachCallback;
     19import org.junit.jupiter.api.extension.BeforeEachCallback;
    2020import org.junit.jupiter.api.extension.ExtendWith;
    2121import org.junit.jupiter.api.extension.ExtensionContext;
     
    4040     * @author Taylor Smock
    4141     */
    42     class JosmHomeExtension implements BeforeAllCallback, AfterAllCallback {
     42    class JosmHomeExtension implements BeforeEachCallback, AfterEachCallback {
    4343        @Override
    44         public void afterAll(ExtensionContext context) throws Exception {
     44        public void afterEach(ExtensionContext context) throws Exception {
    4545            Path tempDir = context.getStore(Namespace.create(JosmHome.class)).get("home", Path.class);
    4646            Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
     
    5757                }
    5858            });
     59            System.clearProperty("josm.home");
    5960        }
    6061
    6162        @Override
    62         public void beforeAll(ExtensionContext context) throws Exception {
     63        public void beforeEach(ExtensionContext context) throws Exception {
    6364            Path tempDir = Files.createTempDirectory(UUID.randomUUID().toString());
    6465            context.getStore(Namespace.create(JosmHome.class)).put("home", tempDir);
Note: See TracChangeset for help on using the changeset viewer.