All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.plugin.surefire.util.DependencyScanner Maven / Gradle / Ivy

There is a newer version: 3.5.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.maven.plugin.surefire.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
import org.apache.maven.surefire.api.testset.TestFilter;
import org.apache.maven.surefire.api.testset.TestListResolver;
import org.apache.maven.surefire.api.util.DefaultScanResult;

import static org.apache.maven.plugin.surefire.util.ScannerUtil.convertJarFileResourceToJavaClassName;
import static org.apache.maven.plugin.surefire.util.ScannerUtil.isJavaClassFile;

/**
 * Scans dependencies looking for tests.
 *
 * @author Aslak Knutsen
 */
public class DependencyScanner {
    private final List dependenciesToScan;

    private final TestListResolver filter;

    public DependencyScanner(List dependenciesToScan, TestListResolver filter) {
        this.dependenciesToScan = dependenciesToScan;
        this.filter = filter;
    }

    public DefaultScanResult scan() throws MojoExecutionException {
        Set classes = new LinkedHashSet<>();
        for (File artifact : dependenciesToScan) {
            if (artifact != null && artifact.isFile() && artifact.getName().endsWith(".jar")) {
                try {
                    scanArtifact(artifact, filter, classes);
                } catch (IOException e) {
                    throw new MojoExecutionException("Could not scan dependency " + artifact.toString(), e);
                }
            }
        }
        return new DefaultScanResult(new ArrayList<>(classes));
    }

    private static void scanArtifact(File artifact, TestFilter filter, Set classes)
            throws IOException {
        try (JarFile jar = new JarFile(artifact)) {
            for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
                JarEntry entry = entries.nextElement();
                String path = entry.getName();
                if (!entry.isDirectory() && isJavaClassFile(path) && filter.shouldRun(path, null)) {
                    classes.add(convertJarFileResourceToJavaClassName(path));
                }
            }
        }
    }

    /**
     *
     * @param artifacts a list to filter
     * @param artifactPatterns a list of strings in the form
     *                         
groupId[:artifactId[:type[:classifier][:version]]]
* @return list of items from artifacts that match any of the filters in groupArtifactIds, * empty if none match */ public static List filter(List artifacts, List artifactPatterns) { if (artifactPatterns == null || artifacts == null || artifacts.isEmpty()) { return Collections.emptyList(); } PatternIncludesArtifactFilter artifactFilter = new PatternIncludesArtifactFilter(artifactPatterns); List matches = new ArrayList<>(); for (Artifact artifact : artifacts) { if (artifactFilter.include(artifact)) { matches.add(artifact); } } return matches; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy