1 /*
2  * Copyright (C) 2016 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.tradefed.result;
17 
18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
19 import com.android.tradefed.log.LogUtil.CLog;
20 import com.android.tradefed.util.FileUtil;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 
28 /**
29  * A helper class for setting and checking the number of expected test runs.
30  */
31 public class TestRunHandler {
32 
33     private static final String MAP_DELIMITER = "->";
34 
35     /**
36      * Determine the number of expected test runs for the module
37      *
38      * @param buildHelper the {@link CompatibilityBuildHelper} from which to retrieve invocation
39      * failure file
40      * @return the number of expected test runs, or 1 if module is not found
41      */
getTestRuns(final CompatibilityBuildHelper buildHelper, String id)42     public static int getTestRuns(final CompatibilityBuildHelper buildHelper, String id) {
43         try {
44             File f = buildHelper.getTestRunsFile();
45             if (!f.exists() || f.length() == 0) {
46                 return 1; // test runs file doesn't exist, expect one test run by default
47             }
48             String mapString = FileUtil.readStringFromFile(f);
49             Map<String, Integer> map = stringToMap(mapString);
50             Integer testRuns = map.get(id);
51             return (testRuns == null) ? 1 : testRuns;
52         } catch (IOException e) {
53             CLog.e("Could not read test run file for session %s",
54                 buildHelper.getDirSuffix(buildHelper.getStartTime()));
55             CLog.e(e);
56             return 1;
57         }
58     }
59 
60     /**
61      * Write the number of expected test runs to the result's test run file.
62      *
63      * @param buildHelper the {@link CompatibilityBuildHelper} used to write the
64      * test run file
65      * @param testRuns a mapping of module names to number of test runs expected
66      */
setTestRuns(final CompatibilityBuildHelper buildHelper, Map<String, Integer> testRuns)67     public static void setTestRuns(final CompatibilityBuildHelper buildHelper,
68             Map<String, Integer> testRuns) {
69         try {
70             File f = buildHelper.getTestRunsFile();
71             if (!f.exists()) {
72                 f.createNewFile();
73             }
74             FileUtil.writeToFile(mapToString(testRuns), f);
75         } catch (IOException e) {
76             CLog.e("Exception while writing test runs file.");
77             CLog.e(e);
78         }
79     }
80 
mapToString(Map<String, Integer> map)81     private static String mapToString(Map<String, Integer> map) {
82         StringBuilder sb = new StringBuilder("");
83         for (Map.Entry<String, Integer> entry : map.entrySet()) {
84             sb.append(String.format("%s%s%d\n", entry.getKey(), MAP_DELIMITER, entry.getValue()));
85         }
86         return sb.toString();
87     }
88 
stringToMap(String str)89     private static Map<String, Integer> stringToMap(String str) {
90         Map<String, Integer> map = new HashMap<>();
91         for (String entry : str.split("\n")) {
92             String[] parts = entry.split(MAP_DELIMITER);
93             map.put(parts[0], Integer.parseInt(parts[1]));
94         }
95         return map;
96     }
97 }
98