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  * @run main VersionProps
  29  * @author Volker Simonis
  30  */
  31 
  32 import java.lang.reflect.InvocationTargetException;
  33 import java.lang.reflect.Method;
  34 import java.util.Arrays;
  35 import java.util.List;
  36 
  37 public class VersionProps {
  38 
  39     final 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" };
  43 
  44     @SuppressWarnings("rawtypes")
  45     final static List[] validLists = {
  46         Arrays.asList(1),
  47         Arrays.asList(1, 2),
  48         Arrays.asList(1, 2, 3),
  49         Arrays.asList(1, 2, 3, 4),
  50         Arrays.asList(1, 0, 0, 1),
  51         Arrays.asList(1, 10000, 1),
  52         Arrays.asList(1, 0, 2, 0, 0, 3, 0, 0, 0, 4, 5, 0, 0, 6),
  53         Arrays.asList(1000001),
  54         Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1) };
  55 
  56     final 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." };
  59 
  60     public static void main(String[] args) throws Exception {
  61         Class<?> versionProps = Class.forName("java.lang.VersionProps");
  62         Method parseVersionNumbers =
  63             versionProps.getDeclaredMethod("parseVersionNumbers", String.class);
  64         parseVersionNumbers.setAccessible(true);
  65 
  66         for (int i = 0; i < validVersions.length; i++) {
  67             @SuppressWarnings("unchecked")
  68             List<Integer> li =
  69                 (List<Integer>)parseVersionNumbers.invoke(null, validVersions[i]);
  70             System.out.println(li);
  71             if (!validLists[i].equals(li))
  72                 throw new Exception(li + " != " + validLists[i]);
  73             li = Runtime.Version.parse(validVersions[i]).version();
  74             if (!validLists[i].equals(li))
  75                 throw new Exception(li + " != " + validLists[i]);
  76         }
  77 
  78         for (int i = 0; i < invalidVersions.length; i++) {
  79             boolean error = true;
  80             try {
  81                 List<Integer> li =
  82                     (List<Integer>)parseVersionNumbers.invoke(null, invalidVersions[i]);
  83             } catch (InvocationTargetException ex) {
  84                 if (ex.getCause() instanceof IllegalArgumentException) {
  85                     error = false;
  86                     System.out.println("OK - caught bad version string " +
  87                             invalidVersions[i]);
  88                 } else {
  89                     throw ex;
  90                 }
  91             }
  92             if (error) {
  93                 throw new Exception(invalidVersions[i] +
  94                         " not recognized as invalid by VersionProps.parseVersionNumbers()");
  95             }
  96 
  97             try {
  98                 List<Integer> li = Runtime.Version.parse(invalidVersions[i]).version();
  99             } catch (IllegalArgumentException ex) {
 100                 continue;
 101             }
 102             throw new Exception(invalidVersions[i] +
 103                     " not recognized as invalid by Runtime.Version.parse()");
 104         }
 105     }
 106 }