From cd1909c6a4117a65feb0a2f6e62bfcd9a4aa837b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 9 May 2016 11:47:52 +0200 Subject: Fix random test by resetting static state --- src/ch/epfl/xblast/server/Block.java | 14 +++++++++++++- test/ch/epfl/xblast/etape6/RandomTestGame.java | 11 ++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index 4f3d033..5cb662c 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -55,7 +55,19 @@ public enum Block { /** * Pseudo-random source for randomized behaviours. */ - private static final Random RANDOM_SOURCE = new Random(RANDOM_SEED); + private static Random RANDOM_SOURCE; + + /** + * Resets the random source used for bonus block spawning. + * Useful for deterministic tests. + */ + public static void resetRandomGenerator() { + RANDOM_SOURCE = new Random(RANDOM_SEED); + } + + static { + resetRandomGenerator(); + } /** * Corresponding bonus, or null. diff --git a/test/ch/epfl/xblast/etape6/RandomTestGame.java b/test/ch/epfl/xblast/etape6/RandomTestGame.java index 8192956..90435c8 100644 --- a/test/ch/epfl/xblast/etape6/RandomTestGame.java +++ b/test/ch/epfl/xblast/etape6/RandomTestGame.java @@ -9,6 +9,7 @@ import ch.epfl.xblast.server.GameState; import ch.epfl.xblast.server.Player; import ch.epfl.xblast.server.Player.DirectedPosition; import ch.epfl.xblast.server.debug.RandomEventGenerator; +import org.junit.Before; import org.junit.Test; import java.io.IOException; @@ -26,6 +27,7 @@ import static org.junit.Assert.assertTrue; * Checks that the player move as in the example for the random game provided as example * * @author EPFL + * @author Pacien TRAN-GIRARD (261948) */ public class RandomTestGame { @@ -56,7 +58,6 @@ public class RandomTestGame { new Player(PlayerID.PLAYER_4, lives, p4, maxBombs, bombRange)); } - @Test public void testPositionsRandomGame() throws InterruptedException, IOException, URISyntaxException { String fileName = getClass().getResource("/stage6files/randomgame_positions.txt").toURI().getPath(); @@ -83,4 +84,12 @@ public class RandomTestGame { player_positions.close(); } + /** + * Resets the random bonus generator. + */ + @Before + public void resetRandomSource() { + Block.resetRandomGenerator(); + } + } -- cgit v1.2.3