diff --git a/src/main/java/org/apache/maven/shared/io/scan/StaleResourceScanner.java b/src/main/java/org/apache/maven/shared/io/scan/StaleResourceScanner.java index c942ef6..f961d64 100644 --- a/src/main/java/org/apache/maven/shared/io/scan/StaleResourceScanner.java +++ b/src/main/java/org/apache/maven/shared/io/scan/StaleResourceScanner.java @@ -93,6 +93,10 @@ public Set getIncludedSources(File sourceDir, File targetDir) throws Inclu for (SourceMapping mapping : srcMappings) { Set targetFiles = mapping.getTargetFiles(targetDir, path); + if (targetFiles == null) { + continue; + } + // never include files that don't have corresponding target mappings. // the targets don't have to exist on the filesystem, but the // mappers must tell us to look for them. diff --git a/src/test/java/org/apache/maven/shared/io/scan/StaleResourceScannerTest.java b/src/test/java/org/apache/maven/shared/io/scan/StaleResourceScannerTest.java new file mode 100644 index 0000000..73e89d3 --- /dev/null +++ b/src/test/java/org/apache/maven/shared/io/scan/StaleResourceScannerTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.shared.io.scan; + +import java.io.File; +import java.nio.file.Files; +import java.util.Set; + +import org.apache.maven.shared.io.scan.mapping.SourceMapping; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +class StaleResourceScannerTest { + + @Test + void shouldNotNpeWhenGetTargetFilesReturnsNull() throws Exception { + StaleResourceScanner scanner = new StaleResourceScanner(); + scanner.addSourceMapping(new SourceMapping() { + @Override + public Set getTargetFiles(File targetDir, String source) { + return null; + } + }); + + File baseDir = new File("target"); + baseDir.mkdirs(); + File sourceDir = Files.createTempDirectory(baseDir.toPath(), "stale-scanner-src-") + .toFile(); + File targetDir = Files.createTempDirectory(baseDir.toPath(), "stale-scanner-tgt-") + .toFile(); + Files.createTempFile(sourceDir.toPath(), "source", ".txt"); + + assertDoesNotThrow(() -> scanner.getIncludedSources(sourceDir, targetDir)); + } +}