created FileIOTest and changed Exception Handling of File IO
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
package ch.zhaw.projekt2.turnierverwaltung;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class FileIOTest {
|
||||
|
||||
String RESOURCES_DIR = ".\\src\\test\\resources\\ch\\zhaw\\projekt2\\turnierverwaltung\\";
|
||||
String mainDir;
|
||||
String saveDir;
|
||||
FileIO io;
|
||||
|
||||
@Test
|
||||
void FileIONewDir() throws IOException {
|
||||
mainDir = RESOURCES_DIR + "FileIONew";
|
||||
saveDir = mainDir + "\\" + "saves";
|
||||
File mainDirFile = new File(mainDir);
|
||||
File saveDirFile = new File(mainDir);
|
||||
try{
|
||||
Files.walk(mainDirFile.toPath())
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
assertFalse(mainDirFile.exists());
|
||||
assertFalse(saveDirFile.exists());
|
||||
io = new FileIO(mainDir);
|
||||
assertTrue(mainDirFile.exists());
|
||||
assertTrue(saveDirFile.exists());
|
||||
Files.walk(mainDirFile.toPath())
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
assertFalse(mainDirFile.exists());
|
||||
assertFalse(saveDirFile.exists());
|
||||
}
|
||||
|
||||
@Nested
|
||||
class Read{
|
||||
@BeforeEach
|
||||
void init() {
|
||||
mainDir = RESOURCES_DIR + "FileIORead";
|
||||
io = new FileIO(mainDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getList() {
|
||||
List<File> tournaments = io.getList();
|
||||
assertEquals("empty.txt", tournaments.get(0).getName());
|
||||
assertEquals("test1.txt", tournaments.get(1).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getListEmpty() {
|
||||
mainDir = RESOURCES_DIR + "FileIOEmpty";
|
||||
io = new FileIO(mainDir);
|
||||
assertEquals(0, io.getList().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadTournament() throws IOException, ClassNotFoundException {
|
||||
mainDir = RESOURCES_DIR + "FileIORead";
|
||||
io = new FileIO(mainDir);
|
||||
Tournament tournament = io.loadTournament(new File(mainDir + "\\saves\\test1.txt"));
|
||||
assertEquals("test1", tournament.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadTournamentNotExisting(){
|
||||
io = new FileIO(mainDir);
|
||||
assertThrows(FileNotFoundException.class, () -> io.loadTournament(new File("Not-existing-File")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadTournamentEmpty(){
|
||||
io = new FileIO(mainDir);
|
||||
assertThrows(IOException.class, () -> io.loadTournament(new File(mainDir + "\\saves\\empty.txt")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadTournamentFileNull(){
|
||||
io = new FileIO(mainDir);
|
||||
assertThrows(IllegalArgumentException.class, () -> io.loadTournament(null));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class Save{
|
||||
@BeforeEach
|
||||
void setup(){
|
||||
mainDir = RESOURCES_DIR + "FileIOSave";
|
||||
io = new FileIO(mainDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveTournament() {
|
||||
Tournament tournament = new Tournament("test1");
|
||||
io.saveTournament(tournament);
|
||||
File file = new File(mainDir + "\\saves\\test1.txt");
|
||||
if(file.exists()){
|
||||
file.delete();
|
||||
} else {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveTournamentNull(){
|
||||
assertThrows(IllegalArgumentException.class, () -> io.saveTournament(null));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user