Issue #3771: fixed in NPE in cache for HeaderCheck

This commit is contained in:
rnveach 2017-01-28 13:47:16 -05:00 committed by Roman Ivanov
parent 861612671a
commit 4cff2eb714
2 changed files with 31 additions and 1 deletions

View File

@ -193,6 +193,15 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
@Override
public Set<String> getExternalResourceLocations() {
return Collections.singleton(headerFile.toString());
final Set<String> result;
if (headerFile == null) {
result = Collections.emptySet();
}
else {
result = Collections.singleton(headerFile.toString());
}
return result;
}
}

View File

@ -268,4 +268,25 @@ public class HeaderCheckTest extends BaseFileSetCheckTestSupport {
verify(checker, getPath("InputHeader.java"), expected);
}
@Test
public void testCacheHeaderWithoutFile() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
checkConfig.addAttribute("header", "Test");
final DefaultConfiguration checkerConfig = new DefaultConfiguration("checkstyle_checks");
checkerConfig.addChild(checkConfig);
checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath());
final Checker checker = new Checker();
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(checkerConfig);
checker.addListener(new BriefUtLogger(stream));
final String[] expected = {
"1: " + getCheckMessage(MSG_MISMATCH, "Test"),
};
verify(checker, getPath("InputHeader.java"), expected);
}
}