Skip to content

Commit

Permalink
Fixed tests: Resource files could not be found when the repository pa…
Browse files Browse the repository at this point in the history
…th contained spaces
  • Loading branch information
tbrunsch committed Feb 8, 2025
1 parent 6420e6f commit 15ec10a
Show file tree
Hide file tree
Showing 48 changed files with 138 additions and 154 deletions.
2 changes: 1 addition & 1 deletion jhdf/src/test/java/io/jhdf/AttributesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class AttributesTest {
private static HdfFile latestHdfFile;

@BeforeAll
static void setup() throws Exception {
static void setup() {
earliestHdfFile = loadTestHdfFile(HDF5_TEST_EARLIEST_FILE_NAME);
latestHdfFile = loadTestHdfFile(HDF5_TEST_LATEST_FILE_NAME);
}
Expand Down
2 changes: 1 addition & 1 deletion jhdf/src/test/java/io/jhdf/CommittedDatatypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CommittedDatatypeTest {
private static HdfFile hdfFile;

@BeforeAll
static void beforeAll() throws Exception {
static void beforeAll() {
hdfFile = loadTestHdfFile(HDF5_TEST_FILE_NAME);
}

Expand Down
2 changes: 1 addition & 1 deletion jhdf/src/test/java/io/jhdf/DatasetImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DatasetImplTest {
private static HdfFile hdfFile;

@BeforeAll
static void beforeAll() throws Exception {
static void beforeAll() {
hdfFile = loadTestHdfFile(HDF5_TEST_FILE_NAME);
}

Expand Down
4 changes: 2 additions & 2 deletions jhdf/src/test/java/io/jhdf/FractalHeapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class FractalHeapTest {

@BeforeEach
void setup() throws IOException {
String testFile = this.getClass().getResource("/hdf5/test_large_group_latest.hdf5").getFile();
try (RandomAccessFile raf = new RandomAccessFile(new File(testFile), "r")) {
File testFile = TestUtils.getTestFile("test_large_group_latest.hdf5");
try (RandomAccessFile raf = new RandomAccessFile(testFile, "r")) {
FileChannel fc = raf.getChannel();
Superblock sb = Superblock.readSuperblock(fc, 0);
HdfBackingStorage hdfBackingStorage = new HdfFileChannel(fc, sb);
Expand Down
12 changes: 5 additions & 7 deletions jhdf/src/test/java/io/jhdf/GlobalHeapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import org.mockito.Mockito;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import static java.nio.charset.StandardCharsets.US_ASCII;
Expand All @@ -40,9 +38,9 @@ class GlobalHeapTest {
private HdfFileChannel hdfFc;

@BeforeEach
void setup() throws IOException, URISyntaxException {
URI testFile = this.getClass().getResource("/hdf5/test_file.hdf5").toURI();
FileChannel fc = FileChannel.open(Paths.get(testFile), StandardOpenOption.READ);
void setup() throws IOException {
Path testPath = TestUtils.getTestPath("test_file.hdf5");
FileChannel fc = FileChannel.open(testPath, StandardOpenOption.READ);
sb = Superblock.readSuperblock(fc, 0);
hdfFc = new HdfFileChannel(fc, sb);

Expand Down Expand Up @@ -108,7 +106,7 @@ void testInvalidVersionThrows() throws IOException {
}

@Test
void testDifferentObjectZero() throws Exception {
void testDifferentObjectZero() {
HdfFile file = TestUtils.loadTestHdfFile("globalheaps_test.hdf5");
Object data = file.getAttribute("attribute").getData();
assertThat((String[]) data, is(arrayContaining(
Expand Down
10 changes: 4 additions & 6 deletions jhdf/src/test/java/io/jhdf/GroupSymbolTableNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -30,9 +28,9 @@ class GroupSymbolTableNodeTest {
private HdfBackingStorage hdfBackingStorage;

@BeforeEach
void setUp() throws IOException, URISyntaxException {
final URI testFileUri = this.getClass().getResource("/hdf5/test_file.hdf5").toURI();
FileChannel fc = FileChannel.open(Paths.get(testFileUri), StandardOpenOption.READ);
void setUp() throws IOException {
Path testPath = TestUtils.getTestPath("test_file.hdf5");
FileChannel fc = FileChannel.open(testPath, StandardOpenOption.READ);
Superblock sb = Superblock.readSuperblock(fc, 0);
hdfBackingStorage = new HdfFileChannel(fc, sb);
}
Expand Down
9 changes: 4 additions & 5 deletions jhdf/src/test/java/io/jhdf/GroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ class GroupTest {

@BeforeEach
void setUp() throws IOException {
final String testFileUrl = this.getClass().getResource("/hdf5/test_file.hdf5").getFile();
File file = new File(testFileUrl);
FileChannel fc = FileChannel.open(file.toPath(), StandardOpenOption.READ);
File testFile = TestUtils.getTestFile("test_file.hdf5");
FileChannel fc = FileChannel.open(testFile.toPath(), StandardOpenOption.READ);
Superblock sb = Superblock.readSuperblock(fc, 0);

hdfBackingStorage = new HdfFileChannel(fc, sb);

rootGroup = mock(Group.class);
when(rootGroup.getPath()).thenReturn("/");
when(rootGroup.getFile()).thenReturn(file);
when(rootGroup.getFileAsPath()).thenReturn(file.toPath());
when(rootGroup.getFile()).thenReturn(testFile);
when(rootGroup.getFileAsPath()).thenReturn(testFile.toPath());
}

@AfterEach
Expand Down
51 changes: 22 additions & 29 deletions jhdf/src/test/java/io/jhdf/HdfFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,16 @@
class HdfFileTest {

private static final String HDF5_TEST_FILE_NAME = "test_file.hdf5";
private static final String HDF5_TEST_FILE_PATH = "/hdf5/" + HDF5_TEST_FILE_NAME;
private static final String HDF5_TEST_FILE_TWO_NAME = "test_file2.hdf5";
private static final String HDF5_TEST_FILE_TWO_PATH = "/hdf5/" + HDF5_TEST_FILE_TWO_NAME;
private static final String NON_HDF5_TEST_FILE_NAME = "/scripts/make_test_files.py";
private Path testFilePath;
private Path testFile2Path;
private Path nonHdfFilePath;

@BeforeEach
void setup() throws URISyntaxException {
testFilePath = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI());
testFile2Path = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_TWO_PATH).toURI());
testFilePath = TestUtils.getTestPath(HDF5_TEST_FILE_NAME);
testFile2Path = TestUtils.getTestPath(HDF5_TEST_FILE_TWO_NAME);
nonHdfFilePath = Paths.get(this.getClass().getResource(NON_HDF5_TEST_FILE_NAME).toURI());
}

Expand All @@ -82,11 +80,11 @@ void testOpeningValidFile() {
}

@Test
void testOpeningFileWithLargeMaxDimensionsSize() throws URISyntaxException {
String filePath = "/hdf5/100B_max_dimension_size.hdf5";
void testOpeningFileWithLargeMaxDimensionsSize() {
String fileName = "100B_max_dimension_size.hdf5";

URI testFileUri = this.getClass().getResource(filePath).toURI();
try (HdfFile hdfFile = new HdfFile(new File(testFileUri))) {
File testFile = TestUtils.getTestFile(fileName);
try (HdfFile hdfFile = new HdfFile(testFile)) {
Dataset dataset = hdfFile.getDatasetByPath("/100B-MaxSize");

assertThat(dataset.getMaxSize().length, is(equalTo(1)));
Expand Down Expand Up @@ -285,24 +283,24 @@ void testLinkCreationOrdered() {
}

@Test
void testURIConstructor() throws URISyntaxException {
URI uri = this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI();
void testURIConstructor() {
URI uri = testFilePath.toUri();
HdfFile hdfFile = new HdfFile(uri);
assertThat(hdfFile.getFile(), is(notNullValue()));
hdfFile.close();
}

@Test
void testFileConstructor() throws URISyntaxException {
File file = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI()).toFile();
void testFileConstructor() {
File file = testFilePath.toFile();
try (HdfFile hdfFile = new HdfFile(file)) {
assertThat(hdfFile.getFile(), is(notNullValue()));
}
}

@Test
void testReadingFromStream() throws IOException {
try (InputStream inputStream = this.getClass().getResource(HDF5_TEST_FILE_PATH).openStream();
try (InputStream inputStream = Files.newInputStream(testFilePath);
HdfFile hdfFile = HdfFile.fromInputStream(inputStream)) {

assertThat(hdfFile.getUserBlockSize(), is(equalTo(0L)));
Expand All @@ -321,7 +319,7 @@ void testReadingFromStreamThrowsWhenStreamCantBeRead() throws IOException {
@Test
void testReadingFromStreamDeletesTempFileOnClose() throws IOException {
File tempFile;
try (InputStream inputStream = this.getClass().getResource(HDF5_TEST_FILE_PATH).openStream();
try (InputStream inputStream = Files.newInputStream(testFilePath);
HdfFile hdfFile = HdfFile.fromInputStream(inputStream)) {
tempFile = hdfFile.getFile();
assertTrue(tempFile.exists());
Expand All @@ -330,9 +328,8 @@ void testReadingFromStreamDeletesTempFileOnClose() throws IOException {
}

@Test
void testLoadingInMemoryFile() throws IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI());
ByteBuffer byteBuffer = ByteBuffer.wrap(Files.readAllBytes(path));
void testLoadingInMemoryFile() throws IOException {
ByteBuffer byteBuffer = ByteBuffer.wrap(Files.readAllBytes(testFilePath));
HdfFile hdfFile = HdfFile.fromByteBuffer(byteBuffer);
assertThat(hdfFile, is(notNullValue()));
assertThat(hdfFile.inMemory(), is(true));
Expand All @@ -341,9 +338,8 @@ void testLoadingInMemoryFile() throws IOException, URISyntaxException {
}

@Test
void testLoadingInMemoryFileFromByteArray() throws IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI());
byte[] bytes = Files.readAllBytes(path);
void testLoadingInMemoryFileFromByteArray() throws IOException {
byte[] bytes = Files.readAllBytes(testFilePath);
HdfFile hdfFile = HdfFile.fromBytes(bytes);
assertThat(hdfFile, is(notNullValue()));
assertThat(hdfFile.inMemory(), is(true));
Expand All @@ -354,9 +350,8 @@ void testLoadingInMemoryFileFromByteArray() throws IOException, URISyntaxExcepti
}

@Test
void testLoadingInMemoryFileFromByteArrayTwo() throws IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI());
byte[] bytes = Files.readAllBytes(path);
void testLoadingInMemoryFileFromByteArrayTwo() throws IOException {
byte[] bytes = Files.readAllBytes(testFilePath);
HdfFile hdfFile = HdfFile.fromBytes(bytes);
assertThat(hdfFile, is(notNullValue()));
assertThat(hdfFile.inMemory(), is(true));
Expand All @@ -373,18 +368,16 @@ void testReadingFromEmptyByteArrayFails() {
}

@Test
void testExternalFilesOnInMemoryThrows() throws IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI());
byte[] bytes = Files.readAllBytes(path);
void testExternalFilesOnInMemoryThrows() throws IOException {
byte[] bytes = Files.readAllBytes(testFilePath);
try (HdfFile hdfFile = HdfFile.fromBytes(bytes)) {
assertThrows(InMemoryHdfException.class, () -> hdfFile.addExternalFile(Mockito.mock(HdfFile.class)));
}
}

@Test
void testGettingFileChannelFromInMemoryFileThrows() throws IOException, URISyntaxException {
Path path = Paths.get(this.getClass().getResource(HDF5_TEST_FILE_PATH).toURI());
byte[] bytes = Files.readAllBytes(path);
void testGettingFileChannelFromInMemoryFileThrows() throws IOException {
byte[] bytes = Files.readAllBytes(testFilePath);
try (HdfFile hdfFile = HdfFile.fromBytes(bytes)) {
HdfBackingStorage hdfBackingStorage = hdfFile.getHdfBackingStorage();
assertThrows(InMemoryHdfException.class, hdfBackingStorage::getFileChannel);
Expand Down
2 changes: 1 addition & 1 deletion jhdf/src/test/java/io/jhdf/LargeAttributesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LargeAttributesTest {
private static HdfFile largeAttrHdfFile;

@BeforeAll
static void setup() throws Exception {
static void setup() {
largeAttrHdfFile = loadTestHdfFile(HDF5_TEST_FILE_NAME);
}

Expand Down
10 changes: 4 additions & 6 deletions jhdf/src/test/java/io/jhdf/LocalHeapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -31,9 +29,9 @@ class LocalHeapTest {
private HdfBackingStorage hdfBackingStorage;

@BeforeEach
void setUp() throws IOException, URISyntaxException {
final URI testFileUri = this.getClass().getResource("/hdf5/test_file.hdf5").toURI();
FileChannel fc = FileChannel.open(Paths.get(testFileUri), StandardOpenOption.READ);
void setUp() throws IOException {
Path testPath = TestUtils.getTestPath("test_file.hdf5");
FileChannel fc = FileChannel.open(testPath, StandardOpenOption.READ);
Superblock sb = Superblock.readSuperblock(fc, 0);
hdfBackingStorage = new HdfFileChannel(fc, sb);
}
Expand Down
10 changes: 4 additions & 6 deletions jhdf/src/test/java/io/jhdf/ObjectHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
import org.mockito.Mockito;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -40,9 +38,9 @@ class ObjectHeaderTest {
private FileChannel fc;

@BeforeEach
void setUp() throws IOException, URISyntaxException {
final URI testFileUri = this.getClass().getResource("/hdf5/test_file.hdf5").toURI();
fc = FileChannel.open(Paths.get(testFileUri), StandardOpenOption.READ);
void setUp() throws IOException {
Path testPath = TestUtils.getTestPath("test_file.hdf5");
fc = FileChannel.open(testPath, StandardOpenOption.READ);
sb = Superblock.readSuperblock(fc, 0);
hdfBackingStorage = new HdfFileChannel(fc, sb);
}
Expand Down
16 changes: 7 additions & 9 deletions jhdf/src/test/java/io/jhdf/ObjectHeaderV2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -47,9 +45,9 @@ class ObjectHeaderV2Test {
private HdfBackingStorage hdfBackingStorage;

@BeforeEach
void setUp() throws IOException, URISyntaxException {
final URI testFileUri = this.getClass().getResource("/hdf5/test_file2.hdf5").toURI();
FileChannel fc = FileChannel.open(Paths.get(testFileUri), StandardOpenOption.READ);
void setUp() throws IOException {
Path testPath = TestUtils.getTestPath("test_file2.hdf5");
FileChannel fc = FileChannel.open(testPath, StandardOpenOption.READ);
Superblock sb = Superblock.readSuperblock(fc, 0);
hdfBackingStorage = new HdfFileChannel(fc, sb);
}
Expand Down Expand Up @@ -189,10 +187,10 @@ void testObjectHeaderOnInt8Dataset() {
}

@Test
void testCreationOrderTracked() throws IOException, URISyntaxException {
void testCreationOrderTracked() throws IOException {
// this test fails without skipping the creation order in Message#readObjectHeaderV2Message
final URI testFileUri = this.getClass().getResource("/hdf5/test_attribute_with_creation_order.hdf5").toURI();
FileChannel fc = FileChannel.open(Paths.get(testFileUri), StandardOpenOption.READ);
Path testPath = TestUtils.getTestPath("test_attribute_with_creation_order.hdf5");
FileChannel fc = FileChannel.open(testPath, StandardOpenOption.READ);
Superblock sb = Superblock.readSuperblock(fc, 0);
HdfBackingStorage hdfBackingStorage = new HdfFileChannel(fc, sb);

Expand Down
2 changes: 1 addition & 1 deletion jhdf/src/test/java/io/jhdf/OrderedGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OrderedGroupTest {
private static HdfFile hdfFile;

@BeforeAll
static void setup() throws Exception {
static void setup() {
hdfFile = TestUtils.loadTestHdfFile(HDF5_TEST_FILE_NAME);
}

Expand Down
8 changes: 4 additions & 4 deletions jhdf/src/test/java/io/jhdf/SuperblockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class SuperblockTest {

@BeforeEach
void setUp() throws FileNotFoundException {
final String testFileUrl = this.getClass().getResource("/hdf5/test_file.hdf5").getFile();
raf = new RandomAccessFile(new File(testFileUrl), "r");
File testFile = TestUtils.getTestFile("test_file.hdf5");
raf = new RandomAccessFile(testFile, "r");
fc = raf.getChannel();
}

Expand Down Expand Up @@ -86,8 +86,8 @@ void testReadSuperblockThrowsWhenGivenInvalidOffset() {

@Test
void testReadingSuperblockExtension() throws FileNotFoundException {
final String testFileUrl = this.getClass().getResource("/hdf5/superblock-extension.hdf5").getFile();
raf = new RandomAccessFile(new File(testFileUrl), "r");
File testFile = TestUtils.getTestFile("superblock-extension.hdf5");
raf = new RandomAccessFile(testFile, "r");
fc = raf.getChannel();
Superblock superblock = Superblock.SuperblockV2V3.readSuperblock(fc, 0);
Optional<ObjectHeader> superblockExtension = superblock.getExtension();
Expand Down
4 changes: 2 additions & 2 deletions jhdf/src/test/java/io/jhdf/SuperblockV3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class SuperblockV3Test {

@BeforeEach
void setUp() throws FileNotFoundException {
final String testFileUrl = this.getClass().getResource("/hdf5/test_file2.hdf5").getFile();
raf = new RandomAccessFile(new File(testFileUrl), "r");
File testFile = TestUtils.getTestFile("test_file2.hdf5");
raf = new RandomAccessFile(testFile, "r");
fc = raf.getChannel();
}

Expand Down
Loading

0 comments on commit 15ec10a

Please sign in to comment.