Changeset 19117 in josm
- Timestamp:
- 2024-06-19T16:35:52+02:00 (5 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/FakeImagery.java
r18972 r19117 15 15 import java.util.List; 16 16 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;21 17 import org.junit.jupiter.api.extension.ExtendWith; 22 18 import 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;26 19 import org.openstreetmap.josm.data.imagery.ImageryInfo; 27 20 import org.openstreetmap.josm.data.imagery.ImageryLayerInfo; … … 46 39 @Target(TYPE) 47 40 @BasicPreferences 48 @ExtendWith(FakeImagery.FakeImagery Extension.class)41 @ExtendWith(FakeImagery.FakeImageryWireMockExtension.class) 49 42 public @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 @Override61 public void afterAll(ExtensionContext extensionContext) throws Exception {62 getActualExtension(extensionContext).afterAll(extensionContext);63 }64 65 @Override66 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 @Override73 public void beforeAll(ExtensionContext extensionContext) throws Exception {74 getActualExtension(extensionContext).beforeAll(extensionContext);75 }76 77 @Override78 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 @Override85 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 @Override94 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 116 43 /** 117 44 * A wiremock extension for fake imagery … … 179 106 } 180 107 108 @Override 181 109 protected void onBeforeEach(ExtensionContext extensionContext, WireMockRuntimeInfo wireMockRuntimeInfo) { 182 110 super.onBeforeEach(wireMockRuntimeInfo); … … 188 116 } 189 117 118 @Override 190 119 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 } 194 126 } 195 127 … … 198 130 try { 199 131 @SuppressWarnings("unchecked") 200 List< SlippyMapBBoxChooser.TileSourceProvider> slippyMapProviders =201 (List< SlippyMapBBoxChooser.TileSourceProvider>) getPrivateStaticField(132 List<JosmMapViewer.TileSourceProvider> slippyMapProviders = 133 (List<JosmMapViewer.TileSourceProvider>) getPrivateStaticField( 202 134 SlippyMapBBoxChooser.class, 203 135 "providers" 204 136 ); 205 137 // 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); 207 139 store.put("slippyMapProviders", slippyMapProviders); 208 140 store.put("slippyMapDefaultProvider", slippyMapDefaultProvider); 209 141 } catch (ReflectiveOperationException e) { 210 142 Logging.warn("Failed to remove default SlippyMapBBoxChooser TileSourceProvider"); 143 Logging.trace(e); 211 144 } 212 145 } 213 146 214 147 if (this.clearLayerList) { 215 store.put("originalImageryInfoList", ImageryLayerInfo.instance.getLayers());148 store.put("originalImageryInfoList", List.copyOf(ImageryLayerInfo.instance.getLayers())); 216 149 ImageryLayerInfo.instance.clear(); 217 150 } … … 223 156 } 224 157 225 private void unregisterLayers(ExtensionContext.Store store) {158 private static void unregisterLayers(ExtensionContext.Store store) { 226 159 @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 = 230 162 store.get("slippyMapDefaultProvider", JosmMapViewer.TileSourceProvider.class); 231 163 @SuppressWarnings("unchecked") 232 List<ImageryInfo> originalImageryInfoList = (List<ImageryInfo>)store.get("originalImageryInfoList", List.class);164 List<ImageryInfo> originalImageryInfoList = store.get("originalImageryInfoList", List.class); 233 165 // clean up to original state 234 166 if (slippyMapDefaultProvider != null && slippyMapProviders != null) {
Note:
See TracChangeset
for help on using the changeset viewer.