Skip to content

Commit c745358

Browse files
committed
readFile
1 parent 32c7251 commit c745358

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

src/main/java/fr/formiko/utils/FLUFiles.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
57
import java.util.List;
6-
import com.google.common.io.Files;
78

89
/**
910
* A utility class to manipulate files.
@@ -27,8 +28,8 @@ private FLUFiles() {} // hide constructor
2728
public static boolean copy(String source, String destination) { return internal.copy(source, destination); }
2829
public static boolean move(String source, String destination) { return internal.move(source, destination); }
2930

30-
public static String readFile(String path) { return null; }
31-
public static List<String> readFileAsList(String path) { return null; }
31+
public static String readFile(String path) { return internal.readFile(path); }
32+
public static List<String> readFileAsList(String path) { return internal.readFileAsList(path); }
3233
public static boolean writeFile(String path, String content) { return false; }
3334
public static boolean appendToFile(String path, String content) { return false; }
3435

@@ -111,7 +112,7 @@ private boolean copy(String source, String destination) {
111112
return true;
112113
}
113114
try {
114-
Files.copy(sourceFile, destinationFile);
115+
com.google.common.io.Files.copy(sourceFile, destinationFile);
115116
} catch (IOException | IllegalArgumentException e) {
116117
return false;
117118
}
@@ -132,5 +133,28 @@ private boolean move(String source, String destination) {
132133
return false;
133134
}
134135
}
136+
private static String readFile(String path) {
137+
if (isAValidePath(path)) {
138+
try {
139+
// return Files.readAllLines(Paths.get(path));
140+
return Files.readString(Paths.get(path));
141+
} catch (IOException e) {
142+
return null;
143+
}
144+
} else {
145+
return null;
146+
}
147+
}
148+
private static List<String> readFileAsList(String path) {
149+
if (isAValidePath(path)) {
150+
try {
151+
return Files.readAllLines(Paths.get(path));
152+
} catch (IOException e) {
153+
return null;
154+
}
155+
} else {
156+
return null;
157+
}
158+
}
135159
}
136160
}

src/test/java/fr/formiko/utils/FLUFilesTest.java

+42
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package fr.formiko.utils;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56
import java.io.File;
7+
import java.util.List;
68
import java.util.stream.Stream;
79
import org.junit.jupiter.api.AfterAll;
810
import org.junit.jupiter.api.BeforeAll;
@@ -139,4 +141,44 @@ private static Stream<Arguments> testMoveDirectorySource() {
139141
return Stream.of(
140142
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "moveOfTestResources/", "subDir/existingFile.txt"));
141143
}
144+
145+
@ParameterizedTest
146+
@MethodSource("testReadFileSource")
147+
void testReadFile(String path, boolean shouldWork, String content) {
148+
if (shouldWork) {
149+
assertEquals(content, FLUFiles.readFile(path));
150+
} else {
151+
assertNull(FLUFiles.readFile(path));
152+
}
153+
}
154+
155+
private static Stream<Arguments> testReadFileSource() {
156+
return Stream.of(Arguments.of(TEST_PATH + "existingFile.x", true, "Some content."),
157+
Arguments.of(TEST_PATH + "unexistingFile.x", false, null), Arguments.of(null, false, null),
158+
Arguments.of(TEST_PATH + "existingDir/subDir/", false, null),
159+
Arguments.of(TEST_PATH + "existingDir/subDir/existingFile.txt", true, "ipnzéfl\n" + //
160+
"zgrebinoa\n" + //
161+
"rez bzn,\n"));
162+
}
163+
164+
@ParameterizedTest
165+
@MethodSource("testReadFileAsListSource")
166+
void testWriteFile(String path, boolean shouldWork, List<String> content) {
167+
if (shouldWork) {
168+
List<String> list = FLUFiles.readFileAsList(path);
169+
assertEquals(content.size(), list.size());
170+
for (int i = 0; i < content.size(); i++) {
171+
assertEquals(content.get(i), list.get(i));
172+
}
173+
} else {
174+
assertNull(FLUFiles.readFileAsList(path));
175+
}
176+
}
177+
178+
private static Stream<Arguments> testReadFileAsListSource() {
179+
return Stream.of(Arguments.of(TEST_PATH + "existingFile.x", true, List.of("Some content.")),
180+
Arguments.of(TEST_PATH + "unexistingFile.x", false, null), Arguments.of(null, false, null),
181+
Arguments.of(TEST_PATH + "existingDir/subDir/", false, null),
182+
Arguments.of(TEST_PATH + "existingDir/subDir/existingFile.txt", true, List.of("ipnzéfl", "zgrebinoa", "rez bzn,")));
183+
}
142184
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
ipnzefl
1+
ipnzéfl
22
zgrebinoa
33
rez bzn,

0 commit comments

Comments
 (0)