8160564: TEST: Add a test to check the implementation of VersionProps.versionNumbers()
authorsimonis
Thu, 14 Jul 2016 16:49:30 +0200
changeset 39648 9bbd1300c256
parent 39647 cd5deb6026dd
child 39649 aa000751d3d9
8160564: TEST: Add a test to check the implementation of VersionProps.versionNumbers() Reviewed-by: mchung, redestad
jdk/src/java.base/share/classes/java/lang/VersionProps.java.template
jdk/test/java/lang/Runtime/Version/VersionProps.java
--- a/jdk/src/java.base/share/classes/java/lang/VersionProps.java.template	Thu Jul 14 19:31:43 2016 +0800
+++ b/jdk/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);
     }
 
-    static List<Integer> versionNumbers() {
-        List<Integer> versionNumbers = new ArrayList<>(4);
+    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_NUMBER.indexOf('.');
+        int index = version.indexOf('.');
         while (index > 0) {
-            versionNumbers.add(
-                    Integer.parseInt(VERSION_NUMBER, prevIndex, index, 10));
+            verNumbers.add(parseVersionNumber(version, prevIndex, index));
             prevIndex = index + 1; // Skip the period
-            index = VERSION_NUMBER.indexOf('.', prevIndex);
+            index = version.indexOf('.', prevIndex);
         }
-        versionNumbers.add(Integer.parseInt(VERSION_NUMBER,
-                prevIndex, VERSION_NUMBER.length(), 10));
-        return versionNumbers;
+        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() {
+        return parseVersionNumbers(VERSION_NUMBER);
     }
 
     static Optional<String> pre() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/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;
+            }
+        }
+    }
+}