View Javadoc
1   /*
2    * Copyright (C) 2009 Jayway AB
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.simpligility.maven.plugins.android.asm;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.codehaus.plexus.util.DirectoryWalkListener;
21  import org.codehaus.plexus.util.DirectoryWalker;
22  import org.objectweb.asm.ClassReader;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.util.LinkedList;
28  import java.util.List;
29  
30  /**
31   * Finds Android instrumentation test classes to be run by InstrumentationTestRunner 
32   * in a directory of compiled Java classes.
33   *
34   * @author hugo.josefson@jayway.com
35   */
36  public class AndroidTestFinder
37  {
38  
39      private static final String[] TEST_PACKAGES = { "junit/framework/", "android/test/" };
40  
41      public static boolean containsAndroidTests( File classesBaseDirectory ) throws MojoExecutionException
42      {
43  
44          if ( classesBaseDirectory == null || ! classesBaseDirectory.isDirectory() )
45          {
46              throw new IllegalArgumentException( "classesBaseDirectory must be a valid directory!" );
47          }
48  
49          final List<File> classFiles = findEligebleClassFiles( classesBaseDirectory );
50          final DescendantFinder descendantFinder = new DescendantFinder( TEST_PACKAGES );
51          final AnnotatedFinder annotationFinder = new AnnotatedFinder( TEST_PACKAGES );
52  
53          for ( File classFile : classFiles )
54          {
55              ClassReader classReader;
56              FileInputStream inputStream = null;
57              try
58              {
59                  inputStream = new FileInputStream( classFile );
60                  classReader = new ClassReader( inputStream );
61  
62                  classReader.accept( descendantFinder, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES
63                          | ClassReader.SKIP_CODE );
64                  classReader.accept( annotationFinder, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES
65                          | ClassReader.SKIP_CODE );
66              }
67              catch ( IOException e )
68              {
69                  throw new MojoExecutionException( "Error reading " + classFile + ".\nCould not determine whether it "
70                          + "contains tests. Please specify with plugin config parameter "
71                          + "<enableIntegrationTest>true|false</enableIntegrationTest>.", e );
72              }
73              finally
74              {
75                  IOUtils.closeQuietly( inputStream );
76              }
77          }
78  
79          return descendantFinder.isDescendantFound() || annotationFinder.isDescendantFound();
80      }
81  
82      private static List<File> findEligebleClassFiles( File classesBaseDirectory )
83      {
84          final List<File> classFiles = new LinkedList<File>();
85          final DirectoryWalker walker = new DirectoryWalker();
86          walker.setBaseDir( classesBaseDirectory );
87          walker.addSCMExcludes();
88          walker.addInclude( "**/*.class" );
89          walker.addDirectoryWalkListener( new DirectoryWalkListener()
90          {
91              public void directoryWalkStarting( File basedir )
92              {
93              }
94  
95              public void directoryWalkStep( int percentage, File file )
96              {
97                  classFiles.add( file );
98              }
99  
100             public void directoryWalkFinished()
101             {
102             }
103 
104             public void debug( String message )
105             {
106             }
107         } );
108         walker.scan();
109         return classFiles;
110     }
111 
112 
113 }