tests/libjava-mauve/src/gnu/testlet/java/text/SimpleDateFormat/Test.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 /*************************************************************************
       
     2 /* Test.java -- Test java.text.SimpleDateFormat
       
     3 /*
       
     4 /* Copyright (c) 1998, 1999, 2001, 2003 Free Software Foundation, Inc.
       
     5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
       
     6 /*
       
     7 /* This program is free software; you can redistribute it and/or modify
       
     8 /* it under the terms of the GNU General Public License as published 
       
     9 /* by the Free Software Foundation, either version 2 of the License, or
       
    10 /* (at your option) any later version.
       
    11 /*
       
    12 /* This program is distributed in the hope that it will be useful, but
       
    13 /* WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 /* GNU General Public License for more details.
       
    16 /*
       
    17 /* You should have received a copy of the GNU General Public License
       
    18 /* along with this program; if not, write to the Free Software Foundation
       
    19 /* Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
       
    20 /*************************************************************************/
       
    21 
       
    22 // Tags: JDK1.1
       
    23 
       
    24 package gnu.testlet.java.text.SimpleDateFormat;
       
    25 
       
    26 import gnu.testlet.Testlet;
       
    27 import gnu.testlet.TestHarness;
       
    28 import java.text.*;
       
    29 import java.util.*;
       
    30 
       
    31 public class Test implements Testlet
       
    32 {
       
    33 
       
    34 public void 
       
    35 test(TestHarness harness)
       
    36 {
       
    37   String pattern_chars = "GyMdhHmsSEDFwWakKz";
       
    38   String pattern = "EEEE, MMMM d, yyyy h:mm:ss 'o''clock' a";
       
    39 
       
    40   DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
       
    41   SimpleDateFormat sdf = new SimpleDateFormat(pattern, dfs);
       
    42   harness.check(sdf.getDateFormatSymbols(), dfs, "getDateFormatSymbols() init");
       
    43 
       
    44   String[] ampms = { "am ", "pm " };
       
    45   dfs.setAmPmStrings(ampms);
       
    46   sdf.setDateFormatSymbols(dfs);
       
    47   harness.check(sdf.getDateFormatSymbols(), dfs, "set/getDateFormatSymbols()");
       
    48   
       
    49   harness.check(sdf.toPattern(), pattern, "toPattern init");
       
    50   String new_pattern = "EMdyH";
       
    51   sdf.applyPattern(new_pattern);
       
    52   harness.check(sdf.toPattern(), new_pattern, "apply/toPattern()");
       
    53   sdf.applyPattern(pattern);
       
    54 
       
    55   harness.check(sdf.equals(new SimpleDateFormat(pattern, dfs)), "equals()");
       
    56   harness.check(sdf.clone().equals(sdf) == true, "clone()");
       
    57 
       
    58   sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
       
    59   Date d = new Date(0);
       
    60   String formatted_date = sdf.format(d);
       
    61   harness.debug(formatted_date);
       
    62   harness.check(formatted_date.equals(
       
    63      "Thursday, January 1, 1970 12:00:00 o'clock am "), "format()");
       
    64 
       
    65   sdf.setLenient(false);
       
    66   try
       
    67     {
       
    68       harness.check(sdf.parse(formatted_date), d, "parse() strict");
       
    69     }
       
    70   catch(Throwable e)
       
    71     {
       
    72       harness.debug(e);
       
    73       harness.check(false, "parse() strict");
       
    74     }
       
    75 
       
    76   sdf.setTimeZone(TimeZone.getDefault());
       
    77   harness.debug(sdf.format(new Date(System.currentTimeMillis())));
       
    78 
       
    79   // Now do some lenient parsing tests.  These might not all work.
       
    80   dfs = new DateFormatSymbols(Locale.US);
       
    81   sdf = new SimpleDateFormat(pattern, dfs);
       
    82 
       
    83   sdf.setLenient(true);
       
    84 
       
    85   String[] date_strs = { 
       
    86     "Tue Feb 23 20:15:34 CST 1999",
       
    87     "10/31/69",
       
    88     "1999/02/23",
       
    89     "6.9.98 12:43pm",
       
    90     "Monday, February 22, 1999 10:24:43",
       
    91     "Wed Feb 24 19:35:02 1999 and a bunch more text",
       
    92     "Wed, 24 Feb 1999 05:12:21 GMT"
       
    93   };
       
    94    
       
    95   harness.debug("The following tests are informational only");
       
    96   for (int i = 0; i < date_strs.length; i++)
       
    97     {
       
    98       d = null;
       
    99       try
       
   100         {
       
   101           d = sdf.parse(date_strs[i]);
       
   102         }
       
   103       catch(Throwable e) { ; }
       
   104       if (d == null)
       
   105         harness.debug("Couldn't parse: " + date_strs[i]);
       
   106       else
       
   107         harness.debug("Parsed: " + date_strs[i] + " as: " + d);
       
   108     }
       
   109 
       
   110   sdf = new SimpleDateFormat("MM'/'dd'/'yyyy' 'H':'m':'s'.'SSS");
       
   111   boolean ok = true;
       
   112   try
       
   113     {
       
   114       d = sdf.parse("05/24/2002 14:30:53.700");
       
   115       // For the time being only check to make sure something
       
   116       // happened.
       
   117       ok = d != null;
       
   118     }
       
   119   catch (Exception _)
       
   120     {
       
   121       ok = false;
       
   122     }
       
   123   harness.check(ok, "format includes '.'");
       
   124 }
       
   125 
       
   126 } // class Test
       
   127