Home | History | Annotate | Download | only in testtype
      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 package com.android.cts.tradefed.testtype;
     17 
     18 import com.android.cts.util.AbiUtils;
     19 import com.android.ddmlib.Log;
     20 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException;
     21 
     22 import java.io.BufferedInputStream;
     23 import java.io.File;
     24 import java.io.FileInputStream;
     25 import java.io.FileNotFoundException;
     26 import java.io.FilenameFilter;
     27 import java.io.InputStream;
     28 import java.util.ArrayList;
     29 import java.util.Collections;
     30 import java.util.HashSet;
     31 import java.util.HashMap;
     32 import java.util.List;
     33 import java.util.Map;
     34 import java.util.Set;
     35 
     36 /**
     37  * Retrieves CTS test package definitions from the repository.
     38  */
     39 public class TestPackageRepo implements ITestPackageRepo {
     40 
     41     private static final String LOG_TAG = "TestCaseRepo";
     42 
     43     /** mapping of ABI to a mapping of appPackageName to test definition */
     44     private final Map<String, Map<String, TestPackageDef>> mTestMap;
     45     private final boolean mIncludeKnownFailures;
     46 
     47     /**
     48      * Creates a {@link TestPackageRepo}, initialized from provided repo files
     49      *
     50      * @param testCaseDir directory containing all test case definition xml and build files
     51      * ABIs supported by the device under test.
     52      * @param includeKnownFailures Whether to run tests which are known to fail.
     53      */
     54     public TestPackageRepo(File testCaseDir, boolean includeKnownFailures) {
     55         mTestMap = new HashMap<>();
     56         mIncludeKnownFailures = includeKnownFailures;
     57         parse(testCaseDir);
     58     }
     59 
     60     /**
     61      * Builds mTestMap based on directory contents
     62      */
     63     private void parse(File dir) {
     64         File[] xmlFiles = dir.listFiles(new XmlFilter());
     65         for (File xmlFile : xmlFiles) {
     66             parseTestFromXml(xmlFile);
     67         }
     68     }
     69 
     70     private void parseTestFromXml(File xmlFile)  {
     71         TestPackageXmlParser parser = new TestPackageXmlParser(mIncludeKnownFailures);
     72         try {
     73             parser.parse(createStreamFromFile(xmlFile));
     74             Set<TestPackageDef> defs = parser.getTestPackageDefs();
     75             if (defs.isEmpty()) {
     76                 Log.w(LOG_TAG, String.format("Could not find test package info in xml file %s",
     77                         xmlFile.getAbsolutePath()));
     78             }
     79             for (TestPackageDef def : defs) {
     80                 String name = def.getAppPackageName();
     81                 String abi = def.getAbi().getName();
     82                 if (!mTestMap.containsKey(abi)) {
     83                     mTestMap.put(abi, new HashMap<String, TestPackageDef>());
     84                 }
     85                 mTestMap.get(abi).put(name, def);
     86             }
     87         } catch (FileNotFoundException e) {
     88             Log.e(LOG_TAG, String.format("Could not find test case xml file %s",
     89                     xmlFile.getAbsolutePath()));
     90             Log.e(LOG_TAG, e);
     91         } catch (ParseException e) {
     92             Log.e(LOG_TAG, String.format("Failed to parse test case xml file %s",
     93                     xmlFile.getAbsolutePath()));
     94             Log.e(LOG_TAG, e);
     95         }
     96     }
     97 
     98     /**
     99      * Helper method to create a stream to read data from given file
    100      * <p/>
    101      * Exposed for unit testing
    102      *
    103      * @param xmlFile The file containing the xml description of the package
    104      * @return stream to read data
    105      *
    106      */
    107     InputStream createStreamFromFile(File xmlFile) throws FileNotFoundException {
    108         return new BufferedInputStream(new FileInputStream(xmlFile));
    109     }
    110 
    111     private static class XmlFilter implements FilenameFilter {
    112 
    113         /**
    114          * {@inheritDoc}
    115          */
    116         @Override
    117         public boolean accept(File dir, String name) {
    118             return name.endsWith(".xml");
    119         }
    120     }
    121 
    122     /**
    123      * {@inheritDoc}
    124      */
    125     @Override
    126     public ITestPackageDef getTestPackage(String id) {
    127         String[] parts = AbiUtils.parseId(id);
    128         String abi = parts[0];
    129         String name = parts[1];
    130         if (mTestMap.containsKey(abi) && mTestMap.get(abi).containsKey(name)) {
    131             return mTestMap.get(abi).get(name);
    132         }
    133         return null;
    134     }
    135 
    136     /**
    137      * {@inheritDoc}
    138      */
    139     @Override
    140     public List<String> getPackageIds() {
    141         Set<String> ids = new HashSet<>();
    142         for (String abi : mTestMap.keySet()) {
    143             Map<String, TestPackageDef> testNameMap = mTestMap.get(abi);
    144             for (TestPackageDef testPackageDef : testNameMap.values()) {
    145                 ids.add(testPackageDef.getId());
    146             }
    147         }
    148         List<String> idList = new ArrayList<>(ids);
    149         Collections.sort(idList);
    150         return idList;
    151     }
    152 
    153     /**
    154      * {@inheritDoc}
    155      */
    156     @Override
    157     public List<String> getPackageNames() {
    158         Set<String> nameSet = new HashSet<String>();
    159         for (String abi : mTestMap.keySet()) {
    160             Map<String, TestPackageDef> testNameMap = mTestMap.get(abi);
    161             for (TestPackageDef testPackageDef : testNameMap.values()) {
    162                 nameSet.add(AbiUtils.parseTestName(testPackageDef.getId()));
    163             }
    164         }
    165         List<String> nameList = new ArrayList<>(nameSet);
    166         Collections.sort(nameList);
    167         return nameList;
    168     }
    169 
    170     /**
    171      * {@inheritDoc}
    172      */
    173     @Override
    174     public Map<String, List<ITestPackageDef>> getTestPackageDefsByName() {
    175         Map<String, List<ITestPackageDef>> packageDefMap =
    176                 new HashMap<String, List<ITestPackageDef>>();
    177 
    178         for (String abi : mTestMap.keySet()) {
    179             Map<String, TestPackageDef> testNameMap = mTestMap.get(abi);
    180             for (String packageName : testNameMap.keySet()) {
    181                 if (!packageDefMap.containsKey(packageName)) {
    182                     packageDefMap.put(packageName, new ArrayList<ITestPackageDef>());
    183                 }
    184                 packageDefMap.get(packageName).add(testNameMap.get(packageName));
    185             }
    186         }
    187         return packageDefMap;
    188     }
    189 
    190     /**
    191      * {@inheritDoc}
    192      */
    193     @Override
    194     public List<String> findPackageIdsForTest(String testClassName) {
    195         Set<String> ids = new HashSet<String>();
    196         for (String abi : mTestMap.keySet()) {
    197             for (String name : mTestMap.get(abi).keySet()) {
    198                 if (mTestMap.get(abi).get(name).isKnownTestClass(testClassName)) {
    199                     ids.add(AbiUtils.createId(abi, name));
    200                 }
    201             }
    202         }
    203         List<String> idList = new ArrayList<String>(ids);
    204         Collections.sort(idList);
    205         return idList;
    206     }
    207 }
    208