# HG changeset patch # User simonis # Date 1468252166 -7200 # Mon Jul 11 17:49:26 2016 +0200 # Node ID 22930433b42f0615961dcd3c659cd94f4a4f7378 # Parent abf68fb8daa5a13c57dafe471152202dda26753e 8160564: TEST: Add a test to check the implementation of VersionProps.versionNumbers() Reviewed-by: mchung, redestad diff --git a/src/java.base/share/classes/java/lang/VersionProps.java.template b/src/java.base/share/classes/java/lang/VersionProps.java.template --- a/src/java.base/share/classes/java/lang/VersionProps.java.template +++ b/src/java.base/share/classes/java/lang/VersionProps.java.template @@ -67,21 +67,42 @@ System.setProperty("java.runtime.name", java_runtime_name); } - static List versionNumbers() { + private static void check(String s, int prevIndex, int index) { + if (index - prevIndex > 1 && + Character.digit(s.charAt(prevIndex), 10) <= 0) + throw new IllegalArgumentException("Leading zeros not supported (" + + s.substring(prevIndex, index) + ")"); + } + + // This method is reflectively regression tested from + // test/java/lang/Runtime/Version/VersionProps.java + // If you rename or remove this method, please update the test accordingly. + static List parseVersionNumbers(String versionNumber) { List versionNumbers = new ArrayList<>(4); int prevIndex = 0; - int index = VERSION_NUMBER.indexOf('.'); + int index = versionNumber.indexOf('.'); while (index > 0) { + check(versionNumber, prevIndex, index); versionNumbers.add( - Integer.parseInt(VERSION_NUMBER, prevIndex, index, 10)); + Integer.parseInt(versionNumber, prevIndex, index, 10)); prevIndex = index + 1; // Skip the period - index = VERSION_NUMBER.indexOf('.', prevIndex); + index = versionNumber.indexOf('.', prevIndex); } - versionNumbers.add(Integer.parseInt(VERSION_NUMBER, - prevIndex, VERSION_NUMBER.length(), 10)); + check(versionNumber, prevIndex, versionNumber.length()); + versionNumbers.add(Integer.parseInt(versionNumber, + prevIndex, versionNumber.length(), 10)); + + if (versionNumbers.get(0) == 0 || versionNumbers.get(versionNumbers.size() - 1) == 0) + throw new IllegalArgumentException("Leading or trailing zeros not supported (" + + versionNumbers + ")"); + return versionNumbers; } + static List versionNumbers() { + return parseVersionNumbers(VERSION_NUMBER); + } + static Optional pre() { return optionalOf(VERSION_PRE); } diff --git a/test/java/lang/Runtime/Version/VersionProps.java b/test/java/lang/Runtime/Version/VersionProps.java new file mode 100644 --- /dev/null +++ b/test/java/lang/Runtime/Version/VersionProps.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2016 SAP SE. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8160564 + * @summary check the implementation of VersionProps.versionNumbers() + * @run main VersionProps + * @author Volker Simonis + */ + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; + +public class VersionProps { + + final static String[] validVersions = { + "1", "1.2", "1.2.3", "1.2.3.4", "1.0.0.1", + "1.10000.1", "1.0.2.0.0.3.0.0.0.4.5.0.0.6", + "1000001", "1.2.3.4.5.6.7.8.9.0.9.8.7.6.5.4.3.2.1" }; + + @SuppressWarnings("rawtypes") + final static List[] validLists = { + Arrays.asList(1), + Arrays.asList(1, 2), + Arrays.asList(1, 2, 3), + Arrays.asList(1, 2, 3, 4), + Arrays.asList(1, 0, 0, 1), + Arrays.asList(1, 10000, 1), + Arrays.asList(1, 0, 2, 0, 0, 3, 0, 0, 0, 4, 5, 0, 0, 6), + Arrays.asList(1000001), + Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1) }; + + final static String[] invalidVersions = { + "01", "0.1.2", "1.02.3", "1.2.03.4", "1.0.0.1.0", + "1.0.1.0.0", "1.00.1", "1.0.1.00", "1.10000." }; + + public static void main(String[] args) throws Exception { + Class versionProps = Class.forName("java.lang.VersionProps"); + Method parseVersionNumbers = + versionProps.getDeclaredMethod("parseVersionNumbers", String.class); + parseVersionNumbers.setAccessible(true); + + for (int i = 0; i < validVersions.length; i++) { + @SuppressWarnings("unchecked") + List li = + (List)parseVersionNumbers.invoke(null, validVersions[i]); + System.out.println(li); + if (!validLists[i].equals(li)) + throw new Exception(li + " != " + validLists[i]); + li = Runtime.Version.parse(validVersions[i]).version(); + if (!validLists[i].equals(li)) + throw new Exception(li + " != " + validLists[i]); + } + + for (int i = 0; i < invalidVersions.length; i++) { + boolean error = true; + try { + List li = + (List)parseVersionNumbers.invoke(null, invalidVersions[i]); + } catch (InvocationTargetException ex) { + if (ex.getCause() instanceof IllegalArgumentException) { + error = false; + System.out.println("OK - caught bad version string " + + invalidVersions[i]); + } else { + throw ex; + } + } + if (error) { + throw new Exception(invalidVersions[i] + + " not recognized as invalid by VersionProps.parseVersionNumbers()"); + } + + try { + List li = Runtime.Version.parse(invalidVersions[i]).version(); + } catch (IllegalArgumentException ex) { + continue; + } + throw new Exception(invalidVersions[i] + + " not recognized as invalid by Runtime.Version.parse()"); + } + } +}