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