|
| 1 | +package com.codedifferently.lesson16.PersonalComputerTest; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.junit.jupiter.api.Assertions.fail; |
| 8 | + |
| 9 | +import com.codedifferently.lesson16.PersonalComputer.Computer; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.PrintStream; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +public class ComputerTest { |
| 16 | + |
| 17 | + private Computer computer; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + public void setUp() { |
| 21 | + computer = new Computer("Dell", "XPS 15", "Intel Core i7", 16, 512, "Windows 11"); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testPowerOn() throws Computer.ComputerAlreadyOnException { |
| 26 | + assertFalse(computer.isPoweredOn()); |
| 27 | + |
| 28 | + computer.powerOn(); |
| 29 | + |
| 30 | + assertTrue(computer.isPoweredOn()); |
| 31 | + assertEquals(Computer.ComputerStatus.ONLINE, computer.getStatus()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testPowerOnAlreadyOn() { |
| 36 | + try { |
| 37 | + computer.powerOn(); |
| 38 | + } catch (Computer.ComputerAlreadyOnException e) { |
| 39 | + fail("Exception should not be thrown"); |
| 40 | + } |
| 41 | + |
| 42 | + assertThrows(Computer.ComputerAlreadyOnException.class, () -> computer.powerOn()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testInstallProgram() { |
| 47 | + // Install a new program |
| 48 | + computer.installProgram("Microsoft Word"); |
| 49 | + |
| 50 | + assertTrue(computer.getInstalledPrograms().contains("Microsoft Word")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testInstallProgramAlreadyInstalled() { |
| 55 | + // Install a program |
| 56 | + computer.installProgram("Google Chrome"); |
| 57 | + |
| 58 | + // Try installing the same program again |
| 59 | + computer.installProgram("Google Chrome"); |
| 60 | + |
| 61 | + // Verify the program is only installed once |
| 62 | + assertEquals(1, computer.getInstalledPrograms().size()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testListPrograms() { |
| 67 | + // Capture the printed output |
| 68 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 69 | + PrintStream printStream = new PrintStream(outputStream); |
| 70 | + System.setOut(printStream); |
| 71 | + |
| 72 | + // Install some programs |
| 73 | + computer.installProgram("Adobe Photoshop"); |
| 74 | + computer.installProgram("Visual Studio Code"); |
| 75 | + |
| 76 | + // List the installed programs |
| 77 | + computer.listPrograms(); |
| 78 | + |
| 79 | + // Check if the correct program names are printed |
| 80 | + String output = outputStream.toString(); |
| 81 | + assertTrue(output.contains("Adobe Photoshop")); |
| 82 | + assertTrue(output.contains("Visual Studio Code")); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testGetters() { |
| 87 | + // Verify that the getters return correct values |
| 88 | + assertEquals("Dell", computer.getBrand()); |
| 89 | + assertEquals("XPS 15", computer.getModel()); |
| 90 | + assertEquals("Intel Core i7", computer.getCPU()); |
| 91 | + assertEquals(16, computer.getRAMGB()); |
| 92 | + assertEquals(512, computer.getSTORAGEGB()); |
| 93 | + } |
| 94 | +} |
0 commit comments