|
| 1 | +package com.codedifferently.lesson16.dylans_xboxTest; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.junit.jupiter.api.Assertions.fail; |
| 6 | + |
| 7 | +import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException; |
| 8 | +import com.codedifferently.lesson16.dylans_xbox.LoadGame; |
| 9 | +import com.codedifferently.lesson16.dylans_xbox.Xbox; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.PrintStream; |
| 12 | +import java.util.HashMap; |
| 13 | +import org.junit.jupiter.api.Test; // Ensure LoadGame is imported |
| 14 | + |
| 15 | +public class XboxTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testAddGame() { |
| 19 | + LoadGame loader = |
| 20 | + new LoadGame( |
| 21 | + "src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); // Ensure |
| 22 | + // LoadGame |
| 23 | + |
| 24 | + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // Create an instance of Xbox |
| 25 | + try { |
| 26 | + loader.loadGamesFromFile(xbox); |
| 27 | + } catch (Exception e) { |
| 28 | + e.printStackTrace(); |
| 29 | + fail("Exception occurred while loading games: " + e.getMessage()); |
| 30 | + } |
| 31 | + |
| 32 | + HashMap<Integer, String> games = xbox.getGames(); |
| 33 | + assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1) |
| 34 | + assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testAddGameIfFull() { |
| 39 | + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // false means not full yet |
| 40 | + LoadGame loader = |
| 41 | + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); |
| 42 | + |
| 43 | + try { |
| 44 | + loader.loadGamesFromFile(xbox); |
| 45 | + |
| 46 | + // Insert two games (disk limit) |
| 47 | + xbox.inputGame(1); |
| 48 | + xbox.inputGame(2); |
| 49 | + |
| 50 | + // This third insert should throw an exception |
| 51 | + xbox.inputGame(3); |
| 52 | + fail("Expected DiskDriveFullException to be thrown."); |
| 53 | + } catch (DiskDriveFullException e) { |
| 54 | + assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); |
| 55 | + } catch (Exception e) { |
| 56 | + e.printStackTrace(); |
| 57 | + fail("Unexpected exception: " + e.getMessage()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testXboxModelEnumValues() { |
| 63 | + Xbox.XboxModel[] models = Xbox.XboxModel.values(); |
| 64 | + assertEquals(6, models.length); |
| 65 | + assertEquals(Xbox.XboxModel.XBOX360, models[0]); |
| 66 | + assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testPrintAllGames() throws Exception { |
| 71 | + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); |
| 72 | + LoadGame loader = |
| 73 | + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); |
| 74 | + |
| 75 | + loader.loadGamesFromFile(xbox); // Load games into library |
| 76 | + |
| 77 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 78 | + PrintStream originalOut = System.out; |
| 79 | + System.setOut(new PrintStream(outputStream)); |
| 80 | + |
| 81 | + xbox.printAllGames(); |
| 82 | + |
| 83 | + System.setOut(originalOut); |
| 84 | + |
| 85 | + String expectedOutput = |
| 86 | + """ |
| 87 | + Game ID: 1, Game Name: Call of Duty |
| 88 | + Game ID: 2, Game Name: Elden Ring |
| 89 | + Game ID: 3, Game Name: Minecraft |
| 90 | + Game ID: 4, Game Name: Monster Hunter |
| 91 | + Game ID: 5, Game Name: Fortnite |
| 92 | + Game ID: 6, Game Name: Marvel Rivals |
| 93 | + Game ID: 7, Game Name: Tetris |
| 94 | + Game ID: 8, Game Name: Madden NFL |
| 95 | + Game ID: 9, Game Name: Terraria |
| 96 | + Game ID: 10, Game Name: Baldur's Gate 3 |
| 97 | + """; |
| 98 | + assertEquals(expectedOutput.trim(), outputStream.toString().trim()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testEjectGame() { |
| 103 | + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); |
| 104 | + LoadGame loader = |
| 105 | + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); |
| 106 | + |
| 107 | + try { |
| 108 | + loader.loadGamesFromFile(xbox); // Load games into library |
| 109 | + xbox.inputGame(1); // Insert game with ID 1 |
| 110 | + assertEquals(1, xbox.getInsertedGamesSize(), "Game should be inserted."); |
| 111 | + |
| 112 | + xbox.ejectGame(1); // Eject by name (you may use ID instead if implemented that way) |
| 113 | + assertEquals(0, xbox.getInsertedGamesSize(), "The game should be ejected."); |
| 114 | + } catch (Exception e) { |
| 115 | + e.printStackTrace(); |
| 116 | + fail("Exception occurred: " + e.getMessage()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testGetGames() { |
| 122 | + |
| 123 | + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); |
| 124 | + LoadGame loader = |
| 125 | + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); |
| 126 | + |
| 127 | + try { |
| 128 | + loader.loadGamesFromFile(xbox); |
| 129 | + } catch (Exception e) { |
| 130 | + e.printStackTrace(); |
| 131 | + fail("Exception occurred while loading games: " + e.getMessage()); |
| 132 | + } |
| 133 | + |
| 134 | + HashMap<Integer, String> games = xbox.getGames(); |
| 135 | + |
| 136 | + assertEquals(10, games.size(), "There should be 10 games loaded."); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testGetPrice() { |
| 141 | + Xbox xbox = new Xbox("XBOX360", 400, "White", true, false); |
| 142 | + int price = xbox.getPrice(); |
| 143 | + assertEquals(400, price, "The price should be 400."); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testGetColor() { |
| 148 | + Xbox xbox = new Xbox("XBOX360", 400, "White", true, false); |
| 149 | + String color = xbox.getColor(); |
| 150 | + assertEquals("White", color, "The color should be White."); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testDiskDrive() { |
| 155 | + Xbox xbox = new Xbox("XBOX360", 400, "White", true, false); |
| 156 | + boolean diskDrive = xbox.DiskDrive(); |
| 157 | + assertTrue(diskDrive, "The disk drive should be present."); |
| 158 | + } |
| 159 | +} |
0 commit comments