Path: blob/master/test/jdk/javax/xml/jaxp/PrecisionDecimalDV/XPrecisionDecimalToString.java
41152 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.reflect.Method;2425/**26* @test27* @bug 801390028* @summary More warning compiling jaxp.29* This test only test one of the methods used to implement hashCode()30* in com.sun.org.apache.xerces.internal.impl.dv.xs.PrecisionDecimalDV$XPrecisionDecimal.31* Since that method is private the test unfortunately needs to use reflection32* to invoke the method.33* @modules java.xml/com.sun.org.apache.xerces.internal.impl.dv.xs:open34* @run main XPrecisionDecimalToString35* @author Daniel Fuchs36*/37public class XPrecisionDecimalToString {3839private static final String className =40"com.sun.org.apache.xerces.internal.impl.dv.xs.PrecisionDecimalDV$XPrecisionDecimal";41private static final String methodName = "canonicalToStringForHashCode";42private static final Class<?>[] signature = { String.class, String.class, int.class, int.class };43private static Method method;4445// Invokes XPrecisionDecimal.canonicalToStringForHashCode through reflection,46// because the method is private...47//48// Construct a canonical String representation of this number49// for the purpose of deriving a hashCode value compliant with50// equals.51// The toString representation will be:52// NaN for NaN, INF for +infinity, -INF for -infinity, 0 for zero,53// and [1-9]\.[0-9]*[1-9]?(E[1-9][0-9]*)? for other numbers.54private static String canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue) {55try {56if (method == null) {57Class<?> type = Class.forName(className);58method = type.getDeclaredMethod(methodName, signature);59method.setAccessible(true);60}61} catch (Exception x) {62throw new Error("Impossible to find '"+className+"."+methodName+"': "+ x, x);63}64try {65return (String) method.invoke(null, new Object[] {ivalue, fvalue, sign, pvalue} );66} catch(Exception x) {67throw new Error("Failed to invoke "+className+"."+methodName+"(\""+68ivalue+"\", \""+fvalue+"\", "+sign+", "+pvalue+"): " +x, x);69}70}7172/**73* @param args the command line arguments74*/75public static void main(String[] args) {76test("123","7890",-1,0,"-1.23789E2");77test("0","007890",-1,0,"-7.89E-3");78test("123","7890",1,0,"1.23789E2");79test("0","007890",1,0,"7.89E-3");80test("123","7890",1,10,"1.23789E12");81test("0","007890",1,33,"7.89E30");82test("INF","",1,0,"INF");83test("INF","",-1,0,"-INF");84test("NaN","",0,0,"NaN");85test("0","",1,0,"0");86test("00000","00000",1,10,"0");87test("00000","00000",-1,10,"0");88test("00000","000001",-1,-10,"-1E-16");89}9091private static void test(String ival, String fval, int sign, int pvalue, String expected) {92final String canonical = canonicalToStringForHashCode(ival, fval, sign, pvalue);93System.out.println((sign == -1 ? "-" : "") + ival +94("INF".equals(ival) || "NaN".equals(ival) ? ""95: ( "." + fval + "E" + pvalue))96+ " => "+ canonical);97if (!expected.equals(canonical)) {98throw new Error("expected: "+expected+" got: "+ canonical);99}100}101102103}104105106