1 /* 2 * Copyright (c) 2010, 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.text.DateFormat; 29 import java.text.SimpleDateFormat; 30 import java.util.Arrays; 31 import java.util.Calendar; 32 import java.util.Collection; 33 import java.util.Date; 34 import java.util.Locale; 35 import java.util.TimeZone; 36 import javafx.util.converter.LocalTimeStringConverterShim; 37 import javafx.util.converter.DateStringConverter; 38 import javafx.util.converter.DateTimeStringConverterShim; 39 import static org.junit.Assert.*; 40 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.Ignore; 44 import org.junit.runner.RunWith; 45 import org.junit.runners.Parameterized; 46 47 /** 48 */ 49 @RunWith(Parameterized.class) 50 public class DateStringConverterTest { 51 private static final Date VALID_DATE; 52 53 static { 54 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 55 Calendar c = Calendar.getInstance(); 56 c.set(Calendar.YEAR, 1985); 57 c.set(Calendar.MONTH, Calendar.JANUARY); 58 c.set(Calendar.DAY_OF_MONTH, 12); 59 c.set(Calendar.HOUR_OF_DAY, 0); 60 c.set(Calendar.MINUTE, 0); 61 c.set(Calendar.SECOND, 0); 62 c.set(Calendar.MILLISECOND, 0); 63 VALID_DATE = c.getTime(); 64 } 65 66 @Parameterized.Parameters public static Collection implementations() { 67 return Arrays.asList(new Object[][] { 68 { new DateStringConverter(), 69 Locale.getDefault(Locale.Category.FORMAT), DateFormat.DEFAULT, 70 VALID_DATE, null, null }, 71 72 { new DateStringConverter(DateFormat.SHORT), 73 Locale.getDefault(Locale.Category.FORMAT), DateFormat.SHORT, 74 VALID_DATE, null, null }, 75 76 { new DateStringConverter(Locale.UK), 77 Locale.UK, DateFormat.DEFAULT, 78 VALID_DATE, null, null }, 79 80 { new DateStringConverter(Locale.UK, DateFormat.SHORT), 81 Locale.UK, DateFormat.SHORT, 82 VALID_DATE, null, null }, 83 84 { new DateStringConverter("dd MM yyyy"), 85 Locale.getDefault(Locale.Category.FORMAT), DateFormat.DEFAULT, 86 VALID_DATE, "dd MM yyyy", null }, 87 88 { new DateStringConverter(DateFormat.getDateInstance(DateFormat.LONG)), 89 Locale.getDefault(Locale.Category.FORMAT), DateFormat.DEFAULT, 90 VALID_DATE, null, DateFormat.getDateInstance(DateFormat.LONG) }, 91 }); 92 } 93 94 private DateStringConverter converter; 95 private Locale locale; 96 private int dateStyle; 97 private String pattern; 98 private DateFormat dateFormat; 99 private Date validDate; 100 private DateFormat validFormatter; 101 102 public DateStringConverterTest(DateStringConverter converter, Locale locale, int dateStyle, Date validDate, String pattern, DateFormat dateFormat) { 103 this.converter = converter; 104 this.locale = locale; 105 this.dateStyle = dateStyle; 106 this.validDate = validDate; 107 this.pattern = pattern; 108 this.dateFormat = dateFormat; 109 110 if (dateFormat != null) { 111 validFormatter = dateFormat; 112 } else if (pattern != null) { 113 validFormatter = new SimpleDateFormat(pattern); 114 } else { 115 validFormatter = DateFormat.getDateInstance(dateStyle, locale); 116 } 117 } 118 119 @Before public void setup() { 120 } 121 122 /********************************************************************* 123 * Test constructors 124 ********************************************************************/ 125 126 @Test public void testConstructor() { 127 assertEquals(locale, DateTimeStringConverterShim.getLocale(converter)); 128 assertEquals(dateStyle, DateTimeStringConverterShim.getDateStyle(converter)); 129 assertEquals(pattern, DateTimeStringConverterShim.getPattern(converter)); 130 assertEquals(dateFormat, DateTimeStringConverterShim.getDateFormatVar(converter)); 131 } 132 133 134 /********************************************************************* 135 * Test methods 136 ********************************************************************/ 137 138 @Test public void getDateFormat() { 139 assertNotNull(DateTimeStringConverterShim.getDateFormat(converter)); 140 } 141 142 @Test public void getDateFormat_nonNullPattern() { 143 converter = new DateStringConverter("yyyy"); 144 assertTrue( 145 DateTimeStringConverterShim.getDateFormat(converter) 146 instanceof SimpleDateFormat); 147 } 148 149 /********************************************************************* 150 * Test toString / fromString methods 151 ********************************************************************/ 152 153 @Test public void fromString_testValidInput() { 154 String input = validFormatter.format(validDate); 155 assertEquals("Input = "+input, validDate, converter.fromString(input)); 156 } 157 158 @Test public void fromString_testValidInputWithWhiteSpace() { 159 String input = validFormatter.format(validDate); 160 assertEquals("Input = "+input, validDate, converter.fromString(" " + input + " ")); 161 } 162 163 @Test(expected=RuntimeException.class) 164 public void fromString_testInvalidInput() { 165 converter.fromString("abcdefg"); 166 } 167 168 @Test public void toString_validOutput() { 169 assertEquals(validFormatter.format(validDate), converter.toString(validDate)); 170 } 171 }