Home | History | Annotate | Download | only in build
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      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 
     17 package com.android.cts.tradefed.build;
     18 
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.build.IFolderBuildInfo;
     21 
     22 import java.io.File;
     23 import java.io.FileNotFoundException;
     24 
     25 /**
     26  * Helper class for retrieving files from the CTS install.
     27  * <p/>
     28  * Encapsulates the filesystem layout of the CTS installation.
     29  */
     30 public class CtsBuildHelper {
     31 
     32     static final String CTS_DIR_NAME = "android-cts";
     33     /** The root location of the extracted CTS package */
     34     private final File mRootDir;
     35     /** the {@link CTS_DIR_NAME} directory */
     36     private final File mCtsDir;
     37 
     38     /**
     39      * Creates a {@link CtsBuildHelper}.
     40      *
     41      * @param rootDir the parent folder that contains the "android-cts" directory and all its
     42      *            contents.
     43      */
     44     public CtsBuildHelper(File rootDir) {
     45         mRootDir = rootDir;
     46         mCtsDir = new File(mRootDir, CTS_DIR_NAME);
     47     }
     48 
     49     /**
     50      * Alternate {@link CtsBuildHelper} constructor that takes the {@link IFolderBuildInfo}
     51      * representation of a CTS build.
     52      *
     53      * @param build the {@link IFolderBuildInfo}
     54      * @throws FileNotFoundException
     55      */
     56     public CtsBuildHelper(IFolderBuildInfo build) throws FileNotFoundException {
     57         this(build.getRootDir());
     58     }
     59 
     60     /**
     61      * A helper factory method that creates and validates a {@link CtsBuildHelper} given an
     62      * {@link IBuildInfo}.
     63      *
     64      * @param build the {@link IBuildInfo}
     65      * @return the {@link CtsBuildHelper}
     66      * @throws IllegalArgumentException if provided <var>build</var> is not a valid CTS build
     67      */
     68     public static CtsBuildHelper createBuildHelper(IBuildInfo build) {
     69         if (!(build instanceof IFolderBuildInfo)) {
     70             throw new IllegalArgumentException(String.format(
     71                     "Wrong build type. Expected %s, received %s", IFolderBuildInfo.class.getName(),
     72                     build.getClass().getName()));
     73         }
     74         try {
     75             CtsBuildHelper ctsBuild = new CtsBuildHelper((IFolderBuildInfo)build);
     76             ctsBuild.validateStructure();
     77             return ctsBuild;
     78         } catch (FileNotFoundException e) {
     79             throw new IllegalArgumentException("Invalid CTS build provided.", e);
     80         }
     81     }
     82 
     83     /**
     84      * @return a {@link File} representing the parent folder of the CTS installation
     85      */
     86     public File getRootDir() {
     87         return mRootDir;
     88     }
     89 
     90     /**
     91      * @return a {@link File} representing the "android-cts" folder of the CTS installation
     92      */
     93     public File getCtsDir() {
     94         return mCtsDir;
     95     }
     96 
     97     /**
     98      * @return a {@link File} representing the test application file with given name
     99      * @throws FileNotFoundException if file does not exist
    100      */
    101     public File getTestApp(String appFileName) throws FileNotFoundException {
    102         File apkFile = new File(getTestCasesDir(), appFileName);
    103         if (!apkFile.exists()) {
    104             throw new FileNotFoundException(String.format("CTS test app file %s does not exist",
    105                     apkFile.getAbsolutePath()));
    106         }
    107         return apkFile;
    108     }
    109 
    110     private File getRepositoryDir() {
    111         return new File(getCtsDir(), "repository");
    112     }
    113 
    114     /**
    115      * @return a {@link File} representing the results directory.
    116      */
    117     public File getResultsDir() {
    118         return new File(getRepositoryDir(), "results");
    119     }
    120 
    121     /**
    122      * @return a {@link File} representing the directory to store result logs.
    123      */
    124     public File getLogsDir() {
    125         return new File(getRepositoryDir(), "logs");
    126     }
    127 
    128     /**
    129      * @return a {@link File} representing the test cases directory
    130      */
    131     public File getTestCasesDir() {
    132         return new File(getRepositoryDir(), "testcases");
    133     }
    134 
    135     /**
    136      * @return a {@link File} representing the test plan directory
    137      */
    138     public File getTestPlansDir() {
    139         return new File(getRepositoryDir(), "plans");
    140     }
    141 
    142     /**
    143      * @return a {@link File} representing the test plan with given name. note: no attempt will be
    144      * made to ensure the plan actually exists
    145      * @throws FileNotFoundException if plans directory does not exist
    146      */
    147     public File getTestPlanFile(String planName) throws FileNotFoundException {
    148         String ctsPlanRelativePath = String.format("%s.xml", planName);
    149         return new File(getTestPlansDir(), ctsPlanRelativePath);
    150     }
    151 
    152     /**
    153      * Check the validity of the CTS build file system structure.
    154      * @throws FileNotFoundException if any major directories are missing
    155      */
    156     public void validateStructure() throws FileNotFoundException {
    157         if (!getCtsDir().exists()) {
    158             throw new FileNotFoundException(String.format(
    159                     "CTS install folder %s does not exist", getCtsDir().getAbsolutePath()));
    160         }
    161         if (!getTestCasesDir().exists()) {
    162             throw new FileNotFoundException(String.format(
    163                     "CTS test cases folder %s does not exist",
    164                     getTestCasesDir().getAbsolutePath()));
    165         }
    166         if (!getTestPlansDir().exists()) {
    167             throw new FileNotFoundException(String.format(
    168                     "CTS test plans folder %s does not exist",
    169                     getTestPlansDir().getAbsolutePath()));
    170         }
    171     }
    172 }
    173