Tournament list #11

Merged
schrom01 merged 13 commits from TournamentList into main 2022-05-01 18:45:02 +02:00
14 changed files with 420 additions and 93 deletions

View File

@ -3,32 +3,44 @@ package ch.zhaw.projekt2.turnierverwaltung;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
public abstract class FXController { public abstract class FXController {
Tournament tournament; TournamentDecorator tournamentDecorator;
Factory factory; FactoryDecorator factoryDecorator;
FileIO fileIO; FileIO fileIO;
Pane pane; Pane pane;
public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){ public void setup(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, Pane pane){
this.tournament = tournament; this.tournamentDecorator = tournamentDecorator;
this.fileIO = fileIO; this.fileIO = fileIO;
this.factory = factory; this.factoryDecorator = factoryDecorator;
this.pane = pane; this.pane = pane;
tournamentDecorator.addListener(new IsObserver() {
@Override
public void update() {
loadContent();
}
});
factoryDecorator.addListener(new IsObserver() {
@Override
public void update() {
loadContent();
}
});
} }
public abstract void loadContent(); public abstract void loadContent();
protected Tournament getTournament() { protected TournamentDecorator getTournamentDecorator() {
return tournament; return tournamentDecorator;
}
protected FactoryDecorator getFactoryDecorator() {
return factoryDecorator;
} }
protected FileIO getFileIO() { protected FileIO getFileIO() {
return fileIO; return fileIO;
} }
protected Factory getFactory() {
return factory;
}
protected Pane getPane() { protected Pane getPane() {
return pane; return pane;
} }

View File

@ -8,19 +8,20 @@ import java.io.IOException;
import java.net.URL; import java.net.URL;
public class Factory { public class Factory {
private Tournament tournament; private TournamentDecorator tournamentDecorator;
private FileIO fileIO; private FileIO fileIO;
public Factory(FileIO fileIO){ public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator){
this.fileIO = fileIO; this.fileIO = fileIO;
this.tournamentDecorator = tournamentDecorator;
} }
public Tournament getTournament() { public TournamentDecorator getTournamentDecorator() {
return tournament; return tournamentDecorator;
} }
public void setTournament(Tournament tournament) { public void setTournament(Tournament tournament) {
this.tournament = tournament; this.tournamentDecorator = tournamentDecorator;
} }
public BorderPane loadMainWindow(){ public BorderPane loadMainWindow(){
@ -34,23 +35,23 @@ public class Factory {
return null; return null;
} }
public void loadTournamentList(BorderPane pane){ public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator){
TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml")); TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator);
} }
//Can be used to Open new Scene in same Stage. //Can be used to Open new Scene in same Stage.
//This way possible to later give object to Controller //This way possible to later give object to Controller
public void loadParticipantFormular(BorderPane pane) { public void loadParticipantFormular(BorderPane pane, FactoryDecorator factoryDecorator) {
setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml")); setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml"), factoryDecorator);
} }
private FXController setCenterOfBorderPane(BorderPane pane, URL location) { private FXController setCenterOfBorderPane(BorderPane pane, URL location, FactoryDecorator factoryDecorator) {
FXController controller = null; FXController controller = null;
try { try {
FXMLLoader loader = new FXMLLoader(location); FXMLLoader loader = new FXMLLoader(location);
pane.setCenter(loader.load()); pane.setCenter(loader.load());
controller = loader.getController(); controller = loader.getController();
controller.setup(tournament, fileIO, this, pane); controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane);
controller.loadContent(); controller.loadContent();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -0,0 +1,62 @@
package ch.zhaw.projekt2.turnierverwaltung;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FactoryDecorator implements IsObservable{
private Factory factory;
private FileIO fileIO;
private Pane pane;
private List<IsObserver> listener = new ArrayList<>();
public FactoryDecorator(FileIO fileIO, Factory factory, Pane pane){
setFactory(factory);
setFileIO(fileIO);
setPane(pane);
}
public void setFileIO(FileIO fileIO) {
this.fileIO = fileIO;
}
public void setFactory(Factory factory) {
this.factory = factory;
}
public void setPane(Pane pane) {
this.pane = pane;
}
@Override
public void addListener(IsObserver observer) {
listener.add(observer);
}
@Override
public void removeListener(IsObserver observer) {
listener.remove(observer);
}
public void openTournament(FileIO.TournamentFile tournamentFile){
try {
factory.setTournament(fileIO.loadTournament(tournamentFile));
factory.loadParticipantFormular((BorderPane) pane, this); //TODO load TournamentView instead of ParticipantFormular?
informListener();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} //TODO handle and logging
}
public void informListener() {
for(IsObserver observer : listener) {
observer.update();
}
}
}

View File

@ -1,6 +1,9 @@
package ch.zhaw.projekt2.turnierverwaltung; package ch.zhaw.projekt2.turnierverwaltung;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
@ -34,13 +37,22 @@ public class FileIO {
} }
} }
public List<TournamentFile> getList() { public ObservableList<TournamentFile> getList() {
logger.fine("Creating a List out of all Files in the save directory and returning it"); logger.fine("Creating a List out of all Files in the save directory and returning it");
List<TournamentFile> tournaments = new ArrayList<>(); ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
for(File tournament : saves.listFiles()){ for(File tournament : saves.listFiles()){
tournaments.add(new TournamentFile(tournament.toURI())); tournamentFiles.add(new TournamentFile(tournament.toURI()));
} }
return tournaments; return tournamentFiles;
}
public boolean tournamentExists(String name){
for(TournamentFile file : getList()) {
if(file.toString().toLowerCase().equals(name.toLowerCase())){
return true;
}
}
return false;
} }
/** /**
@ -86,7 +98,7 @@ public class FileIO {
return tournament; return tournament;
} }
public void saveTournament(Tournament tournament) { public void saveTournament(Tournament tournament) throws IOException {
if (tournament == null) { if (tournament == null) {
logger.warning("Given tournament file is empty"); logger.warning("Given tournament file is empty");
throw new IllegalArgumentException("Null tournament received"); throw new IllegalArgumentException("Null tournament received");
@ -124,9 +136,13 @@ public class FileIO {
} }
public String toString(){ public String toString(){
String name = getName(); return getName().split("\\.")[0];
return name.split("\\.")[0];
} }
} }
public void deleteTournament(File tournamentFile) throws IOException {
throw new UnsupportedOperationException("Method deleteTournament not implemented yet.");
}
} }

View File

@ -0,0 +1,19 @@
package ch.zhaw.projekt2.turnierverwaltung;
/**
* Most basic interface for observing an object
* @author bles
*
*/
public interface IsObservable {
/**
* Add an observer that listens for updates
* @param observer
*/
void addListener(IsObserver observer);
/**
* Remove an observer from the list
* @param observer
*/
void removeListener(IsObserver observer);
}

