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