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.ddmlib.Log;
     19 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException;
     20 
     21 import java.io.BufferedInputStream;
     22 import java.io.File;
     23 import java.io.FileInputStream;
     24 import java.io.FileNotFoundException;
     25 import java.io.FilenameFilter;
     26 import java.io.InputStream;
     27 import java.util.ArrayList;
     28 import java.util.Collection;
     29 import java.util.Collections;
     30 import java.util.Hashtable;
     31 import java.util.List;
     32 import java.util.Map;
     33 
     34 /**
     35  * Retrieves CTS test package definitions from the repository.
     36  */
     37 public class TestPackageRepo implements ITestPackageRepo {
     38 
     39     private static final String LOG_TAG = "TestCaseRepo";
     40 
     41     private final File mTestCaseDir;
     42 
     43     /** mapping of uri to test definition */
     44     private final Map<String, TestPackageDef> mTestMap;
     45 
     46     private final boolean mIncludeKnownFailures;
     47 
     48     /**
     49      * Creates a {@link TestPackageRepo}, initialized from provided repo files
     50      *
     51      * @param testCaseDir directory containing all test case definition xml and build files
     52      */
     53     public TestPackageRepo(File testCaseDir, boolean includeKnownFailures) {
     54         mTestCaseDir = testCaseDir;
     55         mTestMap = new Hashtable<String, TestPackageDef>();
     56         mIncludeKnownFailures = includeKnownFailures;
     57         parse(mTestCaseDir);
     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             TestPackageDef def = parser.getTestPackageDef();
     75             if (def != null) {
     76                 mTestMap.put(def.getUri(), def);
     77             } else {
     78                 Log.w(LOG_TAG, String.format("Could not find test package info in xml file %s",
     79                         xmlFile.getAbsolutePath()));
     80             }
     81         } catch (FileNotFoundException e) {
     82             Log.e(LOG_TAG, String.format("Could not find test case xml file %s",
     83                     xmlFile.getAbsolutePath()));
     84             Log.e(LOG_TAG, e);
     85         } catch (ParseException e) {
     86             Log.e(LOG_TAG, String.format("Failed to parse test case xml file %s",
     87                     xmlFile.getAbsolutePath()));
     88             Log.e(LOG_TAG, e);
     89         }
     90     }
     91 
     92     /**
     93      * Helper method to create a stream to read data from given file
     94      * <p/>
     95      * Exposed for unit testing
     96      *
     97      * @param xmlFile
     98      * @return stream to read data
     99      *
    100      */
    101     InputStream createStreamFromFile(File xmlFile) throws FileNotFoundException {
    102         return new BufferedInputStream(new FileInputStream(xmlFile));
    103     }
    104 
    105     private static class XmlFilter implements FilenameFilter {
    106 
    107         /**
    108          * {@inheritDoc}
    109          */
    110         @Override
    111         public boolean accept(File dir, String name) {
    112             return name.endsWith(".xml");
    113         }
    114     }
    115 
    116     /**
    117      * {@inheritDoc}
    118      */
    119     @Override
    120     public ITestPackageDef getTestPackage(String testUri) {
    121         return mTestMap.get(testUri);
    122     }
    123 
    124     /**
    125      * {@inheritDoc}
    126      */
    127     @Override
    128     public String findPackageForTest(String testClassName) {
    129         for (Map.Entry<String, TestPackageDef> entry : mTestMap.entrySet()) {
    130             if (entry.getValue().isKnownTestClass(testClassName)) {
    131                 return entry.getKey();
    132             }
    133         }
    134         return null;
    135     }
    136 
    137     /**
    138      * @return list of all package names found in repo
    139      */
    140     @Override
    141     public Collection<String> getPackageNames() {
    142         List<String> packageNames = new ArrayList<String>();
    143         packageNames.addAll(mTestMap.keySet());
    144         Collections.sort(packageNames);
    145         return packageNames;
    146     }
    147 }
    148