Repository navigation
Expand file tree
/
Copy pathSystemInfoTest.java
More file actions
192 lines (156 loc) · 7.53 KB
/
Copy pathSystemInfoTest.java
File metadata and controls
192 lines (156 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright 2016-2026 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static oshi.util.SystemInfoHelper.printBluetoothDevices;
import static oshi.util.SystemInfoHelper.printComputerSystem;
import static oshi.util.SystemInfoHelper.printCpu;
import static oshi.util.SystemInfoHelper.printDisks;
import static oshi.util.SystemInfoHelper.printDisplays;
import static oshi.util.SystemInfoHelper.printFileSystem;
import static oshi.util.SystemInfoHelper.printGraphicsCards;
import static oshi.util.SystemInfoHelper.printInstalledApps;
import static oshi.util.SystemInfoHelper.printInternetProtocolStats;
import static oshi.util.SystemInfoHelper.printLVgroups;
import static oshi.util.SystemInfoHelper.printMemory;
import static oshi.util.SystemInfoHelper.printNetworkInterfaces;
import static oshi.util.SystemInfoHelper.printNetworkParameters;
import static oshi.util.SystemInfoHelper.printOperatingSystem;
import static oshi.util.SystemInfoHelper.printPowerSources;
import static oshi.util.SystemInfoHelper.printPrinters;
import static oshi.util.SystemInfoHelper.printProcesses;
import static oshi.util.SystemInfoHelper.printProcessor;
import static oshi.util.SystemInfoHelper.printSensors;
import static oshi.util.SystemInfoHelper.printServices;
import static oshi.util.SystemInfoHelper.printSoundCards;
import static oshi.util.SystemInfoHelper.printUsbDevices;
import static oshi.util.SystemInfoHelper.printVirtualization;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.hardware.ComputerSystem;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.software.os.OperatingSystem;
import oshi.util.PlatformEnum;
/**
* A demonstration of access to many of OSHI's capabilities.
*/
@Execution(ExecutionMode.SAME_THREAD)
@EnabledForJreRange(max = JRE.JAVA_25)
public class SystemInfoTest { // NOSONAR java:S5786
private static final Logger logger = LoggerFactory.getLogger(SystemInfoTest.class);
/**
* Test that this platform is implemented..
*/
@Test
void testPlatformEnum() {
assertThat("Unsupported OS", PlatformEnum.getCurrentPlatform(), is(not(PlatformEnum.UNKNOWN)));
// Exercise the main method
main(null);
}
/**
* Asserts that a GitHub-hosted runner, which is a virtual machine, is detected as one.
* <p>
* {@code RUNNER_ENVIRONMENT} is set to {@code github-hosted} by Actions itself, so this covers every hosted job
* without per-workflow configuration and picks up new ones for free. It cannot run unconditionally: OSHI's tests
* must also pass on physical hardware, where an empty result is the correct answer. The workflows set
* {@code OSHI_BARE_METAL_RUNNER} on the one hosted runner that is not a guest.
*/
@Test
@EnabledIfEnvironmentVariable(named = "RUNNER_ENVIRONMENT", matches = "github-hosted")
@DisabledIfEnvironmentVariable(named = "OSHI_BARE_METAL_RUNNER", matches = "true")
void testVirtualizationDetected() {
HardwareAbstractionLayer hal = new SystemInfo().getHardware();
ComputerSystem cs = hal.getComputerSystem();
// Report the signatures on failure, so the fix is to add whichever one is missing from the tables
assertThat(
"Runner is virtualized but no signature matched. CPU vendor: '"
+ hal.getProcessor().getProcessorIdentifier().getVendor() + "', manufacturer: '"
+ cs.getManufacturer() + "', model: '" + cs.getModel() + "', MACs: "
+ hal.getNetworkIFs().stream().map(NetworkIF::getMacaddr).collect(Collectors.joining(", ")),
hal.getVirtualization().isPresent(), is(true));
}
/**
* The main method, demonstrating use of classes.
*
* @param args the arguments (unused)
*/
public static void main(String @Nullable [] args) {
logger.info("------------------------------------------------------------------------");
logger.info("Using JNA");
logger.info("------------------------------------------------------------------------");
logger.info("Initializing System...");
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
OperatingSystem os = si.getOperatingSystem();
List<String> lines = new ArrayList<>();
printOperatingSystem(lines, os);
logger.info("Checking Installed Apps...");
printInstalledApps(lines, os.getInstalledApplications());
logger.info("Checking computer system...");
printComputerSystem(lines, hal.getComputerSystem());
logger.info("Checking virtualization...");
printVirtualization(lines, hal);
logger.info("Checking Processor...");
printProcessor(lines, hal.getProcessor());
logger.info("Checking Memory...");
printMemory(lines, hal.getMemory());
logger.info("Checking CPU...");
printCpu(lines, hal.getProcessor());
logger.info("Checking Processes...");
printProcesses(lines, os, hal.getMemory());
logger.info("Checking Services...");
printServices(lines, os);
logger.info("Checking Sensors...");
printSensors(lines, hal.getSensors());
logger.info("Checking Power sources...");
printPowerSources(lines, hal.getPowerSources());
logger.info("Checking Disks...");
printDisks(lines, hal.getDiskStores());
logger.info("Checking Logical Volume Groups ...");
printLVgroups(lines, hal.getLogicalVolumeGroups());
logger.info("Checking File System...");
printFileSystem(lines, os.getFileSystem());
logger.info("Checking Network interfaces...");
printNetworkInterfaces(lines, hal.getNetworkIFs());
logger.info("Checking Network parameters...");
printNetworkParameters(lines, os.getNetworkParams());
logger.info("Checking IP statistics...");
printInternetProtocolStats(lines, os.getInternetProtocolStats());
logger.info("Checking Displays...");
printDisplays(lines, hal.getDisplays());
logger.info("Checking USB Devices...");
printUsbDevices(lines, hal.getUsbDevices(true));
logger.info("Checking Bluetooth Devices...");
printBluetoothDevices(lines, hal.getBluetoothDevices());
logger.info("Checking Sound Cards...");
printSoundCards(lines, hal.getSoundCards());
logger.info("Checking Graphics Cards...");
printGraphicsCards(lines, hal.getGraphicsCards());
logger.info("Checking Printers...");
printPrinters(lines, hal.getPrinters());
StringBuilder output = new StringBuilder();
for (String line : lines) {
output.append(line);
if (line != null && !line.endsWith("\n")) {
output.append('\n');
}
}
logger.info("Printing Operating System and Hardware Info:{}{}", '\n', output);
}
}