/* * 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.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", "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(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(validVersions[i] + " != " + validLists[i]); li = Runtime.Version.parse(validVersions[i]).version(); if (!validLists[i].equals(li)) throw new Exception(validVersions[i] + " != " + validLists[i]); } for (int i = 0; i < invalidVersions.length; i++) { // java.lang.VersionProps.parseVersionNumbers() is not doing any error checking because it // expects to get valid input (see 'JDK-8160000: Runtime.version() cause startup regressions' // at https://bugs.openjdk.java.net/browse/JDK-8160000). So we can not test against bad input. // // List li = (List)parseVersionNumbers.invoke(null, invalidVersions[i]); try { List li = Runtime.Version.parse(invalidVersions[i]).version(); } catch (IllegalArgumentException ex) { continue; } throw new Exception(invalidVersions[i] + " not recognized as invalid!"); } } }