Skip to content

Commit

Permalink
Add more API misuse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmudd committed Jul 20, 2024
1 parent cc10bce commit b438766
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions jhdf/src/main/java/io/jhdf/WritableGroupImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.jhdf.object.message.LinkMessage;
import io.jhdf.object.message.Message;
import io.jhdf.storage.HdfFileChannel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -100,6 +101,9 @@ public boolean isAttributeCreationOrderTracked() {

@Override
public WritiableDataset putDataset(String name, Object data) {
if(StringUtils.isBlank(name)) {
throw new IllegalArgumentException("name cannot be null or blank");
}
WritableDatasetImpl writableDataset = new WritableDatasetImpl(data, name, this);
children.put(name, writableDataset);
logger.info("Added dataset [{}] to group [{}]", name, getPath());
Expand All @@ -108,6 +112,9 @@ public WritiableDataset putDataset(String name, Object data) {

@Override
public WritableGroup putGroup(String name) {
if(StringUtils.isBlank(name)) {
throw new IllegalArgumentException("name cannot be null or blank");
}
WritableGroupImpl newGroup = new WritableGroupImpl(this, name);
children.put(name, newGroup);
logger.info("Added group [{}] to group [{}]", name, getPath());
Expand Down
23 changes: 23 additions & 0 deletions jhdf/src/test/java/io/jhdf/writing/DatasetWritingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DatasetWritingTest {

Expand Down Expand Up @@ -235,4 +237,25 @@ void readNDDatasetWithH5Dump() throws Exception {
}
}
}

@Test
void testNoDatasetNameFails() throws IOException {
Path tempFile = Files.createTempFile(this.getClass().getSimpleName(), ".hdf5");
WritableHdfFile writableHdfFile = HdfFile.write(tempFile);

assertThrows(IllegalArgumentException.class,
() -> writableHdfFile.putDataset(null, 111));

assertThrows(IllegalArgumentException.class,
() -> writableHdfFile.putDataset("", 111));
}

@Test
void testNullDatasFails() throws IOException {
Path tempFile = Files.createTempFile(this.getClass().getSimpleName(), ".hdf5");
WritableHdfFile writableHdfFile = HdfFile.write(tempFile);

assertThrows(NullPointerException.class,
() -> writableHdfFile.putDataset("test", null));
}
}

0 comments on commit b438766

Please sign in to comment.