Skip to content

Commit ea6df05

Browse files
committedJan 20, 2025
8317631: Refactor ChoiceFormat tests to use JUnit
Backport-of: 5e8d893c4809fdb30e01bdfe06b4f488f7bbef83
1 parent d3adb1e commit ea6df05

File tree

3 files changed

+127
-75
lines changed

3 files changed

+127
-75
lines changed
 

‎test/jdk/java/text/Format/ChoiceFormat/Bug4185732Test.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121
* questions.
2222
*/
2323

24-
/*
25-
* @test
26-
* @bug 4185732
27-
* @library /java/text/testlib
28-
* @build Bug4185732Test HexDumpReader
29-
* @run junit Bug4185732Test
30-
* @summary test that ChoiceFormat invariants are preserved across serialization.
31-
*/
32-
3324
/*
3425
* This file is available under and governed by the GNU General Public
3526
* License version 2 only, as published by the Free Software Foundation.
@@ -64,36 +55,50 @@
6455
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
6556
*/
6657

67-
import java.util.*;
68-
import java.io.*;
58+
/*
59+
* @test
60+
* @bug 4185732
61+
* @library /java/text/testlib
62+
* @build HexDumpReader
63+
* @summary Test that ChoiceFormat invariants are preserved across serialization.
64+
* This test depends on Bug4185732.ser.txt and will fail otherwise.
65+
* @run junit Bug4185732Test
66+
*/
67+
68+
import java.io.InvalidObjectException;
69+
import java.io.ObjectInputStream;
6970
import java.text.ChoiceFormat;
7071

7172
import org.junit.jupiter.api.Test;
7273

7374
import static org.junit.jupiter.api.Assertions.fail;
7475

