Search in sources :

Example 6 with FilterSet

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

the class FilterSetTest method testGetFilters2.

@Test
public void testGetFilters2() {
    final FilterSet filterSet = new FilterSet();
    filterSet.addFilter(new SeverityMatchFilter());
    assertEquals("size is the same", 1, filterSet.getFilters().size());
}
Also used : FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) Test(org.junit.Test)

Example 7 with FilterSet

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

the class SuppressionsLoaderTest method loadFilterSet.

private static FilterSet loadFilterSet(String url) throws Exception {
    FilterSet filterSet = null;
    if (isUrlReachable(url)) {
        int attemptCount = 0;
        final int attemptLimit = 5;
        while (attemptCount <= attemptLimit) {
            try {
                filterSet = SuppressionsLoader.loadSuppressions(url);
                break;
            } catch (CheckstyleException ex) {
                // for some reason Travis CI failed some times(unstable) on reading this file
                if (attemptCount < attemptLimit && ex.getMessage().contains("Unable to read")) {
                    attemptCount++;
                    // wait for bad/disconnection time to pass
                    Thread.sleep(1000);
                } else {
                    throw ex;
                }
            }
        }
    }
    return filterSet;
}
Also used : FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 8 with FilterSet

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

the class SuppressionsLoaderTest method testNoSuppressions.

@Test
public void testNoSuppressions() throws Exception {
    final FilterSet fc = SuppressionsLoader.loadSuppressions(getPath("InputSuppressionsLoaderNone.xml"));
    final FilterSet fc2 = new FilterSet();
    assertWithMessage("No suppressions should be loaded, but found: " + fc.getFilters().size()).that(fc.getFilters()).isEqualTo(fc2.getFilters());
}
Also used : FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) Test(org.junit.jupiter.api.Test)

Example 9 with FilterSet

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

the class SuppressionsLoaderTest method testNoCheckYesId.

@Test
public void testNoCheckYesId() throws Exception {
    final String fn = getPath("InputSuppressionsLoaderId.xml");
    final FilterSet set = SuppressionsLoader.loadSuppressions(fn);
    assertWithMessage("Invalid number of filters").that(set.getFilters()).hasSize(1);
}
Also used : FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) Test(org.junit.jupiter.api.Test)

Example 10 with FilterSet

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

the class DefaultCheckstyleExecutor method executeCheckstyle.

