mirror of
https://github.com/JetBrains/JetBrainsRuntime.git
synced 2025-12-06 09:29:38 +01:00
8317631: Refactor ChoiceFormat tests to use JUnit
Reviewed-by: naoto
This commit is contained in:
@@ -21,15 +21,6 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4185732
|
||||
* @library /java/text/testlib
|
||||
* @build Bug4185732Test HexDumpReader
|
||||
* @run junit Bug4185732Test
|
||||
* @summary test that ChoiceFormat invariants are preserved across serialization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
@@ -64,36 +55,50 @@
|
||||
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
/*
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* A Locale can never contain language codes of he, yi or id.
|
||||
*/
|
||||
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 testIt() throws Exception {
|
||||
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.
|
||||
// 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 e) {
|
||||
//this is what we want to have happen
|
||||
} catch (Exception e) {
|
||||
fail(e.toString());
|
||||
} catch (InvalidObjectException expectedException) {
|
||||
// Expecting an IOE
|
||||
} catch (Exception wrongException) {
|
||||
fail("Expected an InvalidObjectException, instead got: " + wrongException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* 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
|
||||
@@ -21,16 +21,25 @@
|
||||
* 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;
|
||||
|
||||
public class Bug4387255 {
|
||||
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,
|
||||
@@ -49,21 +58,39 @@ public class Bug4387255 {
|
||||
"|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";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings);
|
||||
ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern);
|
||||
if (!choiceFormat1.equals(choiceFormat2)) {
|
||||
System.out.println("choiceFormat1: " + choiceFormat1.toPattern());
|
||||
System.out.println("choiceFormat2: " + choiceFormat2.toPattern());
|
||||
throw new RuntimeException();
|
||||
}
|
||||
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++) {
|
||||
String result = choiceFormat2.format(doubles[i]);
|
||||
if (!result.equals(strings[i])) {
|
||||
throw new RuntimeException("Wrong format result - expected " +
|
||||
strings[i] + ", got " + result);
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* 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
|
||||
@@ -21,70 +21,90 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
/*
|
||||
* @test
|
||||
* @bug 8001209
|
||||
* @summary Confirm that the values set by setChoices() are not mutable.
|
||||
* @run junit Bug8001209
|
||||
*/
|
||||
import java.text.*;
|
||||
|
||||
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 {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
boolean err = false;
|
||||
// Represents the expected output of formatting the ChoiceFormat
|
||||
private static String expectedFormattedOutput;
|
||||
private static ChoiceFormat cFmt;
|
||||
private static ParsePosition status;
|
||||
private static String[] originalSetterArray;
|
||||
|
||||
// Borrow an example in API doc
|
||||
double[] limits = {1,2,3,4,5,6,7};
|
||||
String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
|
||||
ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
|
||||
ParsePosition status = new ParsePosition(0);
|
||||
// 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 = form.format(i);
|
||||
String s = cFmt.format(i);
|
||||
before.append(" ");
|
||||
before.append(s);
|
||||
before.append(form.parse(form.format(i),status));
|
||||
before.append(cFmt.parse(cFmt.format(i), status));
|
||||
}
|
||||
String original = before.toString();
|
||||
expectedFormattedOutput = before.toString();
|
||||
}
|
||||
|
||||
double[] newLimits = form.getLimits();
|
||||
String[] newFormats = (String[])form.getFormats();
|
||||
/*
|
||||
* 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 = form.format(i);
|
||||
String s = cFmt.format(i);
|
||||
after.append(" ");
|
||||
after.append(s);
|
||||
after.append(form.parse(form.format(i),status));
|
||||
}
|
||||
if (!original.equals(after.toString())) {
|
||||
err = true;
|
||||
System.err.println(" Expected:" + before
|
||||
+ "\n Got: " + after);
|
||||
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");
|
||||
}
|
||||
|
||||
dayOfWeekNames[6] = "Saturday";
|
||||
after = new StringBuilder();
|
||||
/*
|
||||
* 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 = form.format(i);
|
||||
String s = cFmt.format(i);
|
||||
after.append(" ");
|
||||
after.append(s);
|
||||
after.append(form.parse(form.format(i),status));
|
||||
}
|
||||
if (!original.equals(after.toString())) {
|
||||
err = true;
|
||||
System.err.println(" Expected:" + before
|
||||
+ "\n Got: " + after);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
throw new RuntimeException("Failed.");
|
||||
} else {
|
||||
System.out.println("Passed.");
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user