Search in sources :

Example 76 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class OuterTypeFilenameTest method outerTypeFilenameTest3.

@Test
public void outerTypeFilenameTest3() throws Exception {
    final String[] expected = { "3: " + getCheckMessage(OuterTypeFilenameCheck.class, MSG_KEY) };
    final Configuration checkConfig = getCheckConfig("OuterTypeFilename");
    final String filePath = getPath("InputOuterTypeFilename3.java");
    final Integer[] warnList = getLinesWithWarn(filePath);
    verify(checkConfig, filePath, expected, warnList);
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Test(org.junit.Test)

Example 77 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class AvoidEscapedUnicodeCharactersTest method unicodeEscapesTest.

@Test
public void unicodeEscapesTest() throws Exception {
    final String[] expected = { "5: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY), "15: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY), "25: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY), "33: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY), "35: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY), "36: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY) };
    final Configuration checkConfig = getCheckConfig("AvoidEscapedUnicodeCharacters");
    final String filePath = getPath("InputAvoidEscapedUnicodeCharacters.java");
    final Integer[] warnList = getLinesWithWarn(filePath);
    verify(checkConfig, filePath, expected, warnList);
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Test(org.junit.Test)

Example 78 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project maven-plugins by apache.

the class CheckstyleReportGenerator method sortConfiguration.

private void sortConfiguration(List<ConfReference> result, Configuration config, ChainedItem<Configuration> parent, CheckstyleResults results) {
    for (Configuration childConfig : config.getChildren()) {
        String ruleName = childConfig.getName();
        if (treeWalkerNames.contains(ruleName)) {
            // special sub-case: TreeWalker is the parent of multiple rules, not an effective rule
            sortConfiguration(result, childConfig, new ChainedItem<>(config, parent), results);
        } else {
            String fixedmessage = getConfigAttribute(childConfig, null, "message", null);
            // Grab the severity from the rule configuration, use null as default value
            String configSeverity = getConfigAttribute(childConfig, null, "severity", null);
            // count rule violations
            long violations = 0;
            AuditEvent lastMatchedEvent = null;
            for (List<AuditEvent> errors : results.getFiles().values()) {
                for (AuditEvent event : errors) {
                    if (matchRule(event, ruleName, fixedmessage, configSeverity)) {
                        lastMatchedEvent = event;
                        violations++;
                    }
                }
            }
            if (// forget rules without violations
            violations > 0) {
                String category = RuleUtil.getCategory(lastMatchedEvent);
                result.add(new ConfReference(category, childConfig, parent, violations, result.size()));
            }
        }
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent)

Example 79 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project maven-plugins by apache.

the class CheckstyleReportGenerator method doRuleRow.

/**
     * Create a summary for one Checkstyle rule.
     *
     * @param ref The configuration reference for the row
     * @param results The results to summarize
     * @param previousCategory The previous row's category
     */
private void doRuleRow(ConfReference ref, CheckstyleResults results, String previousCategory) {
    Configuration checkerConfig = ref.configuration;
    ChainedItem<Configuration> parentConfiguration = ref.parentConfiguration;
    String ruleName = checkerConfig.getName();
    sink.tableRow();
    // column 1: rule category
    sink.tableCell();
    String category = ref.category;
    if (!category.equals(previousCategory)) {
        sink.text(category);
    }
    sink.tableCell_();
    // column 2: Rule name + configured attributes
    sink.tableCell();
    if (!"extension".equals(category)) {
        sink.link("http://checkstyle.sourceforge.net/config_" + category + ".html#" + ruleName);
        sink.text(ruleName);
        sink.link_();
    } else {
        sink.text(ruleName);
    }
    List<String> attribnames = new ArrayList<>(Arrays.asList(checkerConfig.getAttributeNames()));
    // special value (deserves unique column)
    attribnames.remove("severity");
    if (!attribnames.isEmpty()) {
        sink.list();
        for (String name : attribnames) {
            sink.listItem();
            sink.text(name);
            String value = getConfigAttribute(checkerConfig, null, name, "");
            // special case, Header.header and RegexpHeader.header
            if ("header".equals(name) && ("Header".equals(ruleName) || "RegexpHeader".equals(ruleName))) {
                String[] lines = StringUtils.split(value, "\\n");
                int linenum = 1;
                for (String line : lines) {
                    sink.lineBreak();
                    sink.rawText("<span style=\"color: gray\">");
                    sink.text(linenum + ":");
                    sink.rawText("</span>");
                    sink.nonBreakingSpace();
                    sink.monospaced();
                    sink.text(line);
                    sink.monospaced_();
                    linenum++;
                }
            } else if ("headerFile".equals(name) && "RegexpHeader".equals(ruleName)) {
                sink.text(": ");
                sink.monospaced();
                sink.text("\"");
                if (basedir != null) {
                    // Make the headerFile value relative to ${basedir}
                    String path = siteTool.getRelativePath(value, basedir.getAbsolutePath());
                    sink.text(path.replace('\\', '/'));
                } else {
                    sink.text(value);
                }
                sink.text("\"");
                sink.monospaced_();
            } else {
                sink.text(": ");
                sink.monospaced();
                sink.text("\"");
                sink.text(value);
                sink.text("\"");
                sink.monospaced_();
            }
            sink.listItem_();
        }
        sink.list_();
    }
    sink.tableCell_();
    // column 3: rule violation count
    sink.tableCell();
    sink.text(String.valueOf(ref.violations));
    sink.tableCell_();
    // column 4: severity
    sink.tableCell();
    // Grab the severity from the rule configuration, this time use error as default value
    // Also pass along all parent configurations, so that we can try to find the severity there
    String severity = getConfigAttribute(checkerConfig, parentConfiguration, "severity", "error");
    iconTool.iconSeverity(severity, IconTool.TEXT_SIMPLE);
    sink.tableCell_();
    sink.tableRow_();
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) ArrayList(java.util.ArrayList)

Example 80 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration in project checkstyle by checkstyle.

the class CheckerTest method testSetupChildExceptions.

@Test
public void testSetupChildExceptions() {
    final Checker checker = new Checker();
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    checker.setModuleFactory(factory);
    final Configuration config = new DefaultConfiguration("java.lang.String");
    try {
        checker.setupChild(config);
        fail("Exception is expected");
    } catch (CheckstyleException ex) {
        assertEquals("java.lang.String is not allowed as a child in Checker", ex.getMessage());
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.Test)

Aggregations

Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)137 Test (org.junit.Test)127 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)16 Properties (java.util.Properties)13 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)9 File (java.io.File)9 Checker (com.puppycrawl.tools.checkstyle.Checker)5 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)4 OneTopLevelClassCheck (com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)2 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)2 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)2 AnnotationLocationCheck (com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck)2 EmptyLineSeparatorCheck (com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck)2 MethodParamPadCheck (com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2