Changeset 18694 in josm for trunk/test/unit/org/openstreetmap/josm/testutils
- Timestamp:
- 2023-03-16T22:09:01+01:00 (2 years ago)
- 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 16 16 import java.util.stream.StreamSupport; 17 17 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;24 18 import com.github.tomakehurst.wiremock.client.MappingBuilder; 25 19 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; 26 20 import com.github.tomakehurst.wiremock.client.WireMock; 27 21 import com.github.tomakehurst.wiremock.core.Options; 28 import com.github.tomakehurst.wiremock.junit.WireMockRule; 22 import com.github.tomakehurst.wiremock.junit5.WireMockExtension; 23 import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; 24 import org.openstreetmap.josm.TestUtils; 25 import org.openstreetmap.josm.tools.Logging; 29 26 30 27 public class PluginServer { … … 138 135 } 139 136 140 public String getPluginURL(WireMock ServerwireMockServer) {137 public String getPluginURL(WireMockRuntimeInfo wireMock) { 141 138 if (this.pluginURL != null) { 142 139 return this.pluginURL; 143 } else if (wireMock Server!= null && this.getJarPathBeneathFilesDir() != null) {144 return wireMock Server.url(this.getPluginURLPath());140 } else if (wireMock != null && this.getJarPathBeneathFilesDir() != null) { 141 return wireMock.getHttpBaseUrl() + this.getPluginURLPath(); 145 142 } 146 143 return "http://example.com" + this.getPluginURLPath(); … … 156 153 } 157 154 158 public String getRemotePluginsListSection(WireMock ServerwireMockServer) {155 public String getRemotePluginsListSection(WireMockRuntimeInfo wireMock) { 159 156 return String.format( 160 157 "%s.jar;%s\n%s", 161 158 this.getName(), 162 this.getPluginURL(wireMock Server),159 this.getPluginURL(wireMock), 163 160 this.getRemotePluginsListManifestSection() 164 161 ); … … 192 189 } 193 190 194 public void applyToWireMockServer(WireMockServer wireMockServer) { 191 public void applyToWireMockServer(WireMockRuntimeInfo wireMock) { 192 final WireMock wireMockServer = wireMock.getWireMock(); 195 193 // first add the plugins list 196 wireMockServer. stubFor(194 wireMockServer.register( 197 195 WireMock.get(WireMock.urlEqualTo("/plugins")).willReturn( 198 196 WireMock.aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody( 199 197 this.pluginList.stream().map( 200 remotePlugin -> remotePlugin.getRemotePluginsListSection(wireMock Server)198 remotePlugin -> remotePlugin.getRemotePluginsListSection(wireMock) 201 199 ).collect(Collectors.joining()) 202 200 ) … … 210 208 211 209 if (mappingBuilder != null && responseDefinitionBuilder != null) { 212 wireMockServer. stubFor(210 wireMockServer.register( 213 211 remotePlugin.getMappingBuilder().willReturn(remotePlugin.getResponseDefinitionBuilder()) 214 212 ); … … 228 226 } 229 227 230 public class PluginServerRule extends WireMock Rule{228 public class PluginServerRule extends WireMockExtension { 231 229 public PluginServerRule(Options ruleOptions, boolean failOnUnmatchedRequests) { 232 super(ruleOptions ,failOnUnmatchedRequests);230 super(extensionOptions().options(ruleOptions).failOnUnmatchedRequests(failOnUnmatchedRequests)); 233 231 } 234 232 … … 238 236 239 237 @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); 248 240 } 249 241 } -
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/BasicPreferences.java
r18491 r18694 4 4 import java.lang.annotation.Documented; 5 5 import java.lang.annotation.ElementType; 6 import java.lang.annotation.Inherited; 6 7 import java.lang.annotation.Retention; 7 8 import java.lang.annotation.RetentionPolicy; 8 9 import java.lang.annotation.Target; 10 import java.util.Map; 9 11 10 12 import org.junit.jupiter.api.extension.AfterAllCallback; … … 15 17 import org.junit.jupiter.api.extension.ExtensionContext; 16 18 import org.junit.jupiter.api.extension.ExtensionContext.Namespace; 19 import org.junit.platform.commons.support.AnnotationSupport; 20 import org.openstreetmap.josm.TestUtils; 17 21 import org.openstreetmap.josm.data.Preferences; 18 22 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 19 23 import org.openstreetmap.josm.data.preferences.JosmUrls; 20 24 import org.openstreetmap.josm.spi.preferences.Config; 25 import org.openstreetmap.josm.spi.preferences.Setting; 21 26 import org.openstreetmap.josm.testutils.JOSMTestRules; 22 27 … … 32 37 @Retention(RetentionPolicy.RUNTIME) 33 38 @Target({ElementType.TYPE, ElementType.METHOD}) 39 @Inherited 34 40 @ExtendWith(BasicPreferences.BasicPreferencesExtension.class) 35 41 public @interface BasicPreferences { … … 47 53 @Override 48 54 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)) { 50 56 this.afterAll(context); 51 57 } … … 57 63 // Disable saving on put, just to avoid overwriting pref files 58 64 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(); 60 70 Config.setPreferencesInstance(pref); 61 71 Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance()); … … 70 80 @Override 71 81 public void beforeEach(ExtensionContext context) throws Exception { 72 if (Annotation Utils.elementIsAnnotated(context.getElement(), BasicPreferences.class) || Config.getPref() == null) {82 if (AnnotationSupport.isAnnotated(context.getElement(), BasicPreferences.class) || Config.getPref() == null) { 73 83 this.beforeAll(context); 74 84 } -
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/FullPreferences.java
r18037 r18694 7 7 import java.lang.annotation.RetentionPolicy; 8 8 import java.lang.annotation.Target; 9 import java.util.Map;10 9 11 import org.junit.jupiter.api.extension.BeforeAllCallback;12 10 import org.junit.jupiter.api.extension.BeforeEachCallback; 13 11 import org.junit.jupiter.api.extension.ExtendWith; 14 12 import org.junit.jupiter.api.extension.ExtensionContext; 15 13 import org.junit.jupiter.api.extension.ExtensionContext.Namespace; 16 import org.openstreetmap.josm.TestUtils;17 14 import org.openstreetmap.josm.data.Preferences; 15 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 18 16 import org.openstreetmap.josm.spi.preferences.Config; 19 import org.openstreetmap.josm.spi.preferences.Setting;20 17 import org.openstreetmap.josm.testutils.JOSMTestRules; 21 18 import org.openstreetmap.josm.testutils.annotations.BasicPreferences.BasicPreferencesExtension; … … 37 34 * Initialize preferences. 38 35 */ 39 class UsePreferencesExtension implements Before AllCallback, BeforeEachCallback {36 class UsePreferencesExtension implements BeforeEachCallback { 40 37 @Override 41 public void before All(ExtensionContext context) throws Exception {38 public void beforeEach(ExtensionContext context) throws Exception { 42 39 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); 46 44 pref.resetToInitialState(); 47 45 pref.enableSaveOnPut(false); … … 50 48 Config.getPref().put("osm-server.url", "http://invalid"); 51 49 } 52 53 @Override54 public void beforeEach(ExtensionContext context) throws Exception {55 if (AnnotationUtils.elementIsAnnotated(context.getElement(), FullPreferences.class)) {56 this.beforeAll(context);57 }58 }59 50 } 60 51 } -
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java
r18037 r18694 7 7 import java.lang.annotation.RetentionPolicy; 8 8 import java.lang.annotation.Target; 9 import java.util.Locale; 9 10 10 11 import org.junit.jupiter.api.extension.AfterEachCallback; … … 13 14 import org.junit.jupiter.api.extension.ExtensionContext; 14 15 import org.junit.platform.commons.support.AnnotationSupport; 16 import org.openstreetmap.josm.tools.LanguageInfo; 15 17 16 18 /** … … 41 43 public void beforeEach(ExtensionContext context) { 42 44 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 } 44 48 } 45 49 46 50 @Override 47 51 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 } 50 57 } 51 58 } -
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/JosmHome.java
r18037 r18694 16 16 import java.util.UUID; 17 17 18 import org.junit.jupiter.api.extension.After AllCallback;19 import org.junit.jupiter.api.extension.Before AllCallback;18 import org.junit.jupiter.api.extension.AfterEachCallback; 19 import org.junit.jupiter.api.extension.BeforeEachCallback; 20 20 import org.junit.jupiter.api.extension.ExtendWith; 21 21 import org.junit.jupiter.api.extension.ExtensionContext; … … 40 40 * @author Taylor Smock 41 41 */ 42 class JosmHomeExtension implements Before AllCallback, AfterAllCallback {42 class JosmHomeExtension implements BeforeEachCallback, AfterEachCallback { 43 43 @Override 44 public void after All(ExtensionContext context) throws Exception {44 public void afterEach(ExtensionContext context) throws Exception { 45 45 Path tempDir = context.getStore(Namespace.create(JosmHome.class)).get("home", Path.class); 46 46 Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() { … … 57 57 } 58 58 }); 59 System.clearProperty("josm.home"); 59 60 } 60 61 61 62 @Override 62 public void before All(ExtensionContext context) throws Exception {63 public void beforeEach(ExtensionContext context) throws Exception { 63 64 Path tempDir = Files.createTempDirectory(UUID.randomUUID().toString()); 64 65 context.getStore(Namespace.create(JosmHome.class)).put("home", tempDir);
Note:
See TracChangeset
for help on using the changeset viewer.