Home | History | Annotate | Download | only in util
      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.util;
     17 
     18 import static org.junit.Assert.fail;
     19 
     20 import com.android.compatibility.common.util.HostInfoStore;
     21 import com.android.tradefed.log.LogUtil.CLog;
     22 import com.android.tradefed.result.FileInputStreamSource;
     23 import com.android.tradefed.result.LogDataType;
     24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
     25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
     26 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
     27 import com.android.tradefed.util.FileUtil;
     28 import com.android.tradefed.util.StreamUtil;
     29 
     30 import org.junit.Rule;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 
     34 import java.io.File;
     35 import java.io.EOFException;
     36 
     37 /**
     38  * Collect device information from host and write to a JSON file.
     39  */
     40 @RunWith(DeviceJUnit4ClassRunner.class)
     41 public abstract class DeviceInfo extends BaseHostJUnit4Test {
     42 
     43     // Default name of the directory for storing device info files within the result directory
     44     public static final String RESULT_DIR_NAME = "device-info-files";
     45 
     46     public static final String FILE_SUFFIX = ".deviceinfo.json";
     47 
     48     @Rule
     49     public TestLogData mLogger = new TestLogData();
     50 
     51     @Test
     52     public void testCollectDeviceInfo() throws Exception {
     53         String deviceInfoName = getClass().getSimpleName() + FILE_SUFFIX;
     54         File jsonFile = null;
     55         FileInputStreamSource source = null;
     56         try {
     57             jsonFile = FileUtil.createTempFile(getClass().getSimpleName(), FILE_SUFFIX);
     58             try (HostInfoStore store = new HostInfoStore(jsonFile)) {
     59                 store.open();
     60                 collectDeviceInfo(store);
     61             } finally {
     62                 // If file is empty throw exception so it is not copied to the results.
     63                 if (jsonFile != null && jsonFile.exists() &&
     64                         jsonFile.length() == 0) {
     65                     throw new EOFException(String.format("File is empty: %s", deviceInfoName));
     66                 }
     67             }
     68             source = new FileInputStreamSource(jsonFile);
     69             mLogger.addTestLog(deviceInfoName, LogDataType.TEXT, source);
     70         } catch (Exception e) {
     71             CLog.e(e);
     72             fail(String.format("Failed to collect device info (%s): %s",
     73                     deviceInfoName, e.getMessage()));
     74         } finally {
     75             FileUtil.deleteFile(jsonFile);
     76             StreamUtil.close(source);
     77         }
     78     }
     79 
     80     /**
     81      * Method to collect device information.
     82      */
     83     protected abstract void collectDeviceInfo(HostInfoStore store) throws Exception;
     84 }
     85