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