1/* 2 * Copyright (c) 2016 SAP SE. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8160564 27 * @summary check the implementation of VersionProps.versionNumbers() 28 * @modules java.base/java.lang:open 29 * @run main VersionProps 30 * @author Volker Simonis 31 */ 32 33import java.lang.reflect.InvocationTargetException; 34import java.lang.reflect.Method; 35import java.util.Arrays; 36import java.util.List; 37 38public class VersionProps { 39 40 final static String[] validVersions = { 41 "1", "1.2", "1.2.3", "1.2.3.4", "1.0.0.1", 42 "1.10000.1", "1.0.2.0.0.3.0.0.0.4.5.0.0.6", 43 "1000001", "1.2.3.4.5.6.7.8.9.0.9.8.7.6.5.4.3.2.1" }; 44 45 @SuppressWarnings("rawtypes") 46 final static List[] validLists = { 47 Arrays.asList(1), 48 Arrays.asList(1, 2), 49 Arrays.asList(1, 2, 3), 50 Arrays.asList(1, 2, 3, 4), 51 Arrays.asList(1, 0, 0, 1), 52 Arrays.asList(1, 10000, 1), 53 Arrays.asList(1, 0, 2, 0, 0, 3, 0, 0, 0, 4, 5, 0, 0, 6), 54 Arrays.asList(1000001), 55 Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1) }; 56 57 final static String[] invalidVersions = { 58 "01", "0.1.2", "1.02.3", "1.2.03.4", "1.0.0.1.0", 59 "1.0.1.0.0", "1.00.1", "1.0.1.00", "1.10000." }; 60 61 public static void main(String[] args) throws Exception { 62 Class<?> versionProps = Class.forName("java.lang.VersionProps"); 63 Method parseVersionNumbers = 64 versionProps.getDeclaredMethod("parseVersionNumbers", String.class); 65 parseVersionNumbers.setAccessible(true); 66 67 for (int i = 0; i < validVersions.length; i++) { 68 @SuppressWarnings("unchecked") 69 List<Integer> li = 70 (List<Integer>)parseVersionNumbers.invoke(null, validVersions[i]); 71 System.out.println(li); 72 if (!validLists[i].equals(li)) 73 throw new Exception(li + " != " + validLists[i]); 74 li = Runtime.Version.parse(validVersions[i]).version(); 75 if (!validLists[i].equals(li)) 76 throw new Exception(li + " != " + validLists[i]); 77 } 78 79 for (int i = 0; i < invalidVersions.length; i++) { 80 try { 81 List<Integer> li = 82 (List<Integer>)parseVersionNumbers.invoke(null, invalidVersions[i]); 83 throw new Exception(invalidVersions[i] + 84 " not recognized as invalid by VersionProps.parseVersionNumbers()"); 85 } catch (InvocationTargetException ex) { 86 if (ex.getCause() instanceof IllegalArgumentException) { 87 System.out.println("OK - caught bad version string " + 88 invalidVersions[i]); 89 } else { 90 throw ex; 91 } 92 } 93 94 try { 95 List<Integer> li = Runtime.Version.parse(invalidVersions[i]).version(); 96 throw new Exception(invalidVersions[i] + 97 " not recognized as invalid by Runtime.Version.parse()"); 98 } catch (IllegalArgumentException ex) { 99 continue; 100 } 101 } 102 } 103} 104