mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-13 17:11:49 +00:00
feat: dev-web - add timer and fix tests
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
|
||||
package uppa.project.pojo;
|
||||
|
||||
import jakarta.persistence.Transient;
|
||||
|
||||
/**
|
||||
* Représentation d'une carte
|
||||
*
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package uppa.project.pojo;
|
||||
|
||||
import jakarta.persistence.Transient;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
@@ -17,6 +18,14 @@ import java.util.Collections;
|
||||
*/
|
||||
public class Deck {
|
||||
|
||||
public static final int NB_COLORS_MIN = 2;
|
||||
|
||||
public static final int NB_COLORS_MAX = Card.Color.values().length;
|
||||
|
||||
public static final int NB_VALUES_PER_COLOR_MIN = 2;
|
||||
|
||||
public static final int NB_VALUES_PER_COLOR_MAX = Card.Value.values().length;
|
||||
|
||||
/**
|
||||
* Ensemble de cartes du paquet
|
||||
* @see Card
|
||||
@@ -54,8 +63,8 @@ public class Deck {
|
||||
* @return true si le prédicat est vérifié, false sinon
|
||||
*/
|
||||
public static boolean isDeckValid(int nbColors, int nbValues) {
|
||||
return 1 <= nbColors && nbColors <= Card.Color.values().length
|
||||
&& 1 <= nbValues && nbValues <= Card.Value.values().length;
|
||||
return NB_COLORS_MIN <= nbColors && nbColors <= NB_COLORS_MAX
|
||||
&& NB_VALUES_PER_COLOR_MIN <= nbValues && nbValues <= NB_VALUES_PER_COLOR_MAX;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,8 @@ import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* Représentation d'une partie de jeu
|
||||
@@ -65,6 +67,18 @@ public class Game implements Serializable {
|
||||
@Transient
|
||||
private Deck deck;
|
||||
|
||||
@Transient
|
||||
private int timer;
|
||||
|
||||
@Transient
|
||||
public static final int TIMER_MIN = 10;
|
||||
|
||||
@Transient
|
||||
public static final int TIMER_MAX = 60;
|
||||
|
||||
@Transient
|
||||
public static final int NB_ROUNDS_MIN = 3;
|
||||
|
||||
/**
|
||||
* Constructeur par défaut
|
||||
*/
|
||||
@@ -76,15 +90,18 @@ public class Game implements Serializable {
|
||||
*
|
||||
* @param difficulty la difficulté de la partie
|
||||
* @see Difficulty
|
||||
* @param timer le timer de chaque rounds en seconde
|
||||
* @param nbRounds le nombre de tours de la partie
|
||||
* @param nbColors le nombre de couleurs présente dans le deck
|
||||
* @param nbValuesPerColor le nombre de valeurs par couleur
|
||||
*/
|
||||
public Game(Difficulty difficulty, int nbRounds, int nbColors, int nbValuesPerColor) {
|
||||
public Game(Difficulty difficulty, int timer, int nbRounds, int nbColors, int nbValuesPerColor) {
|
||||
if (isValidNumberRound(nbRounds, nbColors, nbValuesPerColor)){
|
||||
throw new IllegalArgumentException("Le nombre de tours doit être compris entre 1 et " + nbColors * nbValuesPerColor);
|
||||
throw new IllegalArgumentException("Le nombre de tours doit être compris entre 2 et " + nbColors * nbValuesPerColor);
|
||||
}
|
||||
if (timer < TIMER_MIN || timer > TIMER_MAX){
|
||||
throw new IllegalArgumentException("Le timer doit être compris entre" + TIMER_MIN + " et " + TIMER_MAX + " secondes");
|
||||
}
|
||||
|
||||
this.difficulty = difficulty;
|
||||
this.nbRounds = nbRounds;
|
||||
this.nbColors = nbColors;
|
||||
@@ -93,6 +110,7 @@ public class Game implements Serializable {
|
||||
this.deck.shuffle();
|
||||
this.players = new ArrayList<>();
|
||||
this.createdAt = new Date();
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,11 +124,13 @@ public class Game implements Serializable {
|
||||
* @param nbValuesPerColor le nombre de valeurs par couleur
|
||||
* @param players les joueurs de la partie
|
||||
*/
|
||||
public Game(BigDecimal id, Date createdAt, Difficulty difficulty, int nbRounds, int nbColors, int nbValuesPerColor, ArrayList<Player> players) {
|
||||
public Game(BigDecimal id, Date createdAt, Difficulty difficulty, int timer ,int nbRounds, int nbColors, int nbValuesPerColor, ArrayList<Player> players) {
|
||||
if (isValidNumberRound(nbRounds, nbColors, nbValuesPerColor)){
|
||||
throw new IllegalArgumentException("Le nombre de tours doit être compris entre 1 et " + nbColors * nbValuesPerColor);
|
||||
}
|
||||
|
||||
if (timer < TIMER_MIN || timer > TIMER_MAX){
|
||||
throw new IllegalArgumentException("Le timer doit être compris entre" + TIMER_MIN + " et " + TIMER_MAX + " secondes");
|
||||
}
|
||||
this.id = id;
|
||||
this.createdAt = createdAt;
|
||||
this.difficulty = difficulty;
|
||||
@@ -119,6 +139,7 @@ public class Game implements Serializable {
|
||||
this.nbValuesPerColor = nbValuesPerColor;
|
||||
this.players = players;
|
||||
this.deck = new Deck(nbColors, nbValuesPerColor);
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -166,14 +187,14 @@ public class Game implements Serializable {
|
||||
* @param nbRounds le nouveau nombre de tours de la partie
|
||||
*/
|
||||
public void setNbRounds(int nbRounds) {
|
||||
if (nbRounds < 1 || nbRounds > nbColors * nbValuesPerColor){
|
||||
throw new IllegalArgumentException("Le nombre de tours doit être compris entre 1 et " + nbColors * nbValuesPerColor);
|
||||
if (nbRounds < NB_ROUNDS_MIN || nbRounds > nbColors * nbValuesPerColor){
|
||||
throw new IllegalArgumentException("Le nombre de tours doit être compris entre " + NB_ROUNDS_MIN + " et " + nbColors * nbValuesPerColor);
|
||||
}
|
||||
if (nbColors < 1 || nbColors > Card.Color.values().length) {
|
||||
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
|
||||
if (nbColors < Deck.NB_COLORS_MIN || nbColors > Deck.NB_COLORS_MAX) {
|
||||
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre" + NB_ROUNDS_MIN + " et " + Deck.NB_COLORS_MAX);
|
||||
}
|
||||
if (nbValuesPerColor < 1 || nbValuesPerColor > Card.Value.values().length) {
|
||||
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre 1 et " + Card.Value.values().length);
|
||||
if (nbValuesPerColor < Deck.NB_VALUES_PER_COLOR_MIN || nbValuesPerColor > Deck.NB_VALUES_PER_COLOR_MAX) {
|
||||
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre " + Deck.NB_VALUES_PER_COLOR_MIN + " et " + Deck.NB_VALUES_PER_COLOR_MAX);
|
||||
}
|
||||
this.nbRounds = nbRounds;
|
||||
}
|
||||
@@ -189,8 +210,8 @@ public class Game implements Serializable {
|
||||
* @param nbColors le nouveau nombre de couleurs présente dans le deck
|
||||
*/
|
||||
public void setNbColors(int nbColors) {
|
||||
if (nbColors < 1 || nbColors > Card.Color.values().length) {
|
||||
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
|
||||
if (nbColors < Deck.NB_COLORS_MIN || nbColors > Deck.NB_COLORS_MAX) {
|
||||
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre " + Deck.NB_COLORS_MIN + " et " + Deck.NB_COLORS_MAX);
|
||||
}
|
||||
this.nbColors = nbColors;
|
||||
}
|
||||
@@ -206,8 +227,8 @@ public class Game implements Serializable {
|
||||
* @param nbValuesPerColor le nouveau nombre de valeurs par couleur
|
||||
*/
|
||||
public void setNbValuesPerColor(int nbValuesPerColor) {
|
||||
if (nbValuesPerColor < 1 || nbValuesPerColor > Card.Value.values().length) {
|
||||
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre 1 et " + Card.Value.values().length);
|
||||
if (nbValuesPerColor < Deck.NB_VALUES_PER_COLOR_MIN || nbValuesPerColor > Deck.NB_VALUES_PER_COLOR_MAX) {
|
||||
throw new IllegalArgumentException("Le nombre de valeurs par couleur doit être compris entre " + Deck.NB_VALUES_PER_COLOR_MIN + " et " + Deck.NB_VALUES_PER_COLOR_MAX);
|
||||
}
|
||||
this.nbValuesPerColor = nbValuesPerColor;
|
||||
}
|
||||
@@ -244,6 +265,14 @@ public class Game implements Serializable {
|
||||
this.players.add(player);
|
||||
}
|
||||
|
||||
public int getTimer() {
|
||||
return timer;
|
||||
}
|
||||
|
||||
public void setTimer(int timer) {
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
public Deck getDeck() {
|
||||
return deck;
|
||||
}
|
||||
@@ -264,7 +293,7 @@ public class Game implements Serializable {
|
||||
}
|
||||
|
||||
public boolean isValidNumberRound(int nbRounds, int nbColors, int nbValuesPerColor){
|
||||
return nbRounds < 1 || nbRounds > nbColors * nbValuesPerColor;
|
||||
return nbRounds < NB_ROUNDS_MIN || nbRounds > nbColors * nbValuesPerColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -39,6 +39,7 @@ class DAO_JPA_GameTest {
|
||||
new BigDecimal(1),
|
||||
new Date(100, Calendar.NOVEMBER, 1),
|
||||
Game.Difficulty.EASY,
|
||||
10,
|
||||
4,
|
||||
2,
|
||||
4,
|
||||
@@ -48,6 +49,7 @@ class DAO_JPA_GameTest {
|
||||
new BigDecimal(2),
|
||||
new Date(100, Calendar.DECEMBER, 1),
|
||||
Game.Difficulty.HARD,
|
||||
10,
|
||||
3,
|
||||
2,
|
||||
4,
|
||||
|
||||
@@ -40,13 +40,13 @@ class DAO_JPA_PlayerTest {
|
||||
fixture = new Player[] {
|
||||
new Player(
|
||||
new BigDecimal(1),
|
||||
new Game(new BigDecimal(1), new Date(100, 10, 1), Game.Difficulty.EASY, 4, 2, 4, new ArrayList<>()),
|
||||
new Game(new BigDecimal(1), new Date(100, 10, 1), Game.Difficulty.EASY, 10, 4, 2, 4, new ArrayList<>()),
|
||||
new User("user1", "email1", "password1", new Date(100, Calendar.JANUARY, 12), User.Gender.OTHER),
|
||||
2, false, 3, 1, 2
|
||||
),
|
||||
new Player(
|
||||
new BigDecimal(2),
|
||||
new Game(new BigDecimal(1), new Date(100, 10, 1), Game.Difficulty.EASY, 4, 2, 4, new ArrayList<>()),
|
||||
new Game(new BigDecimal(1), new Date(100, 10, 1), Game.Difficulty.EASY, 10, 4, 2, 4, new ArrayList<>()),
|
||||
new User("user2", "email2", "password2", new Date(100, Calendar.MARCH, 15), User.Gender.MALE)
|
||||
, 3, true, 4, 2, 3
|
||||
),
|
||||
|
||||
@@ -16,7 +16,7 @@ class DeckTest {
|
||||
new Deck(4, 13);
|
||||
new Deck(3, 10);
|
||||
new Deck(2, 7);
|
||||
new Deck(1, 1);
|
||||
new Deck(2, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -65,7 +65,7 @@ class DeckTest {
|
||||
put(new Deck(4, 13), 52);
|
||||
put(new Deck(3, 11), 33);
|
||||
put(new Deck(2, 7), 14);
|
||||
put(new Deck(1, 1), 1);
|
||||
put(new Deck(2, 5), 10);
|
||||
}};
|
||||
for (Deck deck : TESTS.keySet()) {
|
||||
assertEquals(TESTS.get(deck), deck.getCards().size());
|
||||
@@ -78,7 +78,7 @@ class DeckTest {
|
||||
put(new Deck(4, 13), 4);
|
||||
put(new Deck(3, 10), 3);
|
||||
put(new Deck(2, 7), 2);
|
||||
put(new Deck(1, 1), 1);
|
||||
put(new Deck(2, 2), 2);
|
||||
}};
|
||||
|
||||
for (Deck deck : TESTS.keySet()) {
|
||||
@@ -94,7 +94,7 @@ class DeckTest {
|
||||
put(new Deck(4, 13), 13);
|
||||
put(new Deck(3, 10), 10);
|
||||
put(new Deck(2, 7), 7);
|
||||
put(new Deck(1, 1), 1);
|
||||
put(new Deck(2, 2), 2);
|
||||
}};
|
||||
|
||||
for (Deck deck : TESTS.keySet()) {
|
||||
|
||||
@@ -2,8 +2,10 @@ package uppa.project.pojo;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -14,38 +16,42 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
class GameTest {
|
||||
|
||||
Game[] fixture;
|
||||
static Player[][] playersFixture;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll(){
|
||||
playersFixture = new Player[][]{
|
||||
{new Player(new BigDecimal(1), new Game(Game.Difficulty.EASY, 10, 17, 3, 6), new User(), 10, true, 5, 5, 5)},
|
||||
{new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 10, 17, 3, 6), new User(), 15, true, 5, 5, 5),
|
||||
new Player(new BigDecimal(3), new Game(Game.Difficulty.EASY, 10, 17, 3, 6), new User(), 20, true, 5, 5, 5)},
|
||||
{new Player(new BigDecimal(4), new Game(Game.Difficulty.EASY, 15, 17, 3, 6), new User(), 10, true, 5, 5, 5),
|
||||
new Player(new BigDecimal(5), new Game(Game.Difficulty.EASY, 15, 17, 3, 6), new User(), 30, true, 5, 5, 5),
|
||||
new Player(new BigDecimal(6), new Game(Game.Difficulty.EASY, 15, 17, 3, 6), new User(), 20, true, 5, 5, 5)}
|
||||
};
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
fixture = new Game[]{
|
||||
new Game(new BigDecimal(1), new Date(2024 - 1900, 5, 6), Game.Difficulty.EASY, 17, 3, 6, new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(1), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}}),
|
||||
new Game(new BigDecimal(2), new Date(2023 - 1900, 7, 9), Game.Difficulty.HARD, 28, 4, 13, new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(3), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}}),
|
||||
new Game(new BigDecimal(3), new Date(2022 - 1900, 11, 12), Game.Difficulty.EASY, 16, 2, 9, new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(4), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(5), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(6), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}}),
|
||||
new Game(new BigDecimal(1), new Date(2024 - 1900, 5, 6), Game.Difficulty.EASY, 50, 17, 3, 6, new ArrayList<>(Arrays.asList(playersFixture[0]))),
|
||||
new Game(new BigDecimal(2), new Date(2023 - 1900, 7, 9), Game.Difficulty.HARD, 10, 28, 4, 13, new ArrayList<>(Arrays.asList(playersFixture[1]))),
|
||||
new Game(new BigDecimal(3), new Date(2022 - 1900, 11, 12), Game.Difficulty.EASY, 15, 16, 2, 9, new ArrayList<>(Arrays.asList(playersFixture[2])))
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_constructor() {
|
||||
new Game();
|
||||
new Game(Game.Difficulty.EASY, 17, 3, 6);
|
||||
new Game(new BigDecimal(1), new Date(2023, 12, 25), Game.Difficulty.EASY, 17, 3, 6, new ArrayList<Player>());
|
||||
new Game(new BigDecimal(2), new Date(2024, 3, 26), Game.Difficulty.HARD, 52, 4, 13, new ArrayList<Player>());
|
||||
new Game(Game.Difficulty.EASY, 10, 17, 3, 6);
|
||||
new Game(new BigDecimal(1), new Date(2023, 12, 25), Game.Difficulty.EASY,15, 17, 3, 6, new ArrayList<Player>());
|
||||
new Game(new BigDecimal(2), new Date(2024, 3, 26), Game.Difficulty.HARD, 20, 52, 4, 13, new ArrayList<Player>());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_constructor_throwIllegalArgumentExceptionOnInvalidValues() {
|
||||
int[] INCORRECT_VALUES = {Integer.MIN_VALUE, -2, 0, 14, Integer.MAX_VALUE};
|
||||
for (int incorrect_value : INCORRECT_VALUES) {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 4 * incorrect_value, 4, incorrect_value));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY,10, 4 * incorrect_value, 4, incorrect_value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,16 +59,25 @@ class GameTest {
|
||||
void test_constructor_throwIllegalArgumentExceptionOnInvalidColors() {
|
||||
int[] INCORRECT_VALUES = {Integer.MIN_VALUE, -9, -2, 0, 5, 8, Integer.MAX_VALUE};
|
||||
for (int incorrect_value : INCORRECT_VALUES) {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 4 * incorrect_value, incorrect_value, 13));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 4 * incorrect_value, incorrect_value, 13));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_constructor_throwIllegalArgumentExceptionOnInvalidNbRounds() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 0, 4, 8));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 33, 4, 8));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, -5, 4, 8));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 13, 2, 6));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 0, 4, 8));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 33, 4, 8));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, -5, 4, 8));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, 10, 13, 2, 6));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void test_constructor_throwIllegalArgumentExceptionOnInvalidTimers() {
|
||||
int[] INCORRECT_VALUES = {Integer.MIN_VALUE, -9, -2, 0, 5, 61, Integer.MAX_VALUE};
|
||||
for (int incorrect_value : INCORRECT_VALUES) {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Game(Game.Difficulty.EASY, incorrect_value, 13, 2, 6));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,6 +128,29 @@ class GameTest {
|
||||
assertEquals(Game.Difficulty.HARD, game.getDifficulty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_getTimer() {
|
||||
final HashMap<Game, Integer> TESTS = new HashMap<>() {{
|
||||
put(fixture[0], 50);
|
||||
put(fixture[1], 10);
|
||||
put(fixture[2], 15);
|
||||
}};
|
||||
|
||||
for (Game game : TESTS.keySet()) {
|
||||
assertEquals(TESTS.get(game), game.getTimer());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_setTimer() {
|
||||
Game game = fixture[0];
|
||||
assertEquals(50, game.getTimer());
|
||||
|
||||
// Change the timer
|
||||
game.setTimer(25);
|
||||
assertEquals(25, game.getTimer());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_getNbRounds() {
|
||||
final HashMap<Game, Integer> TESTS = new HashMap<>() {{
|
||||
@@ -185,18 +223,9 @@ class GameTest {
|
||||
@Test
|
||||
void test_getPlayers() {
|
||||
final HashMap<Game, ArrayList<Player>> TESTS = new HashMap<>() {{
|
||||
put(fixture[0], new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(1), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}});
|
||||
put(fixture[1], new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(3), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}});
|
||||
put(fixture[2], new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(4), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(5), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(6), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}});
|
||||
put(fixture[0], new ArrayList<>(Arrays.asList(playersFixture[0])));
|
||||
put(fixture[1], new ArrayList<>(Arrays.asList(playersFixture[1])));
|
||||
put(fixture[2], new ArrayList<>(Arrays.asList(playersFixture[2])));
|
||||
}};
|
||||
|
||||
for (Game game : TESTS.keySet()) {
|
||||
@@ -207,21 +236,11 @@ class GameTest {
|
||||
@Test
|
||||
void test_setPlayers() {
|
||||
Game game = fixture[0];
|
||||
assertEquals(new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(1), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}}, game.getPlayers());
|
||||
assertEquals(new ArrayList<>(Arrays.asList(playersFixture[0])), game.getPlayers());
|
||||
|
||||
// Add players
|
||||
ArrayList<Player> players = new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(1), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(3), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(4), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(5), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(6), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
}};
|
||||
game.setPlayers(players);
|
||||
assertEquals(players, game.getPlayers());
|
||||
game.setPlayers(new ArrayList<>(Arrays.asList(playersFixture[1])));
|
||||
assertEquals(new ArrayList<>(Arrays.asList(playersFixture[1])), game.getPlayers());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -240,16 +259,18 @@ class GameTest {
|
||||
@Test
|
||||
void test_addPlayer() {
|
||||
Game game = fixture[0];
|
||||
assertEquals(1, game.getPlayers().size());
|
||||
ArrayList<Player> players = new ArrayList<>(Arrays.asList(playersFixture[0]));
|
||||
assertEquals(players.size(), game.getPlayers().size());
|
||||
|
||||
Player player = new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5);
|
||||
Player player = new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 20, 17, 3, 6), new User(), 10, true, 5, 5, 5);
|
||||
|
||||
// Add a player
|
||||
game.addPlayer(player);
|
||||
assertEquals(2, game.getPlayers().size());
|
||||
players.add(player);
|
||||
assertEquals(players.size(), game.getPlayers().size());
|
||||
|
||||
// Check if the player is the same
|
||||
assertEquals(player, game.getPlayers().get(1));
|
||||
assertEquals(players, game.getPlayers());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -269,16 +290,16 @@ class GameTest {
|
||||
void test_sortPlayersByScore() {
|
||||
final HashMap<Game, ArrayList<Player>> TESTS = new HashMap<>() {{
|
||||
put(fixture[0], new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(1), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(playersFixture[0][0]);
|
||||
}});
|
||||
put(fixture[1], new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(2), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(3), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(playersFixture[1][1]);
|
||||
add(playersFixture[1][0]);
|
||||
}});
|
||||
put(fixture[2], new ArrayList<>() {{
|
||||
add(new Player(new BigDecimal(4), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(5), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(new Player(new BigDecimal(6), new Game(Game.Difficulty.EASY, 17, 3, 6), new User(), 10, true, 5, 5, 5));
|
||||
add(playersFixture[2][1]);
|
||||
add(playersFixture[2][2]);
|
||||
add(playersFixture[2][0]);
|
||||
}});
|
||||
}};
|
||||
|
||||
@@ -305,3 +326,4 @@ class GameTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package uppa.project.pojo;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -11,33 +12,51 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
class PlayerTest {
|
||||
|
||||
static Player[] fixture;
|
||||
static Game[] games;
|
||||
static User[] users;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
games = new Game[]{
|
||||
new Game(new BigDecimal(1), new Date(101, 2, 4), Game.Difficulty.EASY, 50,18,2,10,null),
|
||||
new Game(new BigDecimal(2), new Date(101, 2, 4), Game.Difficulty.EASY, 10,18,4,13,null),
|
||||
new Game(new BigDecimal(3), new Date(101, 2, 4), Game.Difficulty.EASY, 20,18,3,6,null),
|
||||
new Game(new BigDecimal(4), new Date(101, 2, 4), Game.Difficulty.HARD,25,18,3,6,null),
|
||||
};
|
||||
|
||||
users = new User[]{
|
||||
new User("username1", "email1", "password1", new Date(), User.Gender.MALE),
|
||||
new User("username2", "email2", "password2", new Date(), User.Gender.FEMALE),
|
||||
new User("username3", "email3", "password3", new Date(), User.Gender.OTHER),
|
||||
new User("username4", "email4", "password4", new Date(100, 1, 1), User.Gender.OTHER)
|
||||
|
||||
};
|
||||
}
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
fixture = new Player[]{
|
||||
new Player(
|
||||
new BigDecimal(1),
|
||||
new Game(new BigDecimal(1), new Date(101, 2, 4), Game.Difficulty.EASY,18,2,10,null),
|
||||
new User("username1", "email1", "password1", new Date(), User.Gender.MALE),
|
||||
games[0],
|
||||
users[0],
|
||||
10, true, 6,5,5
|
||||
),
|
||||
new Player(
|
||||
new BigDecimal(2),
|
||||
new Game(new BigDecimal(2), new Date(101, 2, 4), Game.Difficulty.EASY,18,4,13,null),
|
||||
new User("username2", "email2", "password2", new Date(), User.Gender.FEMALE),
|
||||
games[1],
|
||||
users[1],
|
||||
20, false, 4,2,1
|
||||
),
|
||||
new Player(
|
||||
new BigDecimal(3),
|
||||
new Game(new BigDecimal(3), new Date(101, 2, 4), Game.Difficulty.EASY,18,3,6,null),
|
||||
new User("username3", "email3", "password3", new Date(), User.Gender.OTHER),
|
||||
games[2],
|
||||
users[2],
|
||||
15, true, 10,5,5
|
||||
),
|
||||
new Player(
|
||||
new BigDecimal(4),
|
||||
new Game(new BigDecimal(4), new Date(101, 2, 4), Game.Difficulty.HARD,18,3,6,null),
|
||||
new User("username4", "email4", "password4",
|
||||
new Date(100, 1, 1), User.Gender.OTHER),
|
||||
games[3],
|
||||
users[3],
|
||||
15, true, 10,5,5
|
||||
)
|
||||
};
|
||||
@@ -47,19 +66,13 @@ class PlayerTest {
|
||||
@Test
|
||||
void test_constructor() {
|
||||
new Player();
|
||||
new Player(new Game(Game.Difficulty.EASY,4,2,3), new User());
|
||||
new Player(new Game(Game.Difficulty.EASY,4,2,3), new User());
|
||||
new Player(new Game(Game.Difficulty.EASY,4,2,3), new User());
|
||||
new Player(new Game(Game.Difficulty.EASY,25,4,2,3), new User());
|
||||
new Player(new Game(Game.Difficulty.EASY,10,4,2,3), new User());
|
||||
new Player(new Game(Game.Difficulty.EASY,20,4,2,3), new User());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_getGame() {
|
||||
Game[] games = new Game[]{
|
||||
new Game(new BigDecimal(1), new Date(101, 2, 4), Game.Difficulty.EASY,18,2,10,null),
|
||||
new Game(new BigDecimal(2), new Date(101, 2, 4), Game.Difficulty.EASY,18,4,13,null),
|
||||
new Game(new BigDecimal(3), new Date(101, 2, 4), Game.Difficulty.EASY,18,3,6,null),
|
||||
};
|
||||
|
||||
final HashMap<Player, Game> TESTS = new HashMap<>() {{
|
||||
put(fixture[0], games[0]);
|
||||
put(fixture[1], games[1]);
|
||||
@@ -77,7 +90,7 @@ class PlayerTest {
|
||||
Game game = player.getGame();
|
||||
|
||||
// Set a new game
|
||||
Game newGame = new Game(new BigDecimal(4), new Date(), Game.Difficulty.HARD,20,4,5,null);
|
||||
Game newGame = new Game(new BigDecimal(4), new Date(), Game.Difficulty.HARD,10,20,4,5,null);
|
||||
player.setGame(newGame);
|
||||
|
||||
assertNotEquals(game, player.getGame());
|
||||
|
||||
@@ -23,26 +23,26 @@ class UserTest {
|
||||
playersFixture = new Player[][]{
|
||||
{
|
||||
new Player(new BigDecimal(1),
|
||||
new Game(Game.Difficulty.EASY, 20, 4, 13),
|
||||
new Game(Game.Difficulty.EASY, 50,20, 4, 13),
|
||||
new User(), 10, false, 10, 9, 5)
|
||||
},
|
||||
{
|
||||
new Player(new BigDecimal(2),
|
||||
new Game(Game.Difficulty.EASY, 20, 4, 13),
|
||||
new Game(Game.Difficulty.EASY, 50, 20, 4, 13),
|
||||
new User(), 10, true, 20, 17, 12),
|
||||
new Player(new BigDecimal(3),
|
||||
new Game(Game.Difficulty.EASY, 20, 4, 13),
|
||||
new Game(Game.Difficulty.EASY, 10, 20, 4, 13),
|
||||
new User(), 10, false, 15, 5, 2)
|
||||
},
|
||||
{
|
||||
new Player(new BigDecimal(4),
|
||||
new Game(Game.Difficulty.EASY, 20, 4, 13),
|
||||
new Game(Game.Difficulty.EASY, 50, 20, 4, 13),
|
||||
new User(), 10, true, 5, 3, 1),
|
||||
new Player(new BigDecimal(5),
|
||||
new Game(Game.Difficulty.EASY, 20, 4, 13),
|
||||
new Game(Game.Difficulty.EASY, 10, 20, 4, 13),
|
||||
new User(), 10, true, 16, 16, 10),
|
||||
new Player(new BigDecimal(6),
|
||||
new Game(Game.Difficulty.EASY, 20, 4, 13),
|
||||
new Game(Game.Difficulty.EASY, 15, 20, 4, 13),
|
||||
new User(), 10, true, 17, 11, 4)
|
||||
}
|
||||
};
|
||||
@@ -241,7 +241,7 @@ class UserTest {
|
||||
void test_addPlayedGame() {
|
||||
User user = fixture[0];
|
||||
Player player = new Player(new BigDecimal(4),
|
||||
new Game(Game.Difficulty.EASY, 20, 4, 13),
|
||||
new Game(Game.Difficulty.EASY, 10,20, 4, 13),
|
||||
new User(), 10, true, 5, 3, 1);
|
||||
user.addPlayedGame(player);
|
||||
ArrayList<Player> expected = new ArrayList<>(Arrays.asList(playersFixture[0]));
|
||||
|
||||
Reference in New Issue
Block a user