-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77fc789
commit 0a65b46
Showing
7 changed files
with
132 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,83 @@ | ||
package core.basesyntax; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import core.basesyntax.dao.FileReader; | ||
import core.basesyntax.dao.FileReaderImpl; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class FileReaderTest { | ||
private final FileReader fileReader = new FileReaderImpl(); | ||
private final List<Path> tempPaths = new ArrayList<>(); | ||
|
||
@AfterEach | ||
void cleanUp() { | ||
for (Path path : tempPaths) { | ||
try { | ||
Files.deleteIfExists(path); | ||
} catch (IOException e) { | ||
fail("Failed to delete temporary file or directory: " + path, e); | ||
} | ||
} | ||
tempPaths.clear(); | ||
} | ||
|
||
@Test | ||
void readFromFile_validFile_ok() throws IOException { | ||
String testFilePath = "src/main/resources/testFile.csv"; | ||
String fileContent = "type,fruit,quantity\nb,banana,20\ns,apple,50"; | ||
Files.writeString(Path.of(testFilePath), fileContent); | ||
List<String> result = fileReader.readFromFile(testFilePath); | ||
Assertions.assertEquals(3, result.size()); | ||
Assertions.assertEquals("type,fruit,quantity", result.get(0)); | ||
Assertions.assertEquals("b,banana,20", result.get(1)); | ||
Assertions.assertEquals("s,apple,50", result.get(2)); | ||
Files.delete(Path.of(testFilePath)); | ||
Path testFile = createTempFile("testFile.csv", """ | ||
type,fruit,quantity | ||
b,banana,20 | ||
s,apple,50 | ||
"""); | ||
|
||
List<String> result = fileReader.readFromFile(testFile.toString()); | ||
assertEquals(3, result.size()); | ||
assertEquals("type,fruit,quantity", result.get(0)); | ||
assertEquals("b,banana,20", result.get(1)); | ||
assertEquals("s,apple,50", result.get(2)); | ||
} | ||
|
||
@Test | ||
void readFromFile_emptyFile_ok() throws IOException { | ||
String emptyFilePath = "src/main/resources/emptyFile.csv"; | ||
Files.createFile(Path.of(emptyFilePath)); | ||
List<String> result = fileReader.readFromFile(emptyFilePath); | ||
Assertions.assertTrue(result.isEmpty()); | ||
Files.delete(Path.of(emptyFilePath)); | ||
Path emptyFile = createTempFile("emptyFile.csv", ""); | ||
List<String> result = fileReader.readFromFile(emptyFile.toString()); | ||
assertTrue(result.isEmpty()); | ||
} | ||
|
||
@Test | ||
void readFromFile_fileDoesNotExist_notOk() { | ||
String nonExistentFilePath = "src/main/resources/nonExistentFile.csv"; | ||
|
||
RuntimeException exception = Assertions.assertThrows(RuntimeException.class, () -> { | ||
RuntimeException exception = assertThrows(RuntimeException.class, () -> { | ||
fileReader.readFromFile(nonExistentFilePath); | ||
}); | ||
|
||
Assertions.assertTrue(exception.getMessage() | ||
.contains("Can't read file " + nonExistentFilePath)); | ||
assertTrue(exception.getMessage().contains("Can't read file " + nonExistentFilePath)); | ||
} | ||
|
||
@Test | ||
void readFromFile_directoryInsteadOfFile_notOk() { | ||
Path tempDirectory = null; | ||
try { | ||
tempDirectory = Files.createTempDirectory("testDirectory"); | ||
Path finalTempDirectory = tempDirectory; | ||
RuntimeException exception = Assertions.assertThrows(RuntimeException.class, () -> { | ||
fileReader.readFromFile(finalTempDirectory.toString()); | ||
}); | ||
void readFromFile_directoryInsteadOfFile_notOk() throws IOException { | ||
Path tempDirectory = Files.createTempDirectory("testDirectory"); | ||
tempPaths.add(tempDirectory); | ||
|
||
Assertions.assertTrue(exception.getMessage() | ||
.contains("Can't read file " + tempDirectory)); | ||
} catch (IOException e) { | ||
Assertions.fail("Failed to create temporary directory", e); | ||
} finally { | ||
if (tempDirectory != null) { | ||
try { | ||
Files.deleteIfExists(tempDirectory); | ||
} catch (IOException e) { | ||
Assertions.fail("Failed to delete temporary directory", e); | ||
} | ||
} | ||
} | ||
RuntimeException exception = assertThrows(RuntimeException.class, () -> { | ||
fileReader.readFromFile(tempDirectory.toString()); | ||
}); | ||
|
||
assertTrue(exception.getMessage().contains("Can't read file " + tempDirectory)); | ||
} | ||
|
||
private Path createTempFile(String fileName, String content) throws IOException { | ||
Path tempFile = Files.createTempFile(fileName, null); | ||
Files.writeString(tempFile, content); | ||
tempPaths.add(tempFile); | ||
return tempFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.