1 /*
2  * Copyright (C) 2015 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.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 
29 /**
30  * Data structure for the detailed Compatibility test results.
31  */
32 public class InvocationResult implements IInvocationResult {
33 
34     /** Helper object for JSON conversion. */
35     public static final class RunHistory {
36         public long startTime;
37         public long endTime;
38     }
39 
40     private Collection<RunHistory> mRunHistories = new ArrayList<>();
41 
42     private long mTimestamp;
43     private Map<String, IModuleResult> mModuleResults = new LinkedHashMap<>();
44     private Map<String, String> mInvocationInfo = new HashMap<>();
45     private Set<String> mSerials = new HashSet<>();
46     private String mBuildFingerprint;
47     private String mTestPlan;
48     private String mCommandLineArgs;
49     private RetryChecksumStatus mRetryChecksumStatus = RetryChecksumStatus.NotRetry;
50     private File mRetryDirectory = null;
51 
52     /** @return a collection of the run history of previous runs. */
getRunHistories()53     public Collection<RunHistory> getRunHistories() {
54         return mRunHistories;
55     }
56 
57     /**
58      * {@inheritDoc}
59      */
60     @Override
getModules()61     public List<IModuleResult> getModules() {
62         ArrayList<IModuleResult> modules = new ArrayList<>(mModuleResults.values());
63         Collections.sort(modules);
64         return modules;
65     }
66 
67     /**
68      * {@inheritDoc}
69      */
70     @Override
countResults(TestStatus result)71     public int countResults(TestStatus result) {
72         int total = 0;
73         for (IModuleResult m : mModuleResults.values()) {
74             total += m.countResults(result);
75         }
76         return total;
77     }
78 
79     /**
80      * {@inheritDoc}
81      */
82     @Override
getNotExecuted()83     public int getNotExecuted() {
84         int numTests = 0;
85         for (IModuleResult module : mModuleResults.values()) {
86             numTests += module.getNotExecuted();
87         }
88         return numTests;
89     }
90 
91     /**
92      * {@inheritDoc}
93      */
94     @Override
getOrCreateModule(String id)95     public IModuleResult getOrCreateModule(String id) {
96         IModuleResult moduleResult = mModuleResults.get(id);
97         if (moduleResult == null) {
98             moduleResult = new ModuleResult(id);
99             mModuleResults.put(id, moduleResult);
100         }
101         return moduleResult;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
mergeModuleResult(IModuleResult moduleResult)108     public void mergeModuleResult(IModuleResult moduleResult) {
109         // Merge the moduleResult with any existing module result
110         IModuleResult existingModuleResult = getOrCreateModule(moduleResult.getId());
111         existingModuleResult.mergeFrom(moduleResult);
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
addInvocationInfo(String key, String value)118     public void addInvocationInfo(String key, String value) {
119         mInvocationInfo.put(key, value);
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
getInvocationInfo()126     public Map<String, String> getInvocationInfo() {
127         return mInvocationInfo;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
setStartTime(long time)134     public void setStartTime(long time) {
135         mTimestamp = time;
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
getStartTime()142     public long getStartTime() {
143         return mTimestamp;
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
setTestPlan(String plan)150     public void setTestPlan(String plan) {
151         mTestPlan = plan;
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
getTestPlan()158     public String getTestPlan() {
159         return mTestPlan;
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
addDeviceSerial(String serial)166     public void addDeviceSerial(String serial) {
167         mSerials.add(serial);
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
getDeviceSerials()174     public Set<String> getDeviceSerials() {
175         return mSerials;
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
setCommandLineArgs(String commandLineArgs)182     public void setCommandLineArgs(String commandLineArgs) {
183         mCommandLineArgs = commandLineArgs;
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
getCommandLineArgs()190     public String getCommandLineArgs() {
191         return mCommandLineArgs;
192     }
193 
194     @Override
setBuildFingerprint(String buildFingerprint)195     public void setBuildFingerprint(String buildFingerprint) {
196         mBuildFingerprint = buildFingerprint;
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
getBuildFingerprint()203     public String getBuildFingerprint() {
204         return mBuildFingerprint;
205     }
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
getModuleCompleteCount()211     public int getModuleCompleteCount() {
212         int completeModules = 0;
213         for (IModuleResult module : mModuleResults.values()) {
214             if (module.isDone()) {
215                 completeModules++;
216             }
217         }
218         return completeModules;
219     }
220 
221     /**
222      * {@inheritDoc}
223      */
224     @Override
getRetryChecksumStatus()225     public RetryChecksumStatus getRetryChecksumStatus() {
226         return mRetryChecksumStatus;
227     }
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
setRetryChecksumStatus(RetryChecksumStatus retryStatus)233     public void setRetryChecksumStatus(RetryChecksumStatus retryStatus) {
234         mRetryChecksumStatus = retryStatus;
235     }
236 
237     /**
238      * {@inheritDoc}
239      */
240     @Override
getRetryDirectory()241     public File getRetryDirectory() {
242         return mRetryDirectory;
243     }
244 
245     /**
246      * {@inheritDoc}
247      */
248     @Override
setRetryDirectory(File resultDir)249     public void setRetryDirectory(File resultDir) {
250         mRetryDirectory = resultDir;
251     }
252 }
253