diff --git a/saferalloc/build.gradle b/saferalloc/build.gradle index 1a0d45c..afc4692 100644 --- a/saferalloc/build.gradle +++ b/saferalloc/build.gradle @@ -12,7 +12,7 @@ def arch = System.getProperty('os.arch', '').toLowerCase() def nativesProject def nativeExt -if (osName.contains('win')) { +if (osName.startsWith('win')) { if (arch == 'aarch64' || arch == 'arm64') { nativesProject = project(':saferalloc-natives-windows-aarch64') } else { diff --git a/saferalloc/src/main/java/org/ngengine/saferalloc/NativeLibraryLoader.java b/saferalloc/src/main/java/org/ngengine/saferalloc/NativeLibraryLoader.java index 51dd56f..468a2cd 100644 --- a/saferalloc/src/main/java/org/ngengine/saferalloc/NativeLibraryLoader.java +++ b/saferalloc/src/main/java/org/ngengine/saferalloc/NativeLibraryLoader.java @@ -176,7 +176,7 @@ private static String detectOs() { return "android"; } String n = System.getProperty("os.name", "").toLowerCase(Locale.ROOT); - if (n.contains("win")) return "windows"; + if (n.startsWith("win")) return "windows"; if (n.contains("mac") || n.contains("darwin")) return "macos"; return "linux"; } diff --git a/saferalloc/src/test/java/org/ngengine/saferalloc/NativeLibraryLoaderTest.java b/saferalloc/src/test/java/org/ngengine/saferalloc/NativeLibraryLoaderTest.java new file mode 100644 index 0000000..6a342ce --- /dev/null +++ b/saferalloc/src/test/java/org/ngengine/saferalloc/NativeLibraryLoaderTest.java @@ -0,0 +1,43 @@ +package org.ngengine.saferalloc; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.lang.reflect.Method; +import org.junit.jupiter.api.Test; + +class NativeLibraryLoaderTest { + @Test + void detectsDesktopOperatingSystems() throws Exception { + String originalOs = System.getProperty("os.name"); + String originalRuntime = System.getProperty("java.runtime.name"); + String originalVm = System.getProperty("java.vm.name"); + try { + System.setProperty("java.runtime.name", "OpenJDK Runtime Environment"); + System.setProperty("java.vm.name", "OpenJDK 64-Bit Server VM"); + Method detectOs = NativeLibraryLoader.class.getDeclaredMethod("detectOs"); + detectOs.setAccessible(true); + String[][] cases = { + {"Linux", "linux"}, + {"Windows 11", "windows"}, + {"Mac OS X", "macos"}, + {"Darwin", "macos"} + }; + for (String[] testCase : cases) { + System.setProperty("os.name", testCase[0]); + assertEquals(testCase[1], detectOs.invoke(null), testCase[0]); + } + } finally { + restoreProperty("os.name", originalOs); + restoreProperty("java.runtime.name", originalRuntime); + restoreProperty("java.vm.name", originalVm); + } + } + + private static void restoreProperty(String name, String value) { + if (value == null) { + System.clearProperty(name); + } else { + System.setProperty(name, value); + } + } +}