75-
/**
76-
* A Locale can never contain language codes of he, yi or id.
77-
*/
7876
public class Bug4185732Test {
77+
78+
/*
79+
* The ChoiceFormat class requires that its choiceFormats and choiceLimits
80+
* arrays have the same length. This test ensures that the invariant is enforced
81+
* during the readObject() call.
82+
*/
7983
@Test
80-
public void testIt() throws Exception {
84+
public void choiceFormatSerializationInvariantsTest() {
8185
try {
86+
// A serialized ChoiceFormat with unequal formats and limits
8287
final ObjectInputStream in
8388
= new ObjectInputStream(HexDumpReader.getStreamFromHexDump("Bug4185732.ser.txt"));
8489
final ChoiceFormat loc = (ChoiceFormat)in.readObject();
8590
if (loc.getFormats().length != loc.getLimits().length) {
8691
fail("ChoiceFormat did not properly check stream");
8792
} else {
88-
//for some reason, the data file was VALID. This test
89-
//requires a corrupt data file the format and limit
90-
//arrays are of different length.
93+
// for some reason, the data file was VALID. This test
94+
// requires a corrupt data file the format and limit
95+
// arrays are of different length.
9196
fail("Test data file was not properly created");
9297
}
93-
} catch (InvalidObjectException e) {
94-
//this is what we want to have happen
95-
} catch (Exception e) {
96-
fail(e.toString());
98+
} catch (InvalidObjectException expectedException) {
99+
// Expecting an IOE
100+
} catch (Exception wrongException) {
101+
fail("Expected an InvalidObjectException, instead got: " + wrongException);
97102
}
98103
}
99104
}

‎test/jdk/java/text/Format/ChoiceFormat/Bug4387255.java

+43-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,16 +21,25 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
2626
* @bug 4387255
2727
* @summary Verifies that ChoiceFormat can handle large numbers of choices
28+
* (previously capped at 30).
29+
* @run junit Bug4387255
2830
*/
2931

3032
import java.text.ChoiceFormat;
3133

32-
public class Bug4387255 {
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.Arguments;
37+
import org.junit.jupiter.params.provider.MethodSource;
38+
39+
import static org.junit.jupiter.api.Assertions.assertEquals;
40+
import static org.junit.jupiter.params.provider.Arguments.arguments;
3341

42+
public class Bug4387255 {
3443
private static final double[] doubles = {
3544
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
3645
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
@@ -49,21 +58,39 @@ public class Bug4387255 {
4958
"|20#K|21#L|22#M|23#N|24#O|25#P|26#Q|27#R|28#S|29#T" +
5059
"|30#U|31#V|32#W|33#X|34#Y|35#Z";
5160

52-
public static void main(String[] args) throws Exception {
53-
ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
54-
ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
55-
if (!choiceFormat1.equals(choiceFormat2)) {
56-
System.out.println("choiceFormat1: " + choiceFormat1.toPattern());
57-
System.out.println("choiceFormat2: " + choiceFormat2.toPattern());
58-
throw new RuntimeException();
59-
}
61+
private static final ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
62+
private static final ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
6063

64+
// Ensure that both the large ChoiceFormats format each value properly
65+
@ParameterizedTest
66+
@MethodSource
67+
public void largeChoicesTest(double db, String expectedString) {
68+
String result = choiceFormat2.format(db);
69+
assertEquals(expectedString, result,
70+
"Wrong format result with: " + choiceFormat2);
71+
}
72+
73+
74+
/*
75+
* Create arguments in form of : (double, string)
76+
* Each string is the expected result of ChoiceFormat.format(double)
77+
*/
78+
private static Arguments[] largeChoicesTest() {
79+
Arguments[] doublesAndStrings = new Arguments[doubles.length];
6180
for (int i = 0; i < doubles.length; i++) {
62-
String result = choiceFormat2.format(doubles[i]);
63-
if (!result.equals(strings[i])) {
64-
throw new RuntimeException("Wrong format result - expected " +
65-
strings[i] + ", got " + result);
66-
}
81+
doublesAndStrings[i] = arguments(doubles[i], strings[i]);
6782
}
83+
return doublesAndStrings;
84+
}
85+
86+
/*
87+
* Check that creating a ChoiceFormat with limits and formats arrays
88+
* equivalent to a string pattern are equal. (Checks that both constructors
89+
* allow for a large number of choices and formats)
90+
*/
91+
@Test
92+
public void patternEqualsArraysTest() {
93+
assertEquals(choiceFormat1, choiceFormat2, "Pattern is equivalent to " +
94+
"formats and limits, but ChoiceFormats are not equal");
6895
}
6996
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,70 +21,90 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
2626
* @bug 8001209
2727
* @summary Confirm that the values set by setChoices() are not mutable.
28+
* @run junit Bug8001209
2829
*/
29-
import java.text.*;
30+
31+
import java.text.ChoiceFormat;
32+
import java.text.ParsePosition;
33+
34+
import org.junit.jupiter.api.BeforeAll;
35+
import org.junit.jupiter.api.Test;
36+
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
3038

3139
public class Bug8001209 {
3240

33-
public static void main(String[] args) throws Exception {
34-
boolean err = false;
41+
// Represents the expected output of formatting the ChoiceFormat
42+
private static String expectedFormattedOutput;
43+
private static ChoiceFormat cFmt;
44+
private static ParsePosition status;
45+
private static String[] originalSetterArray;
3546

36-
// Borrow an example in API doc
37-
double[] limits = {1,2,3,4,5,6,7};
38-
String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
39-
ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
40-
ParsePosition status = new ParsePosition(0);
47+
// Build the original ChoiceFormat to test if it can be mutated
48+
@BeforeAll
49+
static void setUpChoiceFormatAndOutput() {
50+
double[] limits = {1, 2, 3, 4, 5, 6, 7};
51+
originalSetterArray = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
52+
// Constructor calls setChoices
53+
cFmt = new ChoiceFormat(limits, originalSetterArray);
54+
status = new ParsePosition(0);
4155

56+
// Build the expected results of formatting with the original ChoiceFormat
4257
StringBuilder before = new StringBuilder();
4358
for (double i = 1.0; i <= 7.0; ++i) {
4459
status.setIndex(0);
45-
String s = form.format(i);
60+
String s = cFmt.format(i);
4661
before.append(" ");
4762
before.append(s);
48-
before.append(form.parse(form.format(i),status));
63+
before.append(cFmt.parse(cFmt.format(i), status));
4964
}
50-
String original = before.toString();
65+
expectedFormattedOutput = before.toString();
66+
}
5167

52-
double[] newLimits = form.getLimits();
53-
String[] newFormats = (String[])form.getFormats();
68+
/*
69+
* Ensure that mutating the arrays returned by getChoices and getLimits does
70+
* not affect the internal representation of the ChoiceFormat.
71+
*/
72+
@Test
73+
public void immutableArraysFromGetters() {
74+
// Modify the array returned by getFormats() -> newFormats
75+
String[] newFormats = (String[]) cFmt.getFormats();
5476
newFormats[6] = "Doyoubi";
5577
StringBuilder after = new StringBuilder();
5678
for (double i = 1.0; i <= 7.0; ++i) {
5779
status.setIndex(0);
58-
String s = form.format(i);
80+
String s = cFmt.format(i);
5981
after.append(" ");
6082
after.append(s);
61-
after.append(form.parse(form.format(i),status));
62-
}
63-
if (!original.equals(after.toString())) {
64-
err = true;
65-
System.err.println(" Expected:" + before
66-
+ "\n Got: " + after);
83+
after.append(cFmt.parse(cFmt.format(i), status));
6784
}
85+
// Compare the expected results with the new formatted results
86+
assertEquals(after.toString(), expectedFormattedOutput,
87+
"Mutating array returned from getter changed internals of ChoiceFormat");
88+
}
6889

69-
dayOfWeekNames[6] = "Saturday";
70-
after = new StringBuilder();
90+
/*
91+
* Ensure that mutating the arrays passed to setChoices/constructor does
92+
* not affect the internal representation of the ChoiceFormat.
93+
*/
94+
@Test
95+
public void immutableArraysFromSetter() {
96+
// Modify the array passed to setFormats() -> dayOfWeekNames
97+
originalSetterArray[6] = "Saturday";
98+
StringBuilder after = new StringBuilder();
7199
for (double i = 1.0; i <= 7.0; ++i) {
72100
status.setIndex(0);
73-
String s = form.format(i);
101+
String s = cFmt.format(i);
74102
after.append(" ");
75103
after.append(s);
76-
after.append(form.parse(form.format(i),status));
77-
}
78-
if (!original.equals(after.toString())) {
79-
err = true;
80-
System.err.println(" Expected:" + before
81-
+ "\n Got: " + after);
82-
}
83-
84-
if (err) {
85-
throw new RuntimeException("Failed.");
86-
} else {
87-
System.out.println("Passed.");
104+
after.append(cFmt.parse(cFmt.format(i), status));
88105
}
106+
// Compare the expected results with the new formatted results
107+
assertEquals(after.toString(), expectedFormattedOutput,
108+
"Mutating array passed to setter changed internals of ChoiceFormat");
89109
}
90110
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jan 20, 2025

@openjdk-notifier[bot]
Please sign in to comment.