< prev index next >

test/jdk/java/text/Format/DateFormat/IntlTestDateFormat.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.
--- 1,7 ---
  /*
!  * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

*** 23,12 ***
  
  /*
   * @test
   * @summary test International Date Format
   * @bug 8008577
!  * @library /java/text/testlib
-  * @run main/othervm -Djava.locale.providers=COMPAT,SPI IntlTestDateFormat
   * @key randomness
   */
  /*
  (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
--- 23,11 ---
  
  /*
   * @test
   * @summary test International Date Format
   * @bug 8008577
!  * @run junit/othervm -Djava.locale.providers=COMPAT,SPI IntlTestDateFormat
   * @key randomness
   */
  /*
  (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved

*** 42,11 ***
  */
  
  import java.text.*;
  import java.util.*;
  
! public class IntlTestDateFormat extends IntlTest {
      // Values in milliseconds (== Date)
      private static final long ONESECOND = 1000;
      private static final long ONEMINUTE = 60 * ONESECOND;
      private static final long ONEHOUR = 60 * ONEMINUTE;
      private static final long ONEDAY = 24 * ONEHOUR;
--- 41,15 ---
  */
  
  import java.text.*;
  import java.util.*;
  
! import org.junit.jupiter.api.Test;
+ 
+ import static org.junit.jupiter.api.Assertions.fail;
+ 
+ public class IntlTestDateFormat {
      // Values in milliseconds (== Date)
      private static final long ONESECOND = 1000;
      private static final long ONEMINUTE = 60 * ONESECOND;
      private static final long ONEHOUR = 60 * ONEMINUTE;
      private static final long ONEDAY = 24 * ONEHOUR;

*** 60,14 ***
  
      private DateFormat fFormat = DateFormat.getInstance();
      private String fTestName = new String("getInstance");
      private int fLimit = 3; // How many iterations it should take to reach convergence
  
!     public static void main(String[] args) throws Exception {
-         new IntlTestDateFormat().run(args);
-     }
- 
      public void TestLocale() {
          localeTest(Locale.getDefault(), "Default Locale");
      }
  
      // This test does round-trip testing (format -> parse -> format -> parse -> etc.) of DateFormat.
--- 63,11 ---
  
      private DateFormat fFormat = DateFormat.getInstance();
      private String fTestName = new String("getInstance");
      private int fLimit = 3; // How many iterations it should take to reach convergence
  
!     @Test
      public void TestLocale() {
          localeTest(Locale.getDefault(), "Default Locale");
      }
  
      // This test does round-trip testing (format -> parse -> format -> parse -> etc.) of DateFormat.

*** 83,11 ***
              fTestName = new String("Time test " + timeStyle + " (" + localeName + ")");
              try {
                  fFormat = DateFormat.getTimeInstance(timeStyle, locale);
              }
              catch(StringIndexOutOfBoundsException e) {
!                 errln("FAIL: localeTest time getTimeInstance exception");
                  throw e;
              }
              TestFormat();
          }
  
--- 83,11 ---
              fTestName = new String("Time test " + timeStyle + " (" + localeName + ")");
              try {
                  fFormat = DateFormat.getTimeInstance(timeStyle, locale);
              }
              catch(StringIndexOutOfBoundsException e) {
!                 fail("FAIL: localeTest time getTimeInstance exception");
                  throw e;
              }
              TestFormat();
          }
  

*** 97,11 ***
              fTestName = new String("Date test " + dateStyle + " (" + localeName + ")");
              try {
                  fFormat = DateFormat.getDateInstance(dateStyle, locale);
              }
              catch(StringIndexOutOfBoundsException e) {
!                 errln("FAIL: localeTest date getTimeInstance exception");
                  throw e;
              }
              TestFormat();
          }
  
--- 97,11 ---
              fTestName = new String("Date test " + dateStyle + " (" + localeName + ")");
              try {
                  fFormat = DateFormat.getDateInstance(dateStyle, locale);
              }
              catch(StringIndexOutOfBoundsException e) {
!                 fail("FAIL: localeTest date getTimeInstance exception");
                  throw e;
              }
              TestFormat();
          }
  

