source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/annotations/BasicPreferences.java@ 18037

Last change on this file since 18037 was 18037, checked in by Don-vip, 3 years ago

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
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.AfterAllCallback;
11import org.junit.jupiter.api.extension.AfterEachCallback;
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.junit.jupiter.api.extension.ExtensionContext.Namespace;
17import org.openstreetmap.josm.data.Preferences;
18import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
19import org.openstreetmap.josm.data.preferences.JosmUrls;
20import org.openstreetmap.josm.spi.preferences.Config;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22
23/**
24 * Allow tests to use JOSM preferences (see {@link JOSMTestRules#preferences()}).
25 * This is often enough for basic tests.
26 *
27 * @author Taylor Smock
28 * @see FullPreferences
29 * @since 18037
30 */
31@Documented
32@Retention(RetentionPolicy.RUNTIME)
33@Target({ElementType.TYPE, ElementType.METHOD})
34@ExtendWith(BasicPreferences.BasicPreferencesExtension.class)
35public @interface BasicPreferences {
36
37 /**
38 * Initialize basic preferences. This is often more than enough for basic tests.
39 * @author Taylor Smock
40 */
41 class BasicPreferencesExtension implements AfterAllCallback, AfterEachCallback, BeforeAllCallback, BeforeEachCallback {
42 @Override
43 public void afterAll(ExtensionContext context) throws Exception {
44 AnnotationUtils.resetStaticClass(Config.class);
45 }
46
47 @Override
48 public void afterEach(ExtensionContext context) throws Exception {
49 if (context.getElement().isPresent() && context.getElement().get().isAnnotationPresent(BasicPreferences.class)) {
50 this.afterAll(context);
51 }
52 }
53
54 @Override
55 public void beforeAll(ExtensionContext context) throws Exception {
56 Preferences pref = Preferences.main();
57 // Disable saving on put, just to avoid overwriting pref files
58 pref.enableSaveOnPut(false);
59 Config.setPreferencesInstance(pref);
60 Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
61 Config.setUrlsProvider(JosmUrls.getInstance());
62 // Force an invalid URL just to avoid accidents
63 Config.getPref().put("osm-server.url", "http://invalid");
64
65 // Store the pref for other extensions
66 context.getStore(Namespace.create(BasicPreferencesExtension.class)).put("preferences", pref);
67 }
68
69 @Override
70 public void beforeEach(ExtensionContext context) throws Exception {
71 if (AnnotationUtils.elementIsAnnotated(context.getElement(), BasicPreferences.class) || Config.getPref() == null) {
72 this.beforeAll(context);
73 }
74 }
75 }
76}
Note: See TracBrowser for help on using the repository browser.