Search in sources :

Example 1 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration 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 2 with Configuration

use of com.puppycrawl.tools.checkstyle.api.Configuration 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 3 with Configuration

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

the class Main method runCheckstyle.

/**
     * Executes required Checkstyle actions based on passed parameters.
     * @param cliOptions
     *        pojo object that contains all options
     * @return number of violations of ERROR level
     * @throws FileNotFoundException
     *         when output file could not be found
     * @throws CheckstyleException
     *         when properties file could not be loaded
     */
private static int runCheckstyle(CliOptions cliOptions) throws CheckstyleException, FileNotFoundException {
    // setup the properties
    final Properties props;
    if (cliOptions.propertiesLocation == null) {
        props = System.getProperties();
    } else {
        props = loadProperties(new File(cliOptions.propertiesLocation));
    }
    // create a configuration
    final Configuration config = ConfigurationLoader.loadConfiguration(cliOptions.configLocation, new PropertiesExpander(props));
    // create a listener for output
    final AuditListener listener = createListener(cliOptions.format, cliOptions.outputLocation);
    // create RootModule object and run it
    int errorCounter = 0;
    final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
    final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);
    try {
        rootModule.setModuleClassLoader(moduleClassLoader);
        rootModule.configure(config);
        rootModule.addListener(listener);
        // run RootModule
        errorCounter = rootModule.process(cliOptions.files);
    } finally {
        rootModule.destroy();
    }
    return errorCounter;
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) Properties(java.util.Properties) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) File(java.io.File)

Example 4 with Configuration

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

the class CatchParameterNameCheckTest method testDefaultConfigurationOnFileWithViolations.

@Test
public void testDefaultConfigurationOnFileWithViolations() throws Exception {
    final Configuration checkConfig = createCheckConfig(CatchParameterNameCheck.class);
    final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+)$";
    final String[] expected = { "18:28: " + getCheckMessage(MSG_INVALID_PATTERN, "exception1", defaultFormat), "28:39: " + getCheckMessage(MSG_INVALID_PATTERN, "ie", defaultFormat), "31:28: " + getCheckMessage(MSG_INVALID_PATTERN, "iException", defaultFormat), "34:28: " + getCheckMessage(MSG_INVALID_PATTERN, "ok", defaultFormat), "38:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e1", defaultFormat), "40:32: " + getCheckMessage(MSG_INVALID_PATTERN, "e2", defaultFormat), "44:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t1", defaultFormat), "46:32: " + getCheckMessage(MSG_INVALID_PATTERN, "t2", defaultFormat) };
    verify(checkConfig, getPath("InputCatchParameterName.java"), expected);
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Test(org.junit.Test)

Example 5 with Configuration

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

the class PropertyCacheFileTest method testNonExistingResource.

/**
     * This SuppressWarning("unchecked") required to suppress
     * "Unchecked generics array creation for varargs parameter" during mock
     * @throws IOException when smth wrong with file creation or cache.load
     */
@SuppressWarnings("unchecked")
@Test
public void testNonExistingResource() throws IOException {
    final Configuration config = new DefaultConfiguration("myName");
    final String filePath = temporaryFolder.newFile().getPath();
    final PropertyCacheFile cache = new PropertyCacheFile(config, filePath);
    // create cache with one file
    cache.load();
    final String myFile = "myFile";
    cache.put(myFile, 1);
    final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY);
    assertNotNull(hash);
    mockStatic(ByteStreams.class);
    when(ByteStreams.toByteArray(any(BufferedInputStream.class))).thenThrow(IOException.class);
    // apply new external resource to clear cache
    final Set<String> resources = new HashSet<>();
    final String resource = "/com/puppycrawl/tools/checkstyle/java.header";
    resources.add(resource);
    cache.putExternalResources(resources);
    assertFalse(cache.isInCache(myFile, 1));
    assertFalse(cache.isInCache(resource, 1));
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) BufferedInputStream(java.io.BufferedInputStream) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)307 Test (org.junit.jupiter.api.Test)179 Test (org.junit.Test)100 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)34 Properties (java.util.Properties)21 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)16 File (java.io.File)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)13 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)11 Checker (com.puppycrawl.tools.checkstyle.Checker)9 HashSet (java.util.HashSet)8 InputSource (org.xml.sax.InputSource)7 OneTopLevelClassCheck (com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)5 URI (java.net.URI)5 MethodParamPadCheck (com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck)4 Set (java.util.Set)4 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)3