Merge pull request #42 from PM2-IT21bWIN-ruiz-mach-krea/testClass
Test class
This commit is contained in:
		
						commit
						1d6fcce13f
					
				| 
						 | 
				
			
			@ -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()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 developed in future releases
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			@ -107,10 +108,14 @@ 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) throws PlaceExistsException {
 | 
			
		||||
        places.removeIf(place -> place.equals(newPlace));
 | 
			
		||||
    public void savePlace(Place newPlace) {
 | 
			
		||||
        for (Place place : places) {
 | 
			
		||||
            if (place.equals(newPlace)) {
 | 
			
		||||
                place.change(newPlace);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        places.add(newPlace);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -295,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
 | 
			
		||||
     */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ public class TournamentDecorator implements IsObservable{
 | 
			
		|||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
 | 
			
		||||
        executorService = Executors.newFixedThreadPool(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setFactoryDecorator(FactoryDecorator factoryDecorator) {
 | 
			
		||||
| 
						 | 
				
			
			@ -148,13 +148,9 @@ 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 (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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,147 @@
 | 
			
		|||
package ch.zhaw.projekt2.turnierverwaltung;
 | 
			
		||||
 | 
			
		||||
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.mock;
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
            assertEquals(0, tournament.getParticipants().size());
 | 
			
		||||
            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();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @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.savePlace(place);
 | 
			
		||||
            assertEquals(1, tournament.getPlaces().size());
 | 
			
		||||
            tournament.savePlace(place);
 | 
			
		||||
            assertEquals(2, tournament.getPlaces().size());
 | 
			
		||||
            tournament.savePlace(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.savePlace(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());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue