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.jayway.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 in a directory of compiled Java classes.
32   *
33   * @author hugo.josefson@jayway.com
34   */
35  public class AndroidTestFinder
36  {
37  
38      private static final String[] TEST_PACKAGES = { "junit/framework/", "android/test/" };
39  
40      public static boolean containsAndroidTests( File classesBaseDirectory ) throws MojoExecutionException
41      {
42  
43          if ( classesBaseDirectory == null || ! classesBaseDirectory.isDirectory() )
44          {
45              throw new IllegalArgumentException( "classesBaseDirectory must be a valid directory!" );
46          }
47  
48          final List<File> classFiles = findEligebleClassFiles( classesBaseDirectory );
49          final DescendantFinder descendantFinder = new DescendantFinder( TEST_PACKAGES );
50  
51          for ( File classFile : classFiles )
52          {
53              ClassReader classReader;
54              FileInputStream inputStream = null;
55              try
56              {
57                  inputStream = new FileInputStream( classFile );
58                  classReader = new ClassReader( inputStream );
59  
60                  classReader.accept( descendantFinder, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES
61                          | ClassReader.SKIP_CODE );
62              }
63              catch ( IOException e )
64              {
65                  throw new MojoExecutionException( "Error reading " + classFile + ".\nCould not determine whether it "
66                          + "contains tests. Please specify with plugin config parameter "
67                          + "<enableIntegrationTest>true|false</enableIntegrationTest>.", e );
68              }
69              finally
70              {
71                  IOUtils.closeQuietly( inputStream );
72              }
73          }
74  
75          return descendantFinder.isDescendantFound();
76      }
77  
78      private static List<File> findEligebleClassFiles( File classesBaseDirectory )
79      {
80          final List<File> classFiles = new LinkedList<File>();
81          final DirectoryWalker walker = new DirectoryWalker();
82          walker.setBaseDir( classesBaseDirectory );
83          walker.addSCMExcludes();
84          walker.addInclude( "**/*.class" );
85          walker.addDirectoryWalkListener( new DirectoryWalkListener()
86          {
87              public void directoryWalkStarting( File basedir )
88              {
89              }
90  
91              public void directoryWalkStep( int percentage, File file )
92              {
93                  classFiles.add( file );
94              }
95  
96              public void directoryWalkFinished()
97              {
98              }
99  
100             public void debug( String message )
101             {
102             }
103         } );
104         walker.scan();
105         return classFiles;
106     }
107 
108 
109 }