1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.util.converter; 27 28 import java.time.LocalDateTime; 29 import java.time.chrono.Chronology; 30 import java.time.chrono.IsoChronology; 31 import java.time.format.DateTimeFormatter; 32 import java.time.format.FormatStyle; 33 import java.util.Arrays; 34 import java.util.Collection; 35 import java.util.Locale; 36 import static org.junit.Assert.*; 37 38 import javafx.util.StringConverter; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.Ignore; 43 import org.junit.runner.RunWith; 44 import org.junit.runners.Parameterized; 45 46 /** 47 */ 48 @RunWith(Parameterized.class) 49 public class LocalDateTimeStringConverterTest { 50 private static final LocalDateTime VALID_LDT_WITH_SECONDS = LocalDateTime.of(1985, 1, 12, 12, 34, 56); 51 private static final LocalDateTime VALID_LDT_WITHOUT_SECONDS = LocalDateTime.of(1985, 1, 12, 12, 34, 0); 52 53 private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy HH mm ss"); 54 private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd hh mm ss a"); 55 56 @Parameterized.Parameters public static Collection implementations() { 57 return Arrays.asList(new Object[][] { 58 { new LocalDateTimeStringConverter(), 59 Locale.getDefault(Locale.Category.FORMAT), FormatStyle.SHORT, FormatStyle.SHORT, 60 VALID_LDT_WITHOUT_SECONDS, null, null }, 61 62 { new LocalDateTimeStringConverter(aFormatter, aParser), 63 Locale.getDefault(Locale.Category.FORMAT), null, null, 64 VALID_LDT_WITH_SECONDS, aFormatter, aParser }, 65 66 { new LocalDateTimeStringConverter(FormatStyle.SHORT, FormatStyle.SHORT, Locale.UK, IsoChronology.INSTANCE), 67 Locale.UK, FormatStyle.SHORT, FormatStyle.SHORT, 68 VALID_LDT_WITHOUT_SECONDS, null, null }, 69 }); 70 } 71 72 private LocalDateTimeStringConverter converter; 73 private Locale locale; 74 private FormatStyle dateStyle; 75 private FormatStyle timeStyle; 76 private DateTimeFormatter formatter, parser; 77 78 private LocalDateTime validDateTime; 79 80 public LocalDateTimeStringConverterTest(LocalDateTimeStringConverter converter, Locale locale, FormatStyle dateStyle, FormatStyle timeStyle, LocalDateTime validDateTime, DateTimeFormatter formatter, DateTimeFormatter parser) { 81 this.converter = converter; 82 this.locale = locale; 83 this.dateStyle = dateStyle; 84 this.timeStyle = timeStyle; 85 this.validDateTime = validDateTime; 86 this.formatter = formatter; 87 this.parser = parser; 88 } 89 90 @Before public void setup() { 91 } 92 93 /********************************************************************* 94 * Test constructors 95 ********************************************************************/ 96 97 @Test public void testConstructor() { 98 assertEquals(locale, converter.ldtConverter.locale); 99 assertEquals((dateStyle != null) ? dateStyle : FormatStyle.SHORT, converter.ldtConverter.dateStyle); 100 assertEquals((timeStyle != null) ? timeStyle : FormatStyle.SHORT, converter.ldtConverter.timeStyle); 101 if (formatter != null) { 102 assertEquals(formatter, converter.ldtConverter.formatter); 103 } 104 if (parser != null) { 105 assertEquals(parser, converter.ldtConverter.parser); 106 } else if (formatter != null) { 107 assertEquals(formatter, converter.ldtConverter.parser); 108 } 109 } 110 111 112 /********************************************************************* 113 * Test toString / fromString methods 114 ********************************************************************/ 115 116 @Test public void toString_to_fromString_testRoundtrip() { 117 if (formatter == null) { 118 // Only the default formatter/parser can guarantee roundtrip symmetry 119 assertEquals(validDateTime, converter.fromString(converter.toString(validDateTime))); 120 } 121 } 122 123 124 @Test(expected=RuntimeException.class) 125 public void fromString_testInvalidInput() { 126 converter.fromString("abcdefg"); 127 } 128 129 @Test public void converter_with_specified_formatter_and_parser() { 130 String formatPattern = "dd MMMM yyyy, HH:mm:ss"; 131 String parsePattern = "MMMM dd, yyyy, HH:mm:ss"; 132 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern); 133 DateTimeFormatter parser = DateTimeFormatter.ofPattern(parsePattern); 134 StringConverter<LocalDateTime> converter = new LocalDateTimeStringConverter(formatter, parser); 135 assertEquals("12 January 1985, 12:34:56", converter.toString(VALID_LDT_WITH_SECONDS)); 136 assertEquals(VALID_LDT_WITH_SECONDS, converter.fromString("January 12, 1985, 12:34:56")); 137 } 138 139 @Test public void converter_with_specified_formatter_and_null_parser() { 140 String pattern = "dd MMMM yyyy, HH:mm:ss"; 141 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); 142 StringConverter<LocalDateTime> converter = new LocalDateTimeStringConverter(formatter, null); 143 assertEquals("12 January 1985, 12:34:56", converter.toString(VALID_LDT_WITH_SECONDS)); 144 assertEquals(VALID_LDT_WITH_SECONDS, converter.fromString("12 January 1985, 12:34:56")); 145 } 146 }