Skip to content

Add DefaultLookupTest for lookupOptional NPE fix#12385

Open
ascheman wants to merge 1 commit into
apache:maven-4.0.xfrom
aschemaven:defaultlookup-test
Open

Add DefaultLookupTest for lookupOptional NPE fix#12385
ascheman wants to merge 1 commit into
apache:maven-4.0.xfrom
aschemaven:defaultlookup-test

Conversation

@ascheman

Copy link
Copy Markdown
Contributor

What

Adds DefaultLookupTest (maven-core) — the regression test that the backported NPE fix #12340 shipped without.

Why

#12340 (backport of #12336) changed DefaultLookup.lookupOptional(...) to use Optional.ofNullable instead of Optional.of, so a null component lookup returns an empty Optional instead of throwing NullPointerException. Nothing currently guards that behavior on this line — reverting the fix goes undetected by the suite.

Test

DefaultLookupTest covers both lookupOptional overloads:

  • container returns null -> empty Optional (the guarded NPE case)
  • container returns a value -> present Optional
  • ComponentLookupException whose cause is NoSuchElementException -> empty

Verified locally: green on current maven-4.0.x; reverting ofNullable->of turns the two null-case tests red with the NPE from java.util.Optional.of. No production code change.

@ascheman ascheman marked this pull request as ready for review June 28, 2026 17:43
@ascheman ascheman requested review from Copilot and gnodet June 28, 2026 17:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a regression test in maven-core to lock in the behavior of DefaultLookup.lookupOptional(...) after the backported fix that switched to Optional.ofNullable(...), ensuring null container lookups yield Optional.empty() instead of an NPE.

Changes:

  • Introduces DefaultLookupTest covering lookupOptional(Class) / lookupOptional(Class, String) returning empty when the container returns null.
  • Adds coverage for translating ComponentLookupException caused by NoSuchElementException into Optional.empty() (currently only for the (Class) overload).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +98 to +109
@Test
void lookupOptionalReturnsEmptyOnNoSuchElementCause() throws Exception {
ComponentLookupException cle =
new ComponentLookupException(new NoSuchElementException(), String.class.getName(), "");
assertSame(NoSuchElementException.class, cle.getCause().getClass(), "test fixture precondition");
when(container.lookup(String.class)).thenThrow(cle);

Optional<String> result = lookup.lookupOptional(String.class);

assertFalse(result.isPresent(), "expected empty Optional when component is absent");
}
}
@ascheman ascheman force-pushed the defaultlookup-test branch from 2fdc618 to 76f1c09 Compare June 28, 2026 18:31

@gnodet gnodet 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.

Review: Add DefaultLookupTest for lookupOptional NPE fix (maven-4.0.x)

Nice work adding this regression guard — the test is well-structured and covers the key scenarios. This is the backport of the same test from PR #12384 (master), with the only difference being the Javadoc reference to #12340 instead of #12336. A couple of minor suggestions below.

Findings

1. [Low / Suggestion] Missing test for the rethrow path

The production code has a branch where ComponentLookupException with a cause that is not NoSuchElementException rethrows as LookupException. The test covers the NoSuchElementException cause path but does not test that other causes are properly rethrown. This would round out coverage, but is not a blocker for this focused regression guard PR.

2. [Info] No [MNG-XXXX] prefix in commit message

Minor convention deviation — acceptable for a test-only addition.

Positive notes

  • 6 well-structured JUnit 5 test methods covering both lookupOptional overloads × 3 scenarios (null, present, NoSuchElementException)
  • Proper Mockito usage with existing project dependency
  • Correct Apache License 2.0 header
  • CI: all 22 checks pass

This review does not replace specialized tools such as CodeRabbit, Sourcery, or SonarCloud.

Reviewed with Claude Code on behalf of @gnodet. This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.

The backported NPE fix for DefaultLookup.lookupOptional (apache#12340) —
switching Optional.of to Optional.ofNullable so a null component lookup
returns an empty Optional instead of throwing NullPointerException —
shipped without a regression test.

Add DefaultLookupTest in maven-core covering both lookupOptional overloads
across four scenarios: null component -> empty Optional (the guarded NPE
case), present component -> value, ComponentLookupException caused by
NoSuchElementException -> empty, and any other cause -> rethrown as
LookupException.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ascheman ascheman force-pushed the defaultlookup-test branch from 76f1c09 to 7277c0b Compare July 3, 2026 16:41
@ascheman

ascheman commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @gnodet! Both points addressed (backport of #12384):

1. Rethrow path — added coverage for a ComponentLookupException whose cause is not a NoSuchElementException: it is now asserted to rethrow as LookupException (rather than be swallowed into an empty Optional), for both lookupOptional overloads — lookupOptionalRethrowsOnNonNoSuchElementCause and the (Class, String) variant. The matrix is now both overloads × {null → empty, present → value, NoSuchElement cause → empty, other cause → rethrow} = 8 tests.

2. Commit reference — added a [#12340] prefix to the commit (this project tracks via GitHub issues rather than JIRA, so no MNG-XXXX).

Force-pushed (7277c0b); locally green with full gates (spotless/checkstyle/rat), 8/8. CI re-running.

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.

3 participants