Skip to content

Commit f0adf60

Browse files
committed
1466-instantiate-csv-data-as-rdf: Added tests for the FileUtils::listFiles methods.
1 parent 86d6198 commit f0adf60

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

stack-clients/src/main/java/com/cmclinnovations/stack/clients/utils/FileUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ public static boolean filterOnExtension(Path path, String extension) {
7373

7474
public static Collection<URI> listFiles(URL dirURL, String fileExtension) throws IOException, URISyntaxException {
7575
return listFiles(dirURL).stream().filter(uri -> uri.toString().endsWith(fileExtension))
76-
.collect(Collectors.toList());
76+
.collect(Collectors.toSet());
7777
}
7878

7979
public static Collection<URI> listFiles(URL dirURL) throws IOException, URISyntaxException {
8080
if (dirURL != null) {
8181
switch (dirURL.getProtocol()) {
8282
case "file":
8383
// A file path: easy enough
84-
return listFileFromPath(Path.of(dirURL.toURI()));
84+
return listFilesFromPath(Path.of(dirURL.toURI()));
8585
case "jar":
8686
// A JAR path
8787
return listFilesFromJar(dirURL);
@@ -93,7 +93,7 @@ public static Collection<URI> listFiles(URL dirURL) throws IOException, URISynta
9393
"Cannot load config files from URL '" + dirURL + "'. Only 'file' and 'jar' protocols are supported.");
9494
}
9595

96-
private static Set<URI> listFileFromPath(Path configDir) throws IOException {
96+
private static Set<URI> listFilesFromPath(Path configDir) throws IOException {
9797
try (Stream<Path> stream = Files.list(configDir)) {
9898
return stream.map(Path::toUri).collect(Collectors.toSet());
9999
}
Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
package com.cmclinnovations.stack.clients.utils;
22

3-
import static org.junit.jupiter.api.Assertions.fail;
4-
3+
import java.io.IOException;
54
import java.net.URI;
65
import java.net.URISyntaxException;
6+
import java.net.URL;
77
import java.nio.file.Path;
88
import java.nio.file.Paths;
9+
import java.util.Collection;
910
import java.util.List;
1011
import java.util.stream.Collectors;
1112
import java.util.stream.Stream;
1213

13-
import org.junit.Assert;
14-
import org.junit.Test;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Test;
1516

16-
public class FileUtilsTest {
17+
class FileUtilsTest {
1718
@Test
18-
public void testEnsureScriptsExecutable() {
19+
void testEnsureScriptsExecutable() {
1920

2021
}
2122

2223
@Test
23-
public void testSanitiseFilename() {
24+
void testSanitiseFilename() {
2425
Path path = Paths.get("abc", "efg", "+Hello()World-123.csv");
25-
Assert.assertEquals("_Hello__World_123.csv", FileUtils.sanitiseFilename(path));
26+
Assertions.assertEquals("_Hello__World_123.csv", FileUtils.sanitiseFilename(path));
2627
}
2728

2829
@Test
29-
public void testFilterOnExtension() {
30-
Assert.assertEquals(List.of(
30+
void testFilterOnExtension() {
31+
Assertions.assertEquals(List.of(
3132
Paths.get("abc", "efg", "123.csv"),
3233
Paths.get("abc", "efg", "345.csv")),
3334
Stream.of(
@@ -39,8 +40,8 @@ public void testFilterOnExtension() {
3940
}
4041

4142
@Test
42-
public void testGetFileNameWithoutExtension() {
43-
Assert.assertEquals(List.of(
43+
void testGetFileNameWithoutExtension() {
44+
Assertions.assertEquals(List.of(
4445
"123", "345", "678"),
4546
Stream.of(
4647
Paths.get("abc", "efg", "123.csv"),
@@ -51,14 +52,55 @@ public void testGetFileNameWithoutExtension() {
5152
}
5253

5354
@Test
54-
public void testAppendDirectoryPath() throws URISyntaxException {
55-
Assert.assertEquals(List.of(
56-
"/dataset/123.csv", "/dataset/345.csv", "/dataset/678.csv"),
55+
void testAppendDirectoryPath() throws URISyntaxException {
56+
Assertions.assertEquals(Stream.of(
57+
"dataset/123.csv", "dataset/345.csv", "dataset/678.csv")
58+
.map(Path::of)
59+
.collect(Collectors.toList()),
5760
Stream.of(
5861
new URI("123.csv"),
5962
new URI("345.csv"),
6063
new URI("678.csv"))
61-
.map(path -> FileUtils.appendDirectoryPath(path, "/dataset").toAbsolutePath().toString())
64+
.map(path -> FileUtils.appendDirectoryPath(path, "dataset"))
6265
.collect(Collectors.toList()));
6366
}
67+
68+
private Collection<URI> getExpectedURIs(URL dirURL, Collection<String> expectedFiles)
69+
throws URISyntaxException, IOException {
70+
return expectedFiles.stream().map(file -> {
71+
try {
72+
return new URI(dirURL.toString() + "/" + file);
73+
} catch (URISyntaxException e) {
74+
throw new RuntimeException(e);
75+
}
76+
}).collect(Collectors.toSet());
77+
}
78+
79+
@Test
80+
void listFilesFromPath() throws IOException, URISyntaxException {
81+
URL dirURL = FileUtilsTest.class.getResource("files");
82+
Collection<URI> expectedURIs = getExpectedURIs(dirURL, List.of("123.csv", "abc.txt"));
83+
Assertions.assertEquals(expectedURIs, FileUtils.listFiles(dirURL));
84+
}
85+
86+
@Test
87+
void listFilesFromPathFiltered() throws IOException, URISyntaxException {
88+
URL dirURL = FileUtilsTest.class.getResource("files");
89+
Collection<URI> expectedURIs = getExpectedURIs(dirURL, List.of("123.csv"));
90+
Assertions.assertEquals(expectedURIs, FileUtils.listFiles(dirURL, ".csv"));
91+
}
92+
93+
@Test
94+
void listFilesFromJar() throws IOException, URISyntaxException {
95+
URL dirURL = new URL("jar", "", FileUtilsTest.class.getResource("files.jar").getPath() + "!/files");
96+
Collection<URI> expectedURIs = getExpectedURIs(dirURL, List.of("123.csv", "abc.txt"));
97+
Assertions.assertEquals(expectedURIs, FileUtils.listFiles(dirURL));
98+
}
99+
100+
@Test
101+
void listFilesFromJarFiltered() throws IOException, URISyntaxException {
102+
URL dirURL = new URL("jar", "", FileUtilsTest.class.getResource("files.jar").getPath() + "!/files");
103+
Collection<URI> expectedURIs = getExpectedURIs(dirURL, List.of("abc.txt"));
104+
Assertions.assertEquals(expectedURIs, FileUtils.listFiles(dirURL, ".txt"));
105+
}
64106
}
Binary file not shown.

stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/files/123.csv

Whitespace-only changes.

stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/files/abc.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)