Fxml #8
|
@ -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<File> 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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
FileInputStream fileInputStream = new FileInputStream(tournamentFile);
|
||||
ObjectInputStream in = new ObjectInputStream(fileInputStream);
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
|
@ -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<File> 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));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue