Skip to content

Add cross-platform tests for Windows and Mac utility methods - #3191

Merged
dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:improve-platform-test-coverage
Apr 23, 2026
Merged

dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:improve-platform-test-coverage

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 23, 2026 •

Copy link
Copy Markdown
Member

Description

Tests pure lookup/mapping methods in Windows and Mac platform classes that are testable from any platform — no native calls or platform-specific APIs involved.

New test classes

WindowsGlobalMemoryTest — Tests memoryType(int) and smBiosMemoryType(int) switch statements covering WMI memory type codes (0-23) and SMBIOS type codes (DDR through HBM3, plus unknown/default). This file was at 3.3% coverage — the two methods are 58 lines of pure lookup logic.

WindowsCentralProcessorTest — Tests parseIdentifier(String, String) which extracts Family/Model/Stepping values from Windows processor identifier strings. Covers found, not-found, and key-at-end-of-string edge cases.

ThreadInfoTest (Mac) — Tests ThreadStats constructor state mapping for all 7 process states (Sleeping, Idle, Running, Waiting, Zombie, Stopped, Other) and getter methods.

Also adds --add-opens directives to module-info.test for the oshi.hardware.common.platform.windows, oshi.hardware.common.platform.mac, and oshi.driver.common.mac packages.

Testing

All 455 tests pass locally on Linux (Java 25). Spotless, checkstyle, and forbiddenapis all clean.

Summary by CodeRabbit

  • Tests

    • Added test coverage for thread information state mapping and properties.
    • Added test coverage for Windows processor identifier parsing.
    • Added test coverage for Windows memory type and SMBIOS classification.
  • Chores

    • Extended test JVM launch configuration to open additional OS-specific packages for test execution.

Test pure lookup/mapping methods that are testable from any platform:

- WindowsGlobalMemoryTest: memoryType() and smBiosMemoryType() switch
  statements covering all WMI and SMBIOS memory type codes
- WindowsCentralProcessorTest: parseIdentifier() string parsing for
  Family/Model/Stepping extraction
- ThreadInfoTest: ThreadStats constructor state mapping (all 7 states)
  and getter coverage

Also add module-info.test opens for windows, mac, and mac driver
test packages.

Signed-off-by: Daniel Widdis <[email protected]>
Signed-off-by: Dan Widdis <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 30d3b67f-7629-48bb-8dac-5865ec5c95bb

📥 Commits

Reviewing files that changed from the base of the PR and between 437f3fc and cd30b55.

📒 Files selected for processing (1)
  • oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java
✅ Files skipped from review due to trivial changes (1)
  • oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)

📝 Walkthrough

Walkthrough

Adds unit tests for macOS thread parsing and Windows CPU/memory parsing, and extends the test module configuration to open additional macOS and Windows packages to JUnit for reflective access.

Changes

Cohort / File(s) Summary
Module Test Configuration
oshi-common/src/test/java/module-info.test
Added --add-opens directives to grant org.junit.platform.commons reflective access to macOS hardware platform, Windows hardware platform, and mac driver common packages for test execution.
macOS Driver Tests
oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java
New JUnit tests for ThreadInfo.ThreadStats: validates mapping of state characters to OSProcess.State values and verifies thread id, system/user time, uptime, and priority accessors.
Windows Hardware Tests
oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsCentralProcessorTest.java, oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java
Added tests for WindowsCentralProcessor.parseIdentifier() (extracts Family/Model/Stepping with edge cases) and WindowsGlobalMemory.memoryType()/smBiosMemoryType() mappings across various memory type codes and generations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Tests arrive on wings of code, 🎉
Threads and chips in careful mode,
Module doors swung open wide,
Assertions stride on either side,
CI hums — platform checks bestowed. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add cross-platform tests for Windows and Mac utility methods' accurately describes the main change—adding new test classes for Windows and Mac platform utilities—and aligns well with the PR's primary objective of improving test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java (1)

8-8: Consider asserting the deterministic upTime value.

greaterThan(0L) would still pass if the getter returned almost any positive value. With these constructor inputs, the computed value is deterministic, so this can catch regressions more directly.

Proposed tightening
-import static org.hamcrest.Matchers.greaterThan;
 import static org.hamcrest.Matchers.is;
