From f210b0f6c571fb3173897aeaeb507cf89262eca0 Mon Sep 17 00:00:00 2001 From: fyrz Date: Mon, 9 Mar 2015 19:07:08 +0100 Subject: [PATCH] [RocksJava] Fix NativeLibraryLoader - Resolve problem while using a temporary data folder - Fix test --- java/Makefile | 2 +- .../java/org/rocksdb/NativeLibraryLoader.java | 6 +++++- .../java/org/rocksdb/NativeLibraryLoaderTest.java | 15 ++++----------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/java/Makefile b/java/Makefile index e248d0fb72..26a8b5a927 100644 --- a/java/Makefile +++ b/java/Makefile @@ -172,7 +172,7 @@ java_test: resolve_test_deps javah -cp $(MAIN_CLASSES):$(TEST_CLASSES) -d $(NATIVE_INCLUDE) -jni $(NATIVE_JAVA_TEST_CLASSES) test: java resolve_test_deps java_test - java -ea -Xcheck:jni -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH)" org.rocksdb.test.RocksJunitRunner $(JAVA_TESTS) + java -ea -Xcheck:jni -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH):target/*" org.rocksdb.test.RocksJunitRunner $(JAVA_TESTS) db_bench: java mkdir -p $(BENCHMARK_MAIN_CLASSES) diff --git a/java/src/main/java/org/rocksdb/NativeLibraryLoader.java b/java/src/main/java/org/rocksdb/NativeLibraryLoader.java index 06ae773cb0..dca9b3119f 100644 --- a/java/src/main/java/org/rocksdb/NativeLibraryLoader.java +++ b/java/src/main/java/org/rocksdb/NativeLibraryLoader.java @@ -72,7 +72,7 @@ public class NativeLibraryLoader { * * @throws java.io.IOException if a filesystem operation fails. */ - private void loadLibraryFromJar(final String tmpDir) + void loadLibraryFromJar(final String tmpDir) throws IOException { if (!initialized) { final File temp; @@ -80,6 +80,10 @@ public class NativeLibraryLoader { temp = File.createTempFile(tempFilePrefix, tempFileSuffix); } else { temp = new File(tmpDir, jniLibraryFileName); + if (!temp.createNewFile()) { + throw new RuntimeException("File: " + temp.getAbsolutePath() + + " could not be created."); + } } if (!temp.exists()) { diff --git a/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java b/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java index 7fcb009b5c..7d9322a534 100644 --- a/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java +++ b/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java @@ -4,35 +4,28 @@ // of patent rights can be found in the PATENTS file in the same directory. package org.rocksdb; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.rocksdb.util.Environment; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.*; import static org.assertj.core.api.Assertions.assertThat; public class NativeLibraryLoaderTest { - @ClassRule - public static final RocksMemoryResource rocksMemoryResource = - new RocksMemoryResource(); - @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Test public void tempFolder() throws IOException { - NativeLibraryLoader.getInstance().loadLibrary( + NativeLibraryLoader.getInstance().loadLibraryFromJar( temporaryFolder.getRoot().getAbsolutePath()); Path path = Paths.get(temporaryFolder.getRoot().getAbsolutePath(), Environment.getJniLibraryFileName("rocksdb")); - assertThat(Files.exists(path)); - assertThat(Files.isReadable(path)); + assertThat(Files.exists(path)).isTrue(); + assertThat(Files.isReadable(path)).isTrue(); } }