Skip to content

Fix NullPointerException in SingleTargetMapping when source or suffix… - #107

Open
NehaSaini011 wants to merge 1 commit into
apache:masterfrom
NehaSaini011:fix/singletargetmapping-npe-94
Open

Fix NullPointerException in SingleTargetMapping when source or suffix…#107
NehaSaini011 wants to merge 1 commit into
apache:masterfrom
NehaSaini011:fix/singletargetmapping-npe-94

Conversation

@NehaSaini011

@NehaSaini011 NehaSaini011 commented Jul 5, 2026

Copy link
Copy Markdown

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:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the integration tests successfully (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.

@elharo
elharo requested a review from Copilot August 1, 2026 11:14
@elharo elharo changed the title Fix NullPointerException in SIngleTargetMapping when source or suffix… Fix NullPointerException in SingleTargetMapping when source or suffix… Aug 1, 2026

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

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#getTargetFiles before calling source.endsWith(sourceSuffix).
  • Add SingleTargetMappingTest to cover suffix match/non-match and null-input behavior.
  • Remove the previously misplaced SingleTargetMapping test from SuffixMappingTest.

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 null source, but it doesn't cover the reported NPE case where source is non-null and sourceSuffix is 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());
}
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.

SingleTargetMapping: NPE from source.endsWith(null)

2 participants