|
10 | 10 |
|
11 | 11 | package org.junit.jupiter.engine.extension; |
12 | 12 |
|
13 | | -import java.lang.reflect.AnnotatedElement; |
14 | 13 | import java.util.Locale; |
| 14 | +import java.util.Optional; |
15 | 15 |
|
16 | 16 | import org.junit.jupiter.api.extension.AfterEachCallback; |
| 17 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
17 | 18 | import org.junit.jupiter.api.extension.BeforeEachCallback; |
18 | 19 | import org.junit.jupiter.api.extension.ExtensionConfigurationException; |
19 | 20 | import org.junit.jupiter.api.extension.ExtensionContext; |
|
25 | 26 | import org.junit.platform.commons.support.AnnotationSupport; |
26 | 27 | import org.junit.platform.commons.support.ReflectionSupport; |
27 | 28 |
|
28 | | -class DefaultLocaleExtension implements BeforeEachCallback, AfterEachCallback { |
| 29 | +class DefaultLocaleExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback { |
29 | 30 |
|
30 | 31 | private static final Namespace NAMESPACE = Namespace.create(DefaultLocaleExtension.class); |
31 | 32 |
|
32 | | - private static final String KEY = "DefaultLocale"; |
| 33 | + private static final String CUSTOM_KEY = "CustomLocale"; |
| 34 | + private static final String DEFAULT_KEY = "DefaultLocale"; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void beforeAll(ExtensionContext context) throws Exception { |
| 38 | + createLocaleFromAnnotation(context) // |
| 39 | + .ifPresent(locale -> store(context, CUSTOM_KEY, locale)); |
| 40 | + } |
33 | 41 |
|
34 | 42 | @Override |
35 | 43 | public void beforeEach(ExtensionContext context) { |
36 | | - AnnotatedElement element = context.getElement().orElse(null); |
37 | | - AnnotationSupport.findAnnotation(element, DefaultLocale.class).ifPresent( |
38 | | - annotation -> setDefaultLocale(context, annotation)); |
| 44 | + createLocaleFromAnnotation(context) // |
| 45 | + .or(() -> load(context, CUSTOM_KEY)) // |
| 46 | + .ifPresent(locale -> setDefaultLocale(context, locale)); |
39 | 47 | } |
40 | 48 |
|
41 | | - private void setDefaultLocale(ExtensionContext context, DefaultLocale annotation) { |
42 | | - Locale configuredLocale = createLocale(annotation); |
43 | | - // defer storing the current default locale until the new locale could be created from the configuration |
44 | | - // (this prevents cases where misconfigured extensions store default locale now and restore it later, |
45 | | - // which leads to race conditions in our tests) |
46 | | - storeDefaultLocale(context); |
47 | | - Locale.setDefault(configuredLocale); |
| 49 | + private void setDefaultLocale(ExtensionContext context, Locale customLocale) { |
| 50 | + store(context, DEFAULT_KEY, Locale.getDefault()); |
| 51 | + Locale.setDefault(customLocale); |
48 | 52 | } |
49 | 53 |
|
50 | | - private void storeDefaultLocale(ExtensionContext context) { |
51 | | - context.getStore(NAMESPACE).put(KEY, Locale.getDefault()); |
| 54 | + private static Optional<Locale> createLocaleFromAnnotation(ExtensionContext context) { |
| 55 | + return AnnotationSupport.findAnnotation(context.getElement(), DefaultLocale.class) // |
| 56 | + .map(DefaultLocaleExtension::createLocale); |
52 | 57 | } |
53 | 58 |
|
54 | 59 | private static Locale createLocale(DefaultLocale annotation) { |
@@ -108,23 +113,33 @@ private static Locale getFromProvider(DefaultLocale annotation) { |
108 | 113 | throw new ExtensionConfigurationException( |
109 | 114 | "LocaleProvider instance could not be constructed because of an exception", exception); |
110 | 115 | } |
| 116 | + return invoke(provider); |
| 117 | + } |
| 118 | + |
| 119 | + @SuppressWarnings("ConstantValue") |
| 120 | + private static Locale invoke(LocaleProvider provider) { |
111 | 121 | var locale = provider.get(); |
112 | | - if (locale == null) |
113 | | - throw new NullPointerException("LocaleProvider instance returned with null"); |
| 122 | + if (locale == null) { |
| 123 | + throw new ExtensionConfigurationException("LocaleProvider instance returned with null"); |
| 124 | + } |
114 | 125 | return locale; |
115 | 126 | } |
116 | 127 |
|
117 | 128 | @Override |
118 | 129 | public void afterEach(ExtensionContext context) { |
119 | | - AnnotatedElement element = context.getElement().orElse(null); |
120 | | - AnnotationSupport.findAnnotation(element, DefaultLocale.class).ifPresent(__ -> resetDefaultLocale(context)); |
| 130 | + load(context, DEFAULT_KEY).ifPresent(Locale::setDefault); |
| 131 | + } |
| 132 | + |
| 133 | + private static void store(ExtensionContext context, String key, Locale value) { |
| 134 | + getStore(context).put(key, value); |
| 135 | + } |
| 136 | + |
| 137 | + private static Optional<Locale> load(ExtensionContext context, String key) { |
| 138 | + return Optional.ofNullable(getStore(context).get(key, Locale.class)); |
121 | 139 | } |
122 | 140 |
|
123 | | - private void resetDefaultLocale(ExtensionContext context) { |
124 | | - Locale defaultLocale = context.getStore(NAMESPACE).get(KEY, Locale.class); |
125 | | - // default locale is null if the extension was misconfigured and execution failed in "before" |
126 | | - if (defaultLocale != null) |
127 | | - Locale.setDefault(defaultLocale); |
| 141 | + private static ExtensionContext.Store getStore(ExtensionContext context) { |
| 142 | + return context.getStore(NAMESPACE); |
128 | 143 | } |
129 | 144 |
|
130 | 145 | } |
0 commit comments