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
34import java.io.*;
35import java.nio.*;
36import java.nio.charset.*;
37
38
39public 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;
97                            }
98                        };
99                }};
100    }
101
102}
103