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.tradefed.testtype.suite;
17 
18 import com.android.tradefed.log.LogUtil.CLog;
19 
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Properties;
23 
24 /**
25  * A class that resolves loading of build related metadata for test suite
26  * <p>
27  * To properly expose related info, a test suite must include a
28  * <code>test-suite-info.properties</code> file in its jar resources
29  */
30 public class TestSuiteInfo {
31 
32     /** expected property filename in jar resource */
33     private static final String SUITE_INFO_PROPERTY = "/test-suite-info.properties";
34     /** suite info keys */
35     private static final String BUILD_NUMBER = "build_number";
36     private static final String TARGET_ARCH = "target_arch";
37     private static final String NAME = "name";
38     private static final String FULLNAME = "fullname";
39     private static final String VERSION = "version";
40 
41     private static TestSuiteInfo sInstance;
42     private Properties mTestSuiteInfo;
43     private boolean mLoaded = false;
44 
TestSuiteInfo()45     private TestSuiteInfo() {
46         try (InputStream is = TestSuiteInfo.class.getResourceAsStream(SUITE_INFO_PROPERTY)) {
47             if (is != null) {
48                 mTestSuiteInfo = loadSuiteInfo(is);
49                 mLoaded = true;
50             } else {
51                 CLog.w("Unable to load suite info from jar resource %s, using stub info instead",
52                         SUITE_INFO_PROPERTY);
53                 mTestSuiteInfo = new Properties();
54                 mTestSuiteInfo.setProperty(BUILD_NUMBER, "[stub build number]");
55                 mTestSuiteInfo.setProperty(TARGET_ARCH, "[stub target arch]");
56                 mTestSuiteInfo.setProperty(NAME, "[stub name]");
57                 mTestSuiteInfo.setProperty(FULLNAME, "[stub fullname]");
58                 mTestSuiteInfo.setProperty(VERSION, "[stub version]");
59             }
60         } catch (IOException ioe) {
61             // rethrow as runtime exception
62             throw new RuntimeException(String.format(
63                     "error loading jar resource file \"%s\" for test suite info",
64                     SUITE_INFO_PROPERTY));
65         }
66     }
67 
68     /** Performs the actual loading of properties */
loadSuiteInfo(InputStream is)69     protected Properties loadSuiteInfo(InputStream is) throws IOException {
70         Properties p = new Properties();
71         p.load(is);
72         return p;
73     }
74 
75     /**
76      * Retrieves the singleton instance, which also triggers loading of the related test suite info
77      * from embedded resource files
78      */
getInstance()79     public static TestSuiteInfo getInstance() {
80         if (sInstance == null) {
81             sInstance = new TestSuiteInfo();
82         }
83         return sInstance;
84     }
85 
86     /** Gets the build number of the test suite */
getBuildNumber()87     public String getBuildNumber() {
88         return mTestSuiteInfo.getProperty(BUILD_NUMBER);
89     }
90 
91     /** Gets the target archs supported by the test suite */
getTargetArch()92     public String getTargetArch() {
93         return mTestSuiteInfo.getProperty(TARGET_ARCH);
94     }
95 
96     /** Gets the short name of the test suite */
getName()97     public String getName() {
98         return mTestSuiteInfo.getProperty(NAME);
99     }
100 
101     /** Gets the full name of the test suite */
getFullName()102     public String getFullName() {
103         return mTestSuiteInfo.getProperty(FULLNAME);
104     }
105 
106     /** Gets the version name of the test suite */
getVersion()107     public String getVersion() {
108         return mTestSuiteInfo.getProperty(VERSION);
109     }
110 
111     /**
112      * Retrieves test information keyed with the provided name. Or null if not property associated.
113      */
get(String name)114     public String get(String name) {
115         return mTestSuiteInfo.getProperty(name);
116     }
117 
118     /** Returns true if the values were loaded from a property file, false otherwise. */
didLoadFromProperties()119     public boolean didLoadFromProperties() {
120         return mLoaded;
121     }
122 }
123