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.util.Arrays; 29 import java.time.LocalDate; 30 import java.time.chrono.Chronology; 31 import java.time.chrono.IsoChronology; 32 import java.time.format.DateTimeFormatter; 33 import java.time.format.FormatStyle; 34 import java.util.Collection; 35 import java.util.Locale; 36 37 import javafx.util.StringConverter; 38 import javafx.util.converter.LocalTimeStringConverterShim; 39 import javafx.util.converter.LocalDateStringConverter; 40 import javafx.util.converter.LocalDateStringConverterShim; 41 42 import static org.junit.Assert.*; 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 LocalDateStringConverterTest { 53 private static final LocalDate VALID_DATE = LocalDate.of(1985, 1, 12); 54 55 private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy"); 56 private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd"); 57 58 59 @Parameterized.Parameters public static Collection implementations() { 60 return Arrays.asList(new Object[][] { 61 { new LocalDateStringConverter(), 62 Locale.getDefault(Locale.Category.FORMAT), FormatStyle.SHORT, 63 VALID_DATE, null, null }, 64 65 { new LocalDateStringConverter(aFormatter, aParser), 66 Locale.getDefault(Locale.Category.FORMAT), null, 67 VALID_DATE, aFormatter, aParser }, 68 69 { new LocalDateStringConverter(FormatStyle.SHORT, Locale.UK, IsoChronology.INSTANCE), 70 Locale.UK, FormatStyle.SHORT, 71 VALID_DATE, null, null }, 72 }); 73 } 74 75 private LocalDateStringConverter converter; 76 private Locale locale; 77 private FormatStyle dateStyle; 78 private DateTimeFormatter formatter, parser; 79 private LocalDate validDate; 80 81 public LocalDateStringConverterTest(LocalDateStringConverter converter, Locale locale, FormatStyle dateStyle, LocalDate validDate, DateTimeFormatter formatter, DateTimeFormatter parser) { 82 this.converter = converter; 83 this.locale = locale; 84 this.dateStyle = dateStyle; 85 this.validDate = validDate; 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, LocalDateStringConverterShim.getldtConverterLocale(converter)); 99 assertEquals((dateStyle != null) ? dateStyle : FormatStyle.SHORT, 100 LocalDateStringConverterShim.getldtConverterDateStyle(converter)); 101 assertNull(LocalDateStringConverterShim.getldtConverterTimeStyle(converter)); 102 if (formatter != null) { 103 assertEquals(formatter, 104 LocalDateStringConverterShim.getldtConverterFormatter(converter)); 105 } 106 if (parser != null) { 107 assertEquals(parser, 108 LocalDateStringConverterShim.getldtConverterParser(converter)); 109 } else if (formatter != null) { 110 assertEquals(formatter, 111 LocalDateStringConverterShim.getldtConverterParser(converter)); 112 } 113 } 114 115 116 /********************************************************************* 117 * Test toString / fromString methods 118 ********************************************************************/ 119 120 @Test public void toString_to_fromString_testRoundtrip() { 121 if (formatter == null) { 122 // Only the default formatter/parser can guarantee roundtrip symmetry 123 assertEquals(validDate, converter.fromString(converter.toString(validDate))); 124 } 125 } 126 127 @Test(expected=RuntimeException.class) 128 public void fromString_testInvalidInput() { 129 converter.fromString("abcdefg"); 130 } 131 }