Skip to content

Add DefaultLookupTest for lookupOptional NPE fix#12384

Open
ascheman wants to merge 1 commit into
apache:masterfrom
aschemaven:defaultlookup-test-master
Open

Add DefaultLookupTest for lookupOptional NPE fix#12384
ascheman wants to merge 1 commit into
apache:masterfrom
aschemaven:defaultlookup-test-master

Conversation

@ascheman

Copy link
Copy Markdown
Contributor

What

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

Why

#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 — 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 master; 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 Maven Core regression test to ensure DefaultLookup.lookupOptional(...) continues to return Optional.empty() when the underlying PlexusContainer returns null (the behavior introduced by switching to Optional.ofNullable in #12336), preventing silent reintroduction of the prior NPE.

Changes:

  • Introduces DefaultLookupTest covering both lookupOptional overloads for the null-return case.
  • Adds assertions for the “value present” case and for translation of ComponentLookupException caused by NoSuchElementException to Optional.empty().

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

Comment on lines +94 to +108
/**
* A {@link ComponentLookupException} whose cause is a {@link NoSuchElementException} signals an
* absent component and must be translated into an empty {@link Optional}.
*/
@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");
}
The NPE fix for DefaultLookup.lookupOptional (apache#12336) — 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: null component -> empty Optional (the guarded NPE case),
present component -> value, and a ComponentLookupException whose cause is
NoSuchElementException -> empty.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ascheman ascheman force-pushed the defaultlookup-test-master branch from 0aa9008 to 0a65c49 Compare June 28, 2026 18:31
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.

2 participants