Search in sources :

Example 6 with Checker

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);
    }
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with Checker

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;
}
Also used : Locale(java.util.Locale) JavadocTypeCheck(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTypeCheck) Checker(com.puppycrawl.tools.checkstyle.Checker) ParameterNumberCheck(com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger) IllegalCatchCheck(com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck)

Example 8 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class ImportControlCheckTest method testCacheWhenUrlExternalResourceContentDoesNotChange.

@Test
public void testCacheWhenUrlExternalResourceContentDoesNotChange() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
    checkConfig.addAttribute("url", getUriString("import-control_one.xml"));
    final Checker checker = createMockCheckerWithCache(checkConfig);
    final String pathToEmptyFile = temporaryFolder.newFile("TestFile.java").getPath();
    final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
    verify(checker, pathToEmptyFile, pathToEmptyFile, expected);
    // One more time to use cache.
    verify(checker, pathToEmptyFile, pathToEmptyFile, expected);
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Test(org.junit.Test)

Example 9 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class ImportControlCheckTest method createMockCheckerWithCache.

private Checker createMockCheckerWithCache(DefaultConfiguration checkConfig) throws IOException, CheckstyleException {
    final DefaultConfiguration treeWalkerConfig = createCheckConfig(TreeWalker.class);
    treeWalkerConfig.addChild(checkConfig);
    final DefaultConfiguration checkerConfig = new DefaultConfiguration("checkstyle_checks");
    checkerConfig.addChild(treeWalkerConfig);
    checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath());
    final Checker checker = new Checker();
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(checkerConfig);
    checker.addListener(new BriefUtLogger(stream));
    return checker;
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger)

Example 10 with Checker

use of com.puppycrawl.tools.checkstyle.Checker in project checkstyle by checkstyle.

the class ImportControlCheckTest method testCacheWhenFileExternalResourceContentDoesNotChange.

@Test
public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
    checkConfig.addAttribute("file", getPath("import-control_one-re.xml"));
    final Checker checker = createMockCheckerWithCache(checkConfig);
    final String filePath = temporaryFolder.newFile("EmptyFile.java").getPath();
    final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
    verify(checker, filePath, filePath, expected);
    // One more time to use cache.
    verify(checker, filePath, filePath, expected);
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Test(org.junit.Test)

Aggregations

Checker (com.puppycrawl.tools.checkstyle.Checker)22 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)17 Test (org.junit.Test)13 BriefUtLogger (com.puppycrawl.tools.checkstyle.BriefUtLogger)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Locale (java.util.Locale)5 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)4 File (java.io.File)4 IllegalCatchCheck (com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck)3 ArrayList (java.util.ArrayList)3 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)2 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)2 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2 LocalizedMessage (com.puppycrawl.tools.checkstyle.api.LocalizedMessage)2 FileContentsHolder (com.puppycrawl.tools.checkstyle.checks.FileContentsHolder)2 Field (java.lang.reflect.Field)2 Properties (java.util.Properties)2 BaseCheckTestSupport (com.puppycrawl.tools.checkstyle.BaseCheckTestSupport)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1 PackageObjectFactory (com.puppycrawl.tools.checkstyle.PackageObjectFactory)1