|
| 1 | +package com.baeldung.oshi; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | + |
| 8 | +import oshi.SystemInfo; |
| 9 | +import oshi.hardware.CentralProcessor; |
| 10 | +import oshi.hardware.GlobalMemory; |
| 11 | +import oshi.hardware.HWDiskStore; |
| 12 | +import oshi.software.os.OperatingSystem; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +class OSHIUnitTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void givenSystem_whenUsingOSHI_thenExtractOSDetails() { |
| 20 | + SystemInfo si = new SystemInfo(); |
| 21 | + OperatingSystem os = si.getOperatingSystem(); |
| 22 | + |
| 23 | + assertNotNull(os, "Operating System object should not be null"); |
| 24 | + assertNotNull(os.getFamily(), "OS Family should not be null"); |
| 25 | + assertNotNull(os.getVersionInfo(), "OS Version info should not be null"); |
| 26 | + assertTrue(os.getBitness() == 32 || os.getBitness() == 64, "OS Bitness should be 32 or 64"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void givenSystem_whenUsingOSHI_thenExtractSystemUptime() { |
| 31 | + SystemInfo si = new SystemInfo(); |
| 32 | + OperatingSystem os = si.getOperatingSystem(); |
| 33 | + |
| 34 | + long uptime = os.getSystemUptime(); |
| 35 | + assertTrue(uptime >= 0, "System uptime should be non-negative"); |
| 36 | + try { |
| 37 | + Thread.sleep(2000); |
| 38 | + } catch (InterruptedException e) { |
| 39 | + fail("Test interrupted"); |
| 40 | + } |
| 41 | + long newUptime = os.getSystemUptime(); |
| 42 | + assertTrue(newUptime >= uptime, "Uptime should increase over time"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void givenSystem_whenUsingOSHI_thenExtractCPUDetails() { |
| 47 | + SystemInfo si = new SystemInfo(); |
| 48 | + CentralProcessor processor = si.getHardware() |
| 49 | + .getProcessor(); |
| 50 | + |
| 51 | + assertNotNull(processor, "Processor object should not be null"); |
| 52 | + assertTrue(processor.getPhysicalProcessorCount() > 0, "CPU must have at least one physical core"); |
| 53 | + assertTrue(processor.getLogicalProcessorCount() >= processor.getPhysicalProcessorCount(), |
| 54 | + "Logical cores should be greater than or equal to physical cores"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void givenSystem_whenUsingOSHI_thenExtractCPULoad() throws InterruptedException { |
| 59 | + SystemInfo si = new SystemInfo(); |
| 60 | + CentralProcessor processor = si.getHardware() |
| 61 | + .getProcessor(); |
| 62 | + |
| 63 | + long[] prevTicks = processor.getSystemCpuLoadTicks(); |
| 64 | + TimeUnit.SECONDS.sleep(1); |
| 65 | + double cpuLoad = processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100; |
| 66 | + |
| 67 | + assertTrue(cpuLoad >= 0 && cpuLoad <= 100, "CPU load should be between 0% and 100%"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void givenSystem_whenUsingOSHI_thenExtractMemoryDetails() { |
| 72 | + SystemInfo si = new SystemInfo(); |
| 73 | + GlobalMemory memory = si.getHardware() |
| 74 | + .getMemory(); |
| 75 | + |
| 76 | + assertTrue(memory.getTotal() > 0, "Total memory should be positive"); |
| 77 | + assertTrue(memory.getAvailable() >= 0, "Available memory should not be negative"); |
| 78 | + assertTrue(memory.getAvailable() <= memory.getTotal(), "Available memory should not exceed total memory"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void givenSystem_whenUsingOSHI_thenExtractDiskDetails() { |
| 83 | + SystemInfo si = new SystemInfo(); |
| 84 | + List<HWDiskStore> diskStores = si.getHardware() |
| 85 | + .getDiskStores(); |
| 86 | + |
| 87 | + assertFalse(diskStores.isEmpty(), "There should be at least one disk"); |
| 88 | + |
| 89 | + for (HWDiskStore disk : diskStores) { |
| 90 | + assertNotNull(disk.getModel(), "Disk model should not be null"); |
| 91 | + assertTrue(disk.getSize() >= 0, "Disk size should be non-negative"); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments