Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 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.compatibility.common.util;
     17 
     18 import java.io.File;
     19 import java.util.ArrayList;
     20 import java.util.HashMap;
     21 import java.util.HashSet;
     22 import java.util.List;
     23 import java.util.Map;
     24 import java.util.Set;
     25 
     26 /**
     27  * Data structure for storing finalized Compatibility test results with minimum memory.
     28  * This implementation stores only enough ModuleResult information to return empty modules
     29  * of the correct ids (names and abis) upon {@link IInvocationResult}'s getModules() method.
     30  */
     31 public class LightInvocationResult implements IInvocationResult {
     32 
     33     private long mTimestamp;
     34     private Map<String, String> mInvocationInfo;
     35     private Set<String> mSerials;
     36     private String mBuildFingerprint;
     37     private String mTestPlan;
     38     private String mCommandLineArgs;
     39     private int mNotExecuted;
     40     private int mModuleCompleteCount;
     41     private RetryChecksumStatus mRetryChecksumStatus;
     42     private File mRetryDirectory;
     43     private Set<String> mModuleIds;
     44     private Map<TestStatus, Integer> mResultCounts;
     45 
     46     /**
     47      * Constructor that takes a reference to an existing result (light or complete) and
     48      * initializes instance variables accordingly. This class must NOT save any reference to the
     49      * result param to remain lightweight.
     50      */
     51     public LightInvocationResult(IInvocationResult result) {
     52         mTimestamp = result.getStartTime();
     53         mInvocationInfo = new HashMap<String, String>(result.getInvocationInfo());
     54         mSerials = new HashSet<String>(result.getDeviceSerials());
     55         mBuildFingerprint = result.getBuildFingerprint();
     56         mTestPlan = result.getTestPlan();
     57         mCommandLineArgs = result.getCommandLineArgs();
     58         mNotExecuted = result.getNotExecuted();
     59         mModuleCompleteCount = result.getModuleCompleteCount();
     60         mRetryChecksumStatus = RetryChecksumStatus.NotRetry;
     61         mRetryDirectory = result.getRetryDirectory();
     62         mModuleIds = new HashSet<String>();
     63         for (IModuleResult module : result.getModules()) {
     64             mModuleIds.add(module.getId());
     65         }
     66         mResultCounts = new HashMap<TestStatus, Integer>();
     67         for (TestStatus status : TestStatus.values()) {
     68             mResultCounts.put(status, result.countResults(status));
     69         }
     70     }
     71 
     72     /**
     73      * {@inheritDoc}
     74      */
     75     @Override
     76     public List<IModuleResult> getModules() {
     77         List<IModuleResult> modules = new ArrayList<IModuleResult>();
     78         for (String id : mModuleIds) {
     79             modules.add(new ModuleResult(id));
     80         }
     81         return modules; // return empty modules
     82     }
     83 
     84     /**
     85      * {@inheritDoc}
     86      */
     87     @Override
     88     public int countResults(TestStatus result) {
     89         return mResultCounts.get(result);
     90     }
     91 
     92     /**
     93      * {@inheritDoc}
     94      */
     95     @Override
     96     public int getNotExecuted() {
     97         return mNotExecuted;
     98     }
     99 
    100     /**
    101      * {@inheritDoc}
    102      */
    103     @Override
    104     public IModuleResult getOrCreateModule(String id) {
    105         mModuleIds.add(id);
    106         return new ModuleResult(id);
    107     }
    108 
    109     /**
    110      * {@inheritDoc}
    111      */
    112     @Override
    113     public void mergeModuleResult(IModuleResult moduleResult) {
    114         mModuleIds.add(moduleResult.getId());
    115     }
    116 
    117     /**
    118      * {@inheritDoc}
    119      */
    120     @Override
    121     public void addInvocationInfo(String key, String value) {
    122         mInvocationInfo.put(key, value);
    123     }
    124 
    125     /**
    126      * {@inheritDoc}
    127      */
    128     @Override
    129     public Map<String, String> getInvocationInfo() {
    130         return mInvocationInfo;
    131     }
    132 
    133     /**
    134      * {@inheritDoc}
    135      */
    136     @Override
    137     public void setStartTime(long time) {
    138         mTimestamp = time;
    139     }
    140 
    141     /**
    142      * {@inheritDoc}
    143      */
    144     @Override
    145     public long getStartTime() {
    146         return mTimestamp;
    147     }
    148 
    149     /**
    150      * {@inheritDoc}
    151      */
    152     @Override
    153     public void setTestPlan(String plan) {
    154         mTestPlan = plan;
    155     }
    156 
    157     /**
    158      * {@inheritDoc}
    159      */
    160     @Override
    161     public String getTestPlan() {
    162         return mTestPlan;
    163     }
    164 
    165     /**
    166      * {@inheritDoc}
    167      */
    168     @Override
    169     public void addDeviceSerial(String serial) {
    170         mSerials.add(serial);
    171     }
    172 
    173     /**
    174      * {@inheritDoc}
    175      */
    176     @Override
    177     public Set<String> getDeviceSerials() {
    178         return mSerials;
    179     }
    180 
    181     /**
    182      * {@inheritDoc}
    183      */
    184     @Override
    185     public void setCommandLineArgs(String commandLineArgs) {
    186         mCommandLineArgs = commandLineArgs;
    187     }
    188 
    189     /**
    190      * {@inheritDoc}
    191      */
    192     @Override
    193     public String getCommandLineArgs() {
    194         return mCommandLineArgs;
    195     }
    196 
    197     @Override
    198     public void setBuildFingerprint(String buildFingerprint) {
    199         mBuildFingerprint = buildFingerprint;
    200     }
    201 
    202     /**
    203      * {@inheritDoc}
    204      */
    205     @Override
    206     public String getBuildFingerprint() {
    207         return mBuildFingerprint;
    208     }
    209 
    210     /**
    211      * {@inheritDoc}
    212      */
    213     @Override
    214     public int getModuleCompleteCount() {
    215         return mModuleCompleteCount;
    216     }
    217 
    218     /**
    219      * {@inheritDoc}
    220      */
    221     @Override
    222     public RetryChecksumStatus getRetryChecksumStatus() {
    223         return mRetryChecksumStatus;
    224     }
    225 
    226     /**
    227      * {@inheritDoc}
    228      */
    229     @Override
    230     public void setRetryChecksumStatus(RetryChecksumStatus retryStatus) {
    231         mRetryChecksumStatus = retryStatus;
    232     }
    233 
    234     /**
    235      * {@inheritDoc}
    236      */
    237     @Override
    238     public File getRetryDirectory() {
    239         return mRetryDirectory;
    240     }
    241 
    242     /**
    243      * {@inheritDoc}
    244      */
    245     @Override
    246     public void setRetryDirectory(File resultDir) {
    247         mRetryDirectory = resultDir;
    248     }
    249 }
    250