This is an automated email from the ASF dual-hosted git repository. gk pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/turbine-core.git
commit ba1d39cebd12d04289d3fde8198cc8a9ed12fc45 Author: Georg Kallidis <[email protected]> AuthorDate: Fri Nov 10 12:21:38 2023 +0100 Refactored and Fixed Date time formatter: extend with zoneId, use locale in tool, if available, add debug log in RundataLocalizationService --- conf/test/CompleteTurbineResources.properties | 17 ++++++++++++ conf/test/fulcrumRoleConfiguration.xml | 7 +++++ conf/test/log4j2-test.xml | 4 +++ .../localization/DateTimeFormatterInterface.java | 4 ++- .../localization/DateTimeFormatterService.java | 25 ++++++++--------- .../localization/RundataLocalizationService.java | 3 +++ .../services/pull/util/DateTimeFormatterTool.java | 21 ++++++++------- .../localization/DateTimeFormatterServiceTest.java | 31 +++++++++++++++++++--- 8 files changed, 86 insertions(+), 26 deletions(-) diff --git a/conf/test/CompleteTurbineResources.properties b/conf/test/CompleteTurbineResources.properties index 62269e40..c8cbc016 100644 --- a/conf/test/CompleteTurbineResources.properties +++ b/conf/test/CompleteTurbineResources.properties @@ -332,6 +332,23 @@ services.UIService.classname = org.apache.turbine.services.ui.TurbineUIService services.DateTimeFormatterService.classname= org.apache.turbine.services.localization.DateTimeFormatterService services.DateTimeFormatterService.earlyInit=true +#default is ZoneId.systemDefault() +datetime.zoneId=Europe/Berlin +#Australia/Sydney + +# default is MM/dd/yyyy +#datetime.format=dd.MM.yyyy + +# default is true +#datetime.use.turbine.locale=false + +# default is false +#tool.use.request.locale=true + +# default is en_US +#locale.default.language=de +#locale.default.country=DE + # Turn on the appropriate template service. services.VelocityService.classname=org.apache.turbine.services.velocity.TurbineVelocityService diff --git a/conf/test/fulcrumRoleConfiguration.xml b/conf/test/fulcrumRoleConfiguration.xml index cc8cfe0d..531c364c 100644 --- a/conf/test/fulcrumRoleConfiguration.xml +++ b/conf/test/fulcrumRoleConfiguration.xml @@ -47,6 +47,13 @@ name="org.apache.fulcrum.localization.LocalizationService" shorthand="localization" default-class="org.apache.fulcrum.localization.DefaultLocalizationService"/> + + <!-- + <role + name="org.apache.fulcrum.localization.LocalizationService" + shorthand="localization" + default-class="org.apache.turbine.services.localization.RundataLocalizationService"/> + --> <role name="org.apache.fulcrum.intake.IntakeService" diff --git a/conf/test/log4j2-test.xml b/conf/test/log4j2-test.xml index 8930a613..3e317772 100644 --- a/conf/test/log4j2-test.xml +++ b/conf/test/log4j2-test.xml @@ -34,6 +34,10 @@ <AppenderRef ref="logfile" /> <AppenderRef ref="console" level="info"/> </Logger> + <Logger name="org.apache.turbine.services" level="debug" additivity="false"> + <AppenderRef ref="logfile"/> + <AppenderRef ref="console" level="info"/> + </Logger> <Logger name="avalon" level="info" additivity="false"> <AppenderRef ref="console"/> <AppenderRef ref="logfile"/> diff --git a/src/java/org/apache/turbine/services/localization/DateTimeFormatterInterface.java b/src/java/org/apache/turbine/services/localization/DateTimeFormatterInterface.java index 8e1c8605..95b3efd1 100644 --- a/src/java/org/apache/turbine/services/localization/DateTimeFormatterInterface.java +++ b/src/java/org/apache/turbine/services/localization/DateTimeFormatterInterface.java @@ -24,7 +24,7 @@ public interface DateTimeFormatterInterface { DateTimeFormatter getDefaultFormat(); - String getDateTimeFormatPattern(); + String getFormatPattern(); /** * Formats the given datetime as a String with the #{@link DateTimeFormatterService#defaultFormat}. @@ -109,4 +109,6 @@ public interface DateTimeFormatterInterface { String mapFrom(String src, DateTimeFormatter incomingFormat); String map(String src, DateTimeFormatter outgoingFormat, Locale locale); + + ZoneId getZoneId(); } diff --git a/src/java/org/apache/turbine/services/localization/DateTimeFormatterService.java b/src/java/org/apache/turbine/services/localization/DateTimeFormatterService.java index 4abfcdfc..9733be0b 100644 --- a/src/java/org/apache/turbine/services/localization/DateTimeFormatterService.java +++ b/src/java/org/apache/turbine/services/localization/DateTimeFormatterService.java @@ -31,7 +31,7 @@ public class DateTimeFormatterService public static final String ROLE = DateTimeFormatterService.class.getName(); - private String dateTimeFormatPattern = null; + private String formatPattern = null; private DateTimeFormatter dateTimeFormat = null; @@ -51,8 +51,8 @@ public class DateTimeFormatterService } @Override - public String getDateTimeFormatPattern() { - return dateTimeFormatPattern; + public String getFormatPattern() { + return formatPattern; } private static final Logger log = LogManager.getLogger(DateTimeFormatterService.class); @@ -60,7 +60,7 @@ public class DateTimeFormatterService /** * Initialize the service. * - * the {@link #dateTimeFormat} from {@link #dateTimeFormatPattern} is initialized with + * the {@link #dateTimeFormat} from {@link #formatPattern} is initialized with * * <ol> * <li>{@link Locale}: {@link LocaleUtils#getDefaultLocale()} is used by default. @@ -74,7 +74,7 @@ public class DateTimeFormatterService @Override public void init() { - dateTimeFormatPattern = Turbine.getConfiguration() + formatPattern = Turbine.getConfiguration() .getString(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_DEFAULT); useTurbineLocale = Turbine.getConfiguration() @@ -91,19 +91,15 @@ public class DateTimeFormatterService ZoneId.systemDefault(); setZoneId(zoneId); - dateTimeFormat = DateTimeFormatter.ofPattern(dateTimeFormatPattern) + dateTimeFormat = DateTimeFormatter.ofPattern(formatPattern) .withLocale(locale).withZone(zoneId); log.info("Initialized DateTimeFormatterService with pattern {}, locale {} and zone {}", - dateTimeFormatPattern, dateTimeFormat.getLocale(), + formatPattern, dateTimeFormat.getLocale(), dateTimeFormat.getZone()); setInit(true); } - public ZoneId getZoneId() { - return zoneId; - } - @Override public <T extends TemporalAccessor> String format(T temporalAccessor) { @@ -171,7 +167,7 @@ public class DateTimeFormatterService } if (incomingFormatPattern == null) { - incomingFormatPattern = dateTimeFormatPattern; + incomingFormatPattern = formatPattern; } if (incomingFormatPattern.equals( outgoingFormatPattern )) { return ""; @@ -235,6 +231,11 @@ public class DateTimeFormatterService public void setLocale(Locale locale) { this.locale = locale; } + + @Override + public ZoneId getZoneId() { + return zoneId; + } public void setZoneId(ZoneId zoneId) { this.zoneId = zoneId; diff --git a/src/java/org/apache/turbine/services/localization/RundataLocalizationService.java b/src/java/org/apache/turbine/services/localization/RundataLocalizationService.java index 8743e88f..f174e11b 100644 --- a/src/java/org/apache/turbine/services/localization/RundataLocalizationService.java +++ b/src/java/org/apache/turbine/services/localization/RundataLocalizationService.java @@ -28,6 +28,9 @@ public class RundataLocalizationService extends DefaultLocalizationService imple @Override public Locale getLocale(RunData data) { User user = data.getUser(); + log.debug( "retrieving lang from req header :{}", + (user == null || user.getTemp("locale") == null ) ); + if (user == null) { return getLocale(data.getRequest().getHeader(LocalizationService.ACCEPT_LANGUAGE)); diff --git a/src/java/org/apache/turbine/services/pull/util/DateTimeFormatterTool.java b/src/java/org/apache/turbine/services/pull/util/DateTimeFormatterTool.java index 6ff87deb..1edb6c83 100644 --- a/src/java/org/apache/turbine/services/pull/util/DateTimeFormatterTool.java +++ b/src/java/org/apache/turbine/services/pull/util/DateTimeFormatterTool.java @@ -77,7 +77,7 @@ public class DateTimeFormatterTool extends DateFormatter * <li>For session and persistent tools data will be of type User</li> * </ul> * - * the {@link #defaultFormat} from {@link #dateTimeFormatPattern} + * the {@link #defaultFormat} from {@link #formatPattern} * with {@link DateTimeFormatterService#getLocale()} * and zoneId {@link DateTimeFormatterService#getZoneId()} is used. * @@ -107,10 +107,8 @@ public class DateTimeFormatterTool extends DateFormatter { // Pull necessary information out of RunData while we have // a reference to it. - locale = (localizationService instanceof RundataLocalizationService)? - ((RundataLocalizationService)localizationService).getLocale((RunData) data): - localizationService.getLocale(((RunData) data).getRequest()); - log.info("Override {} with request locale {}.", dtfs.getLocale(), locale); + locale = localizationService.getLocale(((RunData) data).getRequest()); + log.info("Override {} with request locale {} from {}", dtfs.getLocale(), locale, localizationService); } } @@ -138,8 +136,8 @@ public class DateTimeFormatterTool extends DateFormatter } @Override - public String getDateTimeFormatPattern() { - return getDtfs().getDateTimeFormatPattern(); + public String getFormatPattern() { + return getDtfs().getFormatPattern(); } /** @@ -152,13 +150,13 @@ public class DateTimeFormatterTool extends DateFormatter @Override public <T extends TemporalAccessor> String format(T temporalAccessor) { - return getDtfs().getDefaultFormat().format(temporalAccessor); + return getDtfs().format(temporalAccessor, getDtfs().getFormatPattern(), getLocale()); } @Override public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString) { - return getDtfs().format(temporalAccessor, dateFormatString); + return getDtfs().format(temporalAccessor, dateFormatString, getLocale()); } @Override @@ -212,4 +210,9 @@ public class DateTimeFormatterTool extends DateFormatter this.locale = locale; } + @Override + public ZoneId getZoneId() { + return (getDtfs()!= null)? getDtfs().getZoneId():ZoneId.systemDefault(); + } + } diff --git a/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java b/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java index 0f2e50aa..37105970 100644 --- a/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java +++ b/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java @@ -1,5 +1,7 @@ package org.apache.turbine.services.localization; +import static org.junit.Assert.assertTrue; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -131,7 +133,27 @@ public class DateTimeFormatterServiceTest extends BaseTestCase { dateTimeFormatterTool = (DateTimeFormatterTool) requestContext.get("dateTimeFormatter"); assertNotNull(dateTimeFormatterTool); + String resultFormat = dateTimeFormatterTool.format(Instant.now()); + + System.out.println("format Instant now in tool:"+resultFormat ); + assertTrue(resultFormat.length()>5); + + System.out.println("locale in tool:"+ dateTimeFormatterTool.getLocale()); + // tool.use.request.locale is by default false, tool will use service locale + assertTrue(dateTimeFormatterTool.getLocale() == null); + } + + // to test configuration + // datetime.zoneId + // and locale.default.language l.d.country + @Test + void testDefault() throws Exception { assertNotNull(df); + System.out.println("zoneid in service:"+ df.getZoneId().getId()); +// assertEquals("Europe/Berlin",df.getZoneId().getId()); + System.out.println("locale in service:"+ df.getLocale()); +// assertEquals("de_DE",df.getLocale().toString()); + } @Order(2) @@ -179,8 +201,8 @@ public class DateTimeFormatterServiceTest extends BaseTestCase { assertEquals(mmddyyyy, dateTime.format(ldt, "MM/dd/yyyy")); } - void formatZonedDateString(DateTimeFormatterInterface dateTime) { - ZonedDateTime zdt = ZonedDateTime.now(); + void formatZonedDateString(DateTimeFormatterInterface dateTime) { + ZonedDateTime zdt = ZonedDateTime.now(dateTime.getZoneId()); int day = zdt.get(ChronoField.DAY_OF_MONTH); int month = zdt.get(ChronoField.MONTH_OF_YEAR); // one based int year = zdt.get(ChronoField.YEAR); @@ -208,7 +230,8 @@ public class DateTimeFormatterServiceTest extends BaseTestCase { } void defaultMapFromInstant(DateTimeFormatterInterface dateTime) { - DateTimeFormatter incomingFormat = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.systemDefault()); + DateTimeFormatter incomingFormat = DateTimeFormatter.ISO_DATE_TIME + .withZone(dateTime.getZoneId()); // may throws an DateTimeParseException Instant now = Instant.now().truncatedTo(ChronoUnit.MINUTES); String source = incomingFormat.format(now); @@ -303,7 +326,7 @@ public class DateTimeFormatterServiceTest extends BaseTestCase { void formatInstantString(DateTimeFormatterInterface dateTime) { - ZonedDateTime zonedToday = ZonedDateTime.now(ZoneOffset.UTC.normalized()); + ZonedDateTime zonedToday = ZonedDateTime.now(dateTime.getZoneId()); int day = zonedToday.get(ChronoField.DAY_OF_MONTH); int month = zonedToday.get(ChronoField.MONTH_OF_YEAR); // one based int year = zonedToday.get(ChronoField.YEAR);
