From 068dc44e891ad49175904295ae4e5a2fd1a0da9b Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Fri, 29 Apr 2022 11:52:19 +0200 Subject: [PATCH] started adding logging to FileIO --- .../projekt2/turnierverwaltung/FileIO.java | 90 +++++++++++++++---- .../turnierverwaltung/FileIOTest.java | 3 +- 2 files changed, 73 insertions(+), 20 deletions(-) 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 ad2e92d..d072da5 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -4,57 +4,109 @@ 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); - mainDir.mkdir(); + 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"); - saves.mkdir(); + 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. + * @throws IOException File not found or not readable. */ - public Tournament loadTournament(File tournamentFile) throws ClassNotFoundException, IOException { + 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; - 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()); + 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) throws IllegalArgumentException { + 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(); - } catch (IOException e) { - e.printStackTrace(); - } - - try { - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(newSave)); + out = new ObjectOutputStream(new FileOutputStream(newSave)); out.writeObject(tournament); - out.close(); System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } } diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java index e11bff8..433148e 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java @@ -12,6 +12,7 @@ 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.*; @@ -107,7 +108,7 @@ class FileIOTest { } @Test - void saveTournament() { + void saveTournament() throws IOException { Tournament tournament = new Tournament("test1"); io.saveTournament(tournament); File file = new File(mainDir + "\\saves\\test1.txt");