Fix NullPointerException in SingleTargetMapping when source or suffix… - #107
Open
NehaSaini011 wants to merge 1 commit into
Open
Fix NullPointerException in SingleTargetMapping when source or suffix…#107NehaSaini011 wants to merge 1 commit into
NehaSaini011 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a NullPointerException in SingleTargetMapping#getTargetFiles by short-circuiting when source or sourceSuffix is null, and reorganizes test coverage by moving SingleTargetMapping expectations into a dedicated test class.
Changes:
- Add a null-guard in
SingleTargetMapping#getTargetFilesbefore callingsource.endsWith(sourceSuffix). - Add
SingleTargetMappingTestto cover suffix match/non-match and null-input behavior. - Remove the previously misplaced
SingleTargetMappingtest fromSuffixMappingTest.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/main/java/org/apache/maven/shared/io/scan/mapping/SingleTargetMapping.java | Adds a defensive null check to prevent NPE during suffix evaluation. |
| src/test/java/org/apache/maven/shared/io/scan/mapping/SingleTargetMappingTest.java | Introduces dedicated tests for SingleTargetMapping behavior and null handling. |
| src/test/java/org/apache/maven/shared/io/scan/mapping/SuffixMappingTest.java | Removes the unrelated SingleTargetMapping test to keep responsibilities separated. |
Suppressed comments (1)
src/test/java/org/apache/maven/shared/io/scan/mapping/SingleTargetMappingTest.java:60
- The null-handling test currently calls
getTargetFiles(..., null)with a nullsource, but it doesn't cover the reported NPE case wheresourceis non-null andsourceSuffixis null (i.e.source.endsWith(null)). Adding an assertion for the non-null source + null suffix path ensures the regression is actually covered.
void testGetTargetFilesShouldHandleNullSourceAndSuffixGracefully() {
SingleTargetMapping mapping = new SingleTargetMapping(null, "/foo");
File basedir = new File("target/");
assertDoesNotThrow(() -> {
Set<File> results = mapping.getTargetFiles(basedir, null);
assertTrue(results.isEmpty());
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+32
to
+51
| @Test | ||
| void testGetTargetFilesShouldReturnEmptySetWhenSuffixDoesNotMatch() throws Exception { | ||
| SingleTargetMapping mapping = new SingleTargetMapping(".cs", "/foo"); | ||
| File basedir = new File("target/"); | ||
|
|
||
| Set<File> results = mapping.getTargetFiles(basedir, "path/to/file.apt"); | ||
|
|
||
| assertTrue(results.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetTargetFilesShouldReturnCorrectFileWhenSuffixMatches() throws Exception { | ||
| SingleTargetMapping mapping = new SingleTargetMapping(".cs", "/foo"); | ||
| File basedir = new File("target/"); | ||
|
|
||
| Set<File> results = mapping.getTargetFiles(basedir, "path/to/file.cs"); | ||
|
|
||
| assertEquals(1, results.size()); | ||
| assertEquals(new File(basedir, "/foo"), results.iterator().next()); | ||
| } |
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.
Description
This pull request addresses a NullPointerException (NPE) in SingleTargetMapping.java that occurs when evaluating source.endsWith(sourceSuffix) if either source or sourceSuffix is passed as a null value.
To resolve this, a defensive guard clause has been implemented to return an empty set (Collections.emptySet()) immediately if any parameter validation fails, avoiding the crash.
Configuration Details / Changes
Fix Implementation: Added short-circuit null validation for source and sourceSuffix in SingleTargetMapping#getTargetFiles.
Test Isolation & Coverage:
Created a dedicated JUnit 5 test class SingleTargetMappingTest.java to explicitly cover suffix matching, non-matching, and null input edge cases.
Removed the legacy, misplaced test method singleTargetMapper() from SuffixMappingTest.java to clean up duplicate test footprints and enforce correct separation of concerns.
Style & Quality Gates: Fixed all checkstyle rules (avoiding star imports and matching method camelCase patterns) to ensure compliance with project conventions.
Closes #94
Following this checklist to help us incorporate your
contribution quickly and easily:
Note that commits might be squashed by a maintainer on merge.
This may not always be possible but is a best-practice.
mvn verifyto make sure basic checks pass.A more thorough check will be performed on your pull request automatically.
mvn -Prun-its verify).If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.
To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.