1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. 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 package org.graalvm.compiler.hotspot.test;
  26 
  27 import java.util.HashMap;
  28 import java.util.Map;
  29 import java.util.Properties;
  30 
  31 import org.graalvm.compiler.core.test.GraalCompilerTest;
  32 import org.graalvm.compiler.hotspot.JVMCIVersionCheck;
  33 import org.junit.Assert;
  34 import org.junit.Test;
  35 
  36 public class JVMCIVersionCheckTest extends GraalCompilerTest {
  37 
  38     @Test
  39     public void test01() {
  40         Properties sprops = System.getProperties();
  41         Map<String, String> props = new HashMap<>(sprops.size());
  42         for (String name : sprops.stringPropertyNames()) {
  43             props.put(name, sprops.getProperty(name));
  44         }
  45 
  46         for (int i = 0; i < 100; i++) {
  47             int minMajor = i;
  48             int minMinor = 100 - i;
  49             for (int j = 0; j < 100; j++) {
  50                 int major = j;
  51                 int minor = 100 - j;
  52 
  53                 boolean ok = (major > minMajor) || (major == minMajor && minor >= minMinor);
  54                 for (String sep : new String[]{".", "-b"}) {
  55                     String javaVmVersion = String.format("prefix-jvmci-%03d%s%03d-suffix", major, sep, minor);
  56                     if (ok) {
  57                         JVMCIVersionCheck.check(props, minMajor, minMinor, "1.8", javaVmVersion, false);
  58                     } else {
  59                         try {
  60                             JVMCIVersionCheck.check(props, minMajor, minMinor, "1.8", javaVmVersion, false);
  61                             Assert.fail("expected to fail checking " + javaVmVersion + " against " + minMajor + "." + minMinor);
  62                         } catch (InternalError e) {
  63                             // pass
  64                         }
  65                     }
  66                 }
  67             }
  68         }
  69 
  70         // Test handling of version components bigger than Integer.MAX_VALUE
  71         for (String sep : new String[]{".", "-b"}) {
  72             for (String version : new String[]{"0" + sep + Long.MAX_VALUE, Long.MAX_VALUE + sep + 0}) {
  73                 String javaVmVersion = String.format("prefix-jvmci-%s-suffix", version);
  74                 try {
  75                     JVMCIVersionCheck.check(props, 0, 59, "1.8", javaVmVersion, false);
  76                     Assert.fail("expected to fail checking " + javaVmVersion + " against 0.59");
  77                 } catch (InternalError e) {
  78                     // pass
  79                 }
  80             }
  81         }
  82     }
  83 }