diff --git a/src/main/java/nl/knaw/dans/dvcli/action/SingleIdOrIdsFile.java b/src/main/java/nl/knaw/dans/dvcli/action/SingleIdOrIdsFile.java index 97f6ac6..6b1b9d7 100644 --- a/src/main/java/nl/knaw/dans/dvcli/action/SingleIdOrIdsFile.java +++ b/src/main/java/nl/knaw/dans/dvcli/action/SingleIdOrIdsFile.java @@ -38,7 +38,7 @@ public Stream getPids() throws IOException { return Stream.of(defaultId); } - Stream lines = null; + Stream lines; if ("-".equals(singleIdOrIdFile)) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); @@ -47,6 +47,9 @@ public Stream getPids() throws IOException { else { var pidFile = Paths.get(singleIdOrIdFile); if (Files.exists(pidFile)) { + if (!Files.isRegularFile(pidFile)) { + throw new IOException(singleIdOrIdFile + " is not a regular file"); + } lines = Files.lines(pidFile) .flatMap(line -> Arrays.stream(line.trim().split("\\s+"))); } diff --git a/src/test/java/nl/knaw/dans/dvcli/action/SingleOrTest.java b/src/test/java/nl/knaw/dans/dvcli/action/SingleOrTest.java index e22cc42..1025698 100644 --- a/src/test/java/nl/knaw/dans/dvcli/action/SingleOrTest.java +++ b/src/test/java/nl/knaw/dans/dvcli/action/SingleOrTest.java @@ -25,8 +25,8 @@ import org.mockito.Mockito; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; -import java.io.UncheckedIOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Files; @@ -36,7 +36,6 @@ import static nl.knaw.dans.dvcli.action.SingleIdOrIdsFile.DEFAULT_TARGET_PLACEHOLDER; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -112,11 +111,10 @@ public void getDatasets_should_parse_file_with_white_space() throws Exception { @Test public void getDatasets_should_throw_when_parsing_a_directory() throws Exception { - var datasets = new SingleDatasetOrDatasetsFile("target", getClient()) - .getDatasets(); - var exception = assertThrows(RuntimeException.class, datasets::toList); - assertThat( exception.getMessage()) - .isEqualTo("java.io.IOException: Is a directory"); + var ids = new SingleDatasetOrDatasetsFile("target", getClient()); + assertThatThrownBy(ids::getDatasets) + .isInstanceOf(IOException.class) + .hasMessage("target is not a regular file"); } @Test