Changeset 19117 in josm


Ignore:
Timestamp:
2024-06-19T16:35:52+02:00 (5 months ago)
Author:
taylor.smock
Message:

Fix #23743: MinimapDialogTest was failing

This does two things:

  1. Remove hack that is no longer necessary with wiremock 3.6.0
  2. Create a copy of the layer list
File:
1 edited

Legend:

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

    r18972 r19117  
    1515import java.util.List;
    1616
    17 import org.junit.jupiter.api.extension.AfterAllCallback;
    18 import org.junit.jupiter.api.extension.AfterEachCallback;
    19 import org.junit.jupiter.api.extension.BeforeAllCallback;
    20 import org.junit.jupiter.api.extension.BeforeEachCallback;
    2117import org.junit.jupiter.api.extension.ExtendWith;
    2218import org.junit.jupiter.api.extension.ExtensionContext;
    23 import org.junit.jupiter.api.extension.ParameterContext;
    24 import org.junit.jupiter.api.extension.ParameterResolutionException;
    25 import org.junit.jupiter.api.extension.ParameterResolver;
    2619import org.openstreetmap.josm.data.imagery.ImageryInfo;
    2720import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
     
    4639@Target(TYPE)
    4740@BasicPreferences
    48 @ExtendWith(FakeImagery.FakeImageryExtension.class)
     41@ExtendWith(FakeImagery.FakeImageryWireMockExtension.class)
    4942public @interface FakeImagery {
    50     /**
    51      * This is a stop-gap for <a href="https://github.com/wiremock/wiremock/pull/1981">WireMock #1981</a>.
    52      * We just wrap everything.
    53      */
    54     class FakeImageryExtension implements ParameterResolver,
    55             BeforeEachCallback,
    56             BeforeAllCallback,
    57             AfterEachCallback,
    58             AfterAllCallback {
    59 
    60         @Override
    61         public void afterAll(ExtensionContext extensionContext) throws Exception {
    62             getActualExtension(extensionContext).afterAll(extensionContext);
    63         }
    64 
    65         @Override
    66         public void afterEach(ExtensionContext extensionContext) throws Exception {
    67             final FakeImageryWireMockExtension extension = getActualExtension(extensionContext);
    68             extension.afterEach(extensionContext);
    69             extension.onAfterEach(extensionContext, getWireMockRuntimeInfo(extensionContext));
    70         }
    71 
    72         @Override
    73         public void beforeAll(ExtensionContext extensionContext) throws Exception {
    74             getActualExtension(extensionContext).beforeAll(extensionContext);
    75         }
    76 
    77         @Override
    78         public void beforeEach(ExtensionContext extensionContext) throws Exception {
    79             final FakeImageryWireMockExtension extension = getActualExtension(extensionContext);
    80             extension.beforeEach(extensionContext);
    81             extension.onBeforeEach(extensionContext, getWireMockRuntimeInfo(extensionContext));
    82         }
    83 
    84         @Override
    85         public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
    86                 throws ParameterResolutionException {
    87             if (parameterContext.getParameter().getType().equals(FakeImageryWireMockExtension.class)) {
    88                 return true;
    89             }
    90             return getActualExtension(extensionContext).supportsParameter(parameterContext, extensionContext);
    91         }
    92 
    93         @Override
    94         public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
    95                 throws ParameterResolutionException {
    96             if (parameterContext.getParameter().getType().equals(FakeImageryWireMockExtension.class)) {
    97                 return getActualExtension(extensionContext);
    98             }
    99             return getActualExtension(extensionContext).resolveParameter(parameterContext, extensionContext);
    100         }
    101 
    102         private static FakeImageryWireMockExtension getActualExtension(ExtensionContext extensionContext) {
    103             return FakeImageryWireMockExtension.getStore(extensionContext)
    104                     .getOrComputeIfAbsent(FakeImageryWireMockExtension.class, ignored -> new FakeImageryWireMockExtension(),
    105                             FakeImageryWireMockExtension.class);
    106         }
    107 
    108         private static WireMockRuntimeInfo getWireMockRuntimeInfo(ExtensionContext extensionContext) {
    109             return FakeImageryWireMockExtension.getStore(extensionContext)
    110                     .getOrComputeIfAbsent(WireMockRuntimeInfo.class, ignored -> getActualExtension(extensionContext).getRuntimeInfo(),
    111                             WireMockRuntimeInfo.class);
    112 
    113         }
    114     }
    115 
    11643    /**
    11744     * A wiremock extension for fake imagery
     
    179106        }
    180107
     108        @Override
    181109        protected void onBeforeEach(ExtensionContext extensionContext, WireMockRuntimeInfo wireMockRuntimeInfo) {
    182110            super.onBeforeEach(wireMockRuntimeInfo);
     
    188116        }
    189117
     118        @Override
    190119        protected void onAfterEach(ExtensionContext extensionContext, WireMockRuntimeInfo wireMockRuntimeInfo) {
    191             super.onAfterEach(wireMockRuntimeInfo);
    192             final ExtensionContext.Store store = getStore(extensionContext);
    193             unregisterLayers(store);
     120            try {
     121                super.onAfterEach(wireMockRuntimeInfo);
     122            } finally {
     123                final ExtensionContext.Store store = getStore(extensionContext);
     124                unregisterLayers(store);
     125            }
    194126        }
    195127
     
    198130                try {
    199131                    @SuppressWarnings("unchecked")
    200                     List<SlippyMapBBoxChooser.TileSourceProvider> slippyMapProviders =
    201                             (List<SlippyMapBBoxChooser.TileSourceProvider>) getPrivateStaticField(
     132                    List<JosmMapViewer.TileSourceProvider> slippyMapProviders =
     133                            (List<JosmMapViewer.TileSourceProvider>) getPrivateStaticField(
    202134                                    SlippyMapBBoxChooser.class,
    203135                                    "providers"
    204136                            );
    205137                    // pop this off the beginning of the list, keep for later
    206                     SlippyMapBBoxChooser.TileSourceProvider slippyMapDefaultProvider = slippyMapProviders.remove(0);
     138                    JosmMapViewer.TileSourceProvider slippyMapDefaultProvider = slippyMapProviders.remove(0);
    207139                    store.put("slippyMapProviders", slippyMapProviders);
    208140                    store.put("slippyMapDefaultProvider", slippyMapDefaultProvider);
    209141                } catch (ReflectiveOperationException e) {
    210142                    Logging.warn("Failed to remove default SlippyMapBBoxChooser TileSourceProvider");
     143                    Logging.trace(e);
    211144                }
    212145            }
    213146
    214147            if (this.clearLayerList) {
    215                 store.put("originalImageryInfoList", ImageryLayerInfo.instance.getLayers());
     148                store.put("originalImageryInfoList", List.copyOf(ImageryLayerInfo.instance.getLayers()));
    216149                ImageryLayerInfo.instance.clear();
    217150            }
     
    223156        }
    224157
    225         private void unregisterLayers(ExtensionContext.Store store) {
     158        private static void unregisterLayers(ExtensionContext.Store store) {
    226159            @SuppressWarnings("unchecked")
    227             final List<SlippyMapBBoxChooser.TileSourceProvider> slippyMapProviders =
    228                     (List<SlippyMapBBoxChooser.TileSourceProvider>) store.get("slippyMapProviders", List.class);
    229             SlippyMapBBoxChooser.TileSourceProvider slippyMapDefaultProvider =
     160            final List<JosmMapViewer.TileSourceProvider> slippyMapProviders = store.get("slippyMapProviders", List.class);
     161            JosmMapViewer.TileSourceProvider slippyMapDefaultProvider =
    230162                    store.get("slippyMapDefaultProvider", JosmMapViewer.TileSourceProvider.class);
    231163            @SuppressWarnings("unchecked")
    232             List<ImageryInfo> originalImageryInfoList = (List<ImageryInfo>) store.get("originalImageryInfoList", List.class);
     164            List<ImageryInfo> originalImageryInfoList = store.get("originalImageryInfoList", List.class);
    233165            // clean up to original state
    234166            if (slippyMapDefaultProvider != null && slippyMapProviders != null) {
Note: See TracChangeset for help on using the changeset viewer.