diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java new file mode 100644 index 0000000..d072da5 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -0,0 +1,113 @@ +package ch.zhaw.projekt2.turnierverwaltung; + + +import java.io.*; +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; + + +public class FileIO { + private File mainDir; + private File saves; + + private static final Logger logger = Logger.getLogger(FileIO.class.getName()); + + + public FileIO(String saveLocation) { + this.mainDir = new File(saveLocation); + if (!mainDir.exists()) { + logger.fine("Creating main directory in given path" + saveLocation); + mainDir.mkdir(); + } else { + logger.finer("main directory already exists"); + } + + saves = new File(mainDir, "saves"); + if (!saves.exists()) { + saves.mkdir(); + logger.fine("Creating save directory"); + } else { + logger.finer("save directory already exists"); + } + } + + public List getList() { + logger.fine("Creating a List out of all Files in the save directory and returning it"); + return Arrays.asList(saves.listFiles()); + } + + /** + * @param tournamentFile + * @return + * @throws ClassNotFoundException + * @throws IOException File not found or not readable. + */ + public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException { + if (tournamentFile == null) { + logger.warning("Given tournament file is empty"); + throw new IllegalArgumentException("Tournament File is null"); + } + Tournament tournament; + logger.finer("Starting up Input Stream to read File"); + ObjectInputStream in = null; + try { + logger.fine("Setting up input file and reading it"); + FileInputStream fileInputStream = new FileInputStream(tournamentFile); + in = new ObjectInputStream(fileInputStream); + logger.finer("Starting to read tournament File"); + tournament = (Tournament) in.readObject(); + } catch (FileNotFoundException e) { + logger.severe("Could not find tournament File"); + throw e; + } catch (IOException e) { + logger.severe("Failed to read File" + tournamentFile.getName()); + throw new IOException("Error while reading File",e); + } catch (ClassNotFoundException e) { + logger.severe("No definition for the class with the specified name could be found"); + throw new ClassNotFoundException("No definition for the class with the specified name could be found",e); + } finally { + if (in != null) { + try { + logger.finer("Trying to close input stream"); + in.close(); + } catch (IOException e) { + logger.severe("Failed to close input stream"); + throw new IOException("Error while closing input stream",e); + } + } + } + return tournament; + } + + public void saveTournament(Tournament tournament) { + if (tournament == null) { + logger.warning("Given tournament file is empty"); + throw new IllegalArgumentException("Null tournament received"); + } + File newSave = new File(saves, tournament.getName() + ".txt"); + ObjectOutputStream out = null; + + try { + newSave.createNewFile(); + out = new ObjectOutputStream(new FileOutputStream(newSave)); + out.writeObject(tournament); + System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); + + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + } + + } +} diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java index 5845a12..0da286d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -1,6 +1,8 @@ package ch.zhaw.projekt2.turnierverwaltung; -public class Tournament { +import java.io.Serializable; + +public class Tournament implements Serializable { private String name; public Tournament(String name){ 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..ad0b9e8 --- /dev/null +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java @@ -0,0 +1,127 @@ +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 java.util.logging.Logger; + +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() throws IOException { + 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 diff --git a/app/src/test/resources/ch/zhaw/projekt2/turnierverwaltung/FileIORead/saves/test1.txt b/app/src/test/resources/ch/zhaw/projekt2/turnierverwaltung/FileIORead/saves/test1.txt new file mode 100644 index 0000000..1053e93 Binary files /dev/null and b/app/src/test/resources/ch/zhaw/projekt2/turnierverwaltung/FileIORead/saves/test1.txt differ