tests/libjava-mauve/src/gnu/testlet/java/util/TimeZone/zdump.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Copyright (C) 2007 Red Hat, Inc.
       
     2 // Written by Gary Benson <gbenson@redhat.com>
       
     3 // Based on code by Jakub Jelinek <jakub@redhat.com>
       
     4 
       
     5 // This file is part of Mauve.
       
     6 
       
     7 // Mauve is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2, or (at your option)
       
    10 // any later version.
       
    11 
       
    12 // Mauve is distributed in the hope that it will be useful,
       
    13 // but 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 Mauve; see the file COPYING.  If not, write to
       
    19 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    20 // Boston, MA 02111-1307, USA.
       
    21 
       
    22 // Tags: JDK1.4
       
    23 
       
    24 package gnu.testlet.java.util.TimeZone;
       
    25 
       
    26 import java.io.BufferedReader;
       
    27 import java.io.File;
       
    28 import java.io.IOException;
       
    29 import java.io.InputStreamReader;
       
    30 import java.util.Calendar;
       
    31 import java.util.Date;
       
    32 import java.util.SimpleTimeZone;
       
    33 import java.util.TimeZone;
       
    34 
       
    35 import gnu.testlet.Testlet;
       
    36 import gnu.testlet.TestHarness;
       
    37 
       
    38 public class zdump implements Testlet
       
    39 {
       
    40   public static final String zdump = "/usr/sbin/zdump";
       
    41   
       
    42   public void test(TestHarness harness)
       
    43   {
       
    44     String zoneinfodir = System.getProperty("gnu.java.util.zoneinfo.dir");
       
    45     if (zoneinfodir == null)
       
    46       return;
       
    47 
       
    48     if (!new File(zdump).exists() || !new File(zoneinfodir).isDirectory())
       
    49       return;
       
    50 
       
    51     TimeZone utc = (TimeZone) new SimpleTimeZone(0, "GMT");
       
    52     TimeZone.setDefault(utc);
       
    53 
       
    54     String[] zones = TimeZone.getAvailableIDs();
       
    55     for (int i = 0; i < zones.length; i++)
       
    56       {
       
    57 	if (!new File(zoneinfodir, zones[i]).exists())
       
    58 	  continue;
       
    59 
       
    60 	// These two timezones have different definitions between
       
    61 	// tzdata and JDK.  In JDK EST is EST5EDT, while in tzdata
       
    62 	// just EST5, similarly for MST.
       
    63 	if (zones[i].equals("EST") || zones[i].equals("MST"))
       
    64 	  continue;
       
    65 
       
    66 	checkZone(harness, zones[i]);
       
    67       }
       
    68   }
       
    69 
       
    70   public static void checkZone(TestHarness harness, String zone)
       
    71   {
       
    72     harness.checkPoint(zone);
       
    73     
       
    74     TimeZone tz = TimeZone.getTimeZone(zone);
       
    75     if (tz == null)
       
    76       {
       
    77 	harness.check(false);
       
    78 	return;
       
    79       }
       
    80 
       
    81     Calendar cal = Calendar.getInstance(tz);
       
    82 
       
    83     BufferedReader br = null;
       
    84     Process process = null;
       
    85     try
       
    86       {
       
    87 	process = Runtime.getRuntime().exec(zdump + " -v " + zone);
       
    88 	br = new BufferedReader(new InputStreamReader(
       
    89 	  process.getInputStream()));
       
    90 	    
       
    91 	for (String line = br.readLine(); line != null; line = br.readLine())
       
    92 	  {
       
    93 	    int end1 = line.indexOf(" UTC = ");
       
    94 	    if (end1 < 0)
       
    95 	      continue;
       
    96 	    int start1 = line.indexOf("  ");
       
    97 	    if (start1 < 0 || start1 >= end1)
       
    98 	      continue;
       
    99 	    int start2 = line.indexOf(" isdst=");
       
   100 	    int start3 = line.indexOf(" gmtoff=");
       
   101 	    if (start2 <= end1 || start3 <= start2)
       
   102 	      continue;
       
   103 
       
   104 	    Date d = new Date(line.substring(start1 + 2, end1 + 4));
       
   105 	    cal.setTime(d);
       
   106 
       
   107 	    int isdst = Integer.parseInt(line.substring(start2 + 7, start3));
       
   108 	    int gmtoff = Integer.parseInt(
       
   109 	      line.substring(start3 + 8, line.length()));
       
   110 
       
   111 	    harness.debug("Zone " + zone + " " + d +
       
   112 			  " isdst=" + isdst +
       
   113 			  " inDaylightTime=" + tz.inDaylightTime(d));
       
   114 	    harness.check(tz.inDaylightTime(d) == (isdst != 0));
       
   115 
       
   116 	    harness.debug("Zone " + zone + " " + d +
       
   117 			  " gmtoff=" + gmtoff +
       
   118 			  " getOffset=" + tz.getOffset(d.getTime()));
       
   119 	    harness.check(tz.getOffset(d.getTime()) == gmtoff * 1000);
       
   120 
       
   121 	    int offset = cal.get(Calendar.DST_OFFSET) +
       
   122 	      cal.get(Calendar.ZONE_OFFSET);
       
   123 	    
       
   124 	    harness.debug("Zone " + zone + " " + d +
       
   125 			  " gmtoff=" + gmtoff +
       
   126 			  " DST_OFFSET+ZONE_OFFSET=" + offset);
       
   127 	    harness.check(offset == gmtoff * 1000);
       
   128 	  }
       
   129       }
       
   130     catch (IOException ioe)
       
   131       {
       
   132       }
       
   133     finally
       
   134       {
       
   135 	try
       
   136 	  {
       
   137 	    if (br != null)
       
   138 	      br.close();
       
   139 	    if (process != null)
       
   140 	      {
       
   141 		process.waitFor();
       
   142 		process.exitValue();
       
   143 	      }
       
   144 	  }
       
   145 	catch (IOException ioe)
       
   146 	  {
       
   147 	  }
       
   148 	catch (InterruptedException ine)
       
   149 	  {
       
   150 	  }
       
   151       }
       
   152   }
       
   153 }