8310049: Refactor Charset tests to use JUnit

Reviewed-by: lancea, naoto
This commit is contained in:
Justin Lu 2023-06-20 17:21:00 +00:00
parent 99d2a9afa9
commit 09174e0c99
8 changed files with 1694 additions and 1645 deletions

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
@ -25,27 +25,26 @@
* @bug 4422044
* @summary Ensure that keys in available-charset map
* are identical to canonical names
* @run junit AvailableCharsetNames
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.util.*;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class AvailableCharsetNames {
public static void main(String[] args) throws Exception {
Iterator charsetIterator = Charset.availableCharsets().keySet().iterator();
while (charsetIterator.hasNext()) {
String charsetName = (String) charsetIterator.next();
/**
* Test that the keys in Charset.availableCharsets()
* are equal to the associated Charset.name() value.
*/
@Test
public void canonicalNamesTest() {
for (String charsetName : Charset.availableCharsets().keySet()) {
Charset charset = Charset.forName(charsetName);
if (!charset.name().equals(charsetName)) {
throw new Exception("Error: Charset name mismatch - expected "
+ charsetName + ", got " + charset.name());
}
assertEquals(charset.name(), charsetName, "Charset name mismatch");
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
@ -25,93 +25,116 @@
* @bug 4626545 4696726
* @summary Checks the inter containment relationships between NIO charsets
* @modules jdk.charsets
* @run junit CharsetContainmentTest
*/
import java.nio.charset.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
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.assertTrue;
public class CharsetContainmentTest {
static String[] encodings =
{ "US-ASCII", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-8",
"windows-1252", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3",
"ISO-8859-4", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7",
"ISO-8859-8", "ISO-8859-9", "ISO-8859-13", "ISO-8859-15", "ISO-8859-16",
"ISO-2022-JP", "ISO-2022-KR",
// Temporarily remove ISO-2022-CN-* charsets until full encoder/decoder
// support is added (4673614)
// "x-ISO-2022-CN-CNS", "x-ISO-2022-CN-GB",
/**
* Test that the charsets in 'encodings' contain the charsets
* inside 'contains'. Each value in 'encodings' is mapped to a String
* array in 'contains'. For example, the value, "TIS-620" in 'encodings'
* should contain "US-ASCII", "TIS-620".
*/
@ParameterizedTest
@MethodSource("charsets")
public void interContainmentTest(String containerName, String containedName) {
Charset container = Charset.forName(containerName);
Charset contained = Charset.forName(containedName);
assertTrue(container.contains(contained),
String.format("Charset: %s does not contain: %s", containerName, containedName));
}
"x-ISCII91", "GBK", "GB18030", "Big5",
"x-EUC-TW", "GB2312", "EUC-KR", "x-Johab", "Big5-HKSCS",
"x-MS950-HKSCS", "windows-1251", "windows-1253", "windows-1254",
"windows-1255", "windows-1256", "windows-1257", "windows-1258",
"x-mswin-936", "x-windows-949", "x-windows-950", "windows-31j",
"Shift_JIS", "EUC-JP", "KOI8-R", "TIS-620"
};
private static Stream<Arguments> charsets() {
String[] encodings = {
"US-ASCII", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-8",
"windows-1252", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3",
"ISO-8859-4", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7",
"ISO-8859-8", "ISO-8859-9", "ISO-8859-13", "ISO-8859-15", "ISO-8859-16",
"ISO-2022-JP", "ISO-2022-KR",
// Temporarily remove ISO-2022-CN-* charsets until full encoder/decoder
// support is added (4673614)
// "x-ISO-2022-CN-CNS", "x-ISO-2022-CN-GB",
"x-ISCII91", "GBK", "GB18030", "Big5",
"x-EUC-TW", "GB2312", "EUC-KR", "x-Johab", "Big5-HKSCS",
"x-MS950-HKSCS", "windows-1251", "windows-1253", "windows-1254",
"windows-1255", "windows-1256", "windows-1257", "windows-1258",
"x-mswin-936", "x-windows-949", "x-windows-950", "windows-31j",
"Shift_JIS", "EUC-JP", "KOI8-R", "TIS-620"
};
static String[][] contains = {
{ "US-ASCII"},
encodings,
encodings,
encodings,
encodings,
{"US-ASCII", "windows-1252"},
{"US-ASCII", "ISO-8859-1"},
{"US-ASCII", "ISO-8859-2"},
{"US-ASCII", "ISO-8859-3"},
{"US-ASCII", "ISO-8859-4"},
{"US-ASCII", "ISO-8859-5"},
{"US-ASCII", "ISO-8859-6"},
{"US-ASCII", "ISO-8859-7"},
{"US-ASCII", "ISO-8859-8"},
{"US-ASCII", "ISO-8859-9"},
{"US-ASCII", "ISO-8859-13"},
{"US-ASCII", "ISO-8859-15"},
{"US-ASCII", "ISO-8859-16"},
{"ISO-2022-JP"},
{"ISO-2022-KR"},
// Temporarily remove ISO-2022-CN-* charsets until full encoder/decoder
// support is added (4673614)
//{"x-ISO-2022-CN-CNS"},
//{"x-ISO-2022-CN-GB"},
{"US-ASCII", "x-ISCII91"},
{"US-ASCII", "GBK"},
encodings,
{"US-ASCII", "Big5"},
{"US-ASCII", "x-EUC-TW"},
{"US-ASCII", "GB2312"},
{"US-ASCII", "EUC-KR"},
{"US-ASCII", "x-Johab"},
{"US-ASCII", "Big5-HKSCS", "Big5"},
{"US-ASCII", "x-MS950-HKSCS", "x-windows-950"},
{"US-ASCII", "windows-1251"},
{"US-ASCII", "windows-1253"},
{"US-ASCII", "windows-1254"},
{"US-ASCII", "windows-1255"},
{"US-ASCII", "windows-1256"},
{"US-ASCII", "windows-1257"},
{"US-ASCII", "windows-1258"},
{"US-ASCII", "x-mswin-936"},
{"US-ASCII", "x-windows-949"},
{"US-ASCII", "x-windows-950"},
{"US-ASCII", "windows-31j" },
{"US-ASCII", "Shift_JIS"},
{"US-ASCII", "EUC-JP"},
{"US-ASCII", "KOI8-R"},
{"US-ASCII", "TIS-620"}};
String[][] contains = {
{"US-ASCII"},
encodings,
encodings,
encodings,
encodings,
{"US-ASCII", "windows-1252"},
{"US-ASCII", "ISO-8859-1"},
{"US-ASCII", "ISO-8859-2"},
{"US-ASCII", "ISO-8859-3"},
{"US-ASCII", "ISO-8859-4"},
{"US-ASCII", "ISO-8859-5"},
{"US-ASCII", "ISO-8859-6"},
{"US-ASCII", "ISO-8859-7"},
{"US-ASCII", "ISO-8859-8"},
{"US-ASCII", "ISO-8859-9"},
{"US-ASCII", "ISO-8859-13"},
{"US-ASCII", "ISO-8859-15"},
{"US-ASCII", "ISO-8859-16"},
{"ISO-2022-JP"},
{"ISO-2022-KR"},
// Temporarily remove ISO-2022-CN-* charsets until full encoder/decoder
// support is added (4673614)
//{"x-ISO-2022-CN-CNS"},
//{"x-ISO-2022-CN-GB"},
{"US-ASCII", "x-ISCII91"},
{"US-ASCII", "GBK"},
encodings,
{"US-ASCII", "Big5"},
{"US-ASCII", "x-EUC-TW"},
{"US-ASCII", "GB2312"},
{"US-ASCII", "EUC-KR"},
{"US-ASCII", "x-Johab"},
{"US-ASCII", "Big5-HKSCS", "Big5"},
{"US-ASCII", "x-MS950-HKSCS", "x-windows-950"},
{"US-ASCII", "windows-1251"},
{"US-ASCII", "windows-1253"},
{"US-ASCII", "windows-1254"},
{"US-ASCII", "windows-1255"},
{"US-ASCII", "windows-1256"},
{"US-ASCII", "windows-1257"},
{"US-ASCII", "windows-1258"},
{"US-ASCII", "x-mswin-936"},
{"US-ASCII", "x-windows-949"},
{"US-ASCII", "x-windows-950"},
{"US-ASCII", "windows-31j"},
{"US-ASCII", "Shift_JIS"},
{"US-ASCII", "EUC-JP"},
{"US-ASCII", "KOI8-R"},
{"US-ASCII", "TIS-620"}};
public static void main(String[] args) throws Exception {
for (int i = 0; i < encodings.length; i++) {
Charset c = Charset.forName(encodings[i]);
for (int j = 0 ; j < contains[i].length; j++) {
if (c.contains(Charset.forName(contains[i][j])))
continue;
else {
throw new Exception ("Error: charset " + encodings[i] +
"doesn't contain " + contains[i][j]);
}
}
// Length of encodings and contains should always be equal
if (encodings.length != contains.length) {
throw new RuntimeException("Testing data is not set up correctly");
}
List<Arguments> charsetList = new ArrayList<Arguments>();
for (int i = 0; i < encodings.length; i++) {
for (int j = 0 ; j < contains[i].length; j++) {
charsetList.add(Arguments.of(encodings[i], contains[i][j]));
}
}
return charsetList.stream();
}
}

@ -25,24 +25,137 @@
* @summary Unit test for charset containment
* @bug 6798572 8167252
* @modules jdk.charsets
* @run junit Contains
*/
import java.nio.charset.*;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.stream.Stream;
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.api.Assertions.assertTrue;
public class Contains {
static void ck(Charset cs1, Charset cs2, boolean cont) throws Exception {
if ((cs1.contains(cs2)) != cont)
throw new Exception("Wrong answer: "
+ cs1.name() + " contains " + cs2.name());
System.err.println(cs1.name()
+ (cont ? " contains " : " does not contain ")
+ cs2.name());
/**
* Tests the containment of some charsets against themselves.
* This test takes both true and false for 'cont'.
*/
@ParameterizedTest
@MethodSource("charsets")
public void charsetsTest(Charset containerCs, Charset cs, boolean cont){
shouldContain(containerCs, cs, cont);
}
public static void main(String[] args) throws Exception {
/**
* Tests UTF charsets with other charsets. In this case, each UTF charset
* should contain every single charset they are tested against. 'cont' is
* always true.
*/
@ParameterizedTest
@MethodSource("utfCharsets")
public void UTFCharsetsTest(Charset containerCs, Charset cs, boolean cont){
shouldContain(containerCs, cs, cont);
}
/**
* Tests the assertion in the contains() method: "Every charset contains itself."
*/
@Test
public void containsSelfTest() {
for (var entry : Charset.availableCharsets().entrySet()) {
Charset charset = entry.getValue();
boolean contains = charset.contains(charset);
assertTrue(contains, String.format("Charset(%s).contains(Charset(%s)) returns %s",
charset.name(), charset.name(), contains));
}
}
/**
* Helper method that checks if a charset should contain another charset.
*/
static void shouldContain(Charset containerCs, Charset cs, boolean cont){
assertEquals((containerCs.contains(cs)), cont, String.format("%s %s %s",
containerCs.name(), (cont ? " contains " : " does not contain "), cs.name()));
}
private static Stream<Arguments> utfCharsets() {
String[] utfNames = {
"utf-16",
"utf-8",
"utf-16le",
"utf-16be",
"x-utf-16le-bom"
};
String[] charsetNames = {
"US-ASCII",
"UTF-8",
"UTF-16",
"UTF-16BE",
"UTF-16LE",
"x-UTF-16LE-BOM",
"GBK",
"GB18030",
"ISO-8859-1",
"ISO-8859-15",
"ISO-8859-2",
"ISO-8859-3",
"ISO-8859-4",
"ISO-8859-5",
"ISO-8859-6",
"ISO-8859-7",
"ISO-8859-8",
"ISO-8859-9",
"ISO-8859-13",
"JIS_X0201",
"x-JIS0208",
"JIS_X0212-1990",
"GB2312",
"EUC-KR",
"x-EUC-TW",
"EUC-JP",
"x-euc-jp-linux",
"KOI8-R",
"TIS-620",
"x-ISCII91",
"windows-1251",
"windows-1252",
"windows-1253",
"windows-1254",
"windows-1255",
"windows-1256",
"windows-1257",
"windows-1258",
"windows-932",
"x-mswin-936",
"x-windows-949",
"x-windows-950",
"windows-31j",
"Big5",
"Big5-HKSCS",
"x-MS950-HKSCS",
"ISO-2022-JP",
"ISO-2022-KR",
"x-ISO-2022-CN-CNS",
"x-ISO-2022-CN-GB",
"Big5-HKSCS",
"x-Johab",
"Shift_JIS"
};
// All charsets in utfNames should contain
// all charsets in charsetNames
return Arrays.stream(utfNames).flatMap(cs1 -> Arrays.stream(charsetNames)
.map(cs2 -> Arguments.of(Charset.forName(cs1), Charset.forName(cs2), true)));
}
private static Stream<Arguments> charsets() {
Charset us_ascii = Charset.forName("US-ASCII");
Charset iso_8859_1 = Charset.forName("ISO-8859-1");
Charset iso_8859_15 = Charset.forName("ISO-8859-15");
@ -50,141 +163,48 @@ public class Contains {
Charset utf_16be = Charset.forName("UTF-16BE");
Charset cp1252 = Charset.forName("CP1252");
ck(us_ascii, us_ascii, true);
ck(us_ascii, iso_8859_1, false);
ck(us_ascii, iso_8859_15, false);
ck(us_ascii, utf_8, false);
ck(us_ascii, utf_16be, false);
ck(us_ascii, cp1252, false);
return Stream.of(
Arguments.of(us_ascii, us_ascii, true),
Arguments.of(us_ascii, iso_8859_1, false),
Arguments.of(us_ascii, iso_8859_15, false),
Arguments.of(us_ascii, utf_8, false),
Arguments.of(us_ascii, utf_16be, false),
Arguments.of(us_ascii, cp1252, false),
ck(iso_8859_1, us_ascii, true);
ck(iso_8859_1, iso_8859_1, true);
ck(iso_8859_1, iso_8859_15, false);
ck(iso_8859_1, utf_8, false);
ck(iso_8859_1, utf_16be, false);
ck(iso_8859_1, cp1252, false);
Arguments.of(iso_8859_1, us_ascii, true),
Arguments.of(iso_8859_1, iso_8859_1, true),
Arguments.of(iso_8859_1, iso_8859_15, false),
Arguments.of(iso_8859_1, utf_8, false),
Arguments.of(iso_8859_1, utf_16be, false),
Arguments.of(iso_8859_1, cp1252, false),
ck(iso_8859_15, us_ascii, true);
ck(iso_8859_15, iso_8859_1, false);
ck(iso_8859_15, iso_8859_15, true);
ck(iso_8859_15, utf_8, false);
ck(iso_8859_15, utf_16be, false);
ck(iso_8859_15, cp1252, false);
Arguments.of(iso_8859_15, us_ascii, true),
Arguments.of(iso_8859_15, iso_8859_1, false),
Arguments.of(iso_8859_15, iso_8859_15, true),
Arguments.of(iso_8859_15, utf_8, false),
Arguments.of(iso_8859_15, utf_16be, false),
Arguments.of(iso_8859_15, cp1252, false),
ck(utf_8, us_ascii, true);
ck(utf_8, iso_8859_1, true);
ck(utf_8, iso_8859_15, true);
ck(utf_8, utf_8, true);
ck(utf_8, utf_16be, true);
ck(utf_8, cp1252, true);
Arguments.of(utf_8, us_ascii, true),
Arguments.of(utf_8, iso_8859_1, true),
Arguments.of(utf_8, iso_8859_15, true),
Arguments.of(utf_8, utf_8, true),
Arguments.of(utf_8, utf_16be, true),
Arguments.of(utf_8, cp1252, true),
ck(utf_16be, us_ascii, true);
ck(utf_16be, iso_8859_1, true);
ck(utf_16be, iso_8859_15, true);
ck(utf_16be, utf_8, true);
ck(utf_16be, utf_16be, true);
ck(utf_16be, cp1252, true);
Arguments.of(utf_16be, us_ascii, true),
Arguments.of(utf_16be, iso_8859_1, true),
Arguments.of(utf_16be, iso_8859_15, true),
Arguments.of(utf_16be, utf_8, true),
Arguments.of(utf_16be, utf_16be, true),
Arguments.of(utf_16be, cp1252, true),
ck(cp1252, us_ascii, true);
ck(cp1252, iso_8859_1, false);
ck(cp1252, iso_8859_15, false);
ck(cp1252, utf_8, false);
ck(cp1252, utf_16be, false);
ck(cp1252, cp1252, true);
checkUTF();
containsSelfTest();
Arguments.of(cp1252, us_ascii, true),
Arguments.of(cp1252, iso_8859_1, false),
Arguments.of(cp1252, iso_8859_15, false),
Arguments.of(cp1252, utf_8, false),
Arguments.of(cp1252, utf_16be, false),
Arguments.of(cp1252, cp1252, true)
);
}
static void checkUTF() throws Exception {
for (String utfName : utfNames)
for (String csName : charsetNames)
ck(Charset.forName(utfName),
Charset.forName(csName),
true);
}
/**
* Tests the assertion in the contains() method: "Every charset contains itself."
*/
static void containsSelfTest() {
boolean failed = false;
for (var entry : Charset.availableCharsets().entrySet()) {
Charset charset = entry.getValue();
boolean contains = charset.contains(charset);
System.out.println("Charset(" + charset.name() + ").contains(Charset(" + charset.name()
+ ")) returns " + contains);
if (!contains) {
failed = true;
}
}
if (failed) {
throw new RuntimeException("Charset.contains(itself) returns false for some charsets");
}
}
static String[] utfNames = {"utf-16",
"utf-8",
"utf-16le",
"utf-16be",
"x-utf-16le-bom"};
static String[] charsetNames = {
"US-ASCII",
"UTF-8",
"UTF-16",
"UTF-16BE",
"UTF-16LE",
"x-UTF-16LE-BOM",
"GBK",
"GB18030",
"ISO-8859-1",
"ISO-8859-15",
"ISO-8859-2",
"ISO-8859-3",
"ISO-8859-4",
"ISO-8859-5",
"ISO-8859-6",
"ISO-8859-7",
"ISO-8859-8",
"ISO-8859-9",
"ISO-8859-13",
"JIS_X0201",
"x-JIS0208",
"JIS_X0212-1990",
"GB2312",
"EUC-KR",
"x-EUC-TW",
"EUC-JP",
"x-euc-jp-linux",
"KOI8-R",
"TIS-620",
"x-ISCII91",
"windows-1251",
"windows-1252",
"windows-1253",
"windows-1254",
"windows-1255",
"windows-1256",
"windows-1257",
"windows-1258",
"windows-932",
"x-mswin-936",
"x-windows-949",
"x-windows-950",
"windows-31j",
"Big5",
"Big5-HKSCS",
"x-MS950-HKSCS",
"ISO-2022-JP",
"ISO-2022-KR",
"x-ISO-2022-CN-CNS",
"x-ISO-2022-CN-GB",
"Big5-HKSCS",
"x-Johab",
"Shift_JIS"
};
}

@ -1,84 +0,0 @@
/*
* Copyright (c) 2010, 2017, 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 4786884
* @summary Ensure that passing the empty string to Charset methods and
* constructors causes an IllegalArgumentException to be thrown
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
public class EmptyCharsetName {
static abstract class Test {
public abstract void go() throws Exception;
Test() throws Exception {
try {
go();
} catch (Exception x) {
if (x instanceof IllegalCharsetNameException) {
System.err.println("Thrown as expected: " + x);
return;
}
throw new Exception("Incorrect exception: "
+ x.getClass().getName(),
x);
}
throw new Exception("No exception thrown");
}
}
public static void main(String[] args) throws Exception {
new Test() {
public void go() throws Exception {
Charset.forName("");
}};
new Test() {
public void go() throws Exception {
Charset.isSupported("");
}};
new Test() {
public void go() throws Exception {
new Charset("", new String[] { }) {
public CharsetDecoder newDecoder() {
return null;
}
public CharsetEncoder newEncoder() {
return null;
}
public boolean contains(Charset cs) {
return false;
}
};
}};
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
@ -23,23 +23,42 @@
/* @test
* @summary Unit test for encode/decode convenience methods
* @run junit EncDec
*/
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.stream.Stream;
import java.nio.*;
import java.nio.charset.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EncDec {
public static void main(String[] args) throws Exception {
String s = "Hello, world!";
/**
* Test that the input String is the same after round tripping
* the Charset.encode() and Charset.decode() methods.
*/
@ParameterizedTest
@MethodSource("stringProvider")
public void roundTripTest(String pre) {
ByteBuffer bb = ByteBuffer.allocate(100);
bb.put(Charset.forName("ISO-8859-15").encode(s)).flip();
String t = Charset.forName("UTF-8").decode(bb).toString();
System.err.println(t);
if (!t.equals(s))
throw new Exception("Mismatch: " + s + " != " + t);
Charset preCs = Charset.forName("ISO-8859-15");
if (!preCs.canEncode()) {
throw new RuntimeException("Error: Trying to test encode and " +
"decode methods on a charset that does not support encoding");
}
bb.put(preCs.encode(pre)).flip();
String post = Charset.forName("UTF-8").decode(bb).toString();
assertEquals(pre, post, "Mismatch after encoding + decoding, :");
}
static Stream<String> stringProvider() {
return Stream.of(
"Hello, world!",
"apple, banana, orange",
"car, truck, horse");
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
@ -22,46 +22,71 @@
*/
/* @test
* @bug 6330020 8184665
* @bug 4786884 6330020 8184665
* @summary Ensure Charset.forName/isSupport throws the correct exception
* if the charset names passed in are illegal.
* @run junit IllegalCharsetName
*/
import java.nio.charset.*;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.IllegalCharsetNameException;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class IllegalCharsetName {
public static void main(String[] args) throws Exception {
String[] illegalNames = {
".",
"_",
":",
"-",
".name",
"_name",
":name",
"-name",
"name*name",
"name?name"
};
for (int i = 0; i < illegalNames.length; i++) {
try {
Charset.forName(illegalNames[i]);
throw new Exception("Charset.forName(): No exception thrown");
} catch (IllegalCharsetNameException x) { //expected
}
try {
Charset.isSupported(illegalNames[i]);
throw new Exception("Charset.isSupported(): No exception thrown");
} catch (IllegalCharsetNameException x) { //expected
}
// Charset.forName and Charset.isSupported should throw an
// IllegalCharsetNameException when passed an illegal name
@ParameterizedTest
@MethodSource("illegalNames")
public void illegalCharsetsTest(String name) {
assertThrows(IllegalCharsetNameException.class,
() -> Charset.forName(name));
assertThrows(IllegalCharsetNameException.class,
() -> Charset.forName(name));
}
// Charset.forName, Charset.isSupported, and the Charset constructor should
// throw an IllegalCharsetNameException when passed an empty name
@Test
public void emptyCharsetsTest() {
assertThrows(IllegalCharsetNameException.class,
() -> Charset.forName(""));
assertThrows(IllegalCharsetNameException.class,
() -> Charset.forName(""));
assertThrows(IllegalCharsetNameException.class,
() -> new Charset("", new String[]{}) {
@Override
public boolean contains(Charset cs) {
return false;
}
@Override
public CharsetDecoder newDecoder() {
return null;
}
@Override
public CharsetEncoder newEncoder() {
return null;
}
});
}
// Standard charsets may bypass alias checking during startup, test that
// they're all well-behaved as a sanity test
@Test
public void aliasTest() {
for (Charset cs : Charset.availableCharsets().values()) {
checkAliases(cs);
}
// Standard charsets may bypass alias checking during startup, test that
// they're all well-behaved as a sanity test
checkAliases(StandardCharsets.ISO_8859_1);
checkAliases(StandardCharsets.US_ASCII);
checkAliases(StandardCharsets.UTF_8);
}
private static void checkAliases(Charset cs) {
@ -70,4 +95,19 @@ public class IllegalCharsetName {
Charset.isSupported(alias);
}
}
static Stream<String> illegalNames() {
return Stream.of(
".",
"_",
":",
"-",
".name",
"_name",
":name",
"-name",
"name*name",
"name?name"
);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
@ -24,29 +24,21 @@
/* @test
* @bug 4448594
* @summary Ensure passing null to Charset.forName throws the correct exception
* @run junit NullCharsetName
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.util.*;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NullCharsetName {
public static void main(String[] args) throws Exception {
try {
Charset.forName(null);
} catch (Exception x) {
if (x instanceof IllegalArgumentException) {
System.err.println("Thrown as expected: " + x);
return;
}
throw new Exception("Incorrect exception: "
+ x.getClass().getName(),
x);
}
throw new Exception("No exception thrown");
// Charset.forName should throw an exception when passed null
@Test
public void nullCharsetTest() {
assertThrows(IllegalArgumentException.class,
() -> Charset.forName(null));
}
}

File diff suppressed because it is too large Load Diff