Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/PrecisionDecimalDV/XPrecisionDecimalToString.java
38853 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* @run main XPrecisionDecimalToString34* @author Daniel Fuchs35*/36public class XPrecisionDecimalToString {3738private static final String className =39"com.sun.org.apache.xerces.internal.impl.dv.xs.PrecisionDecimalDV$XPrecisionDecimal";40private static final String methodName = "canonicalToStringForHashCode";41private static final Class<?>[] signature = { String.class, String.class, int.class, int.class };42private static Method method;4344// Invokes XPrecisionDecimal.canonicalToStringForHashCode through reflection,45// because the method is private...46//47// Construct a canonical String representation of this number48// for the purpose of deriving a hashCode value compliant with49// equals.50// The toString representation will be:51// NaN for NaN, INF for +infinity, -INF for -infinity, 0 for zero,52// and [1-9]\.[0-9]*[1-9]?(E[1-9][0-9]*)? for other numbers.53private static String canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue) {54try {55if (method == null) {56Class<?> type = Class.forName(className);57method = type.getDeclaredMethod(methodName, signature);58method.setAccessible(true);59}60} catch (Exception x) {61throw new Error("Impossible to find '"+className+"."+methodName+"': "+ x, x);62}63try {64return (String) method.invoke(null, new Object[] {ivalue, fvalue, sign, pvalue} );65} catch(Exception x) {66throw new Error("Failed to invoke "+className+"."+methodName+"(\""+67ivalue+"\", \""+fvalue+"\", "+sign+", "+pvalue+"): " +x, x);68}69}7071/**72* @param args the command line arguments73*/74public static void main(String[] args) {75test("123","7890",-1,0,"-1.23789E2");76test("0","007890",-1,0,"-7.89E-3");77test("123","7890",1,0,"1.23789E2");78test("0","007890",1,0,"7.89E-3");79test("123","7890",1,10,"1.23789E12");80test("0","007890",1,33,"7.89E30");81test("INF","",1,0,"INF");82test("INF","",-1,0,"-INF");83test("NaN","",0,0,"NaN");84test("0","",1,0,"0");85test("00000","00000",1,10,"0");86test("00000","00000",-1,10,"0");87test("00000","000001",-1,-10,"-1E-16");88}8990private static void test(String ival, String fval, int sign, int pvalue, String expected) {91final String canonical = canonicalToStringForHashCode(ival, fval, sign, pvalue);92System.out.println((sign == -1 ? "-" : "") + ival +93("INF".equals(ival) || "NaN".equals(ival) ? ""94: ( "." + fval + "E" + pvalue))95+ " => "+ canonical);96if (!expected.equals(canonical)) {97throw new Error("expected: "+expected+" got: "+ canonical);98}99}100101102}103104105