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 static org.apache.maven.plugin.surefire.util.ScannerUtil.convertJarFileResourceToJavaClassName;
23  import static org.apache.maven.plugin.surefire.util.ScannerUtil.isJavaClassFile;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.Enumeration;
29  import java.util.LinkedHashSet;
30  import java.util.List;
31  import java.util.Set;
32  import java.util.jar.JarEntry;
33  import java.util.jar.JarFile;
34  
35  import org.apache.maven.artifact.Artifact;
36  import org.apache.maven.plugin.MojoExecutionException;
37  import org.apache.maven.surefire.testset.TestFilter;
38  import org.apache.maven.surefire.testset.TestListResolver;
39  import org.apache.maven.surefire.util.DefaultScanResult;
40  
41  /**
42   * Scans dependencies looking for tests.
43   *
44   * @author Aslak Knutsen
45   */
46  public class DependencyScanner
47  {
48      private final List<File> dependenciesToScan;
49  
50      private final TestListResolver filter;
51  
52      public DependencyScanner( List<File> dependenciesToScan, TestListResolver filter )
53      {
54          this.dependenciesToScan = dependenciesToScan;
55          this.filter = filter;
56      }
57  
58      public DefaultScanResult scan()
59          throws MojoExecutionException
60      {
61          Set<String> classes = new LinkedHashSet<>();
62          for ( File artifact : dependenciesToScan )
63          {
64              if ( artifact != null && artifact.isFile() && artifact.getName().endsWith( ".jar" ) )
65              {
66                  try
67                  {
68                      scanArtifact( artifact, filter, classes );
69                  }
70                  catch ( IOException e )
71                  {
72                      throw new MojoExecutionException( "Could not scan dependency " + artifact.toString(), e );
73                  }
74              }
75          }
76          return new DefaultScanResult( new ArrayList<>( classes ) );
77      }
78  
79      private static void scanArtifact( File artifact, TestFilter<String, String> filter, Set<String> classes )
80          throws IOException
81      {
82          try ( JarFile jar = new JarFile( artifact ) )
83          {
84              for ( Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
85              {
86                  JarEntry entry = entries.nextElement();
87                  String path = entry.getName();
88                  if ( !entry.isDirectory() && isJavaClassFile( path ) && filter.shouldRun( path, null ) )
89                  {
90                      classes.add( convertJarFileResourceToJavaClassName( path ) );
91                  }
92              }
93          }
94      }
95  
96      public static List<Artifact> filter( List<Artifact> artifacts, List<String> groupArtifactIds )
97      {
98          List<Artifact> matches = new ArrayList<>();
99          if ( groupArtifactIds == null || artifacts == null )
100         {
101             return matches;
102         }
103         for ( Artifact artifact : artifacts )
104         {
105             for ( String groups : groupArtifactIds )
106             {
107                 String[] groupArtifact = groups.split( ":" );
108                 if ( groupArtifact.length != 2 )
109                 {
110                     throw new IllegalArgumentException( "dependencyToScan argument should be in format"
111                         + " 'groupid:artifactid': " + groups );
112                 }
113                 if ( artifact.getGroupId().matches( groupArtifact[0] )
114                     && artifact.getArtifactId().matches( groupArtifact[1] ) )
115                 {
116                     matches.add( artifact );
117                 }
118             }
119         }
120         return matches;
121     }
122 }