public CheckstyleResults executeCheckstyle(CheckstyleExecutorRequest request) throws CheckstyleExecutorException, CheckstyleException {
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("executeCheckstyle start headerLocation : " + request.getHeaderLocation());
    }
    MavenProject project = request.getProject();
    configureResourceLocator(locator, request, null);
    configureResourceLocator(licenseLocator, request, request.getLicenseArtifacts());
    // Config is less critical than License, locator can still be used.
    // configureResourceLocator( configurationLocator, request, request.getConfigurationArtifacts() );
    List<File> files;
    try {
        files = getFilesToProcess(request);
    } catch (IOException e) {
        throw new CheckstyleExecutorException("Error getting files to process", e);
    }
    final String suppressionsFilePath = getSuppressionsFilePath(request);
    FilterSet filterSet = getSuppressionsFilterSet(suppressionsFilePath);
    Checker checker = new Checker();
    // setup classloader, needed to avoid "Unable to get class information for ..." errors
    List<String> classPathStrings = new ArrayList<>();
    List<String> outputDirectories = new ArrayList<>();
    // stand-alone
    Collection<File> sourceDirectories = null;
    Collection<File> testSourceDirectories = request.getTestSourceDirectories();
    // aggregator
    Map<MavenProject, Collection<File>> sourceDirectoriesByProject = new HashMap<>();
    Map<MavenProject, Collection<File>> testSourceDirectoriesByProject = new HashMap<>();
    if (request.isAggregate()) {
        for (MavenProject childProject : request.getReactorProjects()) {
            sourceDirectories = new ArrayList<>(childProject.getCompileSourceRoots().size());
            List<String> compileSourceRoots = childProject.getCompileSourceRoots();
            for (String compileSourceRoot : compileSourceRoots) {
                sourceDirectories.add(new File(compileSourceRoot));
            }
            sourceDirectoriesByProject.put(childProject, sourceDirectories);
            testSourceDirectories = new ArrayList<>(childProject.getTestCompileSourceRoots().size());
            List<String> testCompileSourceRoots = childProject.getTestCompileSourceRoots();
            for (String testCompileSourceRoot : testCompileSourceRoots) {
                testSourceDirectories.add(new File(testCompileSourceRoot));
            }
            testSourceDirectoriesByProject.put(childProject, testSourceDirectories);
            prepareCheckstylePaths(request, childProject, classPathStrings, outputDirectories, sourceDirectories, testSourceDirectories);
        }
    } else {
        sourceDirectories = request.getSourceDirectories();
        prepareCheckstylePaths(request, project, classPathStrings, outputDirectories, sourceDirectories, testSourceDirectories);
    }
    final List<URL> urls = new ArrayList<>(classPathStrings.size());
    for (String path : classPathStrings) {
        try {
            urls.add(new File(path).toURI().toURL());
        } catch (MalformedURLException e) {
            throw new CheckstyleExecutorException(e.getMessage(), e);
        }
    }
    for (String outputDirectoryString : outputDirectories) {
        try {
            if (outputDirectoryString != null) {
                File outputDirectoryFile = new File(outputDirectoryString);
                if (outputDirectoryFile.exists()) {
                    URL outputDirectoryUrl = outputDirectoryFile.toURI().toURL();
                    getLogger().debug("Adding the outputDirectory " + outputDirectoryUrl.toString() + " to the Checkstyle class path");
                    urls.add(outputDirectoryUrl);
                }
            }
        } catch (MalformedURLException e) {
            throw new CheckstyleExecutorException(e.getMessage(), e);
        }
    }
    URLClassLoader projectClassLoader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {

        public URLClassLoader run() {
            return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
        }
    });
    checker.setClassLoader(projectClassLoader);
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    if (filterSet != null) {
        checker.addFilter(filterSet);
    }
    Configuration configuration = getConfiguration(request);
    checker.configure(configuration);
    AuditListener listener = request.getListener();
    if (listener != null) {
        checker.addListener(listener);
    }
    if (request.isConsoleOutput()) {
        checker.addListener(request.getConsoleListener());
    }
    CheckstyleCheckerListener checkerListener = new CheckstyleCheckerListener(configuration);
    if (request.isAggregate()) {
        for (MavenProject childProject : request.getReactorProjects()) {
            sourceDirectories = sourceDirectoriesByProject.get(childProject);
            testSourceDirectories = testSourceDirectoriesByProject.get(childProject);
            addSourceDirectory(checkerListener, sourceDirectories, testSourceDirectories, childProject.getResources(), request);
        }
    } else {
        addSourceDirectory(checkerListener, sourceDirectories, testSourceDirectories, request.getResources(), request);
    }
    checker.addListener(checkerListener);
    int nbErrors = checker.process(files);
    checker.destroy();
    if (projectClassLoader instanceof Closeable) {
        try {
            ((Closeable) projectClassLoader).close();
        } catch (IOException ex) {
            // Nothing we can do - and not detrimental to the build (save running out of file handles).
            getLogger().info("Failed to close custom Classloader - this indicated a bug in the code.", ex);
        }
    }
    if (request.getStringOutputStream() != null) {
        String message = request.getStringOutputStream().toString().trim();
        if (message.length() > 0) {
            getLogger().info(message);
        }
    }
    if (nbErrors > 0) {
        StringBuilder message = new StringBuilder("There ");
        if (nbErrors == 1) {
            message.append("is");
        } else {
            message.append("are");
        }
        message.append(" ");
        message.append(nbErrors);
        message.append(" error");
        if (nbErrors != 1) {
            message.append("s");
        }
        message.append(" reported by Checkstyle");
        String version = getCheckstyleVersion();
        if (version != null) {
            message.append(" ");
            message.append(version);
        }
        message.append(" with ");
        message.append(request.getConfigLocation());
        message.append(" ruleset.");
        if (request.isFailsOnError()) {
            // work regardless of config), but should record this information
            throw new CheckstyleExecutorException(message.toString());
        } else {
            getLogger().info(message.toString());
        }
    }
    return checkerListener.getResults();
}
Also used : MalformedURLException(java.net.MalformedURLException) FilterSet(com.puppycrawl.tools.checkstyle.api.FilterSet) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) HashMap(java.util.HashMap) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) URL(java.net.URL) MavenProject(org.apache.maven.project.MavenProject) Checker(com.puppycrawl.tools.checkstyle.Checker) IOException(java.io.IOException) URLClassLoader(java.net.URLClassLoader) Collection(java.util.Collection) File(java.io.File)

Aggregations

FilterSet (com.puppycrawl.tools.checkstyle.api.FilterSet)10 Test (org.junit.jupiter.api.Test)6 Test (org.junit.Test)2 Checker (com.puppycrawl.tools.checkstyle.Checker)1 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)1 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 Closeable (java.io.Closeable)1 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 MavenProject (org.apache.maven.project.MavenProject)1