*** 110,21 ***
                  fTestName = new String("DateTime test " + dateStyle + "/" + timeStyle + " (" + localeName + ")");
                  try {
                      fFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
                  }
                  catch(StringIndexOutOfBoundsException e) {
!                     errln("FAIL: localeTest date/time getDateTimeInstance exception");
                      throw e;
                  }
                  TestFormat();
              }
          }
      }
  
      public void TestFormat() {
          if (fFormat == null) {
!             errln("FAIL: DateFormat creation failed");
              return;
          }
          //        logln("TestFormat: " + fTestName);
          Date now = new Date();
          tryDate(new Date(0));
--- 110,22 ---
                  fTestName = new String("DateTime test " + dateStyle + "/" + timeStyle + " (" + localeName + ")");
                  try {
                      fFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
                  }
                  catch(StringIndexOutOfBoundsException e) {
!                     fail("FAIL: localeTest date/time getDateTimeInstance exception");
                      throw e;
                  }
                  TestFormat();
              }
          }
      }
  
+     @Test
      public void TestFormat() {
          if (fFormat == null) {
!             fail("FAIL: DateFormat creation failed");
              return;
          }
          //        logln("TestFormat: " + fTestName);
          Date now = new Date();
          tryDate(new Date(0));

*** 140,17 ***
              tryDate(new Date((long) (randDouble() * limit.getTime())));
      }
  
      private void describeTest() {
          if (fFormat == null) {
!             errln("FAIL: no DateFormat");
              return;
          }
  
          // Assume it's a SimpleDateFormat and get some info
          SimpleDateFormat s = (SimpleDateFormat) fFormat;
!         logln(fTestName + " Pattern " + s.toPattern());
      }
  
      private void tryDate(Date theDate) {
          final int DEPTH = 10;
          Date[] date = new Date[DEPTH];
--- 141,17 ---
              tryDate(new Date((long) (randDouble() * limit.getTime())));
      }
  
      private void describeTest() {
          if (fFormat == null) {
!             fail("FAIL: no DateFormat");
              return;
          }
  
          // Assume it's a SimpleDateFormat and get some info
          SimpleDateFormat s = (SimpleDateFormat) fFormat;
!         System.out.println(fTestName + " Pattern " + s.toPattern());
      }
  
      private void tryDate(Date theDate) {
          final int DEPTH = 10;
          Date[] date = new Date[DEPTH];

*** 167,46 ***
                  try {
                      date[i] = fFormat.parse(string[i-1].toString());
                  }
                  catch (ParseException e) {
                      describeTest();
!                     errln("********** FAIL: Parse of " + string[i-1] + " failed.");
                      dump = true;
                      break;
                  }
              }
              FieldPosition position = new FieldPosition(0);
              fFormat.format(date[i], string[i], position);
              if (i > 0) {
                  if (dateMatch == 0 && date[i] == date[i-1]) dateMatch = i;
                  else if (dateMatch > 0 && date[i] != date[i-1]) {
                      describeTest();
!                     errln("********** FAIL: Date mismatch after match.");
                      dump = true;
                      break;
                  }
                  if (stringMatch == 0 && string[i] == string[i-1]) stringMatch = i;
                  else if (stringMatch > 0 && string[i] != string[i-1]) {
                      describeTest();
!                     errln("********** FAIL: String mismatch after match.");
                      dump = true;
                      break;
                  }
              }
              if (dateMatch > 0 && stringMatch > 0) break;
          }
          if (i == DEPTH) --i;
  
          if (stringMatch > fLimit || dateMatch > fLimit) {
              describeTest();
!             errln("********** FAIL: No string and/or date match within " + fLimit + " iterations.");
              dump = true;
          }
  
          if (dump) {
              for (int k=0; k<=i; ++k) {
!                 logln("" + k + ": " + date[k] + " F> " + string[k] + " P> ");
              }
          }
      }
  
      // Return a random double from 0.01 to 1, inclusive
