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