• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.util;
6 
7 import static org.mockito.internal.util.StringUtil.join;
8 
9 import java.util.Locale;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12 
13 public abstract class Platform {
14 
15     private static final Pattern JAVA_8_RELEASE_VERSION_SCHEME =
16             Pattern.compile("1\\.8\\.0_(\\d+)(?:-ea)?(?:-b\\d+)?");
17     private static final Pattern JAVA_8_DEV_VERSION_SCHEME =
18             Pattern.compile("1\\.8\\.0b\\d+_u(\\d+)");
19     public static final String JAVA_VERSION = System.getProperty("java.specification.version");
20     public static final String JVM_VERSION = System.getProperty("java.runtime.version");
21     public static final String JVM_VENDOR = System.getProperty("java.vm.vendor");
22     public static final String JVM_VENDOR_VERSION = System.getProperty("java.vm.version");
23     public static final String JVM_NAME = System.getProperty("java.vm.name");
24     public static final String JVM_INFO = System.getProperty("java.vm.info");
25     public static final String OS_NAME = System.getProperty("os.name");
26     public static final String OS_VERSION = System.getProperty("os.version");
27 
Platform()28     private Platform() {}
29 
isAndroid()30     public static boolean isAndroid() {
31         return System.getProperty("java.vendor", "").toLowerCase(Locale.US).contains("android");
32     }
33 
isAndroidMockMakerRequired()34     public static boolean isAndroidMockMakerRequired() {
35         return Boolean.getBoolean("org.mockito.mock.android");
36     }
37 
describe()38     public static String describe() {
39         String description =
40                 String.format(
41                         "Java               : %s\n"
42                                 + "JVM vendor name    : %s\n"
43                                 + "JVM vendor version : %s\n"
44                                 + "JVM name           : %s\n"
45                                 + "JVM version        : %s\n"
46                                 + "JVM info           : %s\n"
47                                 + "OS name            : %s\n"
48                                 + "OS version         : %s\n",
49                         JAVA_VERSION,
50                         JVM_VENDOR,
51                         JVM_VENDOR_VERSION,
52                         JVM_NAME,
53                         JVM_VERSION,
54                         JVM_INFO,
55                         OS_NAME,
56                         OS_VERSION);
57         if (isAndroid()) {
58             description =
59                     join(
60                             "IMPORTANT INFORMATION FOR ANDROID USERS:",
61                             "",
62                             "The regular Byte Buddy mock makers cannot generate code on an Android VM!",
63                             "To resolve this, please use the 'mockito-android' dependency for your application:",
64                             "https://search.maven.org/artifact/org.mockito/mockito-android",
65                             "",
66                             description);
67         }
68         return description;
69     }
70 
isJava8BelowUpdate45()71     public static boolean isJava8BelowUpdate45() {
72         return isJava8BelowUpdate45(JVM_VERSION);
73     }
74 
isJava8BelowUpdate45(String jvmVersion)75     static boolean isJava8BelowUpdate45(String jvmVersion) {
76         Matcher matcher = JAVA_8_RELEASE_VERSION_SCHEME.matcher(jvmVersion);
77         if (matcher.matches()) {
78             int update = Integer.parseInt(matcher.group(1));
79             return update < 45;
80         }
81 
82         matcher = JAVA_8_DEV_VERSION_SCHEME.matcher(jvmVersion);
83         if (matcher.matches()) {
84             int update = Integer.parseInt(matcher.group(1));
85             return update < 45;
86         }
87 
88         matcher = Pattern.compile("1\\.8\\.0-b\\d+").matcher(jvmVersion);
89         return matcher.matches();
90     }
91 
warnForVM( String vmName1, String warnMessage1, String vmName2, String warnMessage2)92     public static String warnForVM(
93             String vmName1, String warnMessage1, String vmName2, String warnMessage2) {
94         return warnForVM(JVM_NAME, vmName1, warnMessage1, vmName2, warnMessage2);
95     }
96 
warnForVM( String current, String vmName1, String warnMessage1, String vmName2, String warnMessage2)97     static String warnForVM(
98             String current,
99             String vmName1,
100             String warnMessage1,
101             String vmName2,
102             String warnMessage2) {
103         if (vmName1 != null && current.contains(vmName1)) {
104             return warnMessage1;
105         }
106         if (vmName2 != null && current.contains(vmName2)) {
107             return warnMessage2;
108         }
109         return "";
110     }
111 }
112