1 /*
   2  * Copyright (c) 2010, 2017, 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  * @bug 4786884
  26  * @summary Ensure that passing the empty string to Charset methods and
  27  *          constructors causes an IllegalArgumentException to be thrown
  28  */
  29 
  30 import java.io.*;
  31 import java.nio.*;
  32 import java.nio.charset.*;
  33 
  34 
  35 public class EmptyCharsetName {
  36 
  37     static abstract class Test {
  38 
  39         public abstract void go() throws Exception;
  40 
  41         Test() throws Exception {
  42             try {
  43                 go();
  44             } catch (Exception x) {
  45                 if (x instanceof IllegalCharsetNameException) {
  46                     System.err.println("Thrown as expected: " + x);
  47                     return;
  48                 }
  49                 throw new Exception("Incorrect exception: "
  50                                     + x.getClass().getName(),
  51                                     x);
  52             }
  53             throw new Exception("No exception thrown");
  54         }
  55 
  56     }
  57 
  58     public static void main(String[] args) throws Exception {
  59 
  60         new Test() {
  61                 public void go() throws Exception {
  62                     Charset.forName("");
  63                 }};
  64         new Test() {
  65                 public void go() throws Exception {
  66                     Charset.isSupported("");
  67                 }};
  68         new Test() {
  69                 public void go() throws Exception {
  70                     new Charset("", new String[] { }) {
  71                             public CharsetDecoder newDecoder() {
  72                                 return null;
  73                             }
  74                             public CharsetEncoder newEncoder() {
  75                                 return null;
  76                             }
  77                             public boolean contains(Charset cs) {
  78                                 return false;
  79                             }
  80                         };
  81                 }};
  82     }
  83 
  84 }