View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.util;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.Set;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarFile;
31  
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
35  import org.apache.maven.surefire.api.testset.TestFilter;
36  import org.apache.maven.surefire.api.testset.TestListResolver;
37  import org.apache.maven.surefire.api.util.DefaultScanResult;
38  
39  import static org.apache.maven.plugin.surefire.util.ScannerUtil.convertJarFileResourceToJavaClassName;
40  import static org.apache.maven.plugin.surefire.util.ScannerUtil.isJavaClassFile;
41  
42  /**
43   * Scans dependencies looking for tests.
44   *
45   * @author Aslak Knutsen
46   */
47  public class DependencyScanner {
48      private final List<File> dependenciesToScan;
49  
50      private final TestListResolver filter;
51  
52      public DependencyScanner(List<File> dependenciesToScan, TestListResolver filter) {
53          this.dependenciesToScan = dependenciesToScan;
54          this.filter = filter;
55      }
56  
57      public DefaultScanResult scan() throws MojoExecutionException {
58          Set<String> classes = new LinkedHashSet<>();
59          for (File artifact : dependenciesToScan) {
60              if (artifact != null && artifact.isFile() && artifact.getName().endsWith(".jar")) {
61                  try {
62                      scanArtifact(artifact, filter, classes);
63                  } catch (IOException e) {
64                      throw new MojoExecutionException("Could not scan dependency " + artifact.toString(), e);
65                  }
66              }
67          }
68          return new DefaultScanResult(new ArrayList<>(classes));
69      }
70  
71      private static void scanArtifact(File artifact, TestFilter<String, String> filter, Set<String> classes)
72              throws IOException {
73          try (JarFile jar = new JarFile(artifact)) {
74              for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); ) {
75                  JarEntry entry = entries.nextElement();
76                  String path = entry.getName();
77                  if (!entry.isDirectory() && isJavaClassFile(path) && filter.shouldRun(path, null)) {
78                      classes.add(convertJarFileResourceToJavaClassName(path));
79                  }
80              }
81          }
82      }
83  
84      /**
85       *
86       * @param artifacts a list to filter
87       * @param artifactPatterns a list of strings in the form
88       *                         <pre>groupId[:artifactId[:type[:classifier][:version]]]</pre>
89       * @return list of items from <code>artifacts</code> that match any of the filters in <code>groupArtifactIds</code>,
90       * empty if none match
91       */
92      public static List<Artifact> filter(List<Artifact> artifacts, List<String> artifactPatterns) {
93          if (artifactPatterns == null || artifacts == null || artifacts.isEmpty()) {
94              return Collections.emptyList();
95          }
96  
97          PatternIncludesArtifactFilter artifactFilter = new PatternIncludesArtifactFilter(artifactPatterns);
98  
99          List<Artifact> matches = new ArrayList<>();
100         for (Artifact artifact : artifacts) {
101             if (artifactFilter.include(artifact)) {
102                 matches.add(artifact);
103             }
104         }
105         return matches;
106     }
107 }