Search in sources :

Example 1 with Checker

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);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) StringReader(java.io.StringReader) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) File(java.io.File)

Example 2 with Checker

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);
    }
}
Also used : BaseCheckTestSupport(com.puppycrawl.tools.checkstyle.BaseCheckTestSupport) Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Test(org.junit.Test)

Example 3 with Checker

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));
}
Also used : Field(java.lang.reflect.Field) Checker(com.puppycrawl.tools.checkstyle.Checker) ArrayList(java.util.ArrayList) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 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 5 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)

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