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 97ccf4b0680b23fc5536f707777bd6e6a43b7d66 Author: Georg Kallidis <[email protected]> AuthorDate: Mon Dec 6 14:27:38 2021 +0100 allow element type method in marker interface turbineServies, set datetime formatter tool as facade for service, delete duplicate test --- .../apache/turbine/annotation/TurbineService.java | 2 +- .../localization/DateTimeFormatterInterface.java | 83 ++++++++ .../localization/DateTimeFormatterService.java | 177 +++++++++++++++++ .../services/pull/util/DateTimeFormatterTool.java | 157 +++++---------- .../annotation/AnnotationProcessorTest.java | 4 - .../turbine/services/LoadingComponentsTest.java | 12 ++ .../services/pull/util/DateTimeFormatterTest.java | 215 --------------------- 7 files changed, 321 insertions(+), 329 deletions(-) diff --git a/src/java/org/apache/turbine/annotation/TurbineService.java b/src/java/org/apache/turbine/annotation/TurbineService.java index 5a4d59a..453d8b2 100644 --- a/src/java/org/apache/turbine/annotation/TurbineService.java +++ b/src/java/org/apache/turbine/annotation/TurbineService.java @@ -29,7 +29,7 @@ import java.lang.annotation.Target; * Annotation to mark fields in modules that require a service to be injected */ @Retention( RetentionPolicy.RUNTIME ) -@Target( ElementType.FIELD ) +@Target( {ElementType.FIELD, ElementType.METHOD} ) public @interface TurbineService { /** diff --git a/src/java/org/apache/turbine/services/localization/DateTimeFormatterInterface.java b/src/java/org/apache/turbine/services/localization/DateTimeFormatterInterface.java new file mode 100644 index 0000000..7978484 --- /dev/null +++ b/src/java/org/apache/turbine/services/localization/DateTimeFormatterInterface.java @@ -0,0 +1,83 @@ +package org.apache.turbine.services.localization; + +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; +import java.util.Locale; + +public interface DateTimeFormatterInterface { + DateTimeFormatter getDefaultFormat(); + + String getDateTimeFormatPattern(); + + /** + * Formats the given datetime as a String with the #{@link DateTimeFormatterService#defaultFormat}. + * using the default date format. + * + * @param the {@link TemporalAccessor to format + * @return String value of the date + */ + <T extends TemporalAccessor> String format(T temporalAccessor); + + /** + * Formats the given date as a String. + * + * @param the TimeDate date to format + * @param dateFormatString format string to use. See {@link DateTimeFormatter} + * for details. + * @return String value of the date + */ + <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString); + + /** + * Formats the given date as a String. + * + * @param the TimeDate date to format + * @param dateFormatString format string to use. See {@link DateTimeFormatter} + * for details. + * @param locale + * @return String value of the date + */ + <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale); + + /** + * Maps from an incoming format to an outgoing format {@link DateTimeFormatter}. + * @param src the formatted datetime + * @param outgoingFormat {@link DateTimeFormatter} + * @param locale Locale, if needed for outgoing formatting, no default. + * @param incomingFormat {@link DateTimeFormatter}, optional, default is {@link #defaultFormat}. + * @return the newly mapped + */ + String map(String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern); + + /** + * Uses as incoming format {@link #defaultFormat} and no locale. + * @param src + * @param outgoingFormat + * @return the formatted string + * + * @throws java.time.temporal.UnsupportedTemporalTypeException + */ + String map(String src, DateTimeFormatter outgoingFormat, Locale locale, + DateTimeFormatter incomingFormat); + + /** + * Uses as outgoing {@link DateTimeFormatter} {@link #defaultFormat} and no locale. + * @param src the datetime formatted string + * @param incomingFormat the format of this string + * @return the date time formatted using the {@link #defaultFormat}. + */ + String mapTo(String src, DateTimeFormatter outgoingFormat); + + /** + * Uses as incoming {@link DateTimeFormatter} {@link #defaultFormat}. + * @param src the datetime formatted string + * @param outgoingFormat the format to which this string should be formatted. + * @param locale + * @return the newly formatted date time string + * + * @throws java.time.temporal.UnsupportedTemporalTypeException + */ + String mapFrom(String src, DateTimeFormatter incomingFormat); + + String map(String src, DateTimeFormatter outgoingFormat, Locale locale); +} diff --git a/src/java/org/apache/turbine/services/localization/DateTimeFormatterService.java b/src/java/org/apache/turbine/services/localization/DateTimeFormatterService.java new file mode 100644 index 0000000..7d27064 --- /dev/null +++ b/src/java/org/apache/turbine/services/localization/DateTimeFormatterService.java @@ -0,0 +1,177 @@ +package org.apache.turbine.services.localization; + + +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.turbine.Turbine; +import org.apache.turbine.services.TurbineBaseService; + +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalAccessor; +import java.util.Locale; + +/** + * This service is used to format {@link TemporalAccessor} and + * {@link #map(String, DateTimeFormatter, Locale)} (different falvors) + * objects into strings. + * + * The methods may throw {@link java.time.temporal.UnsupportedTemporalTypeException} or + * {@link DateTimeParseException}. + * if the source and the target format do not match appropriately. + * + */ +public class DateTimeFormatterService + extends TurbineBaseService implements DateTimeFormatterInterface { + + public static String SERVICE_NAME = "DateTimeFormatterService"; + + public static String ROLE = DateTimeFormatterService.class.getName(); + + /** Default date format. find supporrted formats in {@link DateTimeFormatterService} */ + private static final String DATE_TIME_FORMAT_DEFAULT = "MM/dd/yyyy"; + + /** + * Property tag for the date format that is to be used for the web + * application. + */ + private static final String DATE_TIME_FORMAT_KEY = "tool.datetimeTool.format"; + + private String dateTimeFormatPattern = null; + + private DateTimeFormatter defaultFormat = null; + + @Override + public DateTimeFormatter getDefaultFormat() + { + return defaultFormat; + } + + @Override + public String getDateTimeFormatPattern() { + return dateTimeFormatPattern; + } + + private static final Logger log = LogManager.getLogger(DateTimeFormatterService.class); + + /** + * Initialize the service. + * + * the {@link #defaultFormat} from {@link #dateTimeFormatPattern} is initialized with + * the default Locale {@link Locale#getDefault()} and default zone: {@link ZoneId#systemDefault()}. + * + */ + @Override + public void init() + { + dateTimeFormatPattern = Turbine.getConfiguration() + .getString(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_DEFAULT); + defaultFormat = DateTimeFormatter.ofPattern(dateTimeFormatPattern) + .withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault()); + + log.info("Initialized DateTimeFormatterService with pattern {}, locale {} and zone {}", + dateTimeFormatPattern, defaultFormat.getLocale(), defaultFormat.getZone()); + setInit(true); + } + + @Override + public <T extends TemporalAccessor> String format(T temporalAccessor) + { + return defaultFormat.format(temporalAccessor); + } + + @Override + public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString) + { + return format(temporalAccessor, dateFormatString, null); + } + + @Override + public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale) + { + String result = null; + + if (StringUtils.isEmpty(dateFormatString) || temporalAccessor == null) + { + result = ""; + } + else + { + DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormatString); + if (locale != null) + { + dtf.withLocale(locale); + } + result = dtf.format(temporalAccessor); + } + return result; + } + + @Override + public String map(String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern) + { + if (StringUtils.isEmpty(src) || outgoingFormatPattern == null) + { + return ""; + } + if (incomingFormatPattern == null) + { + incomingFormatPattern = dateTimeFormatPattern; + } + if (incomingFormatPattern.equals( outgoingFormatPattern )) { + return ""; + } + DateTimeFormatter incomingFormat = DateTimeFormatter.ofPattern(incomingFormatPattern); + DateTimeFormatter outgoingFormat = DateTimeFormatter.ofPattern(outgoingFormatPattern); + if (locale != null) + { + outgoingFormat = outgoingFormat.withLocale( locale ); + //incomingFormat = incomingFormat.withLocale( locale ); + } + return map( src, outgoingFormat, locale, incomingFormat ); + } + + @Override + public String map(String src, DateTimeFormatter outgoingFormat, Locale locale, + DateTimeFormatter incomingFormat) + { + if (StringUtils.isEmpty(src) || outgoingFormat == null) + { + return ""; + } + if (incomingFormat == null) + { + incomingFormat = defaultFormat; + } + if (incomingFormat.equals( outgoingFormat )) { + return ""; + } + if (locale != null) + { + outgoingFormat = outgoingFormat.withLocale( locale ); + //incomingFormat = incomingFormat.withLocale( locale ); + } + return outgoingFormat.format( + incomingFormat.parse( src )); + } + + @Override + public String mapTo(String src, DateTimeFormatter outgoingFormat) + { + return map( src, outgoingFormat, null, defaultFormat ); + } + + @Override + public String mapFrom(String src, DateTimeFormatter incomingFormat) + { + return map( src, defaultFormat, null, incomingFormat ); + } + + @Override + public String map(String src, DateTimeFormatter outgoingFormat, Locale locale) + { + return map( src, outgoingFormat, locale, defaultFormat ); + } +} 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 118f662..b9387cb 100644 --- a/src/java/org/apache/turbine/services/pull/util/DateTimeFormatterTool.java +++ b/src/java/org/apache/turbine/services/pull/util/DateTimeFormatterTool.java @@ -1,18 +1,22 @@ package org.apache.turbine.services.pull.util; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.turbine.annotation.TurbineService; +import org.apache.turbine.services.ServiceManager; +import org.apache.turbine.services.TurbineServices; +import org.apache.turbine.services.localization.DateTimeFormatterInterface; +import org.apache.turbine.services.localization.DateTimeFormatterService; +import org.apache.turbine.services.pull.ApplicationTool; + import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.TemporalAccessor; import java.util.Locale; -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.turbine.Turbine; -import org.apache.turbine.services.pull.ApplicationTool; - /** * This pull tool is used to format {@link TemporalAccessor} and * {@link #map(String, DateTimeFormatter, Locale)} (different falvors) @@ -24,25 +28,11 @@ import org.apache.turbine.services.pull.ApplicationTool; * */ public class DateTimeFormatterTool - implements ApplicationTool + implements ApplicationTool, DateTimeFormatterInterface { - /** Default date format. find supporrted formats in {@link DateTimeFormatterTool} */ - private static final String DATE_TIME_FORMAT_DEFAULT = "MM/dd/yyyy"; - /** - * Property tag for the date format that is to be used for the web - * application. - */ - private static final String DATE_TIME_FORMAT_KEY = "tool.datetimeTool.format"; - - private String dateFormat = null; - - private java.time.format.DateTimeFormatter defaultFormat = null; - - public java.time.format.DateTimeFormatter getDefaultFormat() - { - return defaultFormat; - } + @TurbineService + private DateTimeFormatterService dtfs; private static final Logger log = LogManager.getLogger(DateTimeFormatterTool.class); @@ -55,7 +45,7 @@ public class DateTimeFormatterTool * <li>For session and persistent tools data will be of type User</li> * </ul> * - * the {@link #defaultFormat} from {@link #dateFormat} with default Locale {@link Locale#getDefault()} and + * the {@link #defaultFormat} from {@link #dateTimeFormatPattern} with default Locale {@link Locale#getDefault()} and * Default zone: {@link ZoneId#systemDefault()} * * @param data initialization data @@ -63,13 +53,18 @@ public class DateTimeFormatterTool @Override public void init(Object data) { - dateFormat = Turbine.getConfiguration() - .getString(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_DEFAULT); - defaultFormat = DateTimeFormatter.ofPattern(dateFormat) - .withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault()); + log.info("Initialized DateTimeFormatterTool with service {}", + dtfs); + if (dtfs == null) + { + ServiceManager serviceManager = TurbineServices.getInstance(); + dtfs = (DateTimeFormatterService)serviceManager.getService(DateTimeFormatterService.SERVICE_NAME); + } + // dtfs should be already initialized + } - log.info("Initialized DateTimeFormatterTool with pattern {}, locale {} and zone {}", - dateFormat, defaultFormat.getLocale(), defaultFormat.getZone()); + public DateTimeFormatterService getDtfs() { + return dtfs; } /** @@ -84,6 +79,15 @@ public class DateTimeFormatterTool { // empty } + + public DateTimeFormatter getDefaultFormat() + { + return getDtfs().getDefaultFormat(); + } + + public String getDateTimeFormatPattern() { + return getDtfs().getDateTimeFormatPattern(); + } /** * Formats the given datetime as a String with the #{@link DateTimeFormatterTool#defaultFormat}. @@ -94,107 +98,42 @@ public class DateTimeFormatterTool */ public <T extends TemporalAccessor> String format(T temporalAccessor) { - return defaultFormat.format(temporalAccessor); + return getDtfs().getDefaultFormat().format(temporalAccessor); } public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString) { - return format(temporalAccessor, dateFormatString, null); + return getDtfs().format(temporalAccessor, dateFormatString, null); } - /** - * Formats the given date as a String. - * - * @param the TimeDate date to format - * @param dateFormatString format string to use. See java.text.SimpleDateFormat - * for details. - * @return String value of the date - */ + public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale) { - String result = null; - - if (StringUtils.isEmpty(dateFormatString) || temporalAccessor == null) - { - result = ""; - } - else - { - DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormatString); - if (locale != null) - { - dtf.withLocale(locale); - } - result = dtf.format(temporalAccessor); - } - return result; + return getDtfs().format(temporalAccessor, dateFormatString, locale); } - /** - * Maps from an incoming format to an outgoing format {@link java.time.format.DateTimeFormatter}. - * @param src the formatted datetime - * @param outgoingFormat {@link java.time.format.DateTimeFormatter} - * @param locale Locale, if needed for outgoing formatting, no default. - * @param incomingFormat {@link java.time.format.DateTimeFormatter}, optional, default is {@link #defaultFormat}. - * @return the newly mapped - */ + public String map( String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern) + { + return getDtfs().map(src, outgoingFormatPattern, locale, incomingFormatPattern); + } + public String map( String src, java.time.format.DateTimeFormatter outgoingFormat, Locale locale, java.time.format.DateTimeFormatter incomingFormat) { - if (StringUtils.isEmpty(src) || outgoingFormat == null) - { - return ""; - } - if (incomingFormat == null) - { - incomingFormat = defaultFormat; - } - if (incomingFormat.equals( outgoingFormat )) { - return ""; - } - if (locale != null) - { - outgoingFormat = outgoingFormat.withLocale( locale ); - //incomingFormat = incomingFormat.withLocale( locale ); - } - return outgoingFormat.format( - incomingFormat.parse( src )); + return getDtfs().map(src, outgoingFormat, locale, incomingFormat); } - - /** - * Uses as incoming format {@link #defaultFormat} and no locale. - * @param src - * @param outgoingFormat - * @return the formatted string - * - * @throws java.time.temporal.UnsupportedTemporalTypeException - */ + public String mapTo( String src, DateTimeFormatter outgoingFormat ) { - return map( src, outgoingFormat, null, defaultFormat ); + return getDtfs().map( src, outgoingFormat, null, getDtfs().getDefaultFormat() ); } - /** - * Uses as outgoing {@link DateTimeFormatter} {@link #defaultFormat} and no locale. - * @param src the datetime formatted string - * @param incomingFormat the format of this string - * @return the date time formatted using the {@link #defaultFormat}. - */ public String mapFrom( String src, DateTimeFormatter incomingFormat ) { - return map( src, defaultFormat, null, incomingFormat ); + return getDtfs().map( src, getDtfs().getDefaultFormat(), null, incomingFormat ); } - /** - * Uses as incoming {@link DateTimeFormatter} {@link #defaultFormat}. - * @param src the datetime formatted string - * @param outgoingFormat the format to which this string should be formatted. - * @param locale - * @return the newly formatted date time string - * - * @throws java.time.temporal.UnsupportedTemporalTypeException - */ public String map( String src, DateTimeFormatter outgoingFormat, Locale locale ) { - return map( src, outgoingFormat, locale, defaultFormat ); + return getDtfs().map( src, outgoingFormat, locale, getDtfs().getDefaultFormat() ); } } diff --git a/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java b/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java index c69fdc3..5dedc7c 100644 --- a/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java +++ b/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java @@ -222,8 +222,4 @@ public class AnnotationProcessorTest System.out.println(System.currentTimeMillis() - startTime); } - @TurbineService - private DateTimeFormatterService df; - - } diff --git a/src/test/org/apache/turbine/services/LoadingComponentsTest.java b/src/test/org/apache/turbine/services/LoadingComponentsTest.java index 09d9eaf..532819f 100644 --- a/src/test/org/apache/turbine/services/LoadingComponentsTest.java +++ b/src/test/org/apache/turbine/services/LoadingComponentsTest.java @@ -31,9 +31,12 @@ import org.apache.fulcrum.factory.FactoryService; import org.apache.fulcrum.intake.IntakeService; import org.apache.fulcrum.localization.LocalizationService; import org.apache.fulcrum.mimetype.MimeTypeService; +import org.apache.turbine.annotation.AnnotationProcessor; +import org.apache.turbine.annotation.TurbineService; import org.apache.turbine.services.avaloncomponent.AvalonComponentService; import org.apache.turbine.test.BaseTestCase; import org.apache.turbine.util.TurbineConfig; +import org.apache.turbine.util.TurbineException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -50,6 +53,9 @@ import org.junit.Test; public class LoadingComponentsTest extends BaseTestCase { private static TurbineConfig tc = null; + + @TurbineService + GlobalCacheService cacheService; @BeforeClass public static void setUp() throws Exception @@ -119,6 +125,12 @@ public class LoadingComponentsTest extends BaseTestCase MimeTypeService mimetype = (MimeTypeService)serviceManager.getService(MimeTypeService.ROLE); assertNotNull(mimetype); } + + @Test public void testLoadingByAnnotation() throws TurbineException + { + AnnotationProcessor.process(this); + assertNotNull(cacheService); + } /** * Lookup up an unknown servie diff --git a/src/test/org/apache/turbine/services/pull/util/DateTimeFormatterTest.java b/src/test/org/apache/turbine/services/pull/util/DateTimeFormatterTest.java deleted file mode 100644 index 49e9429..0000000 --- a/src/test/org/apache/turbine/services/pull/util/DateTimeFormatterTest.java +++ /dev/null @@ -1,215 +0,0 @@ -package org.apache.turbine.services.pull.util; - - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoField; -import java.time.temporal.ChronoUnit; -import java.time.temporal.Temporal; -import java.time.temporal.TemporalAccessor; - -import org.apache.turbine.test.BaseTestCase; -import org.apache.turbine.util.TurbineConfig; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -/** - * Test class for DateTimeFormatter. - * - */ -public class DateTimeFormatterTest -{ - - private static DateTimeFormatterTool df; - - private static TurbineConfig tc = null; - - @BeforeAll - public static void setup() - { - // required to initialize defaults - tc = new TurbineConfig( - ".", - "/conf/test/TestFulcrumComponents.properties"); - tc.initialize(); - df = new DateTimeFormatterTool(); - df.init(null); - } - - @AfterAll - public static void tearDown() - { - tc.dispose(); - } - /* - * Class under test for String format(Date, String) - */ - @Test public void testFormatDateString() - { - LocalDateTime ldt = LocalDateTime.now(); - int day = ldt.get(ChronoField.DAY_OF_MONTH); - int month = ldt.get(ChronoField.MONTH_OF_YEAR) ; // one based - int year = ldt.get(ChronoField.YEAR); - - String dayString = (day < 10 ? "0" : "") + day; - String monthString = (month < 10 ? "0" : "") + month; - String ddmmyyyy = dayString + "/" + monthString + "/" + year; - assertEquals(ddmmyyyy, df.format(ldt, "dd/MM/yyyy")); - - String mmddyyyy = "" + monthString + "/" + dayString + "/" + year; - assertEquals(mmddyyyy, df.format(ldt, "MM/dd/yyyy")); - } - - @Test public void testFormatZonedDateString() - { - ZonedDateTime zdt = ZonedDateTime.now(); - int day = zdt.get(ChronoField.DAY_OF_MONTH); - int month = zdt.get(ChronoField.MONTH_OF_YEAR) ; // one based - int year = zdt.get(ChronoField.YEAR); - zdt = zdt.truncatedTo( ChronoUnit.MINUTES ); - - String dayString = (day < 10 ? "0" : "") + day; - String monthString = (month < 10 ? "0" : "") + month; - String ddmmyyyy = dayString + "/" + monthString + "/" + year; - assertEquals(ddmmyyyy, df.format(zdt, "dd/MM/yyyy")); - - int hours = zdt.get(ChronoField.HOUR_OF_DAY); - int mins =zdt.get(ChronoField.MINUTE_OF_HOUR); - int secs = zdt.get(ChronoField.SECOND_OF_MINUTE); - String hourString = (hours < 10 ? "0" : "") + hours; - String minsString = (mins < 10 ? "0" : "") + mins; - String secsString = (secs < 10 ? "0" : "") + secs; - - String zone = zdt.getZone().getId(); - String offset = zdt.getOffset().getId(); - // offset formatting not easy matchable, removed - String mmddyyyy = "" + monthString + "/" + dayString + "/" + year + " " + hourString + ":" + minsString + ":"+ secsString + " " + zone; - // zone + offset format, removed offset ZZZ - assertEquals(mmddyyyy, df.format(zdt, "MM/dd/yyyy HH:mm:ss VV")); - } - - /* - * Class under test for String mapFrom(String, DateTimeFormatter) - */ - @Test public void testDefaultMapFromInstant() - { - DateTimeFormatter incomingFormat = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.systemDefault()); - // may throws an DateTimeParseException - Instant now = Instant.now().truncatedTo( ChronoUnit.MINUTES ); - String source = incomingFormat.format(now); - - TemporalAccessor dateTimeFromInstant = incomingFormat.parse(source); - int day = dateTimeFromInstant.get(ChronoField.DAY_OF_MONTH); - int month = dateTimeFromInstant.get(ChronoField.MONTH_OF_YEAR) ; // one based - int year = dateTimeFromInstant.get(ChronoField.YEAR); - - String dayString = (day < 10 ? "0" : "") + day; - String monthString = (month < 10 ? "0" : "") + month; - String mmddyyyy = "" + monthString + "/" + dayString + "/" + year; - assertEquals(mmddyyyy, df.mapFrom(source,incomingFormat)); - } - - /* - * Class under test for String format(Date, "") - */ - @Test public void testDefaultMapInstant() - { - String source = df.format(Instant.now()); - - TemporalAccessor dateTimeFromInstant = df.getDefaultFormat().parse(source); - - int day = dateTimeFromInstant.get(ChronoField.DAY_OF_MONTH); - int month = dateTimeFromInstant.get(ChronoField.MONTH_OF_YEAR) ; // one based - int year = dateTimeFromInstant.get(ChronoField.YEAR); - - String dayString = (day < 10 ? "0" : "") + day; - String monthString = (month < 10 ? "0" : "") + month; - String yyyymmdd = year + "-" + monthString + "-" + dayString; - - // caution we are mapping from the DateTimeFormatterTool defaultFormat7-pattern without time! - // ISO_DATE_TIME will throw an error: - // java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay - DateTimeFormatter outgoingFormat = DateTimeFormatter.ISO_DATE.withZone(ZoneId.systemDefault()); - assertEquals(yyyymmdd, df.mapTo(source,outgoingFormat)); - - outgoingFormat = DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZoneId.systemDefault()); - assertEquals(yyyymmdd, df.mapTo(source,outgoingFormat)); - - // ISO_OFFSET_DATE : Unsupported field: OffsetSeconds - // ISO_INSTANT; Unsupported field: InstantSeconds - yyyymmdd = year + monthString + dayString; - outgoingFormat = DateTimeFormatter.BASIC_ISO_DATE.withZone(ZoneId.systemDefault()); - assertEquals(yyyymmdd, df.mapTo(source,outgoingFormat)); - } - -// /* -// * Class under test for String format(Date, "") -// */ -// @Test public void testDefaultMapInstantString() -// { -// Instant today = Instant.now(); -// String todayFormatted = df.format(today); -// assertEquals("", -// df.map(todayFormatted), "Empty pattern should produce empty String"); -// } - - /* - * Class under test for String format(null, String) - */ - @Test public void testMapDateStringNullString() - { - DateTimeFormatter outgoingFormat = DateTimeFormatter.ISO_INSTANT; - assertEquals("", - df.mapFrom(null, outgoingFormat), "null argument should produce an empty String"); - } - - /* - * Class under test for String format(Date, "") - */ - @Test public void testMapDateStringEmptyString() - { - Instant today = Instant.now(); - String todayFormatted = df.format(today); - assertEquals("", - df.mapFrom(todayFormatted, null), "Empty pattern should map to empty String"); - } - - - /* - * Class under test for String format(null, String) - */ - @Test public void testFormatDateStringNullString() - { - assertEquals("", - df.format(null, "MM/dd/yyyy"), "null argument should produce an empty String"); - } - - /* - * Class under test for String format(Date, "") - */ - @Test public void testFormatDateStringEmptyString() - { - Instant today = Instant.now(); - assertEquals("", - df.format(today, ""), "Empty pattern should produce empty String"); - } - - /* - * Class under test for String format(Date, "") - */ - @Test public void testFormatDateStringNullFormat() - { - Instant today = Instant.now(); - assertEquals("", - df.format(today, null),"null pattern should produce empty String"); - } - -}
