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