< prev index next >

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

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 1997, 2022, 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) 1997, 2024, 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,18 ***
  
  /*
   * @test
   * @summary test Date Format (Round Trip)
   * @bug 8008577
-  * @library /java/text/testlib
   * @run main/othervm -Djava.locale.providers=COMPAT,SPI DateFormatRoundTripTest
   */
  
! import java.text.*;
! import java.util.*;
! 
! public class DateFormatRoundTripTest extends IntlTest {
  
      static Random RANDOM = null;
  
      static final long FIXED_SEED = 3141592653589793238L; // Arbitrary fixed value
  
--- 23,28 ---
  
  /*
   * @test
   * @summary test Date Format (Round Trip)
   * @bug 8008577
   * @run main/othervm -Djava.locale.providers=COMPAT,SPI DateFormatRoundTripTest
   */
  
! import java.text.DateFormat;
! import java.text.ParseException;
! import java.text.SimpleDateFormat;
! import java.util.ArrayList;
+ import java.util.Arrays;
+ import java.util.Calendar;
+ import java.util.Date;
+ import java.util.GregorianCalendar;
+ import java.util.List;
+ import java.util.Locale;
+ import java.util.Random;
+ import java.util.SimpleTimeZone;
+ import java.util.TimeZone;
+ 
+ public class DateFormatRoundTripTest {
  
      static Random RANDOM = null;
  
      static final long FIXED_SEED = 3141592653589793238L; // Arbitrary fixed value
  

*** 105,30 ***
          Date date = null;
  
          List<String> newArgs = new ArrayList<>();
          for (int i=0; i<args.length; ++i) {
              if (args[i].equals("-locale")
!                 && (i+1) < args.length) {
                  loc = createLocale(args[i+1]);
                  ++i;
              } else if (args[i].equals("-date")
!                        && (i+1) < args.length) {
                  date = new Date(Long.parseLong(args[i+1]));
                  ++i;
              } else if (args[i].equals("-pattern")
!                 && (i+1) < args.length) {
                  pat = args[i+1];
                  ++i;
              } else if (args[i].equals("-INFINITE")) {
                  infinite = true;
              } else if (args[i].equals("-random")) {
                  random = true;
              } else if (args[i].equals("-randomseed")) {
                  random = true;
                  seed = System.currentTimeMillis();
              } else if (args[i].equals("-seed")
!                        && (i+1) < args.length) {
                  random = true;
                  seed = Long.parseLong(args[i+1]);
                  ++i;
              } else {
                  newArgs.add(args[i]);
--- 115,30 ---
          Date date = null;
  
          List<String> newArgs = new ArrayList<>();
          for (int i=0; i<args.length; ++i) {
              if (args[i].equals("-locale")
!                     && (i+1) < args.length) {
                  loc = createLocale(args[i+1]);
                  ++i;
              } else if (args[i].equals("-date")
!                     && (i+1) < args.length) {
                  date = new Date(Long.parseLong(args[i+1]));
                  ++i;
              } else if (args[i].equals("-pattern")
!                     && (i+1) < args.length) {
                  pat = args[i+1];
                  ++i;
              } else if (args[i].equals("-INFINITE")) {
                  infinite = true;
              } else if (args[i].equals("-random")) {
                  random = true;
              } else if (args[i].equals("-randomseed")) {
                  random = true;
                  seed = System.currentTimeMillis();
              } else if (args[i].equals("-seed")
!                     && (i+1) < args.length) {
                  random = true;
                  seed = Long.parseLong(args[i+1]);
                  ++i;
              } else {
                  newArgs.add(args[i]);

*** 138,11 ***
          if (newArgs.size() != args.length) {
              args = new String[newArgs.size()];
              newArgs.addAll(Arrays.asList(args));
          }
  
!         new DateFormatRoundTripTest(random, seed, infinite, date, pat, loc).run(args);
      }
  
      /**
       * Print a usage message for this test class.
       */
--- 148,12 ---
          if (newArgs.size() != args.length) {
              args = new String[newArgs.size()];
              newArgs.addAll(Arrays.asList(args));
          }
  
!         new DateFormatRoundTripTest(random, seed, infinite, date, pat, loc)
+                 .TestDateFormatRoundTrip();
      }
  
      /**
       * Print a usage message for this test class.
       */

*** 155,11 ***
          System.out.println("When specifying a locale, use a format such as fr_FR.");
          System.out.println("Use -pattern, -locale, and -date to reproduce a failure.");
          System.out.println("-random     Random with fixed seed (same data every run).");
          System.out.println("-randomseed Random with a random seed.");
          System.out.println("-seed <s>   Random using <s> as seed.");
-         super.usage();
      }
  
      static private class TestCase {
          private int[] date;
          TimeZone zone;
--- 166,10 ---

*** 308,12 ***
                       }}, false),
      };
  
      public void TestDateFormatRoundTrip() {
          avail = DateFormat.getAvailableLocales();
!         logln("DateFormat available locales: " + avail.length);
!         logln("Default TimeZone: " +
                (defaultZone = TimeZone.getDefault()).getID());
  
          if (random || initialDate != null) {
              if (RANDOM == null) {
                  // Need this for sparse coverage to reduce combinatorial explosion,
--- 318,12 ---
                       }}, false),
      };
  
      public void TestDateFormatRoundTrip() {
          avail = DateFormat.getAvailableLocales();
!         System.out.println("DateFormat available locales: " + avail.length);
!         System.out.println("Default TimeZone: " +
                (defaultZone = TimeZone.getDefault()).getID());
  
          if (random || initialDate != null) {
              if (RANDOM == null) {
                  // Need this for sparse coverage to reduce combinatorial explosion,

*** 331,11 ***
  
      /**
       * TimeZone must be set to tc.zone before this method is called.
       */
      private void doTestInZone(TestCase tc) {
!         logln(escape(tc.toString()));
          Locale save = Locale.getDefault();
          try {
              if (locale != null) {
                  Locale.setDefault(locale);
                  doTest(locale, tc.createFormat(), tc.timeOnly, tc.getDate());
--- 341,11 ---
  
      /**
       * TimeZone must be set to tc.zone before this method is called.
       */
      private void doTestInZone(TestCase tc) {
!         System.out.println(escape(tc.toString()));
          Locale save = Locale.getDefault();
          try {
              if (locale != null) {
                  Locale.setDefault(locale);
                  doTest(locale, tc.createFormat(), tc.timeOnly, tc.getDate());

*** 366,14 ***
  
      private void loopedTest() {
          if (INFINITE) {
              // Special infinite loop test mode for finding hard to reproduce errors
              if (locale != null) {
!                 logln("ENTERING INFINITE TEST LOOP, LOCALE " + locale.getDisplayName());
                  for (;;) doTest(locale);
              } else {
!                 logln("ENTERING INFINITE TEST LOOP, ALL LOCALES");
                  for (;;) {
                      for (int i=0; i<avail.length; ++i) {
                          doTest(avail[i]);
                      }
                  }
--- 376,14 ---
  
      private void loopedTest() {
          if (INFINITE) {
              // Special infinite loop test mode for finding hard to reproduce errors
              if (locale != null) {
!                 System.out.println("ENTERING INFINITE TEST LOOP, LOCALE " + locale.getDisplayName());
                  for (;;) doTest(locale);
              } else {
!                 System.out.println("ENTERING INFINITE TEST LOOP, ALL LOCALES");
                  for (;;) {
                      for (int i=0; i<avail.length; ++i) {
                          doTest(avail[i]);
                      }
                  }

*** 391,11 ***
              }
          }
      }
  
      void doTest(Locale loc) {
!         if (!INFINITE) logln("Locale: " + loc.getDisplayName());
  
          if (pattern != null) {
              doTest(loc, new SimpleDateFormat(pattern, loc));
              return;
          }
--- 401,11 ---
              }
          }
      }
  
      void doTest(Locale loc) {
!         if (!INFINITE) System.out.println("Locale: " + loc.getDisplayName());
  
          if (pattern != null) {
              doTest(loc, new SimpleDateFormat(pattern, loc));
              return;
          }

*** 602,16 ***
                                     (j>0?" P> ":"    ") + refFormat.format(d[j]) + " F> " +
                                     escape(s[j]) +
                                     (j>0&&d[j].getTime()==d[j-1].getTime()?" d==":"") +
                                     (j>0&&s[j].equals(s[j-1])?" s==":""));
                      }
!                     errln(escape(out.toString()));
                  }
              }
          }
          catch (ParseException e) {
!             errln(e.toString());
          }
      }
  
      /**
       * Return a field of the given date
--- 612,16 ---
                                     (j>0?" P> ":"    ") + refFormat.format(d[j]) + " F> " +
                                     escape(s[j]) +
                                     (j>0&&d[j].getTime()==d[j-1].getTime()?" d==":"") +
                                     (j>0&&s[j].equals(s[j-1])?" s==":""));
                      }
!                     throw new RuntimeException(escape(out.toString()));
                  }
              }
          }
          catch (ParseException e) {
!             throw new RuntimeException(e.toString());
          }
      }
  
      /**
       * Return a field of the given date
< prev index next >