Skip to content

Commit

Permalink
Add JUNIT TEST FOR CLASSES
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypub2002 committed Apr 16, 2024
1 parent 018c898 commit e2f0820
Show file tree
Hide file tree
Showing 28 changed files with 227 additions and 173 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>

<build>
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/uta/cse3310/Chat.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/uta/cse3310/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ public void start() {

}



}
21 changes: 0 additions & 21 deletions src/main/java/uta/cse3310/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,7 @@ public int insertFillerChar(char[][] matrix){
public char[][] wordSearchMatrix(String filename){
return matrix;
}


//TEST CASE BELOW:
public void testMatrix(){
char[][] grid = new char[50][50];

List<String> wordBank = new ArrayList<String>();

wordBank.add("Ant");
wordBank.add("Zebra");
wordBank.add("Baboon");

//Matrix wordSearch = new Matrix(grid);

//wordSearch.horizontalWordInsert(false,null, grid);

//assertTrue(gridHasWords(grid, wordBank));
}

boolean gridHasWords(char[][] grid, List<String> wordBank){
return false;
}


}
5 changes: 5 additions & 0 deletions src/test/java/uta/cse3310/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uta.cse3310;

public @interface Before {

}
41 changes: 38 additions & 3 deletions src/test/java/uta/cse3310/GameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,44 @@
import junit.framework.TestSuite;
import org.java_websocket.WebSocket;

import junit.framework.TestCase;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Random;

public class GameTest extends TestCase {
public void testApp(){
assertTrue( true );
}

public void testGameInitialization() {
Game game = new Game();

assertTrue(game.isOpen);
assertEquals(0, game.numPlayers);
assertEquals(30, game.matrix.length);
assertEquals(30, game.matrix[0].length);
assertEquals(30, game.colorGrid.length);
assertEquals(30, game.colorGrid[0].length);
assertEquals(0, game.temps.size());
assertEquals(0, game.scores.size());
assertEquals(0, game.names.size());
}

public void testAddEntries() {
Game game = new Game();
Player player1 = Mockito.mock(Player.class);
Player player2 = Mockito.mock(Player.class);

game.addEntries(player1);
game.addEntries(player2);

assertEquals(2, game.scores.size());
assertEquals(2, game.names.size());
assertEquals(2, game.temps.size());
assertEquals(-1, (int) game.temps.get(0)[0]); // Check if initialized to -1
assertEquals(-1, (int) game.temps.get(0)[1]); // Check if initialized to -1
assertEquals(-1, (int) game.temps.get(1)[0]); // Check if initialized to -1
assertEquals(-1, (int) game.temps.get(1)[1]); // Check if initialized to -1
assertEquals(2, game.numPlayers);
}

}
67 changes: 67 additions & 0 deletions src/test/java/uta/cse3310/LobbyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package uta.cse3310;
import junit.framework.TestCase;
import java.util.ArrayList;
import org.java_websocket.WebSocket;
import org.mockito.Mockito;

public class LobbyTest extends TestCase {

public void testLobbyInitialization() {
ArrayList<Player> players = new ArrayList<>();
WebSocket socket1 = Mockito.mock(WebSocket.class);
WebSocket socket2 = Mockito.mock(WebSocket.class);
Player player1 = new Player("Alice", socket1);
Player player2 = new Player("Bob", socket2);
players.add(player1);
players.add(player2);

Lobby lobby = new Lobby(players);

assertEquals(2, lobby.numPlayers);
assertEquals(0, lobby.numReady);
assertEquals(0, lobby.gamesAvailable);
assertTrue(lobby.playerNames.contains("Alice"));
assertTrue(lobby.playerNames.contains("Bob"));
assertFalse(lobby.playerStatuses.get(0)); // Alice's status
assertFalse(lobby.playerStatuses.get(1)); // Bob's status
}

public void testUpdateLobby() {
ArrayList<Player> players = new ArrayList<>();
WebSocket socket1 = Mockito.mock(WebSocket.class);
WebSocket socket2 = Mockito.mock(WebSocket.class);
Player player1 = new Player("Alice", socket1);
Player player2 = new Player("Bob", socket2);
players.add(player1);
players.add(player2);

Lobby lobby = new Lobby(players);

// Update players' readiness
player1.isReady = true;
player2.isReady = false;

lobby.updateLobby(players);

assertEquals(2, lobby.numPlayers);
assertEquals(1, lobby.numReady);
assertEquals(0, lobby.gamesAvailable);
assertTrue(lobby.playerNames.contains("Alice"));
assertTrue(lobby.playerNames.contains("Bob"));
assertTrue(lobby.playerStatuses.get(0)); // Alice's status
assertFalse(lobby.playerStatuses.get(1)); // Bob's status
}

public void testUpdateGamesAvailable() {
Game[] games = new Game[3];
games[0] = new Game(); // Assume isOpen is true for the first game
games[1] = new Game(); // Assume isOpen is false for the second game
games[2] = new Game(); // Assume isOpen is true for the third game

Lobby lobby = new Lobby(new ArrayList<>());
lobby.updateGamesAvailable(games);

assertEquals(2, lobby.gamesAvailable); // Only two games are open
}

}
35 changes: 35 additions & 0 deletions src/test/java/uta/cse3310/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uta.cse3310;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import junit.framework.TestCase;
import org.mockito.Mockito;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;

