From 05045263c53b0b60f40d7b0654b90b98e538370c Mon Sep 17 00:00:00 2001 From: HeEb Date: Thu, 25 Jul 2024 21:09:53 -0500 Subject: [PATCH] encountering 3 errors --- pom.xml | 60 +++++++---- src/main/java/uta/cse3310/App.java | 76 +++++++------- src/main/java/uta/cse3310/Game.java | 33 ++++-- src/main/java/uta/cse3310/GameTimer.java | 5 +- src/main/java/uta/cse3310/Player.java | 10 +- src/main/java/uta/cse3310/Round.java | 18 +++- src/main/java/uta/cse3310/UserEvent.java | 1 + src/main/java/uta/cse3310/Word.java | 1 + src/main/java/uta/cse3310/WordList.java | 2 +- src/test/.DS_Store | Bin 6148 -> 6148 bytes src/test/Java/uta/cse3310/.DS_Store | Bin 6148 -> 6148 bytes src/test/Java/uta/cse3310/AppTest.java | 95 ++++++++++++++++++ src/test/Java/uta/cse3310/GameTest.java | 79 ++++++++++++--- src/test/Java/uta/cse3310/GameTimerTest.java | 60 ++++++++--- src/test/Java/uta/cse3310/HttpServerTest.java | 31 +++++- src/test/Java/uta/cse3310/PlayerTest.java | 32 ++++-- src/test/Java/uta/cse3310/PlayerTypeTest.java | 24 +++++ src/test/Java/uta/cse3310/RoundTest.java | 48 +++++++-- .../Java/uta/cse3310/ServerEventTest.java | 37 +++++++ src/test/Java/uta/cse3310/StakeTest.java | 33 ++++-- src/test/Java/uta/cse3310/StatisticsTest.java | 42 ++++++-- src/test/Java/uta/cse3310/UserEventTest.java | 39 ++++--- src/test/Java/uta/cse3310/WordListTest.java | 42 +++++++- src/test/Java/uta/cse3310/WordTest.java | 56 +++++++++++ src/test/resources/test_words.txt | 5 + 25 files changed, 672 insertions(+), 157 deletions(-) create mode 100644 src/test/Java/uta/cse3310/AppTest.java create mode 100644 src/test/Java/uta/cse3310/PlayerTypeTest.java create mode 100644 src/test/Java/uta/cse3310/ServerEventTest.java create mode 100644 src/test/Java/uta/cse3310/WordTest.java create mode 100644 src/test/resources/test_words.txt diff --git a/pom.xml b/pom.xml index 1641b8a..c7b739f 100644 --- a/pom.xml +++ b/pom.xml @@ -20,29 +20,52 @@ + - junit - junit - 3.8.1 + org.junit.jupiter + junit-jupiter-engine + 5.7.0 + test + + + org.junit.jupiter + junit-jupiter-api + 5.7.0 + test + + + org.junit.jupiter + junit-jupiter-params + 5.7.0 + test + + + + + org.mockito + mockito-core + 3.7.7 + test - - com.google.code.gson - gson - 2.9.1 - - - net.freeutils - jlhttp - 2.6 - + + + com.google.code.gson + gson + 2.9.1 + - - org.java-websocket - Java-WebSocket - 1.5.4 - + + net.freeutils + jlhttp + 2.6 + + + org.java-websocket + Java-WebSocket + 1.5.4 + @@ -97,3 +120,4 @@ + diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index 8ba33b9..59ee4d3 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -35,47 +35,48 @@ public App(int port, Draft_6455 draft) { super(new InetSocketAddress(port), Collections.singletonList(draft)); } - @Override - public void onOpen(WebSocket conn, ClientHandshake handshake) { - connectionId++; - System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected"); - - ServerEvent event = new ServerEvent(); - Game game = null; - - for (Game g : activeGames) { - if (g.getPlayers().size() < 4) { // Assuming a maximum of 4 players per game - game = g; - System.out.println("found a match"); - break; - } + @Override +public void onOpen(WebSocket conn, ClientHandshake handshake) { + connectionId++; + System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected"); + + ServerEvent event = new ServerEvent(); + Game game = null; + + for (Game g : activeGames) { + if (g.getPlayers().size() < 4) { // Assuming a maximum of 4 players per game + game = g; + System.out.println("found a match"); + break; } + } - if (game == null) { - List players = new ArrayList<>(); // Add logic to initialize players - try { - game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stakes.txt", new Statistics()); - } catch (IOException e) { - e.printStackTrace(); - return; - } - game.setGameId(gameId++); - activeGames.add(game); - System.out.println("creating a new Game"); - } else { - System.out.println("joining an existing game"); + if (game == null) { + List players = new ArrayList<>(); + players.add(new Player("Player1")); // Initialize at least one player + try { + game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stakes.txt", new Statistics()); + } catch (IOException e) { + e.printStackTrace(); + return; } + game.setGameId(gameId++); + activeGames.add(game); + System.out.println("creating a new Game"); + } else { + System.out.println("joining an existing game"); + } - conn.setAttachment(game); - Gson gson = new Gson(); - String jsonString = gson.toJson(event); - conn.send(jsonString); - System.out.println("> " + jsonString); + conn.setAttachment(game); // This line ensures that the game is attached to the connection + Gson gson = new Gson(); + String jsonString = gson.toJson(event); + conn.send(jsonString); + System.out.println("> " + jsonString); - jsonString = gson.toJson(game); - System.out.println("< " + jsonString); - broadcast(jsonString); - } + jsonString = gson.toJson(game); + System.out.println("< " + jsonString); + broadcast(jsonString); +} @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { @@ -94,7 +95,7 @@ public void onMessage(WebSocket conn, String message) { System.out.println("< " + message); Gson gson = new GsonBuilder().create(); UserEvent event = gson.fromJson(message, UserEvent.class); - + Game game = conn.getAttachment(); if (game != null) { game.update(event); @@ -103,7 +104,6 @@ public void onMessage(WebSocket conn, String message) { broadcast(jsonString); } } - @Override public void onMessage(WebSocket conn, ByteBuffer message) { diff --git a/src/main/java/uta/cse3310/Game.java b/src/main/java/uta/cse3310/Game.java index c335f37..152c1d1 100644 --- a/src/main/java/uta/cse3310/Game.java +++ b/src/main/java/uta/cse3310/Game.java @@ -42,16 +42,15 @@ public void removePlayer(WebSocket conn) { } public void update(UserEvent event) { - switch (event.type) { + switch (event.getAction()) { case "PLAY": playRound(); break; - // Add cases for other event types as needed + // Add cases for other actions as needed default: - System.out.println("Unknown event type: " + event.type); + System.out.println("Unknown action: " + event.getAction()); } } - public void startGame() { System.out.println("Game started with " + players.size() + " players."); @@ -76,23 +75,30 @@ public void playRound() { System.out.println("Game is not active."); return; } - + Round currentRound = rounds.get(currentRoundIndex - 1); // -1 because we incremented in startNextRound while (currentRound.isRoundActive()) { currentRound.nextTurn(); } - - // After the round ends, start the next round - startNextRound(); + + if (!currentRound.isRoundActive()) { + startNextRound(); + if (currentRoundIndex >= rounds.size()) { + System.out.println("All rounds completed."); + isGameActive = false; + determineWinner(); + } + } } + public void endGame(Player player) { + stats.incrementGamesPlayed(); stats.updateWinner(player); } public Statistics getStatistics() { return stats; } - private void determineWinner() { Player winner = null; int highestScore = 0; @@ -102,8 +108,9 @@ private void determineWinner() { winner = player; } } - + if (winner != null) { + winner.setWinner(true); // Ensure that the winner is marked correctly System.out.println("The winner is " + winner.getName() + " with a score of " + highestScore); stats.updateWinner(winner); } else { @@ -122,8 +129,14 @@ public void resetGame() { } } } + + public boolean isGameActive() { + return isGameActive; + } } + + diff --git a/src/main/java/uta/cse3310/GameTimer.java b/src/main/java/uta/cse3310/GameTimer.java index 872e05d..437dfff 100644 --- a/src/main/java/uta/cse3310/GameTimer.java +++ b/src/main/java/uta/cse3310/GameTimer.java @@ -4,13 +4,14 @@ import java.util.TimerTask; public class GameTimer { - - private Timer timer; + private transient Timer timer; + // private Timer timer; private long startTime; private long elapsedTime; private boolean isRunning; public GameTimer() { + this.timer = new Timer(); this.elapsedTime = 0; this.isRunning = false; diff --git a/src/main/java/uta/cse3310/Player.java b/src/main/java/uta/cse3310/Player.java index d7d8f36..6e4d002 100644 --- a/src/main/java/uta/cse3310/Player.java +++ b/src/main/java/uta/cse3310/Player.java @@ -6,15 +6,17 @@ public class Player { private String id; private String name; private int score; - private boolean winner; + private boolean iswinner; private GameTimer timer; // Updated reference + + public boolean isWinner() { - return winner; + return iswinner; } - public void setWinner(boolean winner) { - this.winner = winner; + public void setWinner(boolean iswinner) { + this.iswinner = iswinner; } public Player(String name) { diff --git a/src/main/java/uta/cse3310/Round.java b/src/main/java/uta/cse3310/Round.java index 99eb7ed..ab355e8 100644 --- a/src/main/java/uta/cse3310/Round.java +++ b/src/main/java/uta/cse3310/Round.java @@ -25,6 +25,7 @@ private String[] loadWords(String filePath) throws IOException { public void startRound() { System.out.println("New round started. Word to guess: " + word.getWordProgress()); + this.isRoundActive = true; } public void nextTurn() { @@ -36,7 +37,8 @@ public void nextTurn() { Player currentPlayer = players.get(currentPlayerIndex); System.out.println("Current player: " + currentPlayer.getName()); - char guessedLetter = 'a'; // Replace with actual guess from player input + // Simulate a guess with random letter, replace with actual user input logic + char guessedLetter = getRandomLetter(); boolean isCorrect = word.guessLetter(guessedLetter); if (isCorrect) { System.out.println("Correct guess!"); @@ -52,17 +54,22 @@ public void nextTurn() { } } + private char getRandomLetter() { + return (char) ('a' + new java.util.Random().nextInt(26)); + } + public String getCurrentWordProgress() { return word.getWordProgress(); } - /* public String getCurrentStake() { - return stake.getCurrentStake(); - }*/ + // Commented out as it does not exist + // public String getCurrentStake() { + // return stake.getCurrentStake(); + // } public void resetRound() throws IOException { this.word.reset(); - // this.stake.reset(); + // this.stake.reset(); this.currentPlayerIndex = 0; this.isRoundActive = true; } @@ -75,3 +82,4 @@ public boolean isRoundActive() { + diff --git a/src/main/java/uta/cse3310/UserEvent.java b/src/main/java/uta/cse3310/UserEvent.java index d1c2f75..11930bc 100644 --- a/src/main/java/uta/cse3310/UserEvent.java +++ b/src/main/java/uta/cse3310/UserEvent.java @@ -1,4 +1,5 @@ package uta.cse3310; +//package org.junit.jupiter.api; public class UserEvent { private PlayerType playerType; diff --git a/src/main/java/uta/cse3310/Word.java b/src/main/java/uta/cse3310/Word.java index 7f54858..edc0197 100644 --- a/src/main/java/uta/cse3310/Word.java +++ b/src/main/java/uta/cse3310/Word.java @@ -60,3 +60,4 @@ public void reset() { } } + diff --git a/src/main/java/uta/cse3310/WordList.java b/src/main/java/uta/cse3310/WordList.java index 3c422f9..26cd7ae 100644 --- a/src/main/java/uta/cse3310/WordList.java +++ b/src/main/java/uta/cse3310/WordList.java @@ -30,7 +30,7 @@ public List getWords() { public static void main(String[] args) { try { - WordList wordList = new WordList("path/to/words.txt"); + WordList wordList = new WordList("src/main/resources/words.txt"); System.out.println("Random word: " + wordList.getRandomWord()); } catch (IOException e) { System.err.println("Error loading words: " + e.getMessage()); diff --git a/src/test/.DS_Store b/src/test/.DS_Store index c4e6c5cd91f0f5d324026d43054d4d56b3238764..ff42965fd8e202685e259db1be1094d7e39e87b9 100644 GIT binary patch delta 322 zcmZoMXfc=|#>B!ku~2NHo+2aP#(>?7i#IScG4gHZVd`ddK1}r)zJ1`xu$0DASZWx@LpIZRd2O+Si%FTCi zf!fERC)lB0qj}5`yFI!2DL7S{pn9SpgBYtPcQ9|<%+A5j0Suwd3z@$&Pv#eKzbA-qmW&n(zNk9Mq delta 68 zcmZoMXfc=|#>B)qu~2NHo+2ar#(>?7jO?3vSh^V}r?M~FtiW-Oc{4i)KL=3FWa zlYvp|KNv7DOy*(vskjEl11c3}2w=!zNMxu4npDIP0;CHVQh^$CIYk(P!D3~TudwQ~ p{sgLBv)PGNn30p0A)Fx}s5k{=J`ZCzNCsl!#KP^H**X650{~*7G7SI# delta 57 zcmZoMXfc@JFUrZlz`)4BAi%&7!jQ^P%uvFh$B?yIkYhPBBj;onmY players; + private Statistics stats; + private static final String TEST_WORD_FILE_PATH = "src/test/resources/test_words.txt"; + private static final String TEST_STAKE_FILE_PATH = "src/test/resources/test_stakes.txt"; @BeforeEach - public void setUp() throws Exception { - player1 = new Player("Player 1"); - player2 = new Player("Player 2"); + public void setUp() throws IOException { players = new ArrayList<>(); - players.add(player1); - players.add(player2); - game = new Game(players, "path/to/words.txt", "path/to/stakes.txt", new Statistics()); + players.add(new Player("Player1")); + players.add(new Player("Player2")); + stats = new Statistics(); + game = new Game(players, TEST_WORD_FILE_PATH, TEST_STAKE_FILE_PATH, stats); } @Test public void testGameInitialization() { + assertNotNull(game.getPlayers()); assertEquals(2, game.getPlayers().size()); + assertEquals(stats, game.getStatistics()); + } + + @Test + public void testStartGame() { + game.startGame(); + assertTrue(game.isGameActive()); + } + + @Test + public void testStartNextRound() { + game.startGame(); + game.startNextRound(); + assertTrue(game.isGameActive()); + } + + @Test + public void testPlayRound() { + game.startGame(); + game.playRound(); + assertFalse(game.isGameActive()); // Ensure the game ends after one full round } @Test - public void testPlayerScores() { - player1.addScore(100); - assertEquals(100, player1.getScore()); - player2.addScore(200); - assertEquals(200, player2.getScore()); + public void testEndGame() { + players.get(0).setWinner(true); // Mark Player 1 as the winner + game.endGame(players.get(0)); + assertEquals(1, stats.getGamesWon()); + assertEquals(1, stats.getGamesPlayed()); + } + + @Test + public void testResetGame() throws IOException { + game.startGame(); + game.resetGame(); + assertTrue(game.isGameActive()); + assertEquals(0, game.getStatistics().getGamesPlayed()); + } + + @Test + public void testDetermineWinner() throws Exception { + players.get(0).addScore(10); // Player 1 should be the winner + // Use reflection to call the private method + Method determineWinnerMethod = Game.class.getDeclaredMethod("determineWinner"); + determineWinnerMethod.setAccessible(true); + determineWinnerMethod.invoke(game); + + assertTrue(players.get(0).isWinner()); + assertFalse(players.get(1).isWinner()); + assertEquals(1, stats.getGamesWon()); } } + + + + + diff --git a/src/test/Java/uta/cse3310/GameTimerTest.java b/src/test/Java/uta/cse3310/GameTimerTest.java index 515391e..6bb3f5a 100644 --- a/src/test/Java/uta/cse3310/GameTimerTest.java +++ b/src/test/Java/uta/cse3310/GameTimerTest.java @@ -2,36 +2,68 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.*; public class GameTimerTest { - private GameTimer timer; + private GameTimer gameTimer; @BeforeEach public void setUp() { - timer = new GameTimer(); + gameTimer = new GameTimer(); // Ensure the variable name is consistent + } + + @Test + public void testInitialConditions() { + assertEquals(0, gameTimer.getElapsedTime()); + assertFalse(gameTimer.isRunning()); + } + + @Test + public void testStart() { + gameTimer.start(); // Use the correct variable name + assertTrue(gameTimer.isRunning()); + } + + @Test + public void testStop() { + gameTimer.start(); // Use the correct variable name + gameTimer.stop(); + assertFalse(gameTimer.isRunning()); } @Test - public void testTimerStart() { - timer.start(); - assertTrue(timer.isRunning()); + public void testReset() { + gameTimer.start(); // Use the correct variable name + gameTimer.reset(); + assertFalse(gameTimer.isRunning()); + assertEquals(0, gameTimer.getElapsedTime()); } @Test - public void testTimerStop() { - timer.start(); - timer.stop(); - assertFalse(timer.isRunning()); + public void testStartWhileRunning() throws InterruptedException { + gameTimer.start(); // Use the correct variable name + Thread.sleep(2000); // Sleep for 2 seconds to allow the timer to run + + long elapsedTime = gameTimer.getElapsedTime(); + gameTimer.start(); // Attempt to start the timer again while it's running + + assertTrue(gameTimer.isRunning()); + Thread.sleep(1000); // Sleep for 1 second to check if the timer continues + + assertTrue(gameTimer.getElapsedTime() >= elapsedTime + 1); + gameTimer.stop(); // Stop the timer to clean up for other tests } @Test - public void testTimerElapsedTime() throws InterruptedException { - timer.start(); - Thread.sleep(1000); - timer.stop(); - assertTrue(timer.getElapsedTime() >= 1); + public void testStopWhileStopped() { + gameTimer.stop(); // Attempt to stop the timer while it's not running + + assertFalse(gameTimer.isRunning()); + assertEquals(0, gameTimer.getElapsedTime()); } } + + diff --git a/src/test/Java/uta/cse3310/HttpServerTest.java b/src/test/Java/uta/cse3310/HttpServerTest.java index 96d5898..9241cfe 100644 --- a/src/test/Java/uta/cse3310/HttpServerTest.java +++ b/src/test/Java/uta/cse3310/HttpServerTest.java @@ -2,19 +2,48 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.io.IOException; + import static org.junit.jupiter.api.Assertions.*; public class HttpServerTest { private HttpServer httpServer; + @TempDir + File tempDir; + @BeforeEach public void setUp() { - httpServer = new HttpServer(8080, "./html"); + httpServer = new HttpServer(9105, tempDir.getAbsolutePath()); } @Test public void testHttpServerInitialization() { assertNotNull(httpServer); + assertEquals(9105, httpServer.port); + assertEquals(tempDir.getAbsolutePath(), httpServer.dirname); + } + + @Test + public void testHttpServerStart() { + // Create a file in the temporary directory to simulate HTML content + File indexFile = new File(tempDir, "index.html"); + try { + assertTrue(indexFile.createNewFile()); + } catch (IOException e) { + fail("Failed to create temporary index.html file"); + } + + // Start the server + httpServer.start(); + + // Ensure the server is running (this is a basic check; ideally, you'd check the actual server status) + assertTrue(indexFile.exists()); + assertTrue(tempDir.canRead()); } } + diff --git a/src/test/Java/uta/cse3310/PlayerTest.java b/src/test/Java/uta/cse3310/PlayerTest.java index a7bef95..655ec9d 100644 --- a/src/test/Java/uta/cse3310/PlayerTest.java +++ b/src/test/Java/uta/cse3310/PlayerTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.*; public class PlayerTest { @@ -10,30 +11,43 @@ public class PlayerTest { @BeforeEach public void setUp() { - player = new Player("Test Player"); + player = new Player("TestPlayer"); } + @Test - public void testPlayerWinner() { - player.setWinner(true); - assertEquals(true, player.isWinner()); + public void testInitialConditions() { + assertEquals("TestPlayer", player.getName()); + assertEquals(0, player.getScore()); } @Test - public void testPlayerInitialization() { - assertEquals("Test Player", player.getName()); + public void testPlayerCreation() { + assertNotNull(player.getId()); + assertEquals("TestPlayer", player.getName()); assertEquals(0, player.getScore()); + assertFalse(player.isWinner()); } @Test public void testAddScore() { - player.addScore(100); - assertEquals(100, player.getScore()); + player.addScore(10); + assertEquals(10, player.getScore()); } @Test public void testResetScore() { - player.addScore(100); + player.addScore(10); player.resetScore(); assertEquals(0, player.getScore()); } + + @Test + public void testSetAndGetWinner() { + player.setWinner(true); + assertTrue(player.isWinner()); + + player.setWinner(false); + assertFalse(player.isWinner()); + } + } diff --git a/src/test/Java/uta/cse3310/PlayerTypeTest.java b/src/test/Java/uta/cse3310/PlayerTypeTest.java new file mode 100644 index 0000000..d971657 --- /dev/null +++ b/src/test/Java/uta/cse3310/PlayerTypeTest.java @@ -0,0 +1,24 @@ +package uta.cse3310; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class PlayerTypeTest { + + @Test + public void testPlayerTypeValues() { + PlayerType[] expectedValues = {PlayerType.NOPLAYER, PlayerType.XPLAYER, PlayerType.HUMAN}; + PlayerType[] actualValues = PlayerType.values(); + + assertArrayEquals(expectedValues, actualValues); + } + + @Test + public void testPlayerTypeValueOf() { + assertEquals(PlayerType.NOPLAYER, PlayerType.valueOf("NOPLAYER")); + assertEquals(PlayerType.XPLAYER, PlayerType.valueOf("XPLAYER")); + assertEquals(PlayerType.HUMAN, PlayerType.valueOf("HUMAN")); + } +} + diff --git a/src/test/Java/uta/cse3310/RoundTest.java b/src/test/Java/uta/cse3310/RoundTest.java index 87cda5e..82c14c8 100644 --- a/src/test/Java/uta/cse3310/RoundTest.java +++ b/src/test/Java/uta/cse3310/RoundTest.java @@ -1,26 +1,56 @@ -import uta.cse3310.Round; -import uta.cse3310.Word; -import uta.cse3310.Stake; +package uta.cse3310; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + public class RoundTest { + private Round round; + private List players; @BeforeEach - public void setUp() { - List players = new ArrayList<>(); - round = new Round(players, "word", "anotherWord"); // Use appropriate parameters + public void setUp() throws IOException { + players = new ArrayList<>(); + players.add(new Player("Player1")); + players.add(new Player("Player2")); + round = new Round(players, "src/test/resources/test_words.txt", "src/test/resources/test_stakes.txt"); + } + + @Test + public void testRoundInitialization() { + assertNotNull(round.getCurrentWordProgress()); + // Commented out as getCurrentStake() does not exist + // assertNotNull(round.getCurrentStake()); + } + + @Test + public void testStartRound() { + round.startRound(); + assertTrue(round.isRoundActive()); } @Test - public void testSomething() { - // Test code here + public void testNextTurn() { + round.startRound(); + round.nextTurn(); + assertTrue(round.isRoundActive()); + } + + @Test + public void testResetRound() throws IOException { + round.startRound(); + round.resetRound(); + assertTrue(round.isRoundActive()); + assertNotNull(round.getCurrentWordProgress()); } } + diff --git a/src/test/Java/uta/cse3310/ServerEventTest.java b/src/test/Java/uta/cse3310/ServerEventTest.java new file mode 100644 index 0000000..1264240 --- /dev/null +++ b/src/test/Java/uta/cse3310/ServerEventTest.java @@ -0,0 +1,37 @@ +package uta.cse3310; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ServerEventTest { + + private ServerEvent serverEvent; + + @BeforeEach + public void setUp() { + serverEvent = new ServerEvent(); + } + + @Test + public void testDefaultConstructor() { + assertNull(serverEvent.YouAre); + assertEquals(0, serverEvent.GameId); + } + + @Test + public void testParameterizedConstructor() { + ServerEvent event = new ServerEvent(PlayerType.HUMAN, 123); + assertEquals(PlayerType.HUMAN, event.YouAre); + assertEquals(123, event.GameId); + } + + @Test + public void testPublicFields() { + serverEvent.YouAre = PlayerType.XPLAYER; + serverEvent.GameId = 456; + assertEquals(PlayerType.XPLAYER, serverEvent.YouAre); + assertEquals(456, serverEvent.GameId); + } +} diff --git a/src/test/Java/uta/cse3310/StakeTest.java b/src/test/Java/uta/cse3310/StakeTest.java index bdf2149..7515b93 100644 --- a/src/test/Java/uta/cse3310/StakeTest.java +++ b/src/test/Java/uta/cse3310/StakeTest.java @@ -2,25 +2,43 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.junit.jupiter.api.Assertions.*; public class StakeTest { + private Stake stake; @BeforeEach public void setUp() { - stake = new Stake("initial stakes"); + stake = new Stake(); + } + + @Test + public void testDefaultConstructor() { + assertNull(stake.getCurrentStake()); } @Test - public void testGetStakes() { - assertEquals("initial stakes", stake.getStakes()); + public void testConstructorWithArgument() { + Stake stakeWithArgument = new Stake("High"); + assertEquals("High", stakeWithArgument.getCurrentStake()); } @Test - public void testSetStakes() { - stake.setStakes("new stakes"); - assertEquals("new stakes", stake.getStakes()); + public void testGetAndSetCurrentStake() { + stake.setCurrentStake("Low"); + assertEquals("Low", stake.getCurrentStake()); + + stake.setCurrentStake("Medium"); + assertEquals("Medium", stake.getCurrentStake()); + } + + @Test + public void testReset() { + stake.setCurrentStake("High"); + stake.reset(); + assertNull(stake.getCurrentStake()); } } @@ -30,3 +48,4 @@ public void testSetStakes() { + diff --git a/src/test/Java/uta/cse3310/StatisticsTest.java b/src/test/Java/uta/cse3310/StatisticsTest.java index 1091e91..9494574 100644 --- a/src/test/Java/uta/cse3310/StatisticsTest.java +++ b/src/test/Java/uta/cse3310/StatisticsTest.java @@ -2,34 +2,60 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.*; public class StatisticsTest { - private Statistics statistics; + + private Statistics stats; @BeforeEach public void setUp() { - statistics = new Statistics(); + stats = new Statistics(); + } + + @Test + public void testInitialConditions() { + assertEquals(0, stats.getGamesPlayed()); + assertEquals(0, stats.getGamesWon()); + assertEquals(0, stats.getGamesLost()); } @Test public void testIncrementGamesPlayed() { - statistics.incrementGamesPlayed(); - assertEquals(1, statistics.getGamesPlayed()); + stats.incrementGamesPlayed(); + assertEquals(1, stats.getGamesPlayed()); } @Test public void testIncrementGamesWon() { - statistics.incrementGamesWon(); - assertEquals(1, statistics.getGamesWon()); + stats.incrementGamesWon(); + assertEquals(1, stats.getGamesWon()); } @Test public void testIncrementGamesLost() { - statistics.incrementGamesLost(); - assertEquals(1, statistics.getGamesLost()); + stats.incrementGamesLost(); + assertEquals(1, stats.getGamesLost()); + } + + @Test + public void testUpdateWinner() { + Player player = new Player("TestPlayer"); + player.setWinner(true); + stats.updateWinner(player); + assertEquals(1, stats.getGamesWon()); + assertEquals(0, stats.getGamesLost()); + + Player player2 = new Player("TestPlayer2"); + player2.setWinner(false); + stats.updateWinner(player2); + assertEquals(1, stats.getGamesWon()); + assertEquals(1, stats.getGamesLost()); } } + + diff --git a/src/test/Java/uta/cse3310/UserEventTest.java b/src/test/Java/uta/cse3310/UserEventTest.java index 037253d..e5a3277 100644 --- a/src/test/Java/uta/cse3310/UserEventTest.java +++ b/src/test/Java/uta/cse3310/UserEventTest.java @@ -1,32 +1,46 @@ package uta.cse3310; -//package org.junit.jupiter.api; -//import org.junit.jupiter.api; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; -import uta.cse3310.PlayerType; // Correctly import PlayerType -import uta.cse3310.UserEvent; // Correctly import UserEvent +import static org.junit.jupiter.api.Assertions.*; public class UserEventTest { + private UserEvent userEvent; @BeforeEach public void setUp() { - userEvent = new UserEvent(PlayerType.NOPLAYER, 1); - userEvent.setPlayerId("player1"); - userEvent.setAction("someAction"); - userEvent.setValue("someValue"); + userEvent = new UserEvent(PlayerType.HUMAN, 1); } @Test - public void testUserEvent() { + public void testSetAndGetPlayerId() { + userEvent.setPlayerId("player1"); assertEquals("player1", userEvent.getPlayerId()); - assertEquals("someAction", userEvent.getAction()); + } + + @Test + public void testSetAndGetAction() { + userEvent.setAction("PLAY"); + assertEquals("PLAY", userEvent.getAction()); + } + + @Test + public void testSetAndGetValue() { + userEvent.setValue("someValue"); assertEquals("someValue", userEvent.getValue()); } + + @Test + public void testGetPlayerType() { + assertEquals(PlayerType.HUMAN, userEvent.getPlayerType()); + } + + @Test + public void testGetId() { + assertEquals(1, userEvent.getId()); + } } @@ -34,3 +48,4 @@ public void testUserEvent() { + diff --git a/src/test/Java/uta/cse3310/WordListTest.java b/src/test/Java/uta/cse3310/WordListTest.java index 8a8f2a7..d7d3106 100644 --- a/src/test/Java/uta/cse3310/WordListTest.java +++ b/src/test/Java/uta/cse3310/WordListTest.java @@ -2,20 +2,52 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + import static org.junit.jupiter.api.Assertions.*; public class WordListTest { private WordList wordList; + private static final String TEST_FILE_PATH = "src/test/resources/test_words.txt"; @BeforeEach - public void setUp() { - wordList = new WordList("path/to/words.txt"); + public void setUp() throws IOException { + // Create a temporary file with test words + List words = List.of("apple", "banana", "cherry", "date", "elderberry"); + Files.write(Path.of(TEST_FILE_PATH), words); + + wordList = new WordList(TEST_FILE_PATH); + } + + @Test + public void testLoadWords() { + List words = wordList.getWords(); + assertNotNull(words); + assertEquals(5, words.size()); + assertTrue(words.contains("apple")); + assertTrue(words.contains("banana")); + assertTrue(words.contains("cherry")); + assertTrue(words.contains("date")); + assertTrue(words.contains("elderberry")); } @Test - public void testWordListLoad() { - assertNotNull(wordList.getWords()); - assertTrue(wordList.getWords().size() > 0); + public void testGetRandomWord() { + String randomWord = wordList.getRandomWord(); + assertNotNull(randomWord); + assertTrue(wordList.getWords().contains(randomWord)); + } + + @Test + public void testGetWords() { + List words = wordList.getWords(); + assertNotNull(words); + assertEquals(5, words.size()); } } + diff --git a/src/test/Java/uta/cse3310/WordTest.java b/src/test/Java/uta/cse3310/WordTest.java new file mode 100644 index 0000000..4bc983e --- /dev/null +++ b/src/test/Java/uta/cse3310/WordTest.java @@ -0,0 +1,56 @@ +package uta.cse3310; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class WordTest { + + private Word word; + private String[] wordList = {"apple", "banana", "cherry"}; + + @BeforeEach + public void setUp() { + word = new Word(wordList); + } + + @Test + public void testGetSelectedWord() { + String selectedWord = word.getSelectedWord(); + assertTrue(selectedWord.equals("apple") || selectedWord.equals("banana") || selectedWord.equals("cherry")); + } + + @Test + public void testGuessLetter() { + String selectedWord = word.getSelectedWord(); + boolean result = word.guessLetter(selectedWord.charAt(0)); + assertTrue(result); + } + + @Test + public void testGetWordProgress() { + String selectedWord = word.getSelectedWord(); + word.guessLetter(selectedWord.charAt(0)); + String progress = word.getWordProgress(); + assertTrue(progress.contains(String.valueOf(selectedWord.charAt(0)))); + } + + @Test + public void testIsFullyGuessed() { + String selectedWord = word.getSelectedWord(); + for (char c : selectedWord.toCharArray()) { + word.guessLetter(c); + } + assertTrue(word.isFullyGuessed()); + } + + @Test + public void testReset() { + String initialWord = word.getSelectedWord(); + word.reset(); + String newWord = word.getSelectedWord(); + assertNotEquals(initialWord, newWord, "The selected word should change after reset"); + } +} + + diff --git a/src/test/resources/test_words.txt b/src/test/resources/test_words.txt new file mode 100644 index 0000000..7e092e5 --- /dev/null +++ b/src/test/resources/test_words.txt @@ -0,0 +1,5 @@ +apple +banana +cherry +date +elderberry