use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.
the class XdocsPagesTest method validateCheckstyleXml.
private static void validateCheckstyleXml(String fileName, String code, String unserializedSource) throws IOException, CheckstyleException {
// can't process non-existent examples, or out of context snippets
if (!code.contains("com.mycompany") && !code.contains("checkstyle-packages") && !code.contains("MethodLimit") && !code.contains("<suppress ") && !code.contains("<import-control ") && !unserializedSource.startsWith("<property ") && !unserializedSource.startsWith("<taskdef ")) {
// validate checkstyle structure and contents
try {
final Properties properties = new Properties();
properties.setProperty("checkstyle.header.file", new File("config/java.header").getCanonicalPath());
final PropertiesExpander expander = new PropertiesExpander(properties);
final Configuration config = ConfigurationLoader.loadConfiguration(new InputSource(new StringReader(code)), expander, false);
final Checker checker = new Checker();
try {
final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
checker.setModuleClassLoader(moduleClassLoader);
checker.configure(config);
} finally {
checker.destroy();
}
} catch (CheckstyleException ex) {
throw new CheckstyleException(fileName + " has invalid Checkstyle xml (" + ex.getMessage() + "): " + unserializedSource, ex);
}
}
}
use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.
the class AllChecksTest method testAllChecksWithDefaultConfiguration.
@Test
public void testAllChecksWithDefaultConfiguration() throws Exception {
final String inputFilePath = getPath("InputDefaultConfig.java");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
for (Class<?> check : CheckUtil.getCheckstyleChecks()) {
final DefaultConfiguration checkConfig = createCheckConfig(check);
final Checker checker;
if (AbstractCheck.class.isAssignableFrom(check)) {
// Checks which have Check as a parent.
if (check.equals(ImportControlCheck.class)) {
// ImportControlCheck must have the import control configuration file to avoid
// violation.
checkConfig.addAttribute("file", getPath("import-control_complete.xml"));
}
checker = createChecker(checkConfig);
} else {
// Checks which have TreeWalker as a parent.
BaseCheckTestSupport testSupport = new BaseCheckTestSupport() {
@Override
protected DefaultConfiguration createCheckerConfig(Configuration config) {
final DefaultConfiguration dc = new DefaultConfiguration("root");
dc.addChild(checkConfig);
return dc;
}
};
checker = testSupport.createChecker(checkConfig);
}
verify(checker, inputFilePath, expected);
}
}
use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.
the class SuppressWarningsHolderTest method testIsSuppressedWithAllArgument.
@Test
public void testIsSuppressedWithAllArgument() throws Exception {
final Class<?> entry = Class.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
final Constructor<?> entryConstr = entry.getDeclaredConstructor(String.class, int.class, int.class, int.class, int.class);
entryConstr.setAccessible(true);
final Object entryInstance = entryConstr.newInstance("all", 100, 100, 350, 350);
final List<Object> entriesList = new ArrayList<>();
entriesList.add(entryInstance);
final ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
PowerMockito.doReturn(entriesList).when(threadLocal, "get");
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
final Field entries = holder.getClass().getDeclaredField("ENTRIES");
entries.setAccessible(true);
entries.set(holder, threadLocal);
final Checker source = new Checker();
final LocalizedMessage firstMessageForTest = new LocalizedMessage(100, 10, null, null, null, "id", MemberNameCheck.class, "msg");
final AuditEvent firstEventForTest = new AuditEvent(source, "fileName", firstMessageForTest);
assertFalse(SuppressWarningsHolder.isSuppressed(firstEventForTest));
final LocalizedMessage secondMessageForTest = new LocalizedMessage(100, 150, null, null, null, "id", MemberNameCheck.class, "msg");
final AuditEvent secondEventForTest = new AuditEvent(source, "fileName", secondMessageForTest);
assertTrue(SuppressWarningsHolder.isSuppressed(secondEventForTest));
final LocalizedMessage thirdMessageForTest = new LocalizedMessage(200, 1, null, null, null, "id", MemberNameCheck.class, "msg");
final AuditEvent thirdEventForTest = new AuditEvent(source, "fileName", thirdMessageForTest);
assertTrue(SuppressWarningsHolder.isSuppressed(thirdEventForTest));
}
use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.
the class SuppressionFilterTest method testRemoteFileExternalResourceContentDoesNotChange.
@Test
public void testRemoteFileExternalResourceContentDoesNotChange() throws Exception {
final String[] urlCandidates = { "http://checkstyle.sourceforge.net/files/suppressions_none.xml", "https://raw.githubusercontent.com/checkstyle/checkstyle/master/src/site/resources/" + "files/suppressions_none.xml" };
String urlForTest = null;
for (String url : urlCandidates) {
if (isConnectionAvailableAndStable(url)) {
urlForTest = url;
break;
}
}
// instead of a skip when it doesn't pass
if (urlForTest != null) {
final DefaultConfiguration firstFilterConfig = createCheckConfig(SuppressionFilter.class);
firstFilterConfig.addAttribute("file", urlForTest);
final DefaultConfiguration firstCheckerConfig = new DefaultConfiguration("checkstyle_checks");
firstCheckerConfig.addChild(firstFilterConfig);
final String cacheFile = temporaryFolder.newFile().getPath();
firstCheckerConfig.addAttribute("cacheFile", cacheFile);
final Checker checker = new Checker();
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(firstCheckerConfig);
checker.addListener(new BriefUtLogger(stream));
final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath();
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checker, pathToEmptyFile, expected);
// One more time to use cache.
final DefaultConfiguration secondFilterConfig = createCheckConfig(SuppressionFilter.class);
secondFilterConfig.addAttribute("file", urlForTest);
final DefaultConfiguration secondCheckerConfig = new DefaultConfiguration("checkstyle_checks");
secondCheckerConfig.addAttribute("cacheFile", cacheFile);
secondCheckerConfig.addChild(secondFilterConfig);
checker.configure(secondCheckerConfig);
verify(checker, pathToEmptyFile, expected);
}
}
use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.
the class SuppressWarningsFilterTest method createChecker.
@Override
public Checker createChecker(Configuration checkConfig) throws Exception {
final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
final DefaultConfiguration checksConfig = createCheckConfig(TreeWalker.class);
final DefaultConfiguration holderConfig = createCheckConfig(SuppressWarningsHolder.class);
holderConfig.addAttribute("aliasList", "com.puppycrawl.tools.checkstyle.checks.sizes." + "ParameterNumberCheck=paramnum");
checksConfig.addChild(holderConfig);
final DefaultConfiguration memberNameCheckConfig = createCheckConfig(MemberNameCheck.class);
memberNameCheckConfig.addAttribute("id", "ignore");
checksConfig.addChild(memberNameCheckConfig);
final DefaultConfiguration constantNameCheckConfig = createCheckConfig(ConstantNameCheck.class);
constantNameCheckConfig.addAttribute("id", "");
checksConfig.addChild(constantNameCheckConfig);
checksConfig.addChild(createCheckConfig(ParameterNumberCheck.class));
checksConfig.addChild(createCheckConfig(IllegalCatchCheck.class));
final DefaultConfiguration uncommentedMainCheckConfig = createCheckConfig(UncommentedMainCheck.class);
uncommentedMainCheckConfig.addAttribute("id", "ignore");
checksConfig.addChild(uncommentedMainCheckConfig);
checksConfig.addChild(createCheckConfig(JavadocTypeCheck.class));
checkerConfig.addChild(checksConfig);
if (checkConfig != null) {
checkerConfig.addChild(checkConfig);
}
final Checker checker = new Checker();
final Locale locale = Locale.ROOT;
checker.setLocaleCountry(locale.getCountry());
checker.setLocaleLanguage(locale.getLanguage());
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(checkerConfig);
checker.addListener(new BriefUtLogger(stream));
return checker;
}
Aggregations