-
Notifications
You must be signed in to change notification settings - Fork 71
/
DefaultLocaleExtension.java
96 lines (81 loc) · 3.66 KB
/
DefaultLocaleExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright 2016-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junitpioneer.jupiter;
import java.util.Locale;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junitpioneer.internal.PioneerAnnotationUtils;
import org.junitpioneer.internal.PioneerUtils;
class DefaultLocaleExtension implements BeforeEachCallback, AfterEachCallback {
private static final Namespace NAMESPACE = Namespace.create(DefaultLocaleExtension.class);
private static final String KEY = "DefaultLocale";
@Override
public void beforeEach(ExtensionContext context) {
PioneerAnnotationUtils
.findClosestEnclosingAnnotation(context, DefaultLocale.class)
.ifPresent(annotation -> setDefaultLocale(context, annotation));
}
private void setDefaultLocale(ExtensionContext context, DefaultLocale annotation) {
Locale configuredLocale = createLocale(annotation);
// defer storing the current default locale until the new locale could be created from the configuration
// (this prevents cases where misconfigured extensions store default locale now and restore it later,
// which leads to race conditions in our tests)
storeDefaultLocale(context);
Locale.setDefault(configuredLocale);
}
private void storeDefaultLocale(ExtensionContext context) {
context.getStore(NAMESPACE).put(KEY, Locale.getDefault());
}
private static Locale createLocale(DefaultLocale annotation) {
if (!annotation.value().isEmpty()) {
return createFromLanguageTag(annotation);
} else {
return createFromParts(annotation);
}
}
private static Locale createFromLanguageTag(DefaultLocale annotation) {
if (!annotation.language().isEmpty() || !annotation.country().isEmpty() || !annotation.variant().isEmpty()) {
throw new ExtensionConfigurationException(
"@DefaultLocale can only be used with language tag if language, country, and variant are not set");
}
return Locale.forLanguageTag(annotation.value());
}
private static Locale createFromParts(DefaultLocale annotation) {
String language = annotation.language();
String country = annotation.country();
String variant = annotation.variant();
if (!language.isEmpty() && !country.isEmpty() && !variant.isEmpty()) {
return PioneerUtils.createLocale(language, country, variant);
} else if (!language.isEmpty() && !country.isEmpty()) {
return PioneerUtils.createLocale(language, country);
} else if (!language.isEmpty() && variant.isEmpty()) {
return PioneerUtils.createLocale(language);
} else {
throw new ExtensionConfigurationException(
"@DefaultLocale not configured correctly. When not using a language tag, specify either"
+ "language, or language and country, or language and country and variant.");
}
}
@Override
public void afterEach(ExtensionContext context) {
PioneerAnnotationUtils
.findClosestEnclosingAnnotation(context, DefaultLocale.class)
.ifPresent(__ -> resetDefaultLocale(context));
}
private void resetDefaultLocale(ExtensionContext context) {
Locale defaultLocale = context.getStore(NAMESPACE).get(KEY, Locale.class);
// default locale is null if the extension was misconfigured and execution failed in "before"
if (defaultLocale != null)
Locale.setDefault(defaultLocale);
}
}