undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,44 @@
#
# Copyright (c) 1999, 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.
#
# Hex dump of a serialized ChiceFormat for Bug4185732Test.
aced0005737200166a6176612e746578742e43686f696365466f726d617418e9
c6bee365b6040200025b000d63686f696365466f726d6174737400135b4c6a61
76612f6c616e672f537472696e673b5b000c63686f6963654c696d6974737400
025b44787200166a6176612e746578742e4e756d626572466f726d6174dff6b3
bf137d07e803000b5a000c67726f7570696e67557365644200116d6178467261
6374696f6e4469676974734200106d6178496e74656765724469676974734900
156d6178696d756d4672616374696f6e4469676974734900146d6178696d756d
496e74656765724469676974734200116d696e4672616374696f6e4469676974
734200106d696e496e74656765724469676974734900156d696e696d756d4672
616374696f6e4469676974734900146d696e696d756d496e7465676572446967
6974735a00107061727365496e74656765724f6e6c7949001573657269616c56
657273696f6e4f6e53747265616d787200106a6176612e746578742e466f726d
6174fbd8bc12e90f184302000078700103280000000300000028000100000000
00000001000000000178757200135b4c6a6176612e6c616e672e537472696e67
3badd256e7e91d7b470200007870000000067400034d6f6e7400035475657400
0357656474000454687572740003467269740003536174757200025b443ea68c
14ab635a1e0200007870000000073ff000000000000040000000000000004008
000000000000401000000000000040140000000000004018000000000000401c
000000000000

View file

@ -0,0 +1,104 @@
/*
* Copyright (c) 1999, 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.
*
* 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.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file and, per its terms, should not be removed:
*
* (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
*
* Portions copyright (c) 2007 Sun Microsystems, Inc.
* All Rights Reserved.
*
* The original version of this source code and documentation
* is copyrighted and owned by Taligent, Inc., a wholly-owned
* subsidiary of IBM. These materials are provided under terms
* of a License Agreement between Taligent and Sun. This technology
* is protected by multiple US and International patents.
*
* This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
/*
* @test
* @bug 4185732
* @library /java/text/testlib
* @build HexDumpReader
* @summary Test that ChoiceFormat invariants are preserved across serialization.
* This test depends on Bug4185732.ser.txt and will fail otherwise.
* @run junit Bug4185732Test
*/
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.text.ChoiceFormat;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
public class Bug4185732Test {
/*
* The ChoiceFormat class requires that its choiceFormats and choiceLimits
* arrays have the same length. This test ensures that the invariant is enforced
* during the readObject() call.
*/
@Test
public void choiceFormatSerializationInvariantsTest() {
try {
// A serialized ChoiceFormat with unequal formats and limits
final ObjectInputStream in
= new ObjectInputStream(HexDumpReader.getStreamFromHexDump("Bug4185732.ser.txt"));
final ChoiceFormat loc = (ChoiceFormat)in.readObject();
if (loc.getFormats().length != loc.getLimits().length) {
fail("ChoiceFormat did not properly check stream");
} else {
// for some reason, the data file was VALID. This test
// requires a corrupt data file the format and limit
// arrays are of different length.
fail("Test data file was not properly created");
}
} catch (InvalidObjectException expectedException) {
// Expecting an IOE
} catch (Exception wrongException) {
fail("Expected an InvalidObjectException, instead got: " + wrongException);
}
}
}

View file

@ -0,0 +1,96 @@
/*
* Copyright (c) 1997, 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.
*
* 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 4387255
* @summary Verifies that ChoiceFormat can handle large numbers of choices
* (previously capped at 30).
* @run junit Bug4387255
*/
import java.text.ChoiceFormat;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.params.provider.Arguments.arguments;
public class Bug4387255 {
private static final double[] doubles = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35};
private static final String[] strings = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"};
private static final String pattern =
"0#0|1#1|2#2|3#3|4#4|5#5|6#6|7#7|8#8|9#9" +
"|10#A|11#B|12#C|13#D|14#E|15#F|16#G|17#H|18#I|19#J" +
"|20#K|21#L|22#M|23#N|24#O|25#P|26#Q|27#R|28#S|29#T" +
"|30#U|31#V|32#W|33#X|34#Y|35#Z";
private static final ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
private static final ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
// Ensure that both the large ChoiceFormats format each value properly
@ParameterizedTest
@MethodSource
public void largeChoicesTest(double db, String expectedString) {
String result = choiceFormat2.format(db);
assertEquals(expectedString, result,
"Wrong format result with: " + choiceFormat2);
}
/*
* Create arguments in form of : (double, string)
* Each string is the expected result of ChoiceFormat.format(double)
*/
private static Arguments[] largeChoicesTest() {
Arguments[] doublesAndStrings = new Arguments[doubles.length];
for (int i = 0; i < doubles.length; i++) {
doublesAndStrings[i] = arguments(doubles[i], strings[i]);
}
return doublesAndStrings;
}
/*
* Check that creating a ChoiceFormat with limits and formats arrays
* equivalent to a string pattern are equal. (Checks that both constructors
* allow for a large number of choices and formats)
*/
@Test
public void patternEqualsArraysTest() {
assertEquals(choiceFormat1, choiceFormat2, "Pattern is equivalent to " +
"formats and limits, but ChoiceFormats are not equal");
}
}

