1 /*
2  * Copyright (c) 2006, 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 /*
25  * @test
26  * @bug 4136371 5017980 6576055
27  * @summary Test Integer.decode method
28  * @author madbot
29  * @author Joseph D. Darcy
30  */
31 
32 /**
33  * There are six methods in java.lang.Integer which transform strings
34  * into an int or Integer value:
35  *
36  * public Integer(String s)
37  * public static Integer decode(String nm)
38  * public static int parseInteger(String s, int radix)
39  * public static int parseInteger(String s)
40  * public static Integer valueOf(String s, int radix)
41  * public static Integer valueOf(String s)
42  *
43  * The other five methods are tested elsewhere.
44  */
45 public class Decode {
46 
check(String val, int expected)47     private static void check(String val, int expected) {
48         int n = (Integer.decode(val)).intValue();
49         if (n != expected)
50             throw new RuntimeException("Integer.decode failed. String:" +
51                                                 val + " Result:" + n);
52     }
53 
checkFailure(String val, String message)54     private static void checkFailure(String val, String message) {
55         try {
56             int n = (Integer.decode(val)).intValue();
57             throw new RuntimeException(message);
58         } catch (NumberFormatException e) { /* Okay */}
59     }
60 
main(String[] args)61     public static void main(String[] args) throws Exception {
62         check(new String(""+Integer.MIN_VALUE), Integer.MIN_VALUE);
63         check(new String(""+Integer.MAX_VALUE), Integer.MAX_VALUE);
64 
65         check("10",   10);
66         check("0x10", 16);
67         check("0X10", 16);
68         check("010",  8);
69         check("#10",  16);
70 
71         check("+10",   10);
72         check("+0x10", 16);
73         check("+0X10", 16);
74         check("+010",  8);
75         check("+#10",  16);
76 
77         check("-10",   -10);
78         check("-0x10", -16);
79         check("-0X10", -16);
80         check("-010",  -8);
81         check("-#10",  -16);
82 
83         check(Long.toString(Integer.MIN_VALUE), Integer.MIN_VALUE);
84         check(Long.toString(Integer.MAX_VALUE), Integer.MAX_VALUE);
85 
86         checkFailure("0x-10",   "Integer.decode allows negative sign in wrong position.");
87         checkFailure("0x+10",   "Integer.decode allows positive sign in wrong position.");
88 
89         checkFailure("+",       "Raw plus sign allowed.");
90         checkFailure("-",       "Raw minus sign allowed.");
91 
92         checkFailure(Long.toString((long)Integer.MIN_VALUE - 1L), "Out of range");
93         checkFailure(Long.toString((long)Integer.MAX_VALUE + 1L), "Out of range");
94 
95         checkFailure("", "Empty String");
96 
97         try {
98             Integer.decode(null);
99             throw new RuntimeException("Integer.decode(null) expected to throw NPE");
100         } catch (NullPointerException npe) {/* Okay */}
101     }
102 }
103