File: Test.java

package info (click to toggle)
mauve 20120103-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,504 kB
  • sloc: java: 250,155; sh: 2,834; xml: 208; makefile: 66
file content (127 lines) | stat: -rw-r--r-- 3,930 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*************************************************************************
/* Test.java -- Test java.text.SimpleDateFormat
/*
/* Copyright (c) 1998, 1999, 2001, 2003 Free Software Foundation, Inc.
/* Written by Aaron M. Renn (arenn@urbanophile.com)
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU General Public License as published 
/* by the Free Software Foundation, either version 2 of the License, or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software Foundation
/* Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
/*************************************************************************/

// Tags: JDK1.1

package gnu.testlet.java.text.SimpleDateFormat;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.text.*;
import java.util.*;

public class Test implements Testlet
{

public void 
test(TestHarness harness)
{
  String pattern_chars = "GyMdhHmsSEDFwWakKz";
  String pattern = "EEEE, MMMM d, yyyy h:mm:ss 'o''clock' a";

  DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
  SimpleDateFormat sdf = new SimpleDateFormat(pattern, dfs);
  harness.check(sdf.getDateFormatSymbols(), dfs, "getDateFormatSymbols() init");

  String[] ampms = { "am ", "pm " };
  dfs.setAmPmStrings(ampms);
  sdf.setDateFormatSymbols(dfs);
  harness.check(sdf.getDateFormatSymbols(), dfs, "set/getDateFormatSymbols()");
  
  harness.check(sdf.toPattern(), pattern, "toPattern init");
  String new_pattern = "EMdyH";
  sdf.applyPattern(new_pattern);
  harness.check(sdf.toPattern(), new_pattern, "apply/toPattern()");
  sdf.applyPattern(pattern);

  harness.check(sdf.equals(new SimpleDateFormat(pattern, dfs)), "equals()");
  harness.check(sdf.clone().equals(sdf) == true, "clone()");

  sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  Date d = new Date(0);
  String formatted_date = sdf.format(d);
  harness.debug(formatted_date);
  harness.check(formatted_date.equals(
     "Thursday, January 1, 1970 12:00:00 o'clock am "), "format()");

  sdf.setLenient(false);
  try
    {
      harness.check(sdf.parse(formatted_date), d, "parse() strict");
    }
  catch(Throwable e)
    {
      harness.debug(e);
      harness.check(false, "parse() strict");
    }

  sdf.setTimeZone(TimeZone.getDefault());
  harness.debug(sdf.format(new Date(System.currentTimeMillis())));

  // Now do some lenient parsing tests.  These might not all work.
  dfs = new DateFormatSymbols(Locale.US);
  sdf = new SimpleDateFormat(pattern, dfs);

  sdf.setLenient(true);

  String[] date_strs = { 
    "Tue Feb 23 20:15:34 CST 1999",
    "10/31/69",
    "1999/02/23",
    "6.9.98 12:43pm",
    "Monday, February 22, 1999 10:24:43",
    "Wed Feb 24 19:35:02 1999 and a bunch more text",
    "Wed, 24 Feb 1999 05:12:21 GMT"
  };
   
  harness.debug("The following tests are informational only");
  for (int i = 0; i < date_strs.length; i++)
    {
      d = null;
      try
        {
          d = sdf.parse(date_strs[i]);
        }
      catch(Throwable e) { ; }
      if (d == null)
        harness.debug("Couldn't parse: " + date_strs[i]);
      else
        harness.debug("Parsed: " + date_strs[i] + " as: " + d);
    }

  sdf = new SimpleDateFormat("MM'/'dd'/'yyyy' 'H':'m':'s'.'SSS");
  boolean ok = true;
  try
    {
      d = sdf.parse("05/24/2002 14:30:53.700");
      // For the time being only check to make sure something
      // happened.
      ok = d != null;
    }
  catch (Exception _)
    {
      ok = false;
    }
  harness.check(ok, "format includes '.'");
}

} // class Test