public class MainTest {

public void testOnCloseRemovesPlayerFromPlayerList() {
// Arrange: Create necessary objects and set up conditions
Main main = new Main(8080);
WebSocket socket1 = Mockito.mock(WebSocket.class);
List<Player> playerList = new ArrayList<>();
Player player1 = new Player("Alice", socket1);
Player player2 = new Player("Bob", socket1);
playerList.add(player1);
playerList.add(player2);

// Act: Call the method to be tested
main.onClose(socket1, 1001, "Normal closure", true);

// Assert: Check if the method behaves as expected
assert !playerList.contains(player1); // Player1 should be removed from the playerList
assert playerList.contains(player2); // Player2 should still be in the playerList
}

}
32 changes: 32 additions & 0 deletions src/test/java/uta/cse3310/MatrixTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uta.cse3310;
import junit.framework.TestCase;
import java.util.List;
import java.util.ArrayList;

public class MatrixTest extends TestCase
{
public void testGridHasWords() {
char[][] grid = new char[50][50];
List<String> wordBank = new ArrayList<String>();
wordBank.add("Ant");
wordBank.add("Zebra");
wordBank.add("Baboon");

boolean result = gridHasWords(grid, wordBank);

assertFalse(result); // Add your assertions based on the expected behavior
}


//Matrix wordSearch = new Matrix(grid);

//wordSearch.horizontalWordInsert(false,null, grid);

//assertTrue(gridHasWords(grid, wordBank));


boolean gridHasWords(char[][] grid, List<String> wordBank){
return false;
}

}
19 changes: 19 additions & 0 deletions src/test/java/uta/cse3310/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uta.cse3310;

import junit.framework.TestCase;
import org.java_websocket.WebSocket;
import org.mockito.Mockito;

public class PlayerTest extends TestCase {

public void testPlayerInitialization() {
WebSocket socket = Mockito.mock(WebSocket.class);
Player player = new Player("Alice", socket);

assertEquals("Alice", player.name);
assertEquals(0, player.score);
assertEquals(false, player.isReady);
assertEquals(socket, player.playerConn);
}

}
23 changes: 23 additions & 0 deletions src/test/java/uta/cse3310/UserMsgTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uta.cse3310;
import junit.framework.TestCase;

public class UserMsgTest extends TestCase {

public void testUserMsgInitialization() {
int[] startCoords = {1, 2};
int[] endCoords = {3, 4};

UserMsg userMsg = new UserMsg();
userMsg.code = 100;
userMsg.name = "Alice";
userMsg.message = "Hello!";
userMsg.startCoords = startCoords;
userMsg.endCoords = endCoords;
userMsg.gameNum = 1;

assertEquals(100, userMsg.code);
assertEquals("Alice", userMsg.name);
assertEquals("Hello!", userMsg.message);
assertEquals(1, userMsg.gameNum);
}
}
Binary file removed target/classes/uta/cse3310/Chat.class
Binary file not shown.
Binary file removed target/classes/uta/cse3310/Game.class
Binary file not shown.
Binary file removed target/classes/uta/cse3310/HttpServer.class
Binary file not shown.
Binary file removed target/classes/uta/cse3310/Lobby.class
Binary file not shown.
Binary file removed target/classes/uta/cse3310/Main.class
Binary file not shown.
Binary file removed target/classes/uta/cse3310/Matrix.class
Binary file not shown.
Binary file removed target/classes/uta/cse3310/Player.class
Binary file not shown.
15 changes: 0 additions & 15 deletions target/classes/uta/cse3310/Tasks.txt

This file was deleted.

Binary file removed target/classes/uta/cse3310/UserMsg.class
Binary file not shown.
Binary file removed target/cse3310_sp24_group_28-1.0-SNAPSHOT.jar
Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit e2f0820

Please sign in to comment.