File: parse.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 (141 lines) | stat: -rw-r--r-- 4,482 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Tags: JDK1.1

// Copyright (C) 2004, 2005 Noa Resare <noa@resare.com>

// This file is part of Mauve.

// Mauve 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, or (at your option)
// any later version.

// Mauve 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 Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.  */

package gnu.testlet.java.text.SimpleDateFormat;

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

public class parse implements Testlet
{
  public void test(TestHarness harness)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy", Locale.UK);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    Map toTest = new HashMap();
    toTest.put("august 1978", new Date(270777600000L));
    toTest.put("August 1978", new Date(270777600000L));
    toTest.put("December 1978", new Date(281318400000L));
    doParse(harness, sdf, toTest);

    sdf.applyPattern("EEEE MMMM yyyy");
    toTest.clear();
    toTest.put("Saturday November 2004", new Date(1099699200000L));
    doParse(harness, sdf, toTest);

    sdf.applyPattern("yyyy-MM-dd HH:mm z");
    toTest.clear();
    toTest.put("2004-08-11 10:42 GMT", new Date(1092220920000L));
    toTest.put("2004-08-11 10:42 GMT+00:00", new Date(1092220920000L));
    toTest.put("2004-08-11 10:42 GMT-00:00", new Date(1092220920000L));
    toTest.put("2004-08-11 12:42 CEST", new Date(1092220920000L));
    toTest.put("2004-08-11 12:42 GMT+02:00", new Date(1092220920000L));
    toTest.put("2004-08-11 12:42 +0200", new Date(1092220920000L));
    doParse(harness, sdf, toTest);

    // Z should work exactly as z when parsing
    sdf.applyPattern("yyyy-MM-dd HH:mm Z");
    doParse(harness, sdf, toTest);

    // long and short names should both work.
    sdf = new SimpleDateFormat("EEE MMM", Locale.UK);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    toTest.clear();
    toTest.put("Sat Jun", new Date(13478400000L)); //saturday, june 6th, 1970, 02:00:00
    toTest.put("Saturday June", new Date(13478400000L));
    doParse(harness, sdf, toTest);
    sdf.applyPattern("EEEE MMMM");
    doParse(harness, sdf, toTest);

    /* Test case from bug #11583 */
    SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd, yyyy", Locale.UK);
    sdf1.setTimeZone(TimeZone.getTimeZone("UTC"));
    toTest = new HashMap();
    toTest.put("dec 31, 2004", new Date(1104451200000L));
    doParse(harness, sdf1, toTest);
    
    // test a case that is failing in statcvs and is the same as (I think) the
    // bug described in bug 13058
    harness.checkPoint("Bug 13058");
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzzzz", Locale.US);
    Date d = null;
    try
    {
      d = sdf2.parse("2004-07-18 17:42:25 +0000 GMT");        
    }
    catch (ParseException e)
    {
      // failure will be caught below  
    }
    harness.check(new Date(1090172545000L).equals(d));
    
    // test null arguments
    harness.checkPoint("Null arguments");
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    boolean pass = false;
    try
    {
      df.parse(null, new ParsePosition(0));
    }
    catch (NullPointerException e)
    {
      pass = true;   
    }
    harness.check(pass);
    
    pass = false;
    try
    {
      df.parse("17-May-2005", null);   
    }
    catch (NullPointerException e)
    {
      pass = true;   
    }
    harness.check(pass);
  }

  /**
   * Test if the date strings in toTest equals to the Date values when parsed
   * with sdf.
   */
  private static void doParse(TestHarness h, SimpleDateFormat sdf, Map toTest)
  {
    h.checkPoint("parse pattern " + sdf.toPattern());
    Iterator cases = toTest.keySet().iterator();
    while (cases.hasNext())
      {
	String dateString = (String)cases.next();
        try
	  {
	    h.check(sdf.parse(dateString), toTest.get(dateString));
          }
        catch(Exception e)
	  {
	    h.check(false, e.getClass().getName() + ": ");
	    h.debug(e);
	  }
      }
  }
}