diff --git a/app/build.gradle b/app/build.gradle index dfbf65f..e1f7b1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,6 +26,8 @@ dependencies { // Use JUnit Jupiter for testing. testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1' + testImplementation 'org.mockito:mockito-core:4.3.+' + // This dependency is used by the application. implementation 'com.google.guava:guava:30.1.1-jre' @@ -37,7 +39,9 @@ application { mainClass = 'ch.zhaw.projekt2.turnierverwaltung.App' } -tasks.named('test') { +test { // Use JUnit Platform for unit tests. useJUnitPlatform() + + } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java index f2e35bd..c09e1bf 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java @@ -29,4 +29,8 @@ public class Place implements Serializable { public boolean equals(Place place){ return name.equals(place.getName()); } + + public void change(Place place) { + //TODO: If Place gets more developt in future releases + } } 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 bb4bd92..ce9e62c 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.collections.FXCollections; import javafx.collections.ObservableList; + import java.io.Serializable; import java.util.*; @@ -28,8 +29,8 @@ public class Tournament implements Serializable { } public void saveParticipant(Participant newParticipant) { - for(Participant participant: participants){ - if(participant.equals(newParticipant)){ + for (Participant participant : participants) { + if (participant.equals(newParticipant)) { participant.change(newParticipant); return; } @@ -50,8 +51,13 @@ public class Tournament implements Serializable { return participantsObservable; } - public void addPlace(Place newPlace) throws PlaceExistsException { - places.removeIf(place -> place.equals(newPlace)); + public void addPlace(Place newPlace) { + for (Place place : places) { + if (place.equals(newPlace)) { + place.change(newPlace); + return; + } + } places.add(newPlace); } @@ -91,7 +97,7 @@ public class Tournament implements Serializable { private boolean numberOfParticipantValid() { double res = Math.log(participants.size()) / Math.log(2); - return (res * 10) % 10 == 0 && participants.size() >=4; + return (res * 10) % 10 == 0 && participants.size() >= 4; } private void calcGameSchedule() { @@ -101,11 +107,11 @@ public class Tournament implements Serializable { List gameRound = new ArrayList<>(); if (i == 0) { for (int j = 0; j < participants.size() - 1; j += 2) { - gameRound.add(new Game(participants.get(j), participants.get(j+1))); + gameRound.add(new Game(participants.get(j), participants.get(j + 1))); } } else { - for (int j = 0; j < (gameList.get(i-1).size()/2); j++) { - gameRound.add(new Game(gameList.get(i-1).get(j*2),gameList.get(i-1).get(j*2+1))); + for (int j = 0; j < (gameList.get(i - 1).size() / 2); j++) { + gameRound.add(new Game(gameList.get(i - 1).get(j * 2), gameList.get(i - 1).get(j * 2 + 1))); } } gameList.add(gameRound); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index 28d6bac..eaa2e8d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -148,10 +148,6 @@ public class TournamentDecorator implements IsObservable{ tournament.addPlace(new Place(name)); factoryDecorator.clearMessage(true); informListener(); - } catch (Tournament.PlaceExistsException e) { - e.printStackTrace(); //TODO handle and logging - factoryDecorator.printMessageToFooter("Ort existiert bereits",true); - } catch (InvalidNameException e) { e.printStackTrace(); //TODO handle and logging factoryDecorator.printMessageToFooter("Invalider Ortsname",true); diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java new file mode 100644 index 0000000..c7aac86 --- /dev/null +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java @@ -0,0 +1,85 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +import javafx.application.Platform; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@DisplayName("TournamentTest") +public class TournamentTest { + + @Nested + @DisplayName("KOMode") + public class KOMode { + private Tournament tournament; + + @BeforeEach + void setup() { + try { + tournament = new Tournament("Name", Tournament.Type.KO); + } catch (InvalidNameException e) { + e.printStackTrace(); + fail(); + } catch (Tournament.InvalidTypeException e) { + e.printStackTrace(); + fail(); + } + } + + @Test + @DisplayName("Test invalid instance") + void makeInvalidInstance() { + assertThrows(InvalidNameException.class,() -> new Tournament(".", Tournament.Type.KO)); + } + + @Test + @DisplayName("Save Participant") + void saveParticipantTest() { + //Checks if one Participant gets added + Participant participantOne = Mockito.mock(Player.class); + when(participantOne.equals(any(Participant.class))).thenReturn(false); + tournament.saveParticipant(participantOne); + assertEquals(1,tournament.getParticipants().size()); + + //Checks if a second Participant gets added + Participant participantTwo = Mockito.mock(Player.class); + tournament.saveParticipant(participantTwo); + assertEquals(2,tournament.getParticipants().size()); + + //Checks if a allready added Particpant does not get added + when(participantOne.equals(any(Participant.class))).thenReturn(true); + tournament.saveParticipant(participantTwo); + assertEquals(2,tournament.getParticipants().size()); + + } + + @Test + @DisplayName("Remove Participant") + void removeParticipantTest() { + Participant participant = Mockito.mock(Player.class); + + //Checks if Error is thrown if Participant not in list + assertThrows(Tournament.ParticipantNotExistsException.class,() -> tournament.removeParticipant(participant)); + + //Checks if participant gets removed + tournament.saveParticipant(participant); + assertEquals(1,tournament.getParticipants().size()); + try { + tournament.removeParticipant(participant); + assertEquals(0,tournament.getParticipants().size()); + } catch (Tournament.ParticipantNotExistsException e) { + fail(); + } + } + + + + } + +}