From 68080c36ff583121cef60dd84327d98f00d611e1 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 12 May 2022 23:15:42 +0200 Subject: [PATCH 1/4] change method addPlace start TournamentTest implementation --- app/build.gradle | 6 +- .../projekt2/turnierverwaltung/Place.java | 4 + .../turnierverwaltung/Tournament.java | 22 +++-- .../TournamentDecorator.java | 4 - .../turnierverwaltung/TournamentTest.java | 85 +++++++++++++++++++ 5 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java 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(); + } + } + + + + } + +} -- 2.40.1 From 40cfd694364f7826270154cdecdd2bb78b685865 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 14:23:17 +0200 Subject: [PATCH 2/4] implement TournamentTest --- .../projekt2/turnierverwaltung/Factory.java | 1 + .../turnierverwaltung/Tournament.java | 3 +- .../turnierverwaltung/TournamentTest.java | 79 +++++++++++++++++-- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java index 53f9f12..3092765 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -12,6 +12,7 @@ import javafx.scene.text.Font; import java.io.IOException; import java.net.URL; +import java.nio.charset.Charset; public class Factory { private TournamentDecorator tournamentDecorator; 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 1745d07..3150c95 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.ArrayList; import java.util.Arrays; @@ -233,7 +234,7 @@ public class Tournament implements Serializable { * @param type */ public void setType(Type type) { - logger.fine("Setting the type of the tournament to: " + type); + logger.fine("Setting the type of the tournament to: " + type); this.type = type; } diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java index c7aac86..135d5c5 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java @@ -9,6 +9,7 @@ import org.mockito.Mockito; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @DisplayName("TournamentTest") @@ -35,7 +36,7 @@ public class TournamentTest { @Test @DisplayName("Test invalid instance") void makeInvalidInstance() { - assertThrows(InvalidNameException.class,() -> new Tournament(".", Tournament.Type.KO)); + assertThrows(InvalidNameException.class, () -> new Tournament(".", Tournament.Type.KO)); } @Test @@ -44,18 +45,20 @@ public class TournamentTest { //Checks if one Participant gets added Participant participantOne = Mockito.mock(Player.class); when(participantOne.equals(any(Participant.class))).thenReturn(false); + + assertEquals(0, tournament.getParticipants().size()); tournament.saveParticipant(participantOne); - assertEquals(1,tournament.getParticipants().size()); + 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()); + 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()); + assertEquals(2, tournament.getParticipants().size()); } @@ -65,21 +68,81 @@ public class TournamentTest { Participant participant = Mockito.mock(Player.class); //Checks if Error is thrown if Participant not in list - assertThrows(Tournament.ParticipantNotExistsException.class,() -> tournament.removeParticipant(participant)); + assertThrows(Tournament.ParticipantNotExistsException.class, () -> tournament.removeParticipant(participant)); //Checks if participant gets removed tournament.saveParticipant(participant); - assertEquals(1,tournament.getParticipants().size()); + assertEquals(1, tournament.getParticipants().size()); try { tournament.removeParticipant(participant); - assertEquals(0,tournament.getParticipants().size()); + assertEquals(0, tournament.getParticipants().size()); } catch (Tournament.ParticipantNotExistsException e) { fail(); } } + @Test + @DisplayName("Add Place") + void addPlaceTest() { + Place place = mock(Place.class); + when(place.equals(any(Place.class))).thenReturn(false).thenReturn(true); + assertEquals(0, tournament.getPlaces().size()); + tournament.addPlace(place); + assertEquals(1, tournament.getPlaces().size()); + tournament.addPlace(place); + assertEquals(2, tournament.getPlaces().size()); + tournament.addPlace(place); + assertEquals(2, tournament.getPlaces().size()); + } + + @Test + @DisplayName("Remove Place") + void removePlaceTest() { + Place place = mock(Place.class); + + assertThrows(Tournament.PlaceNotExistsException.class, () -> tournament.removePlace(place)); + + tournament.addPlace(place); + assertEquals(1, tournament.getPlaces().size()); + try { + tournament.removePlace(place); + assertEquals(0, tournament.getPlaces().size()); + } catch (Tournament.PlaceNotExistsException e) { + fail(); + } + } + + @Test + @DisplayName("Test gameschedule calculation") + void calcGameSchedule() { + Participant participant = mock(Player.class); + when(participant.equals(any(Participant.class))).thenReturn(false); + + //Checks if invalid number of Participants throws error + assertThrows(Tournament.NumberOfParticipantInvalidException.class, () -> tournament.createGameSchedule()); + for (int i = 0; i < 8; i++) { + if (i % 4 == 0 && i > 0) { + try { + tournament.createGameSchedule(); + assertEquals(2, tournament.getGameList().size()); + tournament.saveParticipant(participant); + } catch (Tournament.NumberOfParticipantInvalidException e) { + fail(); + } + + } else { + assertThrows(Tournament.NumberOfParticipantInvalidException.class, () -> tournament.createGameSchedule()); + tournament.saveParticipant(participant); + } + } + try { + tournament.createGameSchedule(); + } catch (Tournament.NumberOfParticipantInvalidException e) { + fail(); + } + assertEquals(3, tournament.getGameList().size()); + } } - } -- 2.40.1 From 127e1526374fcfd9989ab23baa70e84a64c2e231 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 14:52:58 +0200 Subject: [PATCH 3/4] implement TournamentTest --- .../zhaw/projekt2/turnierverwaltung/Place.java | 2 +- .../projekt2/turnierverwaltung/Tournament.java | 17 +---------------- .../turnierverwaltung/TournamentDecorator.java | 2 +- .../turnierverwaltung/TournamentTest.java | 9 ++++----- 4 files changed, 7 insertions(+), 23 deletions(-) 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 c09e1bf..3834101 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java @@ -31,6 +31,6 @@ public class Place implements Serializable { } public void change(Place place) { - //TODO: If Place gets more developt in future releases + //TODO: If Place gets more developed 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 3150c95..44b3817 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -108,9 +108,8 @@ public class Tournament implements Serializable { * Method to add a new Place, also checks if place does already exist. * * @param newPlace to be added in the list of all places - * @throws PlaceExistsException if the place already exists */ - public void addPlace(Place newPlace) { + public void savePlace(Place newPlace) { for (Place place : places) { if (place.equals(newPlace)) { place.change(newPlace); @@ -301,20 +300,6 @@ public class Tournament implements Serializable { } - /** - * Custom Exception thrown when a Place does already exist - */ - public class PlaceExistsException extends Exception { - public PlaceExistsException() { - super(); - } - - public PlaceExistsException(String errorMessage) { - super(errorMessage); - } - - } - /** * Custom Exception thrown when a Place does not exist */ 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 94655aa..ddc4873 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -148,7 +148,7 @@ public class TournamentDecorator implements IsObservable{ public void savePlace(String name){ try { - tournament.addPlace(new Place(name)); + tournament.savePlace(new Place(name)); factoryDecorator.clearMessage(true); informListener(); } catch (InvalidNameException e) { diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java index 135d5c5..3acc30c 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java @@ -1,6 +1,5 @@ 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; @@ -88,11 +87,11 @@ public class TournamentTest { when(place.equals(any(Place.class))).thenReturn(false).thenReturn(true); assertEquals(0, tournament.getPlaces().size()); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(1, tournament.getPlaces().size()); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(2, tournament.getPlaces().size()); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(2, tournament.getPlaces().size()); } @@ -103,7 +102,7 @@ public class TournamentTest { assertThrows(Tournament.PlaceNotExistsException.class, () -> tournament.removePlace(place)); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(1, tournament.getPlaces().size()); try { tournament.removePlace(place); -- 2.40.1 From b4da0b2fefee4634ec01319de04085a7edd312a9 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 15:15:37 +0200 Subject: [PATCH 4/4] Changed number of threads in executerservice to one to prevent false order to save file --- .../ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ddc4873..ec75d5b 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -27,7 +27,7 @@ public class TournamentDecorator implements IsObservable{ } } }); - executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + executorService = Executors.newFixedThreadPool(1); } public void setFactoryDecorator(FactoryDecorator factoryDecorator) { -- 2.40.1