/*
* 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.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @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.*;
import java.lang.reflect.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.text.AttributedCharacterIterator.Attribute;
/**
* 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
* of pairs of maps. The first map specifies the Format that
* should be created, and consists of:
*
* lengthOfFormattedString indicate the total length of formatted
* string. text indicates the resulting string.
* 0...x where x == lengthOfFormattedString - 1 is
* an array of the attributes that should exist at the particular
* location. limits is an array of maps, where each map
* can be used to test the bounds of a set of attributes. Each map will
* consist of:
*
* attributes = array of attributes
* begin = start location
* begin2 = second start location
* end = limit location
* end2 = second limit location
*
* These are tested by iterating from begin to end in the CharacterIterator
* and doing the following at each index:
*
* getRunStart() == begin
* getRunStart(attributes) == begin2
* getRunLimit() == end
* getRunLimit(attributes) == end2
*
* fieldPositions is used to test the results of invoking
* format with a FieldPosition.
* fieldPositions is an array of maps, where each map contains
* the following:
*
* field = Integer field reference (optional)
* fieldID = Object reference
* begin = begin index of FieldPosition after formatting
* end = end index of FieldPosition after formatting
*
* 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 contents = parser.parse(new BufferedReader(
new FileReader(file)));
List