4
4
import java .io .PrintStream ;
5
5
import java .util .HashMap ;
6
6
7
- import static org .junit .jupiter .api .Assertions .assertEquals ;
7
+ import static org .junit .jupiter .api .Assertions .assertEquals ; // Ensure LoadGame is imported
8
8
import static org .junit .jupiter .api .Assertions .assertTrue ;
9
9
import static org .junit .jupiter .api .Assertions .fail ;
10
10
import org .junit .jupiter .api .Test ;
11
11
12
+ import com .codedifferently .lesson16 .dylans_xbox .LoadGame ;
12
13
import com .codedifferently .lesson16 .dylans_xbox .Xbox ;
13
14
14
15
public class XboxTest {
15
16
16
17
@ Test
17
18
public void testAddGame () {
18
- Xbox xbox = new Xbox ("XBOXSERIESX" , 600 , "Black" , false );
19
- loadGames loader =
20
- new loadGames ("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv" );
19
+ LoadGame loader =
20
+ new LoadGame (
21
+ "src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv" ); // Ensure LoadGame
22
+
23
+ Xbox xbox = new Xbox ("XBOXSERIESX" , 600 , "Black" , true , false ); // Create an instance of Xbox
21
24
try {
22
25
loader .loadGamesFromFile (xbox );
23
26
} catch (Exception e ) {
24
27
e .printStackTrace ();
25
28
fail ("Exception occurred while loading games: " + e .getMessage ());
26
29
}
27
30
28
- HashMap <Integer , String > games =
29
- xbox .getGames (); // Fixed: Changed from `loadGames()` to `getGames()`
30
- assertTrue (games .containsKey (10 ));
31
- assertEquals (
32
- "Baldur's Gate 3" , games .get (10 )); // Fixed: Ensured the game name matches the CSV file
31
+ HashMap <Integer , String > games = xbox .getGames ();
32
+ assertTrue (games .containsKey (1 )); // Check that the first game is loaded (ID 1)
33
+ assertEquals ("Call of Duty" , games .get (1 )); // Ensure the first game matches the CSV
34
+ }
35
+
36
+ @ Test
37
+ public void testAddGameIfFull () {
38
+ Xbox xbox = new Xbox ("XBOXSERIESX" , 600 , "Black" , true , true ); // Set diskDriveFull to true
39
+ try {
40
+ xbox .inputGame (1 , "Call of Duty" );
41
+ fail ("Expected an exception to be thrown when adding a game to a full disk drive." );
42
+ } catch (Exception e ) {
43
+ assertEquals ("Disk drive is full. Cannot insert game." , e .getMessage ());
44
+ }
33
45
}
34
46
35
47
@ Test
@@ -45,16 +57,17 @@ public void testXboxModelEnumValues() {
45
57
public void testDiskDrive () {
46
58
Xbox xbox =
47
59
new Xbox (
48
- "XBOXONE" , 400 , "White" , true ); // Fixed: Set diskDrive to `false` to match the test
60
+ "XBOXONE" , 400 , "White" , true ,
61
+ false ); // Fixed: Set diskDrive to `false` to match the test
49
62
assertTrue (
50
63
xbox .DiskDrive (), "Disk drive should be empty" ); // Fixed: Corrected the assertion syntax
51
64
}
52
65
53
66
@ Test
54
67
public void testPrintAllGames () {
55
- Xbox xbox = new Xbox ("XBOXSERIESX" , 600 , "Black" , true );
56
- loadGames loader =
57
- new loadGames ("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv" );
68
+ Xbox xbox = new Xbox ("XBOXSERIESX" , 600 , "Black" , true , false );
69
+ LoadGame loader =
70
+ new LoadGame ("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv" );
58
71
try {
59
72
loader .loadGamesFromFile (xbox );
60
73
} catch (Exception e ) {
@@ -74,16 +87,57 @@ public void testPrintAllGames() {
74
87
75
88
// Assert: Verify the captured output
76
89
String expectedOutput =
77
- "Game ID: 1, Game Name: Call of Duty\n "
78
- + "Game ID: 2, Game Name: Elden Ring\n "
79
- + "Game ID: 3, Game Name: Minecraft\n "
80
- + "Game ID: 4, Game Name: Monster Hunter\n "
81
- + "Game ID: 5, Game Name: Fortnite\n "
82
- + "Game ID: 6, Game Name: Marvel Rivals\n "
83
- + "Game ID: 7, Game Name: Tetris\n "
84
- + "Game ID: 8, Game Name: Madden NFL\n "
85
- + "Game ID: 9, Game Name: Terraria\n "
86
- + "Game ID: 10, Game Name: Baldur's Gate 3\n " ;
90
+ """
91
+ Game ID: 1, Game Name: Call of Duty
92
+ Game ID: 2, Game Name: Elden Ring
93
+ Game ID: 3, Game Name: Minecraft
94
+ Game ID: 4, Game Name: Monster Hunter
95
+ Game ID: 5, Game Name: Fortnite
96
+ Game ID: 6, Game Name: Marvel Rivals
97
+ Game ID: 7, Game Name: Tetris
98
+ Game ID: 8, Game Name: Madden NFL
99
+ Game ID: 9, Game Name: Terraria
100
+ Game ID: 10, Game Name: Baldur's Gate 3
101
+ """ ;
87
102
assertEquals (expectedOutput .trim (), outputStream .toString ().trim ());
88
103
}
104
+
105
+ @ Test
106
+ public void testEjectGame () {
107
+ Xbox xbox = new Xbox ("XBOXSERIESX" , 600 , "Black" , true , false );
108
+ LoadGame loader =
109
+ new LoadGame ("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv" );
110
+ try {
111
+ loader .loadGamesFromFile (xbox );
112
+ } catch (Exception e ) {
113
+ e .printStackTrace ();
114
+ fail ("Exception occurred while loading games: " + e .getMessage ());
115
+ }
116
+
117
+ // Act: Eject a game
118
+ // Act: Eject a game
119
+ xbox .ejectGame (1 );
120
+
121
+ // Assert: Verify the game was ejected (if needed, check the state of the Xbox object)
122
+ assertTrue (!xbox .getGames ().containsKey (1 ), "Game with ID 1 should be removed from the games list." );
123
+ }
124
+
125
+ @ Test
126
+ public void testGetGames () {
127
+ Xbox xbox = new Xbox ("XBOXSERIESX" , 600 , "Black" , true , false );
128
+ LoadGame loader =
129
+ new LoadGame ("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv" );
130
+ try {
131
+ loader .loadGamesFromFile (xbox );
132
+ } catch (Exception e ) {
133
+ e .printStackTrace ();
134
+ fail ("Exception occurred while loading games: " + e .getMessage ());
135
+ }
136
+
137
+ // Act: Get the games
138
+ HashMap <Integer , String > games = xbox .getGames ();
139
+
140
+ // Assert: Verify the games are loaded correctly
141
+ assertEquals (10 , games .size (), "There should be 10 games loaded." );
142
+ }
89
143
}
0 commit comments