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