11package com .cmclinnovations .stack .clients .utils ;
22
3- import static org .junit .jupiter .api .Assertions .fail ;
4-
3+ import java .io .IOException ;
54import java .net .URI ;
65import java .net .URISyntaxException ;
6+ import java .net .URL ;
77import java .nio .file .Path ;
88import java .nio .file .Paths ;
9+ import java .util .Collection ;
910import java .util .List ;
1011import java .util .stream .Collectors ;
1112import 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}
0 commit comments