< prev index next >

test/jdk/java/text/Format/common/FormatIteratorTest.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2000, 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) 2000, 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.

*** 22,13 ***
   */
  
  /**
   * @test
   * @bug 4018937
!  * @library /java/text/testlib
!  * @build FormatIteratorTest PParser IntlTest
-  * @run main FormatIteratorTest
   * @summary Tests the formatToCharacterIterator method of SimpleDateFormat,
   *          MessageFormat and DecimalFormat.
   */
  
  import java.io.*;
--- 22,12 ---
   */
  
  /**
   * @test
   * @bug 4018937
!  * @build FormatIteratorTest PParser
!  * @run junit/othervm FormatIteratorTest
   * @summary Tests the formatToCharacterIterator method of SimpleDateFormat,
   *          MessageFormat and DecimalFormat.
   */
  
  import java.io.*;

*** 37,10 ***
--- 36,15 ---
  import java.util.*;
  import java.util.concurrent.atomic.AtomicInteger;
  import java.util.concurrent.atomic.AtomicLong;
  import java.text.AttributedCharacterIterator.Attribute;
  
+ import org.junit.jupiter.api.Test;
+ import org.junit.jupiter.api.BeforeAll;
+ 
+ import static org.junit.jupiter.api.Assertions.fail;
+ 
  /**
   * FormatTester creates Formats, and tests the resulting FieldPositions
   * and AttributedCharacterIterator based on a file. The file is a hierarchical
   * set of key/value pairs, where each value can also be an array or map. The
   * top map must contain a tests entry, which will be an array consisting

*** 92,79 ***
   *   begin = begin index of FieldPosition after formatting
   *   end = end index of FieldPosition after formatting
   * </pre>
   * Any lines starting with {@code '#'} are comment lines and ignored.
   */