--- 168,46 ---
                  try {
                      date[i] = fFormat.parse(string[i-1].toString());
                  }
                  catch (ParseException e) {
                      describeTest();
!                     fail("********** FAIL: Parse of " + string[i-1] + " failed.");
                      dump = true;
                      break;
                  }
              }
              FieldPosition position = new FieldPosition(0);
              fFormat.format(date[i], string[i], position);
              if (i > 0) {
                  if (dateMatch == 0 && date[i] == date[i-1]) dateMatch = i;
                  else if (dateMatch > 0 && date[i] != date[i-1]) {
                      describeTest();
!                     fail("********** FAIL: Date mismatch after match.");
                      dump = true;
                      break;
                  }
                  if (stringMatch == 0 && string[i] == string[i-1]) stringMatch = i;
                  else if (stringMatch > 0 && string[i] != string[i-1]) {
                      describeTest();
!                     fail("********** FAIL: String mismatch after match.");
                      dump = true;
                      break;
                  }
              }
              if (dateMatch > 0 && stringMatch > 0) break;
          }
          if (i == DEPTH) --i;
  
          if (stringMatch > fLimit || dateMatch > fLimit) {
              describeTest();
!             fail("********** FAIL: No string and/or date match within " + fLimit + " iterations.");
              dump = true;
          }
  
          if (dump) {
              for (int k=0; k<=i; ++k) {
!                 System.out.println("" + k + ": " + date[k] + " F> " + string[k] + " P> ");
              }
          }
      }
  
      // Return a random double from 0.01 to 1, inclusive

*** 233,38 ***
          //        return d;
          Random rand = new Random();
          return rand.nextDouble();
      }
  
      public void TestAvailableLocales() {
          final Locale[] locales = DateFormat.getAvailableLocales();
          long count = locales.length;
!         logln("" + count + " available locales");
          if (locales != null  &&  count != 0) {
              StringBuffer all = new StringBuffer();
              for (int i=0; i<count; ++i) {
                  if (i!=0) all.append(", ");
                  all.append(locales[i].getDisplayName());
              }
!             logln(all.toString());
          }
!         else errln("********** FAIL: Zero available locales or null array pointer");
      }
  
      /* This test is too slow; we disable it for now
      public void TestMonster() {
          final Locale[] locales = DateFormat.getAvailableLocales();
          long count = locales.length;
          if (locales != null  &&  count != 0) {
              for (int i=0; i<count; ++i) {
                  String name = locales[i].getDisplayName();
!                 logln("Testing " + name + "...");
                  try {
                      localeTest(locales[i], name);
                  }
                  catch(Exception e) {
!                     errln("FAIL: TestMonster localeTest exception" + e);
                  }
              }
          }
      }
      */
--- 234,40 ---
          //        return d;
          Random rand = new Random();
          return rand.nextDouble();
      }
  
+     @Test
      public void TestAvailableLocales() {
          final Locale[] locales = DateFormat.getAvailableLocales();
          long count = locales.length;
!         System.out.println("" + count + " available locales");
          if (locales != null  &&  count != 0) {
              StringBuffer all = new StringBuffer();
              for (int i=0; i<count; ++i) {
                  if (i!=0) all.append(", ");
                  all.append(locales[i].getDisplayName());
              }
!             System.out.println(all.toString());
          }
!         else fail("********** FAIL: Zero available locales or null array pointer");
      }
  
      /* This test is too slow; we disable it for now
+     @Test
      public void TestMonster() {
          final Locale[] locales = DateFormat.getAvailableLocales();
          long count = locales.length;
          if (locales != null  &&  count != 0) {
              for (int i=0; i<count; ++i) {
                  String name = locales[i].getDisplayName();
!                 System.out.println("Testing " + name + "...");
                  try {
                      localeTest(locales[i], name);
                  }
                  catch(Exception e) {
!                     fail("FAIL: TestMonster localeTest exception" + e);
                  }
              }
          }
      }
      */
< prev index next >