From 50f98b4ac10f37f31ecbb0d89c8de7637091af30 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 1 May 2022 12:33:14 +0200 Subject: [PATCH] added Type checking in Tournament.java --- .../projekt2/turnierverwaltung/Tournament.java | 17 ++++++++++++++++- .../TournamentListController.java | 4 ++++ .../projekt2/turnierverwaltung/FileIOTest.java | 16 +++++----------- 3 files changed, 25 insertions(+), 12 deletions(-) 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 2966009..e46f505 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -4,6 +4,7 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import java.io.Serializable; +import java.util.Arrays; public class Tournament implements Serializable { private String name; @@ -11,10 +12,13 @@ public class Tournament implements Serializable { - public Tournament(String name, Type type) throws InvalidNameException { + public Tournament(String name, Type type) throws InvalidNameException, InvalidTypeException { if(!name.matches("[\\w]{1,20}")){ throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging. + } else if(!Arrays.asList(Type.values()).contains(type)){ + throw new InvalidTypeException("Invalid Type selected"); //TODO handle en logging. } + setName(name); setType(type); } @@ -67,5 +71,16 @@ public class Tournament implements Serializable { } + public class InvalidTypeException extends Exception { + public InvalidTypeException() { + super(); + } + + public InvalidTypeException(String errorMessage) { + super(errorMessage); + } + + } + } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java index 3ce4cef..433ab41 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java @@ -64,8 +64,11 @@ public class TournamentListController extends FXController { Tournament tournament = new Tournament(tournamentNameField.getText(), modusChoiceBox.getValue()); getFileIO().saveTournament(tournament); loadContent(); + tournamentNameField.clear(); } catch (Tournament.InvalidNameException e) { e.printStackTrace(); //TODO handle and logging + } catch (Tournament.InvalidTypeException e) { + e.printStackTrace(); //TODO handle and logging } catch (IOException e) { e.printStackTrace(); //TODO handle and logging } @@ -92,6 +95,7 @@ public class TournamentListController extends FXController { if(alert.showAndGetResult()){ try { getFileIO().deleteTournament(tournamentFile); + loadContent(); } catch (IOException e) { e.printStackTrace(); //TODO handle and logging } 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 27912a8..c8b8060 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java @@ -108,20 +108,14 @@ class FileIOTest { } @Test - void saveTournament() throws IOException { + void saveTournament() throws IOException, Tournament.InvalidNameException, Tournament.InvalidTypeException { Tournament tournament = null; - try { - tournament = new Tournament("test1", Tournament.Type.KO); - } catch (Tournament.InvalidNameException e) { - e.printStackTrace(); - } + tournament = new Tournament("test1", Tournament.Type.KO); io.saveTournament(tournament); File file = new File(mainDir + "/saves/test1.txt"); - if(file.exists()){ - file.delete(); - } else { - fail(); - } + assertTrue(file.exists()); + assertTrue(file.delete()); + assertFalse(file.exists()); } @Test