1 /*
2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /* @test
25  * @summary Unit test for charset containment
26  * @bug 6798572
27  */
28 
29 import java.nio.charset.*;
30 
31 
32 public class Contains {
33 
ck(Charset cs1, Charset cs2, boolean cont)34     static void ck(Charset cs1, Charset cs2, boolean cont) throws Exception {
35         if ((cs1.contains(cs2)) != cont)
36             throw new Exception("Wrong answer: "
37                                 + cs1.name() + " contains " + cs2.name());
38         System.err.println(cs1.name()
39                            + (cont ? " contains " : " does not contain ")
40                            + cs2.name());
41     }
42 
main(String[] args)43     public static void main(String[] args) throws Exception {
44 
45         Charset us_ascii = Charset.forName("US-ASCII");
46         Charset iso_8859_1 = Charset.forName("ISO-8859-1");
47         Charset iso_8859_15 = Charset.forName("ISO-8859-15");
48         Charset utf_8 = Charset.forName("UTF-8");
49         Charset utf_16be = Charset.forName("UTF-16BE");
50         Charset cp1252 = Charset.forName("CP1252");
51 
52         ck(us_ascii, us_ascii, true);
53         ck(us_ascii, iso_8859_1, false);
54         ck(us_ascii, iso_8859_15, false);
55         ck(us_ascii, utf_8, false);
56         ck(us_ascii, utf_16be, false);
57         ck(us_ascii, cp1252, false);
58 
59         ck(iso_8859_1, us_ascii, true);
60         ck(iso_8859_1, iso_8859_1, true);
61         ck(iso_8859_1, iso_8859_15, false);
62         ck(iso_8859_1, utf_8, false);
63         ck(iso_8859_1, utf_16be, false);
64         ck(iso_8859_1, cp1252, false);
65 
66         ck(iso_8859_15, us_ascii, true);
67         ck(iso_8859_15, iso_8859_1, false);
68         ck(iso_8859_15, iso_8859_15, true);
69         ck(iso_8859_15, utf_8, false);
70         ck(iso_8859_15, utf_16be, false);
71         ck(iso_8859_15, cp1252, false);
72 
73         ck(utf_8, us_ascii, true);
74         ck(utf_8, iso_8859_1, true);
75         ck(utf_8, iso_8859_15, true);
76         ck(utf_8, utf_8, true);
77         ck(utf_8, utf_16be, true);
78         ck(utf_8, cp1252, true);
79 
80         ck(utf_16be, us_ascii, true);
81         ck(utf_16be, iso_8859_1, true);
82         ck(utf_16be, iso_8859_15, true);
83         ck(utf_16be, utf_8, true);
84         ck(utf_16be, utf_16be, true);
85         ck(utf_16be, cp1252, true);
86 
87         ck(cp1252, us_ascii, true);
88         ck(cp1252, iso_8859_1, false);
89         ck(cp1252, iso_8859_15, false);
90         ck(cp1252, utf_8, false);
91         ck(cp1252, utf_16be, false);
92         ck(cp1252, cp1252, true);
93 
94         checkUTF();
95     }
96 
checkUTF()97     static void checkUTF() throws Exception {
98         for (String utfName : utfNames)
99             for (String csName : charsetNames)
100                 ck(Charset.forName(utfName),
101                    Charset.forName(csName),
102                    true);
103     }
104 
105     static String[] utfNames = {"utf-16",
106                          "utf-8",
107                          "utf-16le",
108                          "utf-16be",
109                          "x-utf-16le-bom"};
110 
111     static String[] charsetNames = {
112         "US-ASCII",
113         "UTF-8",
114         "UTF-16",
115         "UTF-16BE",
116         "UTF-16LE",
117         "x-UTF-16LE-BOM",
118         "GBK",
119         "GB18030",
120         "ISO-8859-1",
121         "ISO-8859-15",
122         "ISO-8859-2",
123         "ISO-8859-3",
124         "ISO-8859-4",
125         "ISO-8859-5",
126         "ISO-8859-6",
127         "ISO-8859-7",
128         "ISO-8859-8",
129         "ISO-8859-9",
130         "ISO-8859-13",
131         "JIS_X0201",
132         "x-JIS0208",
133         "JIS_X0212-1990",
134         "GB2312",
135         "EUC-KR",
136         "x-EUC-TW",
137         "EUC-JP",
138         "x-euc-jp-linux",
139         "KOI8-R",
140         "TIS-620",
141         "x-ISCII91",
142         "windows-1251",
143         "windows-1252",
144         "windows-1253",
145         "windows-1254",
146         "windows-1255",
147         "windows-1256",
148         "windows-1257",
149         "windows-1258",
150         "windows-932",
151         "x-mswin-936",
152         "x-windows-949",
153         "x-windows-950",
154         "windows-31j",
155         "Big5",
156         "Big5-HKSCS",
157         "x-MS950-HKSCS",
158         "ISO-2022-JP",
159         "ISO-2022-KR",
160         "x-ISO-2022-CN-CNS",
161         "x-ISO-2022-CN-GB",
162         "Big5-HKSCS",
163         "x-Johab",
164         "Shift_JIS"
165     };
166 }
167