From fc256787f8c69ff3ec5d1653deabac485477ba6c Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 5 Jan 2022 19:55:27 +0100 Subject: [PATCH] player_management: added method loadPlayers --- .../de/tims/player_management/Player.java | 2 +- .../tims/player_management/PlayerManager.java | 4 +++ .../player_management/PlayerManagerTest.java | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/player_management/Player.java b/src/main/java/de/tims/player_management/Player.java index d5c3b22..50f86b7 100644 --- a/src/main/java/de/tims/player_management/Player.java +++ b/src/main/java/de/tims/player_management/Player.java @@ -39,7 +39,7 @@ public class Player { @Override public int hashCode() { - return points; + return points + name.hashCode(); } } diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index fbc5746..b21fe73 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -24,4 +24,8 @@ public class PlayerManager { return new Player(playerName, 0); } + public void loadPlayers(String fileName) { + players = List.of(); + } + } diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index 11db5ab..5def1ec 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -2,6 +2,9 @@ package de.tims.player_management; import static org.assertj.core.api.Assertions.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.List; import java.util.stream.Stream; @@ -29,5 +32,27 @@ class PlayerManagerTest { Arguments.of("PlayerWithNameInListReturnPlayerInList", List.of(new Player("Steffen", 40), new Player("Tobias", 50)), "Tobias", new Player("Tobias", 50))); } + + @ParameterizedTest + @MethodSource("testCasesForLoadPlayers") + void loadPlayersTest(String testName, String fileContent, String fileName, List expectedResult) { + File playerData = new File(fileName); + + try { + FileWriter fw = new FileWriter(playerData, false); + fw.write(fileContent); + fw.close(); + } catch (IOException e) { + fail("Cannot open file"); + } + + manager.loadPlayers(fileName); + List calculatedResult = manager.getPlayers(); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); + } + + private static Stream testCasesForLoadPlayers() { + return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/main/java/resources/player_data.csv", List.of())); + } }