changeset 17130:d24c3e835865

8160564: TEST: Add a test to check the implementation of VersionProps.versionNumbers() Reviewed-by: mchung, redestad
author simonis
date Thu, 14 Jul 2016 16:49:30 +0200
parents 600438917b1e
children d3eba57b40fd
files src/java.base/share/classes/java/lang/VersionProps.java.template test/java/lang/Runtime/Version/VersionProps.java
diffstat 2 files changed, 130 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/VersionProps.java.template	Thu Jul 14 19:31:43 2016 +0800
+++ b/src/java.base/share/classes/java/lang/VersionProps.java.template	Thu Jul 14 16:49:30 2016 +0200
@@ -67,19 +67,35 @@
         System.setProperty("java.runtime.name", java_runtime_name);
     }
 
+    private static int parseVersionNumber(String version, int prevIndex, int index) {
+        if (index - prevIndex > 1 &&
+            Character.digit(version.charAt(prevIndex), 10) <= 0)
+            throw new IllegalArgumentException("Leading zeros not supported (" +
+                    version.substring(prevIndex, index) + ")");
+        return Integer.parseInt(version, prevIndex, index, 10);
+    }
+
+    // This method is reflectively used by regression tests.
+    static List<Integer> parseVersionNumbers(String version) {
+        List<Integer> verNumbers = new ArrayList<>(4);
+        int prevIndex = 0;
+        int index = version.indexOf('.');
+        while (index > 0) {
+            verNumbers.add(parseVersionNumber(version, prevIndex, index));
+            prevIndex = index + 1; // Skip the period
+            index = version.indexOf('.', prevIndex);
+        }
+        verNumbers.add(parseVersionNumber(version, prevIndex, version.length()));
+
+        if (verNumbers.get(0) == 0 || verNumbers.get(verNumbers.size() - 1) == 0)
+            throw new IllegalArgumentException("Leading/trailing zeros not supported (" +
+                    verNumbers + ")");
+
+        return verNumbers;
+    }
+
     static List<Integer> versionNumbers() {
-        List<Integer> versionNumbers = new ArrayList<>(4);
-        int prevIndex = 0;
-        int index = VERSION_NUMBER.indexOf('.');
-        while (index > 0) {
-            versionNumbers.add(
-                    Integer.parseInt(VERSION_NUMBER, prevIndex, index, 10));
-            prevIndex = index + 1; // Skip the period
-            index = VERSION_NUMBER.indexOf('.', prevIndex);
-        }
-        versionNumbers.add(Integer.parseInt(VERSION_NUMBER,
-                prevIndex, VERSION_NUMBER.length(), 10));
-        return versionNumbers;
+        return parseVersionNumbers(VERSION_NUMBER);
     }
 
     static Optional<String> pre() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/Runtime/Version/VersionProps.java	Thu Jul 14 16:49:30 2016 +0200
@@ -0,0 +1,102 @@
+/*
+ * 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<Integer> li =
+                (List<Integer>)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++) {
+            try {
+                List<Integer> li =
+                        (List<Integer>)parseVersionNumbers.invoke(null, invalidVersions[i]);
+                throw new Exception(invalidVersions[i] +
+                        " not recognized as invalid by VersionProps.parseVersionNumbers()");
+            } catch (InvocationTargetException ex) {
+                if (ex.getCause() instanceof IllegalArgumentException) {
+                    System.out.println("OK - caught bad version string " +
+                            invalidVersions[i]);
+                } else {
+                    throw ex;
+                }
+            }
+
+            try {
+                List<Integer> li = Runtime.Version.parse(invalidVersions[i]).version();
+                throw new Exception(invalidVersions[i] +
+                        " not recognized as invalid by Runtime.Version.parse()");
+            } catch (IllegalArgumentException ex) {
+                continue;
+            }
+        }
+    }
+}