Skip to content

Commit cb52e61

Browse files
Neetika23Neetika Khandelwal
andauthored
BAEL-9124 (#18394)
* BAEL-9124 * BAEL-9124 * BAEL-9124 --------- Co-authored-by: Neetika Khandelwal <[email protected]>
1 parent ccbe1e8 commit cb52e61

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

libraries-4/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@
103103
<artifactId>aeron-all</artifactId>
104104
<version>${aeron.version}</version>
105105
</dependency>
106+
<dependency>
107+
<groupId>com.github.oshi</groupId>
108+
<artifactId>oshi-core</artifactId>
109+
<version>${oshi.version}</version>
110+
</dependency>
106111

107112
</dependencies>
108113

@@ -124,6 +129,7 @@
124129
<javaparser.version>3.25.10</javaparser.version>
125130
<dev.failsafe.version>3.3.2</dev.failsafe.version>
126131
<aeron.version>1.44.1</aeron.version>
132+
<oshi.version>6.7.1</oshi.version>
127133
</properties>
128134

129135
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)