Search in sources :

Example 16 with Checker

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

the class BaseCheckTestSupport method createChecker.

/**
     * Creates {@link Checker} instance based on specified {@link Configuration}.
     * @param checkConfig {@link Configuration} instance.
     * @return {@link Checker} instance.
     * @throws CheckstyleException if an exception occurs during checker configuration.
     */
protected Checker createChecker(Configuration checkConfig) throws Exception {
    final DefaultConfiguration dc = createCheckerConfig(checkConfig);
    final Checker checker = new Checker();
    // make sure the tests always run with English error messages
    // so the tests don't fail in supported locales like German
    final Locale locale = Locale.ENGLISH;
    checker.setLocaleCountry(locale.getCountry());
    checker.setLocaleLanguage(locale.getLanguage());
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(dc);
    checker.addListener(new BriefUtLogger(stream));
    return checker;
}
Also used : Locale(java.util.Locale) Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger)

Example 17 with Checker

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

the class SuppressionCommentFilterTest method createChecker.

@Override
public Checker createChecker(Configuration checkConfig) throws CheckstyleException {
    final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
    final DefaultConfiguration checksConfig = createCheckConfig(TreeWalker.class);
    checksConfig.addChild(createCheckConfig(FileContentsHolder.class));
    final DefaultConfiguration memberNameCheckConfig = createCheckConfig(MemberNameCheck.class);
    memberNameCheckConfig.addAttribute("id", "ignore");
    checksConfig.addChild(memberNameCheckConfig);
    final DefaultConfiguration constantNameCheckConfig = createCheckConfig(ConstantNameCheck.class);
    constantNameCheckConfig.addAttribute("id", null);
    checksConfig.addChild(constantNameCheckConfig);
    checksConfig.addChild(createCheckConfig(IllegalCatchCheck.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) Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger) FileContentsHolder(com.puppycrawl.tools.checkstyle.checks.FileContentsHolder) IllegalCatchCheck(com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck)

Example 18 with Checker

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

the class SuppressionFilterTest method testLocalFileExternalResourceContentDoesNotChange.

@Test
public void testLocalFileExternalResourceContentDoesNotChange() throws Exception {
    final DefaultConfiguration filterConfig = createCheckConfig(SuppressionFilter.class);
    filterConfig.addAttribute("file", getPath("suppressions_none.xml"));
    final DefaultConfiguration checkerConfig = new DefaultConfiguration("checkstyle_checks");
    checkerConfig.addChild(filterConfig);
    final String cacheFile = temporaryFolder.newFile().getPath();
    checkerConfig.addAttribute("cacheFile", cacheFile);
    final Checker checker = new Checker();
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.addListener(new BriefUtLogger(stream));
    checker.configure(checkerConfig);
    final String filePath = temporaryFolder.newFile("file.java").getPath();
    final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
    verify(checker, filePath, expected);
    // One more time to use cache.
    verify(checker, filePath, 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 19 with Checker

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

the class SuppressWithNearbyCommentFilterTest method createChecker.

@Override
public Checker createChecker(Configuration checkConfig) throws CheckstyleException {
    final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
    final DefaultConfiguration checksConfig = createCheckConfig(TreeWalker.class);
    checksConfig.addChild(createCheckConfig(FileContentsHolder.class));
    final DefaultConfiguration memberNameCheckConfig = createCheckConfig(MemberNameCheck.class);
    memberNameCheckConfig.addAttribute("id", "ignore");
    checksConfig.addChild(memberNameCheckConfig);
    final DefaultConfiguration constantNameCheckConfig = createCheckConfig(ConstantNameCheck.class);
    constantNameCheckConfig.addAttribute("id", null);
    checksConfig.addChild(constantNameCheckConfig);
    checksConfig.addChild(createCheckConfig(IllegalCatchCheck.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) Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger) FileContentsHolder(com.puppycrawl.tools.checkstyle.checks.FileContentsHolder) IllegalCatchCheck(com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck)

Example 20 with Checker

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

the class TranslationCheckTest method testLogIoExceptionFileNotFound.

@Test
public void testLogIoExceptionFileNotFound() throws Exception {
    //I can't put wrong file here. Checkstyle fails before check started.
    //I saw some usage of file or handling of wrong file in Checker, or somewhere
    //in checks running part. So I had to do it with reflection to improve coverage.
    final TranslationCheck check = new TranslationCheck();
    final DefaultConfiguration checkConfig = createCheckConfig(TranslationCheck.class);
    check.configure(checkConfig);
    final Checker checker = createChecker(checkConfig);
    check.setMessageDispatcher(checker);
    final Method loadKeys = check.getClass().getDeclaredMethod("getTranslationKeys", File.class);
    loadKeys.setAccessible(true);
    loadKeys.invoke(check, new File(""));
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Method(java.lang.reflect.Method) File(java.io.File) 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