Merge pull request #8 from PM2-IT21bWIN-ruiz-mach-krea/fxml

Fxml and Factory
This commit is contained in:
Roman Schenk 2022-04-30 18:09:54 +02:00 committed by GitHub Enterprise
commit 2ce93cbf9b
18 changed files with 855 additions and 20 deletions

View File

@ -9,6 +9,12 @@
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.12'
}
javafx {
version = '17.0.1'
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
repositories {
@ -22,6 +28,8 @@ dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
}
application {

View File

@ -3,12 +3,11 @@
*/
package ch.zhaw.projekt2.turnierverwaltung;
public class App {
public String getGreeting() {
return "Hello World!";
}
import ch.zhaw.projekt2.turnierverwaltung.main.MainWindow;
import javafx.application.Application;
public class App {
public static void main(String[] args) {
System.out.println(new App().getGreeting());
Application.launch(MainWindow.class,args);
}
}

View File

@ -0,0 +1,35 @@
package ch.zhaw.projekt2.turnierverwaltung;
import javafx.scene.layout.Pane;
public abstract class FXController {
Tournament tournament;
Factory factory;
FileIO fileIO;
Pane pane;
public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){
this.tournament = tournament;
this.fileIO = fileIO;
this.factory = factory;
this.pane = pane;
}
public abstract void loadContent();
protected Tournament getTournament() {
return tournament;
}
protected FileIO getFileIO() {
return fileIO;
}
protected Factory getFactory() {
return factory;
}
protected Pane getPane() {
return pane;
}
}

View File

@ -0,0 +1,65 @@
package ch.zhaw.projekt2.turnierverwaltung;
import ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.BorderPane;
import java.io.IOException;
import java.net.URL;
public class Factory {
private Tournament tournament;
private FileIO fileIO;
public Factory(FileIO fileIO){
this.fileIO = fileIO;
}
public Tournament getTournament() {
return tournament;
}
public void setTournament(Tournament tournament) {
this.tournament = tournament;
}
public BorderPane loadMainWindow(){
FXMLLoader loader = new FXMLLoader(getClass().getResource("mainWindow.fxml"));
try {
return loader.load();
} catch (IOException e) {
e.printStackTrace();
//TODO handle and logging
}
return null;
}
public void loadTournamentList(BorderPane pane){
TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"));
}
//Can be used to Open new Scene in same Stage.
//This way possible to later give object to Controller
public void loadParticipantFormular(BorderPane pane) {
setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml"));
}
private FXController setCenterOfBorderPane(BorderPane pane, URL location) {
FXController controller = null;
try {
FXMLLoader loader = new FXMLLoader(location);
pane.setCenter(loader.load());
controller = loader.getController();
controller.setup(tournament, fileIO, this, pane);
controller.loadContent();
} catch (IOException e) {
e.printStackTrace();
//TODO handle and logging?
}
return controller;
}
}

View File

@ -0,0 +1,132 @@
package ch.zhaw.projekt2.turnierverwaltung;
import java.io.*;
import java.net.URI;
import java.util.ArrayList;
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<TournamentFile> getList() {
logger.fine("Creating a List out of all Files in the save directory and returning it");
List<TournamentFile> tournaments = new ArrayList<>();
for(File tournament : saves.listFiles()){
tournaments.add(new TournamentFile(tournament.toURI()));
}
return tournaments;
}
/**
* @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);
}
}
}
}
public class TournamentFile extends File{
public TournamentFile(URI uri) {
super(uri);
}
public String toString(){
String name = getName();
return name.split("\\.")[0];
}
}
}

View File

@ -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){

View File

@ -0,0 +1,35 @@
package ch.zhaw.projekt2.turnierverwaltung.main;
import ch.zhaw.projekt2.turnierverwaltung.Factory;
import ch.zhaw.projekt2.turnierverwaltung.FileIO;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.awt.*;
import java.io.IOException;
public class MainWindow extends Application {
private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds");
private Factory factory = new Factory(fileIO); //TODO make it private!
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane pane = factory.loadMainWindow();
factory.loadTournamentList(pane);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.setResizable(false);
primaryStage.setFullScreen(false);
primaryStage.show();
}
}

View File

@ -0,0 +1,23 @@
package ch.zhaw.projekt2.turnierverwaltung.main;
import ch.zhaw.projekt2.turnierverwaltung.FXController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class MainWindowController extends FXController {
@FXML
void changeLangToGerman(ActionEvent event) {
}
@FXML
void closeApplication(ActionEvent event) {
}
@Override
public void loadContent() {
}
}

View File

@ -0,0 +1,83 @@
package ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular;
import ch.zhaw.projekt2.turnierverwaltung.FXController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
public class ParticipantFormularController extends FXController {
@FXML
private Button addBtn;
@FXML
private Label birthDateLabel;
@FXML
private TextField birthDateTextField;
@FXML
private VBox changeBtn;
@FXML
private Label firstNameLabel;
@FXML
private TextField firstNameTextField;
@FXML
private GridPane grid;
@FXML
private Label newParticipantFormularTitle;
@FXML
private Button openBtn;
@FXML
private Label participantListTitle;
@FXML
private ListView<?> participantListView;
@FXML
private Label participantNameLabel;
@FXML
private TextField participantNameTextField;
@FXML
private Label phoneNumberLabel;
@FXML
private TextField phoneNumberTextField;
@FXML
private Button saveBtn;
@FXML
void addParticipant(ActionEvent event) {
}
@FXML
void changeParticipant(MouseEvent event) {
}
@FXML
void save(ActionEvent event) {
}
@Override
public void loadContent() {
}
}

View File

@ -0,0 +1,79 @@
package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList;
import ch.zhaw.projekt2.turnierverwaltung.FXController;
import ch.zhaw.projekt2.turnierverwaltung.Factory;
import ch.zhaw.projekt2.turnierverwaltung.FileIO;
import ch.zhaw.projekt2.turnierverwaltung.Tournament;
import ch.zhaw.projekt2.turnierverwaltung.main.MainWindow;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import java.io.File;
import java.io.IOException;
public class TournamentListController extends FXController {
@FXML
private Button createBtn;
@FXML
private GridPane grid;
@FXML
private ChoiceBox<?> modusChoiceBox;
@FXML
private Label newTournamentFormularTitle;
@FXML
private Button openBtn;
@FXML
private Label tournierListTitle;
@FXML
private ListView<File> tournierListView;
@FXML
private Label tournierModLabel;
@FXML
private Label turnierNameLabel;
@FXML
void createTournament(ActionEvent event) {
}
@FXML
void openTournament(ActionEvent event) {
try {
File tournamentFile = tournierListView.getSelectionModel().getSelectedItems().get(0);
getFactory().setTournament(getFileIO().loadTournament(tournamentFile));
getFactory().loadParticipantFormular((BorderPane) getPane());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void loadContent() {
ObservableList<File> tournamentFiles = FXCollections.observableArrayList();
for(File tournament : getFileIO().getList()){
tournamentFiles.add(tournament);
}
tournierListView.setItems(tournamentFiles);
}
}

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.MainWindowController">
<top>
<VBox alignment="TOP_CENTER" prefHeight="86.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="Start">
<items>
<Menu mnemonicParsing="false" text="Sprache">
<items>
<MenuItem mnemonicParsing="false" onAction="#changeLangToGerman" text="Deutsch" />
</items>
</Menu>
<MenuItem mnemonicParsing="false" onAction="#closeApplication" text="Close" />
</items>
</Menu>
</menus>
</MenuBar>
<Label text="Turnier Manager">
<font>
<Font name="System Bold" size="40.0" />
</font>
</Label>
</children>
</VBox>
</top>
</BorderPane>

View File

@ -0,0 +1,10 @@
#mainContainer {
-fx-min-height: 100%;
-fx-min-width: 100%;
-fx-background-color: #f8f8f8;
}
/*
Formular Right Side
*/

View File

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?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:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular.ParticipantFormularController">
<children>
<VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children>
<Label fx:id="participantListTitle" text="Hinzugefügt">
<font>
<Font name="System Bold" size="21.0" />
</font>
<VBox.margin>
<Insets bottom="20.0" />
</VBox.margin>
</Label>
<ListView fx:id="participantListView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets />
</VBox.margin>
</ListView>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<Button fx:id="saveBtn" mnemonicParsing="false" onAction="#save" text="Save">
<HBox.margin>
<Insets right="40.0" />
</HBox.margin>
</Button>
<Button fx:id="openBtn" mnemonicParsing="false" text="Bearbeiten" />
</children>
</HBox>
</children>
<HBox.margin>
<Insets left="40.0" />
</HBox.margin>
</VBox>
<Separator orientation="VERTICAL" prefHeight="200.0">
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</Separator>
<VBox fx:id="changeBtn" alignment="TOP_CENTER" onDragDetected="#changeParticipant" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children>
<Label fx:id="newParticipantFormularTitle" text="Neuer Teilnehmer">
<font>
<Font name="System Bold" size="21.0" />
</font>
<VBox.margin>
<Insets bottom="40.0" />
</VBox.margin>
</Label>
<Separator prefWidth="200.0" />
<GridPane fx:id="grid" prefHeight="200.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="participantNameLabel" styleClass="lableGrid" text="Name">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<TextField fx:id="participantNameTextField" styleClass="inputGrid" GridPane.columnIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>
</TextField>
<Label fx:id="firstNameLabel" styleClass="lableGrid" text="Vorname" GridPane.rowIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<TextField fx:id="firstNameTextField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="phoneNumberTextField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField fx:id="birthDateTextField" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label fx:id="phoneNumberLabel" text="Telefonnummer" GridPane.rowIndex="2" />
<Label fx:id="birthDateLabel" text="Geb. Datum" GridPane.rowIndex="3" />
</children>
</GridPane>
<Separator prefWidth="200.0" />
<Button fx:id="addBtn" alignment="TOP_LEFT" mnemonicParsing="false" onAction="#addParticipant" text="Erstellen" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="10.0" top="30.0" />
</VBox.margin>
</Button>
</children>
<HBox.margin>
<Insets right="40.0" />
</HBox.margin>
</VBox>
</children>
</HBox>

View File

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?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">
<children>
<VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children>
<Label fx:id="tournierListTitle" text="Bestehende Turniere">
<font>
<Font name="System Bold" size="21.0" />
</font>
<VBox.margin>
<Insets bottom="20.0" />
</VBox.margin>
</Label>
<ListView fx:id="tournierListView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets />
</VBox.margin>
</ListView>
<Button fx:id="openBtn" mnemonicParsing="false" onAction="#openTournament" text="Öffnen">
<VBox.margin>
<Insets bottom="20.0" top="40.0" />
</VBox.margin>
</Button>
</children>
<HBox.margin>
<Insets left="40.0" />
</HBox.margin>
</VBox>
<Separator orientation="VERTICAL" prefHeight="200.0">
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</Separator>
<VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children>
<Label fx:id="newTournamentFormularTitle" text="Neues Turnier erstellen">
<font>
<Font name="System Bold" size="21.0" />
</font>
<VBox.margin>
<Insets bottom="40.0" />
</VBox.margin></Label>
<Separator prefWidth="200.0" />
<GridPane fx:id="grid" prefHeight="200.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="turnierNameLabel" styleClass="lableGrid" text="Turnier Name:">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<TextField styleClass="inputGrid" GridPane.columnIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>
</TextField>
<Label fx:id="tournierModLabel" styleClass="lableGrid" text="Turnier Modus:" GridPane.rowIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<ChoiceBox fx:id="modusChoiceBox" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</children>
</GridPane>
<Separator prefWidth="200.0" />
<Button fx:id="createBtn" alignment="TOP_LEFT" mnemonicParsing="false" onAction="#createTournament" text="Erstellen" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="20.0" top="40.0" />
</VBox.margin></Button>
</children>
<HBox.margin>
<Insets right="40.0" />
</HBox.margin>
</VBox>
</children>
</HBox>

View File

@ -1,14 +0,0 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package ch.zhaw.projekt2.turnierverwaltung;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}

View File

@ -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<FileIO.TournamentFile> 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));
}
}
}