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