View File

@ -0,0 +1,14 @@
package ch.zhaw.projekt2.turnierverwaltung;
/**
* Most basic interface for beeing an observer
* @author bles
*
*/
public interface IsObserver {
/**
* This method is always called when an observed object
* changes
*/
void update();
}

View File

@ -1,12 +1,26 @@
package ch.zhaw.projekt2.turnierverwaltung; package ch.zhaw.projekt2.turnierverwaltung;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays;
public class Tournament implements Serializable { public class Tournament implements Serializable {
private String name; private String name;
private Type type;
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.
}
public Tournament(String name){
setName(name); setName(name);
setType(type);
} }
public String getName() { public String getName() {
@ -16,4 +30,57 @@ public class Tournament implements Serializable {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public enum Type {
KO("KO-System"), GROUPS("Gruppenspiele");
private String name;
private Type(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
public static ObservableList<Type> getObservableList(){
ObservableList<Type> items = FXCollections.observableArrayList();
items.addAll(values());
return items;
}
}
public class InvalidNameException extends Exception {
public InvalidNameException() {
super();
}
public InvalidNameException(String errorMessage) {
super(errorMessage);
}
}
public class InvalidTypeException extends Exception {
public InvalidTypeException() {
super();
}
public InvalidTypeException(String errorMessage) {
super(errorMessage);
}
}
} }

View File

@ -0,0 +1,68 @@
package ch.zhaw.projekt2.turnierverwaltung;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TournamentDecorator implements IsObservable{
private Tournament tournament;
private FileIO fileIO;
private List<IsObserver> listener = new ArrayList<>();
public TournamentDecorator(FileIO fileIO, Tournament tournament){
setTournament(tournament);
setFileIO(fileIO);
}
public void setFileIO(FileIO fileIO) {
this.fileIO = fileIO;
}
public void setTournament(Tournament tournament) {
this.tournament = tournament;
}
@Override
public void addListener(IsObserver observer) {
listener.add(observer);
}
@Override
public void removeListener(IsObserver observer) {
listener.remove(observer);
}
public void createTournament(String name, Tournament.Type type){
if(fileIO.tournamentExists(name)){
System.out.println("Tournament with same name exists already.");
return; //TODO handle and logging
}
try {
Tournament tournament = new Tournament(name, type);
fileIO.saveTournament(tournament);
informListener();
} 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
}
}
public void deleteTournament(FileIO.TournamentFile tournamentFile){
try {
fileIO.deleteTournament(tournamentFile);
informListener();
} catch (IOException e) {
e.printStackTrace(); //TODO handle and logging
}
}
public void informListener() {
for(IsObserver observer : listener) {
observer.update();
}
}
}

View File

@ -1,7 +1,9 @@
package ch.zhaw.projekt2.turnierverwaltung.main; package ch.zhaw.projekt2.turnierverwaltung.main;
import ch.zhaw.projekt2.turnierverwaltung.Factory; import ch.zhaw.projekt2.turnierverwaltung.Factory;
import ch.zhaw.projekt2.turnierverwaltung.FactoryDecorator;
import ch.zhaw.projekt2.turnierverwaltung.FileIO; import ch.zhaw.projekt2.turnierverwaltung.FileIO;
import ch.zhaw.projekt2.turnierverwaltung.TournamentDecorator;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
@ -14,13 +16,16 @@ import java.io.IOException;
public class MainWindow extends Application { public class MainWindow extends Application {
private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds"); private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds");
private Factory factory = new Factory(fileIO); //TODO make it private! private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO, null);
private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private!
private FactoryDecorator factoryDecorator;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
BorderPane pane = factory.loadMainWindow(); BorderPane pane = factory.loadMainWindow();
factory.loadTournamentList(pane); factoryDecorator = new FactoryDecorator(fileIO, factory, pane);
factory.loadTournamentList(pane, factoryDecorator);
Scene scene = new Scene(pane); Scene scene = new Scene(pane);

View File

@ -0,0 +1,34 @@
package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import java.io.IOException;
public class AlertDelete extends Alert {
ButtonType yesButton = new ButtonType("Ja", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("Nein", ButtonBar.ButtonData.NO);
Boolean result;
public AlertDelete(String name){
super(Alert.AlertType.WARNING);
setTitle("Entfernen");
setHeaderText("Turnier entfernen?");
setContentText("Sind Sie sicher, dass sie das Turnier " + name + " entfernen wollen?\n" +
"Nach diesem Vorgang kann es nicht wiederhergestellt werden.");
getButtonTypes().setAll(yesButton, noButton);
}
public boolean showAndGetResult() {
result = false;
showAndWait().ifPresent(type -> {
if (type == yesButton) {
result = true;
}
});
return result;
}
}

View File

@ -1,19 +1,13 @@
package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList; package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList;
import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.FXController;
import ch.zhaw.projekt2.turnierverwaltung.Factory;
import ch.zhaw.projekt2.turnierverwaltung.FileIO; import ch.zhaw.projekt2.turnierverwaltung.FileIO;
import ch.zhaw.projekt2.turnierverwaltung.Tournament; import ch.zhaw.projekt2.turnierverwaltung.Tournament;
import ch.zhaw.projekt2.turnierverwaltung.main.MainWindow;
import javafx.beans.Observable;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
@ -22,6 +16,11 @@ import java.io.IOException;
public class TournamentListController extends FXController { public class TournamentListController extends FXController {
@FXML
public void initialize(){
modusChoiceBox.setItems(Tournament.Type.getObservableList());
}
@FXML @FXML
private Button createBtn; private Button createBtn;
@ -29,7 +28,7 @@ public class TournamentListController extends FXController {
private GridPane grid; private GridPane grid;
@FXML @FXML
private ChoiceBox<?> modusChoiceBox; private ChoiceBox<Tournament.Type> modusChoiceBox;
@FXML @FXML
private Label newTournamentFormularTitle; private Label newTournamentFormularTitle;
@ -38,42 +37,47 @@ public class TournamentListController extends FXController {
private Button openBtn; private Button openBtn;
@FXML @FXML
private Label tournierListTitle; private Button deleteBtn;
@FXML @FXML
private ListView<File> tournierListView; private Label tournamentListTitle;
@FXML @FXML
private Label tournierModLabel; private ListView<FileIO.TournamentFile> tournamentListView;
@FXML @FXML
private Label turnierNameLabel; private Label tournamentModLabel;
@FXML
private Label tournamentNameLabel;
@FXML
private TextField tournamentNameField;
@FXML @FXML
void createTournament(ActionEvent event) { void createTournament(ActionEvent event) {
getTournamentDecorator().createTournament(tournamentNameField.getText(), modusChoiceBox.getValue());
} }
@FXML @FXML
void openTournament(ActionEvent event) { void openTournament(ActionEvent event) {
try { FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
File tournamentFile = tournierListView.getSelectionModel().getSelectedItems().get(0); getFactoryDecorator().openTournament(tournamentFile);
getFactory().setTournament(getFileIO().loadTournament(tournamentFile)); }
getFactory().loadParticipantFormular((BorderPane) getPane());
} catch (IOException e) { @FXML
e.printStackTrace(); void deleteTournament(ActionEvent event) {
} catch (ClassNotFoundException e) { FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
e.printStackTrace(); AlertDelete alert = new AlertDelete(tournamentFile.toString());
if(alert.showAndGetResult()){
getTournamentDecorator().deleteTournament(tournamentFile);
} }
} }
@Override @Override
public void loadContent() { public void loadContent() {
ObservableList<File> tournamentFiles = FXCollections.observableArrayList(); tournamentListView.setItems(getFileIO().getList());
for(File tournament : getFileIO().getList()){ tournamentNameField.clear();
tournamentFiles.add(tournament);
}
tournierListView.setItems(tournamentFiles);
} }
} }

View File

@ -1,24 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.*?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.*?>
<?import javafx.scene.control.ChoiceBox?> <?import javafx.scene.layout.*?>
<?import javafx.scene.control.Label?> <?import javafx.scene.text.*?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<HBox alignment="CENTER" VBox.vgrow="ALWAYS" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController"> <HBox alignment="CENTER" VBox.vgrow="ALWAYS" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController">
<children> <children>
<VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS"> <VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children> <children>
<Label fx:id="tournierListTitle" text="Bestehende Turniere"> <Label fx:id="tournamentListTitle" text="Bestehende Turniere">
<font> <font>
<Font name="System Bold" size="21.0" /> <Font name="System Bold" size="21.0" />
</font> </font>
@ -26,16 +17,20 @@
<Insets bottom="20.0" /> <Insets bottom="20.0" />
</VBox.margin> </VBox.margin>
</Label> </Label>
<ListView fx:id="tournierListView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS"> <ListView fx:id="tournamentListView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<VBox.margin> <VBox.margin>
<Insets /> <Insets />
</VBox.margin> </VBox.margin>
</ListView> </ListView>
<Button fx:id="openBtn" mnemonicParsing="false" onAction="#openTournament" text="Öffnen"> <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
<VBox.margin> <VBox.margin>
<Insets bottom="20.0" top="40.0" /> <Insets bottom="20.0" top="40.0" />
</VBox.margin> </VBox.margin>
</Button> <children>
<Button fx:id="openBtn" mnemonicParsing="false" onAction="#openTournament" text="Öffnen" />
<Button fx:id="deleteBtn" layoutX="138.0" layoutY="28.0" mnemonicParsing="false" onAction="#deleteTournament" text="Löschen" />
</children>
</HBox>
</children> </children>
<HBox.margin> <HBox.margin>
<Insets left="40.0" /> <Insets left="40.0" />
@ -66,17 +61,17 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <children>
<Label fx:id="turnierNameLabel" styleClass="lableGrid" text="Turnier Name:"> <Label fx:id="tournamentNameLabel" styleClass="lableGrid" text="Turnier Name:">
<GridPane.margin> <GridPane.margin>
<Insets /> <Insets />
</GridPane.margin> </GridPane.margin>
</Label> </Label>
<TextField styleClass="inputGrid" GridPane.columnIndex="1"> <TextField fx:id="tournamentNameField" styleClass="inputGrid" GridPane.columnIndex="1">
<GridPane.margin> <GridPane.margin>
<Insets /> <Insets />
</GridPane.margin> </GridPane.margin>
</TextField> </TextField>
<Label fx:id="tournierModLabel" styleClass="lableGrid" text="Turnier Modus:" GridPane.rowIndex="1"> <Label fx:id="tournamentModLabel" styleClass="lableGrid" text="Turnier Modus:" GridPane.rowIndex="1">
<GridPane.margin> <GridPane.margin>
<Insets /> <Insets />
</GridPane.margin> </GridPane.margin>

View File

@ -82,19 +82,19 @@ class FileIOTest {
@Test @Test
void loadTournamentNotExisting(){ void loadTournamentNotExisting(){
io = new FileIO(mainDir); File file = new File("Not-existing-File");
assertThrows(FileNotFoundException.class, () -> io.loadTournament(new File("Not-existing-File"))); assertFalse(file.exists());
assertThrows(FileNotFoundException.class, () -> io.loadTournament(file));
assertFalse(file.exists());
} }
@Test @Test
void loadTournamentEmpty(){ 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 @Test
void loadTournamentFileNull(){ void loadTournamentFileNull(){
io = new FileIO(mainDir);
assertThrows(IllegalArgumentException.class, () -> io.loadTournament(null)); assertThrows(IllegalArgumentException.class, () -> io.loadTournament(null));
} }
} }
@ -108,15 +108,14 @@ class FileIOTest {
} }
@Test @Test
void saveTournament() throws IOException { void saveTournament() throws IOException, Tournament.InvalidNameException, Tournament.InvalidTypeException {
Tournament tournament = new Tournament("test1"); Tournament tournament = null;
tournament = new Tournament("test1", Tournament.Type.KO);
io.saveTournament(tournament); io.saveTournament(tournament);
File file = new File(mainDir + "/saves/test1.txt"); File file = new File(mainDir + "/saves/test1.txt");
if(file.exists()){ assertTrue(file.exists());
file.delete(); assertTrue(file.delete());
} else { assertFalse(file.exists());
fail();
}
} }
@Test @Test
@ -124,4 +123,35 @@ class FileIOTest {
assertThrows(IllegalArgumentException.class, () -> io.saveTournament(null)); assertThrows(IllegalArgumentException.class, () -> io.saveTournament(null));
} }
} }
@Nested
class Delete{
@BeforeEach
void setup(){
mainDir = RESOURCES_DIR + "FileIODelete";
io = new FileIO(mainDir);
}
@Test
void deleteTournament() throws IOException {
File file = new File(mainDir + "/saves/test1.txt");
file.createNewFile();
assertTrue(file.exists());
io.deleteTournament(file);
assertFalse(file.exists());
}
@Test
void deleteTournamentNotExisting() throws IOException {
File file = new File("Not-existing-File");
assertFalse(file.exists());
assertThrows(FileNotFoundException.class, () -> io.deleteTournament(file));
assertFalse(file.exists());
}
@Test
void deleteTournamentNull(){
assertThrows(IllegalArgumentException.class, () -> io.deleteTournament(null));
}
}
} }