From e0b1616edf9107db99fe61a81aa2292dd1c12782 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Sun, 24 Apr 2022 22:06:03 +0200 Subject: [PATCH 01/10] Created classes to test FileIO --- .../zhaw/projekt2/turnierverwaltung/App.java | 3 ++ .../projekt2/turnierverwaltung/FileIO.java | 51 +++++++++++++++++++ .../turnierverwaltung/Tournament.java | 4 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java 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 e06a54c..d00a1a6 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java @@ -10,5 +10,8 @@ public class App { public static void main(String[] args) { System.out.println(new App().getGreeting()); + FileIO io = new FileIO(); + Tournament tournament= new Tournamenttest("hello"); + io.saveTournament(tournament); } } 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..ca86b5c --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -0,0 +1,51 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + + +public class FileIO { + File saveDir; + File saves; + + + public FileIO() { + String savePath = System.getProperty("user.dir")+File.separator+"tournierverwaltung_angrynerds"; + this.saveDir = new File(savePath); + saves = new File(saveDir, "saves"); + } + + public void getList() { + + } + + public void loadTournament(Tournament tournament) { + + } + + public void saveTournament(Tournament tournament) { + File newSave = new File(saves, tournament.getName()+".txt"); + try { + newSave.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + ObjectOutputStream 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 (IOException e) { + e.printStackTrace(); + } + + } + + public void getTournament(String storageSpace) { + + } + +} 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 c6d6211..4b6b8de 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 abstract class Tournament { +import java.io.Serializable; + +public abstract class Tournament implements Serializable { private String name; public Tournament(String name){ From e1fdc5deac098f113d6d6ede743054b75914e823 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 28 Apr 2022 03:07:51 +0200 Subject: [PATCH 02/10] finished initial draft of file io and also added some test cases to disscuss in app --- .../zhaw/projekt2/turnierverwaltung/App.java | 19 +++++++++++- .../projekt2/turnierverwaltung/FileIO.java | 30 ++++++++++++------- .../turnierverwaltung/Tournamenttest.java | 7 +++++ 3 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java 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 d00a1a6..dc9a2f3 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java @@ -3,6 +3,10 @@ */ package ch.zhaw.projekt2.turnierverwaltung; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + public class App { public String getGreeting() { return "Hello World!"; @@ -10,8 +14,21 @@ public class App { public static void main(String[] args) { System.out.println(new App().getGreeting()); + FileIO io = new FileIO(); - Tournament tournament= new Tournamenttest("hello"); + Tournament tournament= new Tournamenttest("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 ca86b5c..9271812 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import java.io.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; @@ -11,21 +12,33 @@ public class FileIO { public FileIO() { - String savePath = System.getProperty("user.dir")+File.separator+"tournierverwaltung_angrynerds"; + String savePath = System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds"; this.saveDir = new File(savePath); + saveDir.mkdir(); saves = new File(saveDir, "saves"); + saves.mkdir(); } - public void getList() { - + public List getList() { + return Arrays.asList(saves.listFiles()); } - public void loadTournament(Tournament tournament) { + public Tournament loadTournament(File tournamentFile) { + Tournament tournament; + try { + ObjectInputStream in = new ObjectInputStream(new FileInputStream(tournamentFile)); + tournament = (Tournament) in.readObject(); + in.close(); + } catch (ClassNotFoundException | IOException e) { + throw new RuntimeException(e); + } + + return tournament; } public void saveTournament(Tournament tournament) { - File newSave = new File(saves, tournament.getName()+".txt"); + File newSave = new File(saves, tournament.getName() + ".txt"); try { newSave.createNewFile(); } catch (IOException e) { @@ -36,16 +49,11 @@ public class FileIO { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(newSave)); out.writeObject(tournament); out.close(); - System.out.println("Save File" + tournament.getName()+".txt being saved to " + saves.getAbsolutePath()); + System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } } - - public void getTournament(String storageSpace) { - - } - } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java new file mode 100644 index 0000000..2b259a9 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java @@ -0,0 +1,7 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +public class Tournamenttest extends Tournament { + public Tournamenttest(String name) { + super(name); + } +} From 380250f60eb6b628e10d82b85dfcfe767401c892 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 28 Apr 2022 14:35:29 +0200 Subject: [PATCH 03/10] changes so that savepath can be set elsewhere --- .../zhaw/projekt2/turnierverwaltung/App.java | 2 +- .../projekt2/turnierverwaltung/FileIO.java | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) 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 dc9a2f3..816e6f5 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java @@ -15,7 +15,7 @@ public class App { public static void main(String[] args) { System.out.println(new App().getGreeting()); - FileIO io = new FileIO(); + FileIO io = new FileIO(System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds"); Tournament tournament= new Tournamenttest("helloo"); io.saveTournament(tournament); 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 9271812..87e357a 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -1,21 +1,25 @@ package ch.zhaw.projekt2.turnierverwaltung; -import java.io.*; -import java.util.ArrayList; + +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.util.Arrays; import java.util.List; public class FileIO { - File saveDir; - File saves; + private File mainDir; + private File saves; - public FileIO() { - String savePath = System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds"; - this.saveDir = new File(savePath); - saveDir.mkdir(); - saves = new File(saveDir, "saves"); + public FileIO(String saveLocation) { + this.mainDir = new File(saveLocation); + mainDir.mkdir(); + saves = new File(mainDir, "saves"); saves.mkdir(); } @@ -29,6 +33,7 @@ public class FileIO { 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); From 48fba228533f6e9f4cba246f1ba3cb7eae7fe692 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 28 Apr 2022 14:10:41 +0200 Subject: [PATCH 04/10] added Date of Birth to Player.java --- .../zhaw/projekt2/turnierverwaltung/Player.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java index 567f49b..4273fd1 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java @@ -1,10 +1,21 @@ package ch.zhaw.projekt2.turnierverwaltung; -public class Player extends Person{ +import java.util.Date; +public class Player extends Person implements Participant{ - public Player(String firstName, String name, String phoneNumber){ + private Date dateOfBirth; + + public Player(String firstName, String name, String phoneNumber, Date dateOfBirth){ super(firstName, name, phoneNumber); + setDateOfBirth(dateOfBirth); } + public Date getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(Date dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } } From 0164c286fb25a5274d5f14c51f267acabde11aed Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 28 Apr 2022 14:50:45 +0200 Subject: [PATCH 05/10] made Tournament not abstract. --- .../java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 4b6b8de..0da286d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -2,7 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import java.io.Serializable; -public abstract class Tournament implements Serializable { +public class Tournament implements Serializable { private String name; public Tournament(String name){ From 263d79ba8e95a40d926fdb787963e0ca769a2ed4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 28 Apr 2022 14:57:41 +0200 Subject: [PATCH 06/10] removed Class Tournamenttest.java --- .../main/java/ch/zhaw/projekt2/turnierverwaltung/App.java | 2 +- .../ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) delete mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java 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 816e6f5..e04444b 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java @@ -16,7 +16,7 @@ public class App { System.out.println(new App().getGreeting()); FileIO io = new FileIO(System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds"); - Tournament tournament= new Tournamenttest("helloo"); + Tournament tournament= new Tournament("helloo"); io.saveTournament(tournament); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java deleted file mode 100644 index 2b259a9..0000000 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java +++ /dev/null @@ -1,7 +0,0 @@ -package ch.zhaw.projekt2.turnierverwaltung; - -public class Tournamenttest extends Tournament { - public Tournamenttest(String name) { - super(name); - } -} From 509192b4039fffbc5ae8a0ad41d9dd817849fc16 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 28 Apr 2022 18:07:57 +0200 Subject: [PATCH 07/10] created FileIOTest and changed Exception Handling of File IO --- .../zhaw/projekt2/turnierverwaltung/App.java | 14 -- .../projekt2/turnierverwaltung/FileIO.java | 33 +++-- .../turnierverwaltung/FileIOTest.java | 126 ++++++++++++++++++ .../FileIORead/saves/empty.txt | 0 4 files changed, 141 insertions(+), 32 deletions(-) create mode 100644 app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java create mode 100644 app/src/test/resources/ch/zhaw/projekt2/turnierverwaltung/FileIORead/saves/empty.txt 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 From cff22e925334ae7f0f793061f97d35107ee3dfb3 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 29 Apr 2022 08:29:10 +0200 Subject: [PATCH 08/10] added file needed for FileIOTest --- .../turnierverwaltung/FileIORead/saves/test1.txt | Bin 0 -> 102 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/src/test/resources/ch/zhaw/projekt2/turnierverwaltung/FileIORead/saves/test1.txt 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 0000000000000000000000000000000000000000..1053e937a68251e2a9071865be37ff6720565f46 GIT binary patch literal 102 zcmZ4UmVvdnh(R|wL$4|$v0Sg9C_gJTyTnMZq_ik6GqtEJwWvHXr=&D5T`wdbD3q9+ znpg5&>Cuh_+|o%*42(VuEFiHG1|grU#Ii*FoW#6z{osdb){@lX5<>v0 Ca3t#h literal 0 HcmV?d00001 From 068dc44e891ad49175904295ae4e5a2fd1a0da9b Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Fri, 29 Apr 2022 11:52:19 +0200 Subject: [PATCH 09/10] 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"); From 90f6855a155ed6319c0d8592ef945d53b0f56083 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 29 Apr 2022 16:09:44 +0200 Subject: [PATCH 10/10] changed Path Separator --- .../java/ch/zhaw/projekt2/turnierverwaltung/App.java | 2 +- .../ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) 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 ce8229a..3273b7c 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java @@ -15,6 +15,6 @@ public class App { public static void main(String[] args) { System.out.println(new App().getGreeting()); - FileIO io = new FileIO(System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds"); + FileIO io = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds"); } } 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..9e6b63d 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java @@ -17,7 +17,7 @@ import static org.junit.jupiter.api.Assertions.*; class FileIOTest { - String RESOURCES_DIR = ".\\src\\test\\resources\\ch\\zhaw\\projekt2\\turnierverwaltung\\"; + String RESOURCES_DIR = "./src/test/resources/ch/zhaw/projekt2/turnierverwaltung/"; String mainDir; String saveDir; FileIO io; @@ -25,7 +25,7 @@ class FileIOTest { @Test void FileIONewDir() throws IOException { mainDir = RESOURCES_DIR + "FileIONew"; - saveDir = mainDir + "\\" + "saves"; + saveDir = mainDir + "/saves"; File mainDirFile = new File(mainDir); File saveDirFile = new File(mainDir); try{ @@ -75,7 +75,7 @@ class FileIOTest { void loadTournament() throws IOException, ClassNotFoundException { mainDir = RESOURCES_DIR + "FileIORead"; io = new FileIO(mainDir); - Tournament tournament = io.loadTournament(new File(mainDir + "\\saves\\test1.txt")); + Tournament tournament = io.loadTournament(new File(mainDir + "/saves/test1.txt")); assertEquals("test1", tournament.getName()); } @@ -88,7 +88,7 @@ class FileIOTest { @Test void loadTournamentEmpty(){ io = new FileIO(mainDir); - assertThrows(IOException.class, () -> io.loadTournament(new File(mainDir + "\\saves\\empty.txt"))); + assertThrows(IOException.class, () -> io.loadTournament(new File(mainDir + "/saves/empty.txt"))); } @Test @@ -110,7 +110,7 @@ class FileIOTest { void saveTournament() { Tournament tournament = new Tournament("test1"); io.saveTournament(tournament); - File file = new File(mainDir + "\\saves\\test1.txt"); + File file = new File(mainDir + "/saves/test1.txt"); if(file.exists()){ file.delete(); } else {