From 32e51966ddb2eb71858b0880525e52c6f0b66d22 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Tue, 7 Sep 2021 18:02:21 +0200 Subject: [PATCH 1/2] Fix tests on non English systems See gh-27887 --- ...ceMessageInterpolatorIntegrationTests.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java index 34b0b4d4b2..0bef3a0c9e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java @@ -25,6 +25,8 @@ import javax.validation.ConstraintViolation; import javax.validation.Validator; import javax.validation.constraints.NotNull; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.context.i18n.LocaleContextHolder; @@ -41,8 +43,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; */ class MessageSourceMessageInterpolatorIntegrationTests { - private final Validator validator = buildValidator(); - @NotNull private String defaultMessage; @@ -70,6 +70,22 @@ class MessageSourceMessageInterpolatorIntegrationTests { @NotNull(message = "\\\\{null}") private String escapeEscape; + private Locale defaultLocale; + + private Validator validator; + + @BeforeEach + void init() { + this.defaultLocale = Locale.getDefault(); + Locale.setDefault(Locale.ENGLISH); + this.validator = buildValidator(); + } + + @AfterEach + void restoreLocale() { + Locale.setDefault(this.defaultLocale); + } + @Test void defaultMessage() { assertThat(validate("defaultMessage")).containsExactly("must not be null"); From e10ebb17e0ec757dd29d78c5ebb6c01086e3d840 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 8 Sep 2021 08:07:09 +0200 Subject: [PATCH 2/2] Polish "Fix tests on non English systems" See gh-27887 --- ...ceMessageInterpolatorIntegrationTests.java | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java index 0bef3a0c9e..9d0809e03e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorIntegrationTests.java @@ -20,13 +20,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.function.Supplier; import javax.validation.ConstraintViolation; import javax.validation.Validator; import javax.validation.constraints.NotNull; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.context.i18n.LocaleContextHolder; @@ -70,22 +69,6 @@ class MessageSourceMessageInterpolatorIntegrationTests { @NotNull(message = "\\\\{null}") private String escapeEscape; - private Locale defaultLocale; - - private Validator validator; - - @BeforeEach - void init() { - this.defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.ENGLISH); - this.validator = buildValidator(); - } - - @AfterEach - void restoreLocale() { - Locale.setDefault(this.defaultLocale); - } - @Test void defaultMessage() { assertThat(validate("defaultMessage")).containsExactly("must not be null"); @@ -133,12 +116,15 @@ class MessageSourceMessageInterpolatorIntegrationTests { } private List validate(String property) { - List messages = new ArrayList<>(); - Set> constraints = this.validator.validateProperty(this, property); - for (ConstraintViolation constraint : constraints) { - messages.add(constraint.getMessage()); - } - return messages; + return withEnglishLocale(() -> { + Validator validator = buildValidator(); + List messages = new ArrayList<>(); + Set> constraints = validator.validateProperty(this, property); + for (ConstraintViolation constraint : constraints) { + messages.add(constraint.getMessage()); + } + return messages; + }); } private static Validator buildValidator() { @@ -156,4 +142,15 @@ class MessageSourceMessageInterpolatorIntegrationTests { } } + private static T withEnglishLocale(Supplier supplier) { + Locale defaultLocale = Locale.getDefault(); + try { + Locale.setDefault(Locale.ENGLISH); + return supplier.get(); + } + finally { + Locale.setDefault(defaultLocale); + } + } + }