1 package com.android.cts.migration; 2 3 import com.android.tradefed.build.IBuildInfo; 4 import com.android.tradefed.log.LogUtil.CLog; 5 6 import java.io.File; 7 import java.io.FileNotFoundException; 8 import java.io.IOException; 9 import java.lang.reflect.Constructor; 10 import java.lang.reflect.InvocationTargetException; 11 import java.lang.reflect.Method; 12 13 /** 14 * A temporary helper to enable tests to work with both cts v1 and v2. 15 */ 16 public class MigrationHelper { 17 18 private static final String COMPATIBILITY_BUILD_HELPER = 19 "com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper"; 20 private static final String CTS_BUILD_HELPER = 21 "com.android.cts.tradefed.build.CtsBuildHelper"; 22 getTestFile(IBuildInfo mBuild, String filename)23 public static File getTestFile(IBuildInfo mBuild, String filename) 24 throws FileNotFoundException { 25 try { 26 Class<?> cls = Class.forName(COMPATIBILITY_BUILD_HELPER); 27 Constructor<?> cons = cls.getConstructor(IBuildInfo.class); 28 Object instance = cons.newInstance(mBuild); 29 Method method = cls.getMethod("getTestsDir"); 30 File dir = (File) method.invoke(instance); 31 File file = new File(dir, filename); 32 CLog.i("Looking for test file %s in dir %s", filename, dir.getAbsolutePath()); 33 if (file.exists()) { 34 CLog.i("File %s found", filename); 35 return file; 36 } 37 } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | 38 IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 39 // Ignore and fall back to CtsBuildHelper 40 } 41 try { 42 Class<?> cls = Class.forName(CTS_BUILD_HELPER); 43 Method builder = cls.getMethod("createBuildHelper", IBuildInfo.class); 44 Object helper = builder.invoke(null, mBuild); 45 Method method = cls.getMethod("getTestApp", String.class); 46 File file = (File) method.invoke(helper, filename); 47 CLog.i("Looking for test file %s as %s", filename, file.getAbsolutePath()); 48 if (file.exists()) { 49 CLog.i("File %s found", filename); 50 return file; 51 } 52 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | 53 IllegalArgumentException | InvocationTargetException e) { 54 // Ignore 55 } 56 throw new FileNotFoundException("Couldn't load file " + filename); 57 } 58 59 } 60