View file

@ -0,0 +1,110 @@
/*
* Copyright (c) 2012, 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.
*
* 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 8001209
* @summary Confirm that the values set by setChoices() are not mutable.
* @run junit Bug8001209
*/
import java.text.ChoiceFormat;
import java.text.ParsePosition;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Bug8001209 {
// Represents the expected output of formatting the ChoiceFormat
private static String expectedFormattedOutput;
private static ChoiceFormat cFmt;
private static ParsePosition status;
private static String[] originalSetterArray;
// Build the original ChoiceFormat to test if it can be mutated
@BeforeAll
static void setUpChoiceFormatAndOutput() {
double[] limits = {1, 2, 3, 4, 5, 6, 7};
originalSetterArray = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// Constructor calls setChoices
cFmt = new ChoiceFormat(limits, originalSetterArray);
status = new ParsePosition(0);
// Build the expected results of formatting with the original ChoiceFormat
StringBuilder before = new StringBuilder();
for (double i = 1.0; i <= 7.0; ++i) {
status.setIndex(0);
String s = cFmt.format(i);
before.append(" ");
before.append(s);
before.append(cFmt.parse(cFmt.format(i), status));
}
expectedFormattedOutput = before.toString();
}
/*
* Ensure that mutating the arrays returned by getChoices and getLimits does
* not affect the internal representation of the ChoiceFormat.
*/
@Test
public void immutableArraysFromGetters() {
// Modify the array returned by getFormats() -> newFormats
String[] newFormats = (String[]) cFmt.getFormats();
newFormats[6] = "Doyoubi";
StringBuilder after = new StringBuilder();
for (double i = 1.0; i <= 7.0; ++i) {
status.setIndex(0);
String s = cFmt.format(i);
after.append(" ");
after.append(s);
after.append(cFmt.parse(cFmt.format(i), status));
}
// Compare the expected results with the new formatted results
assertEquals(after.toString(), expectedFormattedOutput,
"Mutating array returned from getter changed internals of ChoiceFormat");
}
/*
* Ensure that mutating the arrays passed to setChoices/constructor does
* not affect the internal representation of the ChoiceFormat.
*/
@Test
public void immutableArraysFromSetter() {
// Modify the array passed to setFormats() -> dayOfWeekNames
originalSetterArray[6] = "Saturday";
StringBuilder after = new StringBuilder();
for (double i = 1.0; i <= 7.0; ++i) {
status.setIndex(0);
String s = cFmt.format(i);
after.append(" ");
after.append(s);
after.append(cFmt.parse(cFmt.format(i), status));
}
// Compare the expected results with the new formatted results
assertEquals(after.toString(), expectedFormattedOutput,
"Mutating array passed to setter changed internals of ChoiceFormat");
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (c) 2025, 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 8353585
* @summary Basic parse tests. Enforce regular behavior, no match, and multi match.
* @run junit ParseTest
*/
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.text.ChoiceFormat;
import java.text.ParsePosition;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ParseTest {
// Ensure that the parsed text produces the expected number
// i.e. return limit corresponding to format matched
@ParameterizedTest
@MethodSource
void parseTest(String pattern, String text, Double expected, int index) {
var pp = new ParsePosition(index);
var fmt = new ChoiceFormat(pattern);
assertEquals(expected, fmt.parse(text, pp), "Incorrect limit returned");
if (expected.equals(Double.NaN)) { // AKA failed parse
assertEquals(index, pp.getErrorIndex(),
"Failed parse produced incorrect error index");
} else {
assertEquals(-1, pp.getErrorIndex(),
"Error index should remain -1 on match");
}
}
private static Stream<Arguments> parseTest() {
return Stream.of(
Arguments.of("1#foo", "foo", Double.NaN, -1),
Arguments.of("1#baz", "foo bar baz", Double.NaN, 20),
Arguments.of("1#baz", "foo bar baz", 1d, 8),
Arguments.of("1#baz", "foo baz quux", Double.NaN, 8),
Arguments.of("1#a", "", Double.NaN, 0),
Arguments.of("1#a", "a", 1d, 0),
Arguments.of("1# ", " ", 1d, 0),
Arguments.of("1#a|2#a", "a", 1d, 0),
Arguments.of("1#a|2#aa", "aa", 2d, 0),
Arguments.of("1#a|2#aa", "aabb", 2d, 0),
Arguments.of("1#a|2#aa", "bbaa", Double.NaN, 0),
Arguments.of("1#aa|2#aaa", "a", Double.NaN, 0)
);
}
}

View file

@ -0,0 +1,165 @@
/*
* Copyright (c) 2023, 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.
*
* 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 6285888 6801704 8325898
* @summary Test the expected behavior for a wide range of patterns (both
* correct and incorrect). This test documents the behavior of incorrect
* ChoiceFormat patterns either throwing an exception, or discarding
* the incorrect portion of a pattern.
* @run junit PatternsTest
*/
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.text.ChoiceFormat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;
public class PatternsTest {
private static final String ERR1 =
"Each interval must contain a number before a format";
private static final String ERR2 =
"Incorrect order of intervals, must be in ascending order";
// Check that some valid patterns do not throw an exception. Check
// them against the expected values they should be formatted as.
@ParameterizedTest
@MethodSource
public void validPatternsTest(String pattern, String[] expectedValues) {
var fmt = new ChoiceFormat(pattern);
for (int i=1; i<=expectedValues.length; i++) {
assertEquals(expectedValues[i-1], fmt.format(i),
String.format("ChoiceFormat formatted %s incorrectly:", i));
}
}
// Valid patterns ranging from normal appearing to odd. These should not
// throw an exception or discard any portions of the pattern.
private static Arguments[] validPatternsTest() {
return new Arguments[] {
// Multi pattern with trailing empty string Format
arguments("1#foo|2#bar|3#", new String[]{"foo", "bar", ""}),
// Multi patten with trailing '|'
arguments("1#foo|2#bar|", new String[]{"foo", "bar"}),
// Using a '>' (not a Relation) within a Format
arguments("1#foo|2#bar>", new String[]{"foo", "bar>"}),
// Standard Multi Pattern
arguments("1#foo|2#bar", new String[]{"foo", "bar"}),
// Same numerical value Limits, different Relations
arguments("1#foo|1<baz", new String[]{"foo", "baz"}),
// Standard Single Pattern
arguments("1#foo", new String[]{"foo"}),
// Single pattern with empty string Format
arguments("1#", new String[]{""})
};
}
// Check that the incorrect pattern throws an IAE with the desired error msg
// This also tests applyPattern, as the ChoiceFormat constructor calls applyPattern
@ParameterizedTest
@MethodSource
public void invalidPatternsThrowsTest(String pattern, String errMsg) {
var ex = assertThrows(IllegalArgumentException.class,
() -> new ChoiceFormat(pattern));
assertEquals(errMsg, ex.getMessage());
}
// Variety of patterns that break the ChoiceFormat pattern syntax and throw
// an exception.
private static Arguments[] invalidPatternsThrowsTest() {
return new Arguments[] {
arguments("#", ERR1), // Only relation
arguments("#foo", ERR1), // No Limit
arguments("0#foo|#|1#bar", ERR1), // Missing Relation in SubPattern
arguments("#|", ERR1), // Missing Limit
arguments("##|", ERR1), // Double Relations
arguments("0#test|#", ERR1), // SubPattern missing Limit
arguments("0#foo|3#bar|1#baz", ERR2), // Non-ascending Limits
// No longer throw IAE after 6285888, as relational symbols
// can now be used within the Format segment.
// arguments("0#foo1#", ERR1), // SubPattern not separated by '|'
// arguments("0#foo#", ERR1), // Using a Relation in a format
};
}
// Check that the incorrect pattern discards the trailing incorrect portion.
// These incorrect patterns should ideally throw an exception, but for
// behavioral compatibility reasons do not.
@ParameterizedTest
@MethodSource
public void invalidPatternsDiscardedTest(String brokenPattern, String actualPattern) {
var cf1 = new ChoiceFormat(brokenPattern);
var cf2 = new ChoiceFormat(actualPattern);
assertEquals(cf2, cf1,
String.format("Expected %s, but got %s", cf2.toPattern(), cf1.toPattern()));
}
// Variety of incorrect patterns with the actual expected pattern
// after discarding occurs.
private static Arguments[] invalidPatternsDiscardedTest() {
return new Arguments[] {
// Incomplete SubPattern (limit only) at end of Pattern
arguments("1#bar|2", "1#bar"),
// Incomplete SubPattern at the end of the Pattern
arguments("0#foo|1#bar|baz", "0#foo|1#bar"),
// Incomplete SubPattern with trailing | at the end of the Pattern
// Prior to 6801704, it created the broken "0#foo|1#bar|1#"
// which caused formatting 1 to return an empty string
arguments("0#foo|1#bar|baz|", "0#foo|1#bar"),
// Same as previous, with additional incomplete subPatterns
arguments("0#foo|1#bar|baz|quux", "0#foo|1#bar"),
// --- These throw an ArrayIndexOutOfBoundsException
// when attempting to format with them as the incomplete patterns
// are discarded, initializing the cFmt with empty limits and formats ---
// SubPattern with only a Limit (which is interpreted as a Format)
arguments("0", ""),
// SubPattern with only a Format
arguments("foo", ""),
// empty string
arguments("", "")
};
}
// Calling format() with empty limits and formats
// throws an ArrayIndexOutOfBoundsException
@Test
public void emptyLimitsAndFormatsTest() {
var cf1 = new ChoiceFormat("");
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> cf1.format(1));
var cf2 = new ChoiceFormat(new double[]{}, new String[]{});
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> cf2.format(2));
}
}

