From 04a8c6745ca54267cde8fd5ab5b72766ac94db55 Mon Sep 17 00:00:00 2001 From: Nicolas Roduit Date: Fri, 26 Jul 2024 19:13:13 +0200 Subject: [PATCH] Fix Locale and TimeZone in tests --- .../dcm4che3/img/util/DateTimeUtilsTest.java | 5 +++++ .../org/dcm4che3/img/util/DicomUtilsTest.java | 22 +++++-------------- .../weasis/dicom/ref/AnatomicBuilderTest.java | 22 +++++-------------- 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DateTimeUtilsTest.java b/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DateTimeUtilsTest.java index 3c4ead7..0720fe4 100644 --- a/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DateTimeUtilsTest.java +++ b/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DateTimeUtilsTest.java @@ -24,6 +24,7 @@ import java.util.TimeZone; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.DefaultTimeZone; class DateTimeUtilsTest { @Test @@ -60,6 +61,7 @@ void testGetDicomTime() { @Test @DisplayName("Convert Date to LocalDate") + @DefaultTimeZone("UTC") void testGetDateToLocalDate() { assertNull(DateTimeUtils.toLocalDate(null)); @@ -75,6 +77,7 @@ void testGetDateToLocalDate() { @Test @DisplayName("Convert Date to LocalTime") + @DefaultTimeZone("UTC") void testGetDateToLocalTime() { assertNull(DateTimeUtils.toLocalTime(null)); @@ -90,6 +93,7 @@ void testGetDateToLocalTime() { @Test @DisplayName("Convert date to LocalDateTime") + @DefaultTimeZone("UTC") void testGetDateToLocalDateTime() { assertNull(DateTimeUtils.toLocalDateTime(null)); @@ -105,6 +109,7 @@ void testGetDateToLocalDateTime() { @Test @DisplayName("Get DateTime") + @DefaultTimeZone("UTC") void testGetDateTime() { assertNull(DateTimeUtils.dateTime((LocalDate) null, null)); diff --git a/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DicomUtilsTest.java b/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DicomUtilsTest.java index ab5f23d..55e236c 100644 --- a/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DicomUtilsTest.java +++ b/weasis-dicom-tools/src/test/java/org/dcm4che3/img/util/DicomUtilsTest.java @@ -29,28 +29,14 @@ import org.dcm4che3.data.Attributes; import org.dcm4che3.data.Tag; import org.dcm4che3.data.VR; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.DefaultLocale; +import org.junitpioneer.jupiter.DefaultTimeZone; class DicomUtilsTest { - private Locale defaultLocale; - - @BeforeEach - public void setUp() { - // Save the default locale to restore it after the test - defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.US); - } - - @AfterEach - public void tearDown() { - Locale.setDefault(defaultLocale); - } - @Test @DisplayName("Get Period with different dates") void testGetPeriod() { @@ -158,6 +144,7 @@ void testIsNative() { @Test @DisplayName("Format text with different values and formats") + @DefaultLocale(language = "en", country = "US") void testGetFormattedText() { assertEquals("Value", DicomUtils.getFormattedText("Value", "Format")); assertEquals("", DicomUtils.getFormattedText(null, "foo")); @@ -208,6 +195,7 @@ void shouldFormatNonStringValue() { @Test @DisplayName("Format value with decimal and pattern") + @DefaultLocale(language = "en", country = "US") void shouldFormatValueWithDecimalAndPattern() { String result = DicomUtils.formatValue("1.2345", true, "$V:f$0.00$"); assertEquals("1.23", result); @@ -257,6 +245,7 @@ void testGetStringArrayFromDicomElement() { @Test @DisplayName("Get Date from Dicom Element") + @DefaultTimeZone("Europe/Paris") void testGetDateFromDicomElement() { TimeZone timeZone = TimeZone.getDefault(); Attributes attributes = new Attributes(); @@ -280,6 +269,7 @@ void testGetDateFromDicomElement() { @Test @DisplayName("Get date array from Dicom Element with default value") + @DefaultTimeZone("Europe/Paris") void testGetDatesFromDicomElement() { TimeZone timeZone = TimeZone.getDefault(); Attributes dicom = new Attributes(); diff --git a/weasis-dicom-tools/src/test/java/org/weasis/dicom/ref/AnatomicBuilderTest.java b/weasis-dicom-tools/src/test/java/org/weasis/dicom/ref/AnatomicBuilderTest.java index a5fedca..a036f9d 100644 --- a/weasis-dicom-tools/src/test/java/org/weasis/dicom/ref/AnatomicBuilderTest.java +++ b/weasis-dicom-tools/src/test/java/org/weasis/dicom/ref/AnatomicBuilderTest.java @@ -15,27 +15,12 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Locale; import java.util.Objects; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.DefaultLocale; import org.weasis.dicom.ref.AnatomicBuilder.Category; class AnatomicBuilderTest { - private Locale defaultLocale; - - @BeforeEach - public void setUp() { - // Save the default locale to restore it after the test - defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.FRANCE); - } - - @AfterEach - public void tearDown() { - Locale.setDefault(defaultLocale); - } @Test void getAllBodyParts_returnsAllBodyParts() { @@ -53,6 +38,7 @@ void getBodyPartsFromPredicate_returnsFilteredBodyParts() { } @Test + @DefaultLocale(language = "fr", country = "CH") void getBodyPartFromCode() { assertEquals( BodyPart.values()[0], BodyPart.getBodyPartFromCode(BodyPart.values()[0].getCodeValue())); @@ -70,6 +56,7 @@ void getBodyPartFromCode() { } @Test + @DefaultLocale(language = "fr", country = "CH") void getBodyPartFromLegacyCode() { assertEquals( BodyPart.values()[0], @@ -105,6 +92,7 @@ void getBodyPartFromCodeMeaning() { } @Test + @DefaultLocale(language = "fr", country = "CH") void getAnatomicModifierFromCode() { assertEquals( AnatomicModifier.values()[0], @@ -120,6 +108,7 @@ void getAnatomicModifierFromCode() { } @Test + @DefaultLocale(language = "fr", country = "CH") void getSurfacePartFromCode() { assertEquals( SurfacePart.values()[0], @@ -158,6 +147,7 @@ void getCodingScheme() { } @Test + @DefaultLocale(language = "fr", country = "CH") void category() { assertEquals("Sites anatomiques dermatologiques", Category.SURFACE.toString());