1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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 }