< prev index next >

test/java/nio/charset/Charset/EmptyCharsetName.java

Print this page


   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  * @bug 4786884
  26  * @summary Ensure that passing the empty string to Charset methods and
  27  *          constructors causes an IllegalArgumentException to be thrown
  28  *
  29  * @build EmptyCharsetName
  30  * @run main EmptyCharsetName
  31  * @run main/othervm -Dsun.nio.cs.bugLevel=1.4 EmptyCharsetName
  32  */
  33 
  34 import java.io.*;
  35 import java.nio.*;
  36 import java.nio.charset.*;
  37 
  38 
  39 public class EmptyCharsetName {
  40 
  41     static boolean compat;
  42 
  43     static abstract class Test {
  44 
  45         public abstract void go() throws Exception;
  46 
  47         Test() throws Exception {
  48             try {
  49                 go();
  50             } catch (Exception x) {
  51                 if (compat) {
  52                     if (x instanceof UnsupportedCharsetException) {
  53                         System.err.println("Thrown as expected: " + x);
  54                         return;
  55                     }
  56                     throw new Exception("Exception thrown", x);
  57                 }
  58                 if (x instanceof IllegalCharsetNameException) {
  59                     System.err.println("Thrown as expected: " + x);
  60                     return;
  61                 }
  62                 throw new Exception("Incorrect exception: "
  63                                     + x.getClass().getName(),
  64                                     x);
  65             }
  66             if (!compat)
  67                 throw new Exception("No exception thrown");
  68         }
  69 
  70     }
  71 
  72     public static void main(String[] args) throws Exception {
  73 
  74         // If sun.nio.cs.bugLevel == 1.4 then we want the 1.4 behavior
  75         String bl = System.getProperty("sun.nio.cs.bugLevel");
  76         compat = (bl != null && bl.equals("1.4"));
  77 
  78         new Test() {
  79                 public void go() throws Exception {
  80                     Charset.forName("");
  81                 }};
  82         new Test() {
  83                 public void go() throws Exception {
  84                     Charset.isSupported("");
  85                 }};
  86         new Test() {
  87                 public void go() throws Exception {
  88                     new Charset("", new String[] { }) {
  89                             public CharsetDecoder newDecoder() {
  90                                 return null;
  91                             }
  92                             public CharsetEncoder newEncoder() {
  93                                 return null;
  94                             }
  95                             public boolean contains(Charset cs) {
  96                                 return false;
   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;
< prev index next >