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 test.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 import javafx.util.converter.LocalTimeStringConverterShim; 40 import javafx.util.converter.LocalDateTimeStringConverter; 41 import javafx.util.converter.LocalDateTimeStringConverterShim; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.Ignore; 46 import org.junit.runner.RunWith; 47 import org.junit.runners.Parameterized; 48 49 /** 50 */ 51 @RunWith(Parameterized.class) 52 public class LocalDateTimeStringConverterTest { 53 private static final LocalDateTime VALID_LDT_WITH_SECONDS = LocalDateTime.of(1985, 1, 12, 12, 34, 56); 54 private static final LocalDateTime VALID_LDT_WITHOUT_SECONDS = LocalDateTime.of(1985, 1, 12, 12, 34, 0); 55 56 private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy HH mm ss"); 57 private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd hh mm ss a"); 58 59 @Parameterized.Parameters public static Collection implementations() { 60 return Arrays.asList(new Object[][] { 61 { new LocalDateTimeStringConverter(), 62 Locale.getDefault(Locale.Category.FORMAT), FormatStyle.SHORT, FormatStyle.SHORT, 63 VALID_LDT_WITHOUT_SECONDS, null, null }, 64 65 { new LocalDateTimeStringConverter(aFormatter, aParser), 66 Locale.getDefault(Locale.Category.FORMAT), null, null, 67 VALID_LDT_WITH_SECONDS, aFormatter, aParser }, 68 69 { new LocalDateTimeStringConverter(FormatStyle.SHORT, FormatStyle.SHORT, Locale.UK, IsoChronology.INSTANCE), 70 Locale.UK, FormatStyle.SHORT, FormatStyle.SHORT, 71 VALID_LDT_WITHOUT_SECONDS, null, null }, 72 }); 73 } 74 75 private LocalDateTimeStringConverter converter; 76 private Locale locale; 77 private FormatStyle dateStyle; 78 private FormatStyle timeStyle; 79 private DateTimeFormatter formatter, parser; 80 81 private LocalDateTime validDateTime; 82 83 public LocalDateTimeStringConverterTest(LocalDateTimeStringConverter converter, Locale locale, FormatStyle dateStyle, FormatStyle timeStyle, LocalDateTime validDateTime, DateTimeFormatter formatter, DateTimeFormatter parser) { 84 this.converter = converter; 85 this.locale = locale; 86 this.dateStyle = dateStyle; 87 this.timeStyle = timeStyle; 88 this.validDateTime = validDateTime; 89 this.formatter = formatter; 90 this.parser = parser; 91 } 92 93 @Before public void setup() { 94 } 95 96 /********************************************************************* 97 * Test constructors 98 ********************************************************************/ 99 100 @Test public void testConstructor() { 101 assertEquals(locale, 102 LocalDateTimeStringConverterShim.getldtConverterLocale(converter)); 103 assertEquals((dateStyle != null) ? dateStyle : FormatStyle.SHORT, 104 LocalDateTimeStringConverterShim.getldtConverterDateStyle(converter)); 105 assertEquals((timeStyle != null) ? timeStyle : FormatStyle.SHORT, 106 LocalDateTimeStringConverterShim.getldtConverterTimeStyle(converter)); 107 if (formatter != null) { 108 assertEquals(formatter, 109 LocalDateTimeStringConverterShim.getldtConverterFormatter(converter)); 110 } 111 if (parser != null) { 112 assertEquals(parser, 113 LocalDateTimeStringConverterShim.getldtConverterParser(converter)); 114 } else if (formatter != null) { 115 assertEquals(formatter, 116 LocalDateTimeStringConverterShim.getldtConverterFormatter(converter)); 117 } 118 } 119 120 121 /********************************************************************* 122 * Test toString / fromString methods 123 ********************************************************************/ 124 125 @Test public void toString_to_fromString_testRoundtrip() { 126 if (formatter == null) { 127 // Only the default formatter/parser can guarantee roundtrip symmetry 128 assertEquals(validDateTime, converter.fromString(converter.toString(validDateTime))); 129 } 130 } 131 132 133 @Test(expected=RuntimeException.class) 134 public void fromString_testInvalidInput() { 135 converter.fromString("abcdefg"); 136 } 137 138 @Test public void converter_with_specified_formatter_and_parser() { 139 String formatPattern = "dd MMMM yyyy, HH:mm:ss"; 140 String parsePattern = "MMMM dd, yyyy, HH:mm:ss"; 141 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern); 142 DateTimeFormatter parser = DateTimeFormatter.ofPattern(parsePattern); 143 StringConverter<LocalDateTime> converter = new LocalDateTimeStringConverter(formatter, parser); 144 assertEquals("12 January 1985, 12:34:56", converter.toString(VALID_LDT_WITH_SECONDS)); 145 assertEquals(VALID_LDT_WITH_SECONDS, converter.fromString("January 12, 1985, 12:34:56")); 146 } 147 148 @Test public void converter_with_specified_formatter_and_null_parser() { 149 String pattern = "dd MMMM yyyy, HH:mm:ss"; 150 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); 151 StringConverter<LocalDateTime> converter = new LocalDateTimeStringConverter(formatter, null); 152 assertEquals("12 January 1985, 12:34:56", converter.toString(VALID_LDT_WITH_SECONDS)); 153 assertEquals(VALID_LDT_WITH_SECONDS, converter.fromString("12 January 1985, 12:34:56")); 154 } 155 }