Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.classtemplate.extended;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

class DateFormatter {

public String format(LocalDate date) {
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
.withLocale(Locale.getDefault());

return date.format(formatter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.classtemplate.extended;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

import org.junit.jupiter.api.ClassTemplate;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ClassTemplate
@ExtendWith(DateLocaleClassTemplateProvider.class)
class DateFormatterLocaleUnitTest {

private static final Logger LOG = LoggerFactory.getLogger(DateFormatterLocaleUnitTest.class);

private final DateFormatter formatter = new DateFormatter();

@Test
void givenDefaultLocale_whenFormattingDate_thenMatchesLocalizedOutput() {
LocalDate date = LocalDate.of(2025, 9, 30);

DateTimeFormatter expectedFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
.withLocale(Locale.getDefault());

String expected = date.format(expectedFormatter);
String formatted = formatter.format(date);

LOG.info("Locale: {}, Expected: {}, Formatted: {}", Locale.getDefault(), expected, formatted);

assertEquals(expected, formatted);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.classtemplate.extended;

import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;

import org.junit.jupiter.api.extension.ClassTemplateInvocationContext;
import org.junit.jupiter.api.extension.ClassTemplateInvocationContextProvider;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;

class DateLocaleClassTemplateProvider implements ClassTemplateInvocationContextProvider {

@Override
public boolean supportsClassTemplate(ExtensionContext context) {
return true;
}

@Override
public Stream<ClassTemplateInvocationContext> provideClassTemplateInvocationContexts(ExtensionContext context) {
return Stream.of(invocationContext(Locale.US), invocationContext(Locale.GERMANY), invocationContext(Locale.ITALY), invocationContext(Locale.JAPAN));
}

private ClassTemplateInvocationContext invocationContext(Locale locale) {
return new ClassTemplateInvocationContext() {

@Override
public String getDisplayName(int invocationIndex) {
return "Locale: " + locale.getDisplayName();
}

@Override
public List<Extension> getAdditionalExtensions() {
return List.of(new LocaleExtension(locale));
}
};
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baeldung.classtemplate.extended;

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.Locale;

class LocaleExtension implements BeforeEachCallback, AfterEachCallback {

private final Locale locale;
private Locale previous;

public LocaleExtension(Locale locale) {
this.locale = locale;
}

@Override
public void beforeEach(ExtensionContext context) {
previous = Locale.getDefault();
Locale.setDefault(locale);
}

@Override
public void afterEach(ExtensionContext context) {
Locale.setDefault(previous);
}
}