Add DefaultLookupTest for lookupOptional NPE fix#12385
Open
ascheman wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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
DefaultLookupTestcoveringlookupOptional(Class)/lookupOptional(Class, String)returning empty when the container returnsnull. - Adds coverage for translating
ComponentLookupExceptioncaused byNoSuchElementExceptionintoOptional.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"); | ||
| } | ||
| } |
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: 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>
2fdc618 to
76f1c09
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 useOptional.ofNullableinstead ofOptional.of, so anullcomponent lookup returns an emptyOptionalinstead of throwingNullPointerException. Nothing currently guards that behavior on this line — reverting the fix goes undetected by the suite.Test
DefaultLookupTestcovers bothlookupOptionaloverloads:null-> emptyOptional(the guarded NPE case)OptionalComponentLookupExceptionwhose cause isNoSuchElementException-> emptyVerified locally: green on current maven-4.0.x; reverting
ofNullable->ofturns the two null-case tests red with the NPE fromjava.util.Optional.of. No production code change.