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 includedAndExcludedTests;
50  
51      private final TestListResolver specificTests;
52  
53      public DependencyScanner( List<File> dependenciesToScan,
54                                TestListResolver includedAndExcludedTests, TestListResolver specificTests )
55      {
56          this.dependenciesToScan = dependenciesToScan;
57          this.includedAndExcludedTests = includedAndExcludedTests;
58          this.specificTests = specificTests;
59      }
60  
61      public DefaultScanResult scan()
62          throws MojoExecutionException
63      {
64          Set<String> classes = new LinkedHashSet<String>();
65          for ( File artifact : dependenciesToScan )
66          {
67              try
68              {
69                  scanArtifact( artifact, includedAndExcludedTests.and( specificTests ), classes );
70              }
71              catch ( IOException e )
72              {
73                  throw new MojoExecutionException( "Could not scan dependency " + artifact.toString(), e );
74              }
75          }
76          return new DefaultScanResult( new ArrayList<String>( classes ) );
77      }
78  
79      private static void scanArtifact( File artifact, TestFilter<String, String> filter, Set<String> classes )
80          throws IOException
81      {
82          if ( artifact != null && artifact.isFile() )
83          {
84              JarFile jar = null;
85              try
86              {
87                  jar = new JarFile( artifact );
88                  for ( Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
89                  {
90                      JarEntry entry = entries.nextElement();
91                      if ( filter.shouldRun( entry.getName(), null ) )
92                      {
93                          classes.add( convertJarFileResourceToJavaClassName( entry.getName() ) );
94                      }
95                  }
96              }
97              finally
98              {
99                  if ( jar != null )
100                 {
101                     jar.close();
102                 }
103             }
104         }
105     }
106 
107     public static List<File> filter( List<Artifact> artifacts, List<String> groupArtifactIds )
108     {
109         List<File> matches = new ArrayList<File>();
110         if ( groupArtifactIds == null || artifacts == null )
111         {
112             return matches;
113         }
114         for ( Artifact artifact : artifacts )
115         {
116             for ( String groups : groupArtifactIds )
117             {
118                 String[] groupArtifact = groups.split( ":" );
119                 if ( groupArtifact.length != 2 )
120                 {
121                     throw new IllegalArgumentException( "dependencyToScan argument should be in format"
122                         + " 'groupid:artifactid': " + groups );
123                 }
124                 if ( artifact.getGroupId().matches( groupArtifact[0] )
125                     && artifact.getArtifactId().matches( groupArtifact[1] ) )
126                 {
127                     matches.add( artifact.getFile() );
128                 }
129             }
130         }
131         return matches;
132     }
133 }