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