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.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 39 import static org.junit.Assert.*; 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 LocalDateStringConverterTest { 50 private static final LocalDate VALID_DATE = LocalDate.of(1985, 1, 12); 51 52 private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy"); 53 private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd"); 54 55 56 @Parameterized.Parameters public static Collection implementations() { 57 return Arrays.asList(new Object[][] { 58 { new LocalDateStringConverter(), 59 Locale.getDefault(Locale.Category.FORMAT), FormatStyle.SHORT, 60 VALID_DATE, null, null }, 61 62 { new LocalDateStringConverter(aFormatter, aParser), 63 Locale.getDefault(Locale.Category.FORMAT), null, 64 VALID_DATE, aFormatter, aParser }, 65 66 { new LocalDateStringConverter(FormatStyle.SHORT, Locale.UK, IsoChronology.INSTANCE), 67 Locale.UK, FormatStyle.SHORT, 68 VALID_DATE, null, null }, 69 }); 70 } 71 72 private LocalDateStringConverter converter; 73 private Locale locale; 74 private FormatStyle dateStyle; 75 private DateTimeFormatter formatter, parser; 76 private LocalDate validDate; 77 78 public LocalDateStringConverterTest(LocalDateStringConverter converter, Locale locale, FormatStyle dateStyle, LocalDate validDate, DateTimeFormatter formatter, DateTimeFormatter parser) { 79 this.converter = converter; 80 this.locale = locale; 81 this.dateStyle = dateStyle; 82 this.validDate = validDate; 83 this.formatter = formatter; 84 this.parser = parser; 85 } 86 87 @Before public void setup() { 88 } 89 90 /********************************************************************* 91 * Test constructors 92 ********************************************************************/ 93 94 @Test public void testConstructor() { 95 assertEquals(locale, converter.ldtConverter.locale); 96 assertEquals((dateStyle != null) ? dateStyle : FormatStyle.SHORT, converter.ldtConverter.dateStyle); 97 assertNull(converter.ldtConverter.timeStyle); 98 if (formatter != null) { 99 assertEquals(formatter, converter.ldtConverter.formatter); 100 } 101 if (parser != null) { 102 assertEquals(parser, converter.ldtConverter.parser); 103 } else if (formatter != null) { 104 assertEquals(formatter, converter.ldtConverter.parser); 105 } 106 } 107 108 109 /********************************************************************* 110 * Test toString / fromString methods 111 ********************************************************************/ 112 113 @Test public void toString_to_fromString_testRoundtrip() { 114 if (formatter == null) { 115 // Only the default formatter/parser can guarantee roundtrip symmetry 116 assertEquals(validDate, converter.fromString(converter.toString(validDate))); 117 } 118 } 119 120 @Test(expected=RuntimeException.class) 121 public void fromString_testInvalidInput() { 122 converter.fromString("abcdefg"); 123 } 124 }