View file

@ -0,0 +1,80 @@
/*
* Copyright (c) 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.
*
* 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 6285888
* @summary Ensure ChoiceFormat supports "#", "<", "" within
* the format segment of a ChoiceFormat String pattern
* @run junit SymbolsInFormatSegment
*/
import java.text.ChoiceFormat;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*
* These tests would previously throw IAEs on all input as ChoiceFormat would parse
* the relational symbol syntactically (when not needed). With the associated change
* set, ChoiceFormat knows to not treat any subsequent relational symbols as
* syntactically significant unless a '|' has been parsed.
*/
public class SymbolsInFormatSegment {
// Test a variety of patterns with relational symbols in the Format segment
@ParameterizedTest
@MethodSource("patternsWithSymbols")
public void allowInConstructor(String pattern, String expected, int limit) {
var cf = new ChoiceFormat(pattern);
assertEquals(expected, cf.format(limit));
}
// Same as previous test, but check the applyPattern method
@ParameterizedTest
@MethodSource("patternsWithSymbols")
public void allowInApplyPattern(String pattern, String expected, int limit) {
var cf = new ChoiceFormat("");
cf.applyPattern(pattern);
assertEquals(expected, cf.format(limit));
}
private static Stream<Arguments> patternsWithSymbols() {
return Stream.of(
// CSR example
Arguments.of("1#The code is #7281", "The code is #7281", 1),
// Other examples
Arguments.of("1#<", "<", 1),
Arguments.of("1#foo<", "foo<", 1),
Arguments.of("1<foo\u2264", "foo\u2264", 1),
Arguments.of("1\u2264foo#", "foo#", 1),
Arguments.of("1#foo<#\u2264|2\u2264baz<#\u2264", "baz<#\u2264", 100),
Arguments.of("1#foo<#\u2264|2#baz<#\u2264|3<bar##\u2264", "bar##\u2264", 100),
Arguments.of("1#foo<#\u2264|2#baz<#\u2264|3\u2264bar##\u2264", "bar##\u2264", 100)
);
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 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.
*
* 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 8321545
* @summary Ensure value returned by overridden toString method is as expected
* @run junit ToStringTest
*/
import java.text.ChoiceFormat;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ToStringTest {
// Check a normal expected value. There is no null locale test as
// ChoiceFormat is not localized.
@Test
public void normalValueTest() {
String expectedStr = "ChoiceFormat [pattern: \"1.0#foo\"]\n";
var c = new ChoiceFormat("1.0#foo");
assertEquals(expectedStr, c.toString());
}
// Ensure toString throws no exception when ChoiceFormat is created
// with empty arrays
@Test
public void oddValueTest() {
var c2 = new ChoiceFormat(new double[]{}, new String[]{});
System.out.println(c2);
}
}