@@
         assertThat(ts.getThreadId(), is(5));
         assertThat(ts.getSystemTime(), is(1000L));
         assertThat(ts.getUserTime(), is(2000L));
-        assertThat(ts.getUpTime(), greaterThan(0L));
+        assertThat(ts.getUpTime(), is(11976L));
         assertThat(ts.getPriority(), is(20));

Also applies to: 28-35

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java` at line
8, The test currently asserts upTime with greaterThan(0L), which is too loose;
compute the deterministic expected upTime from the ThreadInfo constructor inputs
used in the test and replace the greaterThan assertion with a strict equality
check (e.g., assertEquals(expectedUpTime, threadInfo.getUpTime()) or use
equalTo(expectedUpTime) matcher). Update every occurrence (including the other
assertions around lines 28-35) that use greaterThan(0L) to assert the exact
expected upTime value for the ThreadInfo instances created in this test.
oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java (1)

15-46: Broaden these lookup tests to cover every switch arm.

A few memoryType and SMBIOS branches remain untested, so regressions in those mappings could still slip through. A small table-driven loop keeps this exhaustive without adding native/platform dependencies.

Proposed table-driven expansion
     `@Test`
     void testMemoryTypeKnownValues() {
-        assertThat(WindowsGlobalMemory.memoryType(0), is("Unknown"));
-        assertThat(WindowsGlobalMemory.memoryType(1), is("Other"));
-        assertThat(WindowsGlobalMemory.memoryType(2), is("DRAM"));
-        assertThat(WindowsGlobalMemory.memoryType(17), is("SDRAM"));
-        assertThat(WindowsGlobalMemory.memoryType(20), is("DDR"));
-        assertThat(WindowsGlobalMemory.memoryType(21), is("DDR2"));
-        assertThat(WindowsGlobalMemory.memoryType(22), is("BRAM"));
-        assertThat(WindowsGlobalMemory.memoryType(23), is("DDR FB-DIMM"));
+        String[] expected = { "Unknown", "Other", "DRAM", "Synchronous DRAM", "Cache DRAM", "EDO", "EDRAM",
+                "VRAM", "SRAM", "RAM", "ROM", "Flash", "EEPROM", "FEPROM", "EPROM", "CDRAM", "3DRAM",
+                "SDRAM", "SGRAM", "RDRAM", "DDR", "DDR2", "BRAM", "DDR FB-DIMM" };
+        for (int type = 0; type < expected.length; type++) {
+            assertThat(WindowsGlobalMemory.memoryType(type), is(expected[type]));
+        }
@@
     `@Test`
     void testSmBiosMemoryTypeKnownValues() {
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x01), is("Other"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x03), is("DRAM"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x12), is("DDR"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x13), is("DDR2"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x14), is("DDR2 FB-DIMM"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x18), is("DDR3"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x1A), is("DDR4"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x1E), is("LPDDR4"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x22), is("DDR5"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x23), is("LPDDR5"));
-        assertThat(WindowsGlobalMemory.smBiosMemoryType(0x24), is("HBM3"));
+        Object[][] expected = { { 0x01, "Other" }, { 0x03, "DRAM" }, { 0x04, "EDRAM" }, { 0x05, "VRAM" },
+                { 0x06, "SRAM" }, { 0x07, "RAM" }, { 0x08, "ROM" }, { 0x09, "FLASH" },
+                { 0x0A, "EEPROM" }, { 0x0B, "FEPROM" }, { 0x0C, "EPROM" }, { 0x0D, "CDRAM" },
+                { 0x0E, "3DRAM" }, { 0x0F, "SDRAM" }, { 0x10, "SGRAM" }, { 0x11, "RDRAM" },
+                { 0x12, "DDR" }, { 0x13, "DDR2" }, { 0x14, "DDR2 FB-DIMM" }, { 0x18, "DDR3" },
+                { 0x19, "FBD2" }, { 0x1A, "DDR4" }, { 0x1B, "LPDDR" }, { 0x1C, "LPDDR2" },
+                { 0x1D, "LPDDR3" }, { 0x1E, "LPDDR4" }, { 0x1F, "Logical non-volatile device" },
+                { 0x20, "HBM" }, { 0x21, "HBM2" }, { 0x22, "DDR5" }, { 0x23, "LPDDR5" },
+                { 0x24, "HBM3" } };
+        for (Object[] type : expected) {
+            assertThat(WindowsGlobalMemory.smBiosMemoryType((int) type[0]), is(type[1]));
+        }
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java`
around lines 15 - 46, Extend the tests to cover every branch of the
WindowsGlobalMemory.memoryType and WindowsGlobalMemory.smBiosMemoryType switch
statements by replacing the scattered assertions in testMemoryTypeKnownValues,
testMemoryTypeFallsBackToSmBios, and testSmBiosMemoryTypeKnownValues with
table-driven loops: create maps/arrays of (inputValue -> expectedString) that
include every mapped constant used in memoryType and smBiosMemoryType (including
the values that trigger the SMBIOS fallback like 0x1A and the ones >23), iterate
and assert each pair with the same assertThat(...) pattern; keep the existing
test method names and use the unique symbols WindowsGlobalMemory.memoryType and
WindowsGlobalMemory.smBiosMemoryType to locate the code to expand.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java`:
- Line 8: The test currently asserts upTime with greaterThan(0L), which is too
loose; compute the deterministic expected upTime from the ThreadInfo constructor
inputs used in the test and replace the greaterThan assertion with a strict
equality check (e.g., assertEquals(expectedUpTime, threadInfo.getUpTime()) or
use equalTo(expectedUpTime) matcher). Update every occurrence (including the
other assertions around lines 28-35) that use greaterThan(0L) to assert the
exact expected upTime value for the ThreadInfo instances created in this test.

In
`@oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java`:
- Around line 15-46: Extend the tests to cover every branch of the
WindowsGlobalMemory.memoryType and WindowsGlobalMemory.smBiosMemoryType switch
statements by replacing the scattered assertions in testMemoryTypeKnownValues,
testMemoryTypeFallsBackToSmBios, and testSmBiosMemoryTypeKnownValues with
table-driven loops: create maps/arrays of (inputValue -> expectedString) that
include every mapped constant used in memoryType and smBiosMemoryType (including
the values that trigger the SMBIOS fallback like 0x1A and the ones >23), iterate
and assert each pair with the same assertThat(...) pattern; keep the existing
test method names and use the unique symbols WindowsGlobalMemory.memoryType and
WindowsGlobalMemory.smBiosMemoryType to locate the code to expand.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cba14d82-0355-4565-b01e-67218cc35ac1

📥 Commits

Reviewing files that changed from the base of the PR and between 7efe507 and 437f3fc.

📒 Files selected for processing (4)
  • oshi-common/src/test/java/module-info.test
  • oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsCentralProcessorTest.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🧰 Additional context used
🧠 Learnings (5)
📓 Common learnings
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java11/src/main/java/module-info.java:10-17
Timestamp: 2026-03-29T20:01:06.452Z
Learning: In the oshi project, `oshi-core-java25/src/main/java/module-info.java` intentionally does NOT export `oshi.util.platform.unix.freebsd`, `oshi.util.platform.unix.openbsd`, or `oshi.util.platform.unix.solaris`, because the FFM (Foreign Function & Memory) implementation only supports Windows, macOS, and Linux. This differs from `oshi-core-java11/src/main/java/module-info.java` which exports all Unix platform packages. The asymmetry is by design.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:51.516Z
Learning: In the oshi/oshi project, the public API is strictly limited to interfaces and classes in the `oshi`, `oshi.hardware`, and `oshi.software.os` packages. All platform-specific code under subpackages like `oshi.software.os.linux`, `oshi.hardware.platform.linux`, etc., is considered implementation detail — not public API. Do not flag changes to platform-specific classes (e.g., making `LinuxOperatingSystem` abstract, narrowing constructor visibility, adding abstract methods) as public API breaks.
📚 Learning: 2026-03-29T20:01:06.452Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java11/src/main/java/module-info.java:10-17
Timestamp: 2026-03-29T20:01:06.452Z
Learning: In the oshi project, `oshi-core-java25/src/main/java/module-info.java` intentionally does NOT export `oshi.util.platform.unix.freebsd`, `oshi.util.platform.unix.openbsd`, or `oshi.util.platform.unix.solaris`, because the FFM (Foreign Function & Memory) implementation only supports Windows, macOS, and Linux. This differs from `oshi-core-java11/src/main/java/module-info.java` which exports all Unix platform packages. The asymmetry is by design.

Applied to files:

  • oshi-common/src/test/java/module-info.test
📚 Learning: 2026-04-06T04:45:51.516Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:51.516Z
Learning: In the oshi/oshi project, the public API is strictly limited to interfaces and classes in the `oshi`, `oshi.hardware`, and `oshi.software.os` packages. All platform-specific code under subpackages like `oshi.software.os.linux`, `oshi.hardware.platform.linux`, etc., is considered implementation detail — not public API. Do not flag changes to platform-specific classes (e.g., making `LinuxOperatingSystem` abstract, narrowing constructor visibility, adding abstract methods) as public API breaks.

Applied to files:

  • oshi-common/src/test/java/module-info.test
📚 Learning: 2026-04-19T01:28:43.529Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3174
File: oshi-benchmark/src/test/java/oshi/comparison/WmiComparisonTest.java:237-257
Timestamp: 2026-04-19T01:28:43.529Z
Learning: In `oshi-benchmark/src/test/java/oshi/comparison/WmiComparisonTest.java`, the WMI comparison tests (e.g., `testProcessor`) intentionally validate only index 0 and a few representative fields per WMI data type (String, uint16, uint32, uint64). Exhaustive row-by-row comparison is deliberately avoided for numerically heavy multi-row tables. The design rationale is: matching row counts + a few selected fields covering each data type is sufficient to verify JNA vs FFM driver parity. Do not suggest looping over all rows for these tests.

Applied to files:

  • oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsCentralProcessorTest.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java
📚 Learning: 2026-04-18T03:18:03.725Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3170
File: oshi-benchmark/src/test/java/oshi/comparison/PerfmonComparisonTest.java:177-183
Timestamp: 2026-04-18T03:18:03.725Z
Learning: In `oshi-benchmark/src/test/java/oshi/comparison/PerfmonComparisonTest.java`, GPU engine PDH instance counts (from `GpuInformationJNA.queryGpuEngineCounters()` / `GpuInformationFFM.queryGpuEngineCounters()`) are highly volatile — counts can differ dramatically (e.g., 53 vs 40) even between queries taken milliseconds apart. Do not assert instance count equality or exact instance list matching between JNA and FFM results. The only reliable assertion is that both are empty or both are non-empty (`assertThat(ffm.getA().isEmpty()).isEqualTo(jna.getA().isEmpty())`).

Applied to files:

  • oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java
  • oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java
🔇 Additional comments (4)
oshi-common/src/test/java/module-info.test (1)

13-18: Looks good.

These test-only --add-opens entries are narrowly scoped to the new package-level tests and avoid exposing implementation packages in production modules.

oshi-common/src/test/java/oshi/driver/common/mac/ThreadInfoTest.java (1)

17-26: Good coverage of the state mapping.

This exercises each switch outcome without invoking macOS-specific/native behavior.

oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsGlobalMemoryTest.java (1)

48-52: Good default-path coverage.

This covers both the explicit SMBIOS unknown code and an unmapped value.

oshi-common/src/test/java/oshi/hardware/common/platform/windows/WindowsCentralProcessorTest.java (1)

14-31: Looks good.

The tests cover successful extraction, missing keys, and the trailing-key edge case while staying platform-independent.

Signed-off-by: Daniel Widdis <[email protected]>
Signed-off-by: Dan Widdis <[email protected]>
@codecov

codecov Bot commented Apr 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.17%. Comparing base (7efe507) to head (cd30b55).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3191      +/-   ##
==========================================
+ Coverage   57.98%   58.17%   +0.18%     
==========================================
  Files         421      421              
  Lines       20272    20272              
  Branches     3563     3563              
==========================================
+ Hits        11755    11793      +38     
+ Misses       6750     6716      -34     
+ Partials     1767     1763       -4     
Flag Coverage Δ
linux 23.72% <ø> (+0.30%) ⬆️
macos 26.76% <ø> (+0.21%) ⬆️
windows 35.68% <ø> (+0.25%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 15b0911 into oshi:master Apr 23, 2026
16 checks passed
@dbwiddis
dbwiddis deleted the improve-platform-test-coverage branch April 23, 2026 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant