1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.testutils.annotations;
|
---|
3 |
|
---|
4 | import java.lang.annotation.Documented;
|
---|
5 | import java.lang.annotation.ElementType;
|
---|
6 | import java.lang.annotation.Retention;
|
---|
7 | import java.lang.annotation.RetentionPolicy;
|
---|
8 | import java.lang.annotation.Target;
|
---|
9 |
|
---|
10 | import org.junit.jupiter.api.extension.AfterAllCallback;
|
---|
11 | import org.junit.jupiter.api.extension.AfterEachCallback;
|
---|
12 | import org.junit.jupiter.api.extension.BeforeAllCallback;
|
---|
13 | import org.junit.jupiter.api.extension.BeforeEachCallback;
|
---|
14 | import org.junit.jupiter.api.extension.ExtendWith;
|
---|
15 | import org.junit.jupiter.api.extension.ExtensionContext;
|
---|
16 | import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
|
---|
17 | import org.openstreetmap.josm.data.Preferences;
|
---|
18 | import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
|
---|
19 | import org.openstreetmap.josm.data.preferences.JosmUrls;
|
---|
20 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
21 | import 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)
|
---|
35 | public @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 | }
|
---|