Path: blob/master/test/jdk/java/lang/RuntimeTests/Version/VersionProps.java
41153 views
/*1* Copyright (c) 2016 SAP SE. 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*/2223/*24* @test25* @bug 816056426* @summary check the implementation of VersionProps.versionNumbers()27* @modules java.base/java.lang:open28* @run main VersionProps29* @author Volker Simonis30*/3132import java.lang.reflect.InvocationTargetException;33import java.lang.reflect.Method;34import java.util.Arrays;35import java.util.List;3637public class VersionProps {3839final static String[] validVersions = {40"1", "1.2", "1.2.3", "1.2.3.4", "1.0.0.1",41"1.10000.1", "1.0.2.0.0.3.0.0.0.4.5.0.0.6",42"1000001", "1.2.3.4.5.6.7.8.9.0.9.8.7.6.5.4.3.2.1" };4344@SuppressWarnings("rawtypes")45final static List[] validLists = {46Arrays.asList(1),47Arrays.asList(1, 2),48Arrays.asList(1, 2, 3),49Arrays.asList(1, 2, 3, 4),50Arrays.asList(1, 0, 0, 1),51Arrays.asList(1, 10000, 1),52Arrays.asList(1, 0, 2, 0, 0, 3, 0, 0, 0, 4, 5, 0, 0, 6),53Arrays.asList(1000001),54Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1) };5556final static String[] invalidVersions = {57"01", "0.1.2", "1.02.3", "1.2.03.4", "1.0.0.1.0",58"1.0.1.0.0", "1.00.1", "1.0.1.00", "1.10000." };5960public static void main(String[] args) throws Exception {61Class<?> versionProps = Class.forName("java.lang.VersionProps");62Method parseVersionNumbers =63versionProps.getDeclaredMethod("parseVersionNumbers", String.class);64parseVersionNumbers.setAccessible(true);6566for (int i = 0; i < validVersions.length; i++) {67@SuppressWarnings("unchecked")68List<Integer> li =69(List<Integer>)parseVersionNumbers.invoke(null, validVersions[i]);70System.out.println(li);71if (!validLists[i].equals(li))72throw new Exception(li + " != " + validLists[i]);73li = Runtime.Version.parse(validVersions[i]).version();74if (!validLists[i].equals(li))75throw new Exception(li + " != " + validLists[i]);76}7778for (int i = 0; i < invalidVersions.length; i++) {79try {80List<Integer> li =81(List<Integer>)parseVersionNumbers.invoke(null, invalidVersions[i]);82throw new Exception(invalidVersions[i] +83" not recognized as invalid by VersionProps.parseVersionNumbers()");84} catch (InvocationTargetException ex) {85if (ex.getCause() instanceof IllegalArgumentException) {86System.out.println("OK - caught bad version string " +87invalidVersions[i]);88} else {89throw ex;90}91}9293try {94List<Integer> li = Runtime.Version.parse(invalidVersions[i]).version();95throw new Exception(invalidVersions[i] +96" not recognized as invalid by Runtime.Version.parse()");97} catch (IllegalArgumentException ex) {98continue;99}100}101}102}103104105