Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implement unit tests for the ByteBuddy platform conditions - Extract the ByteBuddy platform conditions to a separate utility class to enable unit testing them separately. This also enables adding additional checks in the future without making the InlineDelegateByteBuddyMockMaker much more complicated. - Added dependency at test time for junit-jupiter-params to enable writing parameterized unit tests.
- Loading branch information
Showing
5 changed files
with
116 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/org/mockito/internal/creation/bytebuddy/PlatformUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2023 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal.creation.bytebuddy; | ||
|
||
/** | ||
* Helpers for various platform detection mechanisms related to the ByteBuddy mock maker. | ||
* | ||
* @author Ashley Scopes | ||
*/ | ||
final class PlatformUtils { | ||
private PlatformUtils() { | ||
// Static-only class. | ||
} | ||
|
||
static boolean isAndroidPlatform() { | ||
return System.getProperty("java.specification.vendor", "") | ||
.toLowerCase() | ||
.contains("android"); | ||
} | ||
|
||
static boolean isProbablyTermuxEnvironment() { | ||
boolean isLinux = System.getProperty("os.name", "").equalsIgnoreCase("linux"); | ||
boolean isInTermuxData = | ||
System.getProperty("java.home", "").toLowerCase().contains("/com.termux/"); | ||
return isLinux && isInTermuxData; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/test/java/org/mockito/internal/creation/bytebuddy/PlatformUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2023 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal.creation.bytebuddy; | ||
|
||
import java.util.Properties; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.parallel.Isolated; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Ashley Scopes | ||
*/ | ||
@Isolated("modifies system properties temporarily") | ||
class PlatformUtilsTest { | ||
Properties globalProperties; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
globalProperties = new Properties(); | ||
globalProperties.putAll(System.getProperties()); | ||
System.getProperties().clear(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
System.getProperties().clear(); | ||
System.getProperties().putAll(globalProperties); | ||
} | ||
|
||
@CsvSource({ | ||
" android, true", | ||
" AnDrOId, true", | ||
"anythingElse, false", | ||
" , false", | ||
}) | ||
@ParameterizedTest | ||
void isAndroidPlatform_returns_expected_value(String vendor, boolean expectedResult) { | ||
// Given | ||
setProperty("java.specification.vendor", vendor); | ||
|
||
// Then | ||
assertThat(PlatformUtils.isAndroidPlatform()).isEqualTo(expectedResult); | ||
} | ||
|
||
@CsvSource({ | ||
" linux, /data/data/com.termux/whatever, true", | ||
" linux, /foo/bar/com.termux/somedir, true", | ||
" LINUX, /data/data/com.termux/whatever, true", | ||
" Linux, /foo/bar/com.termux/somedir, true", | ||
"Mac OS X 13.0, /foo/bar/com.termux/somedir, false", | ||
" Windows 10, /foo/bar/com.termux/somedir, false", | ||
" OS/2, /foo/bar/com.termux/somedir, false", | ||
" Linux, /usr/share/java, false", | ||
" , , false", | ||
" foo, , false", | ||
" , foo, false", | ||
" linux, , false", | ||
" , /data/data/com.termux/whatever, false", | ||
}) | ||
@ParameterizedTest | ||
void isAndroidPlatform_returns_expected_value( | ||
String os, String javaHome, boolean expectedResult) { | ||
// Given | ||
setProperty("os.name", os); | ||
setProperty("java.home", javaHome); | ||
|
||
assertThat(PlatformUtils.isAndroidPlatform()).isEqualTo(expectedResult); | ||
} | ||
|
||
static void setProperty(String name, String value) { | ||
if (value == null) { | ||
System.clearProperty(name); | ||
} else { | ||
System.setProperty(name, value); | ||
} | ||
} | ||
} |