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