Browse Source

Added test for native library loading.

Use newer java.nio.Files api for library loading.
pull/189/head
weisj 4 years ago
parent
commit
e364b8a71c
  1. 67
      core/src/test/java/test/NativeLibraryTest.java
  2. 29
      native-utils/src/main/java/com/github/weisj/darklaf/platform/NativeUtil.java

67
core/src/test/java/test/NativeLibraryTest.java

@ -0,0 +1,67 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import com.github.weisj.darklaf.platform.AbstractLibrary;
import com.github.weisj.darklaf.platform.macos.MacOSLibrary;
import com.github.weisj.darklaf.platform.windows.WindowsLibrary;
public class NativeLibraryTest {
@Test
@EnabledOnOs(OS.MAC)
public void testMacOSLibraryLoading() {
AbstractLibrary library = new TestMacOsLibrary();
Assertions.assertDoesNotThrow(library::updateLibrary);
Assertions.assertTrue(library.isLoaded(), "MacOS library isn't loaded");
}
@Test
@EnabledOnOs(OS.WINDOWS)
public void testWindowsLibraryLoading() {
AbstractLibrary library = new TestWindowsLibrary();
Assertions.assertDoesNotThrow(library::updateLibrary);
Assertions.assertTrue(library.isLoaded(), "Windows library isn't loaded");
}
private static class TestMacOsLibrary extends MacOSLibrary {
@Override
protected void error(final String message, final Throwable e) {
throw new RuntimeException(e);
}
}
private static class TestWindowsLibrary extends WindowsLibrary {
@Override
protected void error(final String message, final Throwable e) {
throw new RuntimeException(e);
}
}
}

29
native-utils/src/main/java/com/github/weisj/darklaf/platform/NativeUtil.java

@ -48,7 +48,7 @@ public class NativeUtil {
/**
* Temporary directory which will contain the DLLs.
*/
private static File temporaryDir;
private static Path temporaryDir;
private NativeUtil() {}
@ -85,43 +85,36 @@ public class NativeUtil {
// Prepare temporary file
if (temporaryDir == null) {
temporaryDir = createTempDirectory(NATIVE_FOLDER_PATH_PREFIX);
temporaryDir.deleteOnExit();
temporaryDir.toFile().deleteOnExit();
}
File temp = new File(temporaryDir, filename);
Path temp = temporaryDir.resolve(filename);
try (InputStream is = NativeUtil.class.getResourceAsStream(path)) {
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.copy(is, temp, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
temp.delete();
Files.deleteIfExists(temp);
throw e;
} catch (NullPointerException e) {
temp.delete();
Files.deleteIfExists(temp);
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
}
try {
System.load(temp.getAbsolutePath());
System.load(temp.toAbsolutePath().toString());
} finally {
if (isPosixCompliant()) {
// Assume POSIX compliant file system, can be deleted after loading
temp.delete();
Files.deleteIfExists(temp);
} else {
// Assume non-POSIX, and don't delete until last file descriptor closed
temp.deleteOnExit();
temp.toFile().deleteOnExit();
}
}
}
private static File createTempDirectory(final String prefix) throws IOException {
String tempDir = System.getProperty("java.io.tmpdir");
File generatedDir = new File(tempDir, prefix + System.nanoTime());
if (!generatedDir.mkdir()) {
throw new IOException("Failed to create temp directory " + generatedDir.getName());
}
return generatedDir;
private static Path createTempDirectory(final String prefix) throws IOException {
return Files.createTempDirectory(prefix + System.nanoTime());
}
private static boolean isPosixCompliant() {

Loading…
Cancel
Save