Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8167252: Some of Charset.availableCharsets() does not contain itself #14473

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/java.base/share/classes/sun/nio/cs/Unicode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
Expand Down Expand Up @@ -45,6 +45,12 @@ public boolean contains(Charset cs) {
|| (cs instanceof UTF_16BE)
|| (cs instanceof UTF_16LE)
|| (cs instanceof UTF_16LE_BOM)
|| (cs instanceof CESU_8)
|| (cs instanceof UTF_32)
|| (cs instanceof UTF_32BE)
|| (cs instanceof UTF_32BE_BOM)
|| (cs instanceof UTF_32LE)
|| (cs instanceof UTF_32LE_BOM)
|| (cs.name().equals("GBK"))
|| (cs.name().equals("GB18030"))
|| (cs.name().equals("ISO-8859-2"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
Expand Down Expand Up @@ -51,7 +51,8 @@ public class EUC_JP_Open
public boolean contains(Charset cs) {
return ((cs.name().equals("US-ASCII"))
|| (cs instanceof JIS_X_0201)
|| (cs instanceof EUC_JP));
|| (cs instanceof EUC_JP)
|| (cs instanceof EUC_JP_Open));
}

public CharsetDecoder newDecoder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public boolean contains(Charset cs) {
return ((cs.name().equals("US-ASCII"))
|| (cs instanceof SJIS)
|| (cs instanceof EUC_JP)
|| (cs instanceof ISO2022_JP));
|| (cs instanceof ISO2022_JP)
|| (cs instanceof JISAutoDetect));
}

public boolean canEncode() {
Expand Down
24 changes: 22 additions & 2 deletions test/jdk/java/nio/charset/Charset/Contains.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,7 @@

/* @test
* @summary Unit test for charset containment
* @bug 6798572
* @bug 6798572 8167252
* @modules jdk.charsets
*/

Expand Down Expand Up @@ -93,6 +93,8 @@ public static void main(String[] args) throws Exception {
ck(cp1252, cp1252, true);

checkUTF();

selfContainmentTest();
}

static void checkUTF() throws Exception {
Expand All @@ -103,6 +105,24 @@ static void checkUTF() throws Exception {
true);
}

static void selfContainmentTest() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be nice to add a comment regarding the purpose of the test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec is that Charset::contains return true for self so maybe it could be renamed to containsSelfTest, might help with the understanding the purpose (along with a comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Will address these in the next iteration

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",
Expand Down