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.LocalTime; 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 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.runner.RunWith; 43 import org.junit.runners.Parameterized; 44 45 /** 46 */ 47 @RunWith(Parameterized.class) 48 public class LocalTimeStringConverterTest { 49 private static final LocalTime VALID_TIME_WITH_SECONDS; 50 private static final LocalTime VALID_TIME_WITHOUT_SECONDS; 51 52 static { 53 VALID_TIME_WITH_SECONDS = LocalTime.of(12, 34, 56); 54 VALID_TIME_WITHOUT_SECONDS = LocalTime.of(12, 34, 0); 55 } 56 57 private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("HH mm ss"); 58 private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("hh mm ss a"); 59 60 61 @Parameterized.Parameters public static Collection implementations() { 62 return Arrays.asList(new Object[][] { 63 { new LocalTimeStringConverter(), 64 Locale.getDefault(Locale.Category.FORMAT), FormatStyle.SHORT, 65 VALID_TIME_WITHOUT_SECONDS, null, null }, 66 67 { new LocalTimeStringConverter(aFormatter, aParser), 68 Locale.getDefault(Locale.Category.FORMAT), null, 69 VALID_TIME_WITH_SECONDS, aFormatter, aParser }, 70 71 { new LocalTimeStringConverter(FormatStyle.SHORT, Locale.UK), 72 Locale.UK, FormatStyle.SHORT, 73 VALID_TIME_WITHOUT_SECONDS, null, null }, 74 }); 75 } 76 77 private LocalTimeStringConverter converter; 78 private Locale locale; 79 private FormatStyle timeStyle; 80 private DateTimeFormatter formatter, parser; 81 private LocalTime validTime; 82 83 public LocalTimeStringConverterTest(LocalTimeStringConverter converter, Locale locale, FormatStyle timeStyle, LocalTime validTime, DateTimeFormatter formatter, DateTimeFormatter parser) { 84 this.converter = converter; 85 this.locale = locale; 86 this.timeStyle = timeStyle; 87 this.validTime = validTime; 88 this.formatter = formatter; 89 this.parser = parser; 90 } 91 92 @Before public void setup() { 93 } 94 95 /********************************************************************* 96 * Test constructors 97 ********************************************************************/ 98 99 @Test public void testConstructor() { 100 assertEquals(locale, converter.ldtConverter.locale); 101 assertNull(converter.ldtConverter.dateStyle); 102 assertEquals((timeStyle != null) ? timeStyle : FormatStyle.SHORT, converter.ldtConverter.timeStyle); 103 if (formatter != null) { 104 assertEquals(formatter, converter.ldtConverter.formatter); 105 } 106 if (parser != null) { 107 assertEquals(parser, converter.ldtConverter.parser); 108 } else if (formatter != null) { 109 assertEquals(formatter, converter.ldtConverter.parser); 110 } 111 } 112 113 114 115 /********************************************************************* 116 * Test toString / fromString methods 117 ********************************************************************/ 118 119 @Test public void toString_to_fromString_testRoundtrip() { 120 if (formatter == null) { 121 // Only the default formatter/parser can guarantee roundtrip symmetry 122 assertEquals(validTime, converter.fromString(converter.toString(validTime))); 123 } 124 } 125 126 @Test(expected=RuntimeException.class) 127 public void fromString_testInvalidInput() { 128 converter.fromString("abcdefg"); 129 } 130 }