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 44 private TestSuiteInfo() { 45 try (InputStream is = TestSuiteInfo.class.getResourceAsStream(SUITE_INFO_PROPERTY)) { 46 if (is != null) { 47 mTestSuiteInfo = loadSuiteInfo(is); 48 } else { 49 CLog.w("Unable to load suite info from jar resource %s, using stub info instead", 50 SUITE_INFO_PROPERTY); 51 mTestSuiteInfo = new Properties(); 52 mTestSuiteInfo.setProperty(BUILD_NUMBER, "[stub build number]"); 53 mTestSuiteInfo.setProperty(TARGET_ARCH, "[stub target arch]"); 54 mTestSuiteInfo.setProperty(NAME, "[stub name]"); 55 mTestSuiteInfo.setProperty(FULLNAME, "[stub fullname]"); 56 mTestSuiteInfo.setProperty(VERSION, "[stub version]"); 57 } 58 } catch (IOException ioe) { 59 // rethrow as runtime exception 60 throw new RuntimeException(String.format( 61 "error loading jar resource file \"%s\" for test suite info", 62 SUITE_INFO_PROPERTY)); 63 } 64 } 65 66 /** Performs the actual loading of properties */ 67 protected Properties loadSuiteInfo(InputStream is) throws IOException { 68 Properties p = new Properties(); 69 p.load(is); 70 return p; 71 } 72 73 /** 74 * Retrieves the singleton instance, which also triggers loading of the related test suite info 75 * from embedded resource files 76 * @return 77 */ 78 public static TestSuiteInfo getInstance() { 79 if (sInstance == null) { 80 sInstance = new TestSuiteInfo(); 81 } 82 return sInstance; 83 } 84 85 /** Gets the build number of the test suite */ 86 public String getBuildNumber() { 87 return mTestSuiteInfo.getProperty(BUILD_NUMBER); 88 } 89 90 /** Gets the target archs supported by the test suite */ 91 public String getTargetArch() { 92 return mTestSuiteInfo.getProperty(TARGET_ARCH); 93 } 94 95 /** Gets the short name of the test suite */ 96 public String getName() { 97 return mTestSuiteInfo.getProperty(NAME); 98 } 99 100 /** Gets the full name of the test suite */ 101 public String getFullName() { 102 return mTestSuiteInfo.getProperty(FULLNAME); 103 } 104 105 /** Gets the version name of the test suite */ 106 public String getVersion() { 107 return mTestSuiteInfo.getProperty(VERSION); 108 } 109 110 /** 111 * Retrieves test information keyed with the provided name 112 * @param name 113 * @return 114 */ 115 public String get(String name) { 116 return mTestSuiteInfo.getProperty(name); 117 } 118 } 119