Search in sources :

Example 16 with AuditListener

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

the class CheckerTest method testSetupChildListener.

@Test
public void testSetupChildListener() throws Exception {
    final Checker checker = new Checker();
    final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
    checker.setModuleFactory(factory);
    final Configuration config = new DefaultConfiguration(DebugAuditAdapter.class.getCanonicalName());
    checker.setupChild(config);
    final List<AuditListener> listeners = TestUtil.getInternalState(checker, "listeners");
    assertWithMessage("Invalid child listener class").that(listeners.get(listeners.size() - 1) instanceof DebugAuditAdapter).isTrue();
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) Test(org.junit.jupiter.api.Test)

Example 17 with AuditListener

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

the class MainTest method testSarifOutputFormatCreateListener.

@Test
public void testSarifOutputFormatCreateListener() throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final AuditListener listener = Main.OutputFormat.SARIF.createListener(out, AutomaticBean.OutputStreamOptions.CLOSE);
    assertWithMessage("listener is SarifLogger").that(listener instanceof SarifLogger).isTrue();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) Test(org.junit.jupiter.api.Test)

Example 18 with AuditListener

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

AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 File (java.io.File)5 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)4 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)4 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)4 OutputStream (java.io.OutputStream)4 Test (org.junit.jupiter.api.Test)4 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)2 XMLLogger (com.puppycrawl.tools.checkstyle.XMLLogger)2 Properties (java.util.Properties)2 DependencyInformationConsumerInjector (com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector)1 DependencyInformationConsumer (com.github.sevntu.checkstyle.module.DependencyInformationConsumer)1 MethodCallDependencyCheckstyleModule (com.github.sevntu.checkstyle.module.MethodCallDependencyCheckstyleModule)1 ViolationReporterDependencyInformationConsumer (com.github.sevntu.checkstyle.module.ViolationReporterDependencyInformationConsumer)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1