Path: blob/master/test/jdk/java/nio/charset/Charset/IllegalCharsetName.java
41155 views
/*1* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 6330020 818466525* @summary Ensure Charset.forName/isSupport throws the correct exception26* if the charset names passed in are illegal.27*/2829import java.nio.charset.*;3031public class IllegalCharsetName {32public static void main(String[] args) throws Exception {33String[] illegalNames = {34".",35"_",36":",37"-",38".name",39"_name",40":name",41"-name",42"name*name",43"name?name"44};45for (int i = 0; i < illegalNames.length; i++) {46try {47Charset.forName(illegalNames[i]);48throw new Exception("Charset.forName(): No exception thrown");49} catch (IllegalCharsetNameException x) { //expected50}5152try {53Charset.isSupported(illegalNames[i]);54throw new Exception("Charset.isSupported(): No exception thrown");55} catch (IllegalCharsetNameException x) { //expected56}57}5859// Standard charsets may bypass alias checking during startup, test that60// they're all well-behaved as a sanity test61checkAliases(StandardCharsets.ISO_8859_1);62checkAliases(StandardCharsets.US_ASCII);63checkAliases(StandardCharsets.UTF_8);64}6566private static void checkAliases(Charset cs) {67for (String alias : cs.aliases()) {68Charset.forName(alias);69Charset.isSupported(alias);70}71}72}737475