Skip to content

Commit

Permalink
add test for checking date pattern in translations
Browse files Browse the repository at this point in the history
(cherry picked from commit 6ad56f3)
  • Loading branch information
cbellone committed Jan 9, 2020
1 parent 031b7ec commit 44a9b30
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/main/java/alfio/controller/support/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,51 @@
import alfio.model.ContentLanguage;
import alfio.model.Event;
import lombok.experimental.UtilityClass;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.MessageSource;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

@UtilityClass
@Log4j2
public class Formatters {


public static Map<String, String> getFormattedDate(Event event, ZonedDateTime date, String code, MessageSource messageSource) {
return getFormattedDate(event.getContentLanguages(), date, code, messageSource);
}

private static Map<String, String> getFormattedDate(List<ContentLanguage> languages, ZonedDateTime date, String code, MessageSource messageSource) {
Map<String, String> formatted = new HashMap<>();
languages.forEach(cl -> {
var pattern = messageSource.getMessage(code, null, cl.getLocale());
formatted.put(cl.getLanguage(), DateTimeFormatter.ofPattern(pattern, cl.getLocale()).format(date));
});
languages.forEach(cl -> formatDateForLocale(date, code, messageSource, formatted::put, cl, false));
return formatted;
}

static void formatDateForLocale(ZonedDateTime date,
String code,
MessageSource messageSource,
BiConsumer<String, String> storeFunction,
ContentLanguage cl,
boolean notifyError) {
String pattern = null;
try {
pattern = messageSource.getMessage(code, null, cl.getLocale());
storeFunction.accept(cl.getLanguage(), DateTimeFormatter.ofPattern(pattern, cl.getLocale()).format(date));
} catch (RuntimeException e) {
String message = "cannot parse pattern "+code+" ("+pattern+") for language "+ cl.getLanguage();
if(notifyError) {
throw new RuntimeException(message, e);
} else {
log.warn(message, e);
}
}
}

public static FormattedEventDates getFormattedDates(Event e, MessageSource messageSource, List<ContentLanguage> contentLanguages) {
return new FormattedEventDates(
getFormattedDate(contentLanguages, e.getBegin(), "common.event.date-format", messageSource),
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/alfio/controller/support/FormattersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.controller.support;

import alfio.model.ContentLanguage;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

/**
* This test checks if all the date patterns for all defined languages are correct, in order to spot errors at build
* time.
*/
class FormattersTest {

private static final List<String> FORMATTER_CODES = List.of(
"common.event.date-format",
"datetime.pattern",
"common.ticket-category.date-format"
);

@Test
void getFormattedDates() {
var messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("alfio.i18n.public");
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
ContentLanguage.ALL_LANGUAGES.forEach(cl ->
FORMATTER_CODES.forEach(code -> Formatters.formatDateForLocale(now, code, messageSource, (a, b) -> {}, cl, true))
);
}
}

0 comments on commit 44a9b30

Please sign in to comment.