diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java index e04444b..ce8229a 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java @@ -16,19 +16,5 @@ public class App { System.out.println(new App().getGreeting()); FileIO io = new FileIO(System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds"); - Tournament tournament= new Tournament("helloo"); - io.saveTournament(tournament); - - - List tournaments = io.getList(); - - for (File name: tournaments) { - - System.out.println(name.getName()); - } - - Tournament retrieved = io.loadTournament(tournaments.get(0)); - System.out.println("data retreived:"); - System.out.println(retrieved.getName()); } } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java index 87e357a..ad2e92d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -1,12 +1,7 @@ package ch.zhaw.projekt2.turnierverwaltung; -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; -import java.io.ObjectInputStream; +import java.io.*; import java.util.Arrays; import java.util.List; @@ -27,22 +22,24 @@ public class FileIO { return Arrays.asList(saves.listFiles()); } - public Tournament loadTournament(File tournamentFile) { + /** + * + * @param tournamentFile + * @return + * @throws ClassNotFoundException + * @throws IOException File not found or not readable. + */ + public Tournament loadTournament(File tournamentFile) throws ClassNotFoundException, IOException { Tournament tournament; - try { - ObjectInputStream in = new ObjectInputStream(new FileInputStream(tournamentFile)); - tournament = (Tournament) in.readObject(); - in.close(); - System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); - - } catch (ClassNotFoundException | IOException e) { - throw new RuntimeException(e); - } - + FileInputStream fileInputStream = new FileInputStream(tournamentFile); + ObjectInputStream in = new ObjectInputStream(fileInputStream); + tournament = (Tournament) in.readObject(); + in.close(); //TODO: Evtl Try/finally? + System.out.println("Read File" + tournament.getName() + ".txt being read from " + saves.getAbsolutePath()); return tournament; } - public void saveTournament(Tournament tournament) { + public void saveTournament(Tournament tournament) throws IllegalArgumentException { File newSave = new File(saves, tournament.getName() + ".txt"); try { newSave.createNewFile(); diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java new file mode 100644 index 0000000..e11bff8 --- /dev/null +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java @@ -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 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)); + } + } +} \ No newline at end of file diff --git a/app/src/test/resources/ch/zhaw/projekt2/turnierverwaltung/FileIORead/saves/empty.txt b/app/src/test/resources/ch/zhaw/projekt2/turnierverwaltung/FileIORead/saves/empty.txt new file mode 100644 index 0000000..e69de29