! public class FormatIteratorTest extends IntlTest {
      private Format format;
      private Object value;
      private String text;
  
-     public static final Object ARG0_FIELD_ID = MessageFormat.
-                                                      Field.ARGUMENT;
-     public static final Object ARG1_FIELD_ID = MessageFormat.
-                                                      Field.ARGUMENT;
-     public static final Object ARG2_FIELD_ID = MessageFormat.
-                                                      Field.ARGUMENT;
-     public static final Object ARG3_FIELD_ID = MessageFormat.
-                                                      Field.ARGUMENT;
- 
-     public static void main(String[] args) throws Exception {
-         Locale reservedLocale = Locale.getDefault();
-         TimeZone reservedTimeZone = TimeZone.getDefault();
-         try {
-             // The current tests are only appropriate for US. If tests are
-             // added for other locales are added, then a property should be
-             // added to each file (test) to be able to specify the locale.
-             Locale.setDefault(Locale.US);
-             TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
-             new FormatIteratorTest().run(args);
-         } finally {
-             // restore the reserved locale and time zone
-             Locale.setDefault(reservedLocale);
-             TimeZone.setDefault(reservedTimeZone);
-         }
-     }
- 
      public FormatIteratorTest() {
      }
  
      public void testDecimalFormat() {
          _test(new File(System.getProperty("test.src", "."),
                         "decimalFormat.props"));
      }
  
      public void testMessageFormat() {
          _test(new File(System.getProperty("test.src", "."),
                         "messageFormat.props"));
      }
  
      public void testDateFormat() {
          _test(new File(System.getProperty("test.src", "."),
                         "dateFormat.props"));
      }
  
      @SuppressWarnings("unchecked")
      private void _test(File file) {
          try {
!             logln("testing: " + file);
              PParser parser = new PParser();
              Map<String,Object> contents = parser.parse(new BufferedReader(
                  new FileReader(file)));
              List<Object> test = (List)contents.get("tests");
  
              for (int counter = 0; counter < test.size(); counter++) {
!                 logln("creating: " + (counter / 2));
  
                  AttributedCharacterIterator iterator =
                      create((Map)test.get(counter));
  
!                 logln("verifying: " + (counter / 2));
                  verify(iterator, (Map)test.get(++counter));
              }
          } catch (IOException ioe) {
!             errln("Error reading: " + ioe);
          }
      }
  
      @SuppressWarnings("unchecked")
      public void verify(AttributedCharacterIterator iterator,Map<String,Object> table) {
--- 96,69 ---
   *   begin = begin index of FieldPosition after formatting
   *   end = end index of FieldPosition after formatting
   * </pre>
   * Any lines starting with {@code '#'} are comment lines and ignored.
   */
! public class FormatIteratorTest {
+ 
+     /*
+      * Change JVM default Locale and TimeZone.
+      * The current tests are only appropriate for US. If tests are
+      * added for other locales are added, then a property should be
+      * added to each file (test) to be able to specify the locale.
+      */
+     @BeforeAll
+     static void initAll() {
+         Locale.setDefault(Locale.US);
+         TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
+     }
+ 
      private Format format;
      private Object value;
      private String text;
  
      public FormatIteratorTest() {
      }
  
+     @Test
      public void testDecimalFormat() {
          _test(new File(System.getProperty("test.src", "."),
                         "decimalFormat.props"));
      }
  
+     @Test
      public void testMessageFormat() {
          _test(new File(System.getProperty("test.src", "."),
                         "messageFormat.props"));
      }
  
+     @Test
      public void testDateFormat() {
          _test(new File(System.getProperty("test.src", "."),
                         "dateFormat.props"));
      }
  
      @SuppressWarnings("unchecked")
      private void _test(File file) {
          try {
!             System.out.println("testing: " + file);
              PParser parser = new PParser();
              Map<String,Object> contents = parser.parse(new BufferedReader(
                  new FileReader(file)));
              List<Object> test = (List)contents.get("tests");
  
              for (int counter = 0; counter < test.size(); counter++) {
!                 System.out.println("creating: " + (counter / 2));
  
                  AttributedCharacterIterator iterator =
                      create((Map)test.get(counter));
  
!                 System.out.println("verifying: " + (counter / 2));
                  verify(iterator, (Map)test.get(++counter));
              }
          } catch (IOException ioe) {
!             fail("Error reading: " + ioe);
          }
      }
  
      @SuppressWarnings("unchecked")
      public void verify(AttributedCharacterIterator iterator,Map<String,Object> table) {

*** 173,35 ***
          // Verify the text
          if (!getText(iterator).equals(
                  escapeIfNecessary((String)table.get("text")))) {
              String text = getText(iterator);
  
!             errln("text doesn't match, got: " + getText(iterator));
          }
          if (iterator.getBeginIndex() != 0) {
!             errln("Bogus start: " + iterator.getBeginIndex());
          }
          if (iterator.getEndIndex() != length) {
!             errln("Bogus end: " + iterator.getEndIndex());
          }
          for (int counter = 0; counter < length; counter++) {
              iterator.setIndex(counter);
              if (!verifyAttributes(iterator.getAttributes().keySet(),
                      makeAttributes((List)table.get(Integer.
                                                        toString(counter))))) {
!                 errln("Attributes don't match at " + counter + " expecting " +
                        makeAttributes((List)table.get(Integer.toString
                                                         (counter))) + " got " +
                        iterator.getAttributes().keySet());
              }
          }
          for (int counter = length - 1; counter >= 0; counter--) {
              iterator.setIndex(counter);
              if (!verifyAttributes(iterator.getAttributes().keySet(),
                      makeAttributes((List)table.get(Integer.
                                                        toString(counter))))) {
!                 errln("Attributes don't match at " + counter + " expecting " +
                        makeAttributes((List)table.get(Integer.toString
                                                         (counter))) + " got " +
                        iterator.getAttributes().keySet());
              }
          }
--- 167,35 ---
          // Verify the text
          if (!getText(iterator).equals(
                  escapeIfNecessary((String)table.get("text")))) {
              String text = getText(iterator);
  
!             fail("text doesn't match, got: " + getText(iterator));
          }
          if (iterator.getBeginIndex() != 0) {
!             fail("Bogus start: " + iterator.getBeginIndex());
          }
          if (iterator.getEndIndex() != length) {
!             fail("Bogus end: " + iterator.getEndIndex());
          }
          for (int counter = 0; counter < length; counter++) {
              iterator.setIndex(counter);
              if (!verifyAttributes(iterator.getAttributes().keySet(),
                      makeAttributes((List)table.get(Integer.
                                                        toString(counter))))) {
!                 fail("Attributes don't match at " + counter + " expecting " +
                        makeAttributes((List)table.get(Integer.toString
                                                         (counter))) + " got " +
                        iterator.getAttributes().keySet());
              }
          }
          for (int counter = length - 1; counter >= 0; counter--) {
              iterator.setIndex(counter);
              if (!verifyAttributes(iterator.getAttributes().keySet(),
                      makeAttributes((List)table.get(Integer.
                                                        toString(counter))))) {
!                 fail("Attributes don't match at " + counter + " expecting " +
                        makeAttributes((List)table.get(Integer.toString
                                                         (counter))) + " got " +
                        iterator.getAttributes().keySet());
              }
          }

*** 241,26 ***
          int end2 = (end2S != null) ? Integer.parseInt(end2S) : end;
  
          for (int counter = begin; counter < end; counter++) {
              iterator.setIndex(counter);
              if (iterator.getRunStart() != begin) {
!                 errln("Begin doesn't match want " + begin + " got " +
                        iterator.getRunStart() + " at " + counter + " attrs " +
                        attrs);
              }
              if (iterator.getRunStart(attrs) != begin2) {
!                 errln("Begin2 doesn't match want " + begin2 + " got " +
                        iterator.getRunStart(attrs) + " at " + counter +
                        " attrs " + attrs);
              }
              if (iterator.getRunLimit() != end) {
!                 errln("End doesn't match want " + end + " got " +
                        iterator.getRunLimit() + " at " + counter + " attrs " +
                        attrs);
              }
              if (iterator.getRunLimit(attrs) != end2) {
!                 errln("End2 doesn't match want " + end2 + " got " +
                        iterator.getRunLimit(attrs) + " at " + counter +
                        " attrs " + attrs);
              }
          }
      }
--- 235,26 ---
          int end2 = (end2S != null) ? Integer.parseInt(end2S) : end;
  
          for (int counter = begin; counter < end; counter++) {
              iterator.setIndex(counter);
              if (iterator.getRunStart() != begin) {
!                 fail("Begin doesn't match want " + begin + " got " +
                        iterator.getRunStart() + " at " + counter + " attrs " +
                        attrs);
              }
              if (iterator.getRunStart(attrs) != begin2) {
!                 fail("Begin2 doesn't match want " + begin2 + " got " +
                        iterator.getRunStart(attrs) + " at " + counter +
                        " attrs " + attrs);
              }
              if (iterator.getRunLimit() != end) {
!                 fail("End doesn't match want " + end + " got " +
                        iterator.getRunLimit() + " at " + counter + " attrs " +
                        attrs);
              }
              if (iterator.getRunLimit(attrs) != end2) {
!                 fail("End2 doesn't match want " + end2 + " got " +
                        iterator.getRunLimit(attrs) + " at " + counter +
                        " attrs " + attrs);
              }
          }
      }

*** 310,34 ***
                                       int index) {
          StringBuffer buffer = new StringBuffer();
  
          format.format(value, buffer, fp);
          if (fp.getBeginIndex() != begin) {
!             errln("bogus begin want " + begin + " got " + fp.getBeginIndex() +
                    " for " + fp + " at " + index);
          }
          if (fp.getEndIndex() != end) {
!             errln("bogus end want " + end + " got " + fp.getEndIndex() +
                    " for " + fp + " at " + index);
          }
          if (!buffer.toString().equals(text)) {
!             errln("Text does not match, want !" + buffer.toString() +
                    "! got !" + text + "!");
          }
      }
  
      public AttributedCharacterIterator create(Map<String,Object> table) {
          format = (Format)createInstance((String)table.get("class"),
                                          ((List)table.get("args")).toArray());
          value = createInstance((String)table.get("valueClass"),
                                 ((List)table.get("valueArgs")).toArray());
  
!         logln("Created format: " + format + " value " + value);
          AttributedCharacterIterator aci = format.
                             formatToCharacterIterator(value);
  
!         logln("Obtained Iterator: " + aci);
          return aci;
      }
  
      public Format.Field makeAttribute(String name) {
          return (Format.Field)lookupField(name);
--- 304,34 ---
                                       int index) {
          StringBuffer buffer = new StringBuffer();
  
          format.format(value, buffer, fp);
          if (fp.getBeginIndex() != begin) {
!             fail("bogus begin want " + begin + " got " + fp.getBeginIndex() +
                    " for " + fp + " at " + index);
          }
          if (fp.getEndIndex() != end) {
!             fail("bogus end want " + end + " got " + fp.getEndIndex() +
                    " for " + fp + " at " + index);
          }
          if (!buffer.toString().equals(text)) {
!             fail("Text does not match, want !" + buffer.toString() +
                    "! got !" + text + "!");
          }
      }
  
      public AttributedCharacterIterator create(Map<String,Object> table) {
          format = (Format)createInstance((String)table.get("class"),
                                          ((List)table.get("args")).toArray());
          value = createInstance((String)table.get("valueClass"),
                                 ((List)table.get("valueArgs")).toArray());
  
!         System.out.println("Created format: " + format + " value " + value);
          AttributedCharacterIterator aci = format.
                             formatToCharacterIterator(value);
  
!         System.out.println("Obtained Iterator: " + aci);
          return aci;
      }
  
      public Format.Field makeAttribute(String name) {
          return (Format.Field)lookupField(name);

*** 371,11 ***
                  Object value = cons.newInstance(args);
  
                  return value;
              }
          } catch (Throwable th) {
!             errln("Error creating instance " + th);
              return null;
          }
      }
  
      private  Class<?> lookupClass(String name) throws ClassNotFoundException {
--- 365,11 ---
                  Object value = cons.newInstance(args);
  
                  return value;
              }
          } catch (Throwable th) {
!             fail("Error creating instance " + th);
              return null;
          }
      }
  
      private  Class<?> lookupClass(String name) throws ClassNotFoundException {

*** 411,11 ***
                  }
              }
          } catch (Throwable th) {
              error = th;
          }
!         errln("Could not lookup field " + name + " " + error);
          return null;
      }
  
      protected String escapeIfNecessary(String string) {
          if (string != null) {
--- 405,11 ---
                  }
              }
          } catch (Throwable th) {
              error = th;
          }
!         fail("Could not lookup field " + name + " " + error);
          return null;
      }
  
      protected String escapeIfNecessary(String string) {
          if (string != null) {
< prev index next >