Home | History | Annotate | Download | only in result
      1 /*
      2  * Copyright (C) 2011 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.result;
     17 
     18 import com.android.tradefed.log.LogUtil.CLog;
     19 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException;
     20 
     21 import java.io.BufferedReader;
     22 import java.io.File;
     23 import java.io.FileFilter;
     24 import java.io.FileNotFoundException;
     25 import java.io.FileReader;
     26 import java.util.ArrayList;
     27 import java.util.Collections;
     28 import java.util.Comparator;
     29 import java.util.List;
     30 
     31 /**
     32  * An implementation of {@link ITestResultsRepo}.
     33  */
     34 public class TestResultRepo implements ITestResultRepo {
     35 
     36     /**
     37      * ordered list of result directories. the index of each file is its session id.
     38      */
     39     private List<File> mResultDirs;
     40 
     41     /**
     42      * Create a {@link TestResultRepo} from a directory of results
     43      *
     44      * @param testResultsDir the parent directory of results
     45      */
     46     public TestResultRepo(File testResultsDir) {
     47         mResultDirs = new ArrayList<File>();
     48         File[] resultArray = testResultsDir.listFiles(new ResultDirFilter());
     49         if (resultArray != null) {
     50             List<File> resultList = new ArrayList<File>();
     51             Collections.addAll(resultList, resultArray);
     52             Collections.sort(resultList, new FileComparator());
     53             for (int i=0; i < resultList.size(); i++) {
     54                 File resultFile = new File(resultList.get(i),
     55                         CtsXmlResultReporter.TEST_RESULT_FILE_NAME);
     56                 if (resultFile.exists()) {
     57                     mResultDirs.add(resultList.get(i));
     58                 }
     59             }
     60         }
     61     }
     62 
     63     @Override
     64     public File getReportDir(int sessionId) {
     65         return mResultDirs.get(sessionId);
     66     }
     67 
     68     private ITestSummary parseSummary(int id, File resultDir) {
     69         TestSummaryXml result = new TestSummaryXml(id, resultDir.getName());
     70         try {
     71             result.parse(new BufferedReader(new FileReader(new File(resultDir,
     72                     CtsXmlResultReporter.TEST_RESULT_FILE_NAME))));
     73             return result;
     74         } catch (ParseException e) {
     75             CLog.e(e);
     76         } catch (FileNotFoundException e) {
     77             // should never happen, since we check for file existence above. Barf the stack trace
     78             CLog.e(e);
     79         }
     80         return result;
     81     }
     82 
     83     /**
     84      * {@inheritDoc}
     85      */
     86     @Override
     87     public List<ITestSummary> getSummaries() {
     88         // parsing the summary data should be relatively quick, so just parse it every time
     89         // rather than caching it
     90         List<ITestSummary> summaries = new ArrayList<ITestSummary>(mResultDirs.size());
     91         for (int i = 0; i < mResultDirs.size(); i++) {
     92             summaries.add(parseSummary(i, mResultDirs.get(i)));
     93         }
     94         return summaries;
     95     }
     96 
     97     /**
     98      * {@inheritDoc}
     99      */
    100     @Override
    101     public TestResults getResult(int sessionId) {
    102         // TODO: consider caching the results in future
    103         if (mResultDirs.size() <= sessionId) {
    104             CLog.e("Session id %d does not exist", sessionId);
    105             return null;
    106         }
    107         try {
    108             TestResults results = new TestResults();
    109             File resultFile = new File(mResultDirs.get(sessionId),
    110                     CtsXmlResultReporter.TEST_RESULT_FILE_NAME);
    111             results.parse(new BufferedReader(new FileReader(resultFile)));
    112             return results;
    113         } catch (FileNotFoundException e) {
    114             CLog.e("Could not find result file for session %d", sessionId);
    115         } catch (ParseException e) {
    116             CLog.e("Failed to parse result file for session %d", sessionId);
    117         }
    118         return null;
    119     }
    120 
    121     private class ResultDirFilter implements FileFilter {
    122 
    123         /**
    124          * {@inheritDoc}
    125          */
    126         @Override
    127         public boolean accept(File file) {
    128             return file.isDirectory();
    129         }
    130     }
    131 
    132     /**
    133      * A {@link Comparator} that compares {@link File}s by name.
    134      */
    135     private class FileComparator implements Comparator<File> {
    136 
    137         /**
    138          * {@inheritDoc}
    139          */
    140         @Override
    141         public int compare(File file0, File file1) {
    142             return file0.getName().compareTo(file1.getName());
    143         }
    144     }
    145 }
    146