diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2018-06-01 17:57:56 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-06-01 17:57:56 +0000 |
commit | 2cab6aab318d0c1f78164c92216c9690829efb0a (patch) | |
tree | e4b59241d7f74a9fe7faa8fcdcaa52d1e5f88619 | |
parent | 5e5300f5bdc932b370a4a88f5c42f0d3bdbd9fb9 (diff) | |
parent | 7bf9965d9f02cccc6ae644e2bbb8f318893489e3 (diff) | |
download | suite_harness-2cab6aab318d0c1f78164c92216c9690829efb0a.tar.gz |
Merge "Ensure device info files are always closed. Make InfoStore implement AutoCloseable and use try-with-resources." into pi-dev
4 files changed, 19 insertions, 12 deletions
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java index 8a86fe9e..8c133705 100644 --- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java +++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java @@ -110,9 +110,9 @@ public class DeviceInfoCollector extends ApkInstrumentationPreparer implements I deviceInfoDir = FileUtil.createTempDir(DeviceInfo.RESULT_DIR_NAME); if (device.pullDir(mSrcDir, deviceInfoDir)) { for (File deviceInfoFile : deviceInfoDir.listFiles()) { - FileInputStreamSource source = new FileInputStreamSource(deviceInfoFile); - mLogger.testLog(deviceInfoFile.getName(), LogDataType.TEXT, source); - source.close(); + try (FileInputStreamSource source = new FileInputStreamSource(deviceInfoFile)) { + mLogger.testLog(deviceInfoFile.getName(), LogDataType.TEXT, source); + } } } else { CLog.e("Failed to pull device-info files from device %s", device.getSerialNumber()); diff --git a/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java b/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java index 369189d8..da040cbe 100644 --- a/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java +++ b/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java @@ -32,6 +32,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import java.io.File; +import java.io.EOFException; /** * Collect device information from host and write to a JSON file. @@ -44,8 +45,6 @@ public abstract class DeviceInfo extends BaseHostJUnit4Test { public static final String FILE_SUFFIX = ".deviceinfo.json"; - private HostInfoStore mStore; - @Rule public TestLogData mLogger = new TestLogData(); @@ -56,10 +55,16 @@ public abstract class DeviceInfo extends BaseHostJUnit4Test { FileInputStreamSource source = null; try { jsonFile = FileUtil.createTempFile(getClass().getSimpleName(), FILE_SUFFIX); - mStore = new HostInfoStore(jsonFile); - mStore.open(); - collectDeviceInfo(mStore); - mStore.close(); + try (HostInfoStore store = new HostInfoStore(jsonFile)) { + store.open(); + collectDeviceInfo(store); + } finally { + // If file is empty throw exception so it is not copied to the results. + if (jsonFile != null && jsonFile.exists() && + jsonFile.length() == 0) { + throw new EOFException(String.format("File is empty: %s", deviceInfoName)); + } + } source = new FileInputStreamSource(jsonFile); mLogger.addTestLog(deviceInfoName, LogDataType.TEXT, source); } catch (Exception e) { diff --git a/common/host-side/util/src/com/android/compatibility/common/util/HostInfoStore.java b/common/host-side/util/src/com/android/compatibility/common/util/HostInfoStore.java index 1fa149f7..431a8af1 100644 --- a/common/host-side/util/src/com/android/compatibility/common/util/HostInfoStore.java +++ b/common/host-side/util/src/com/android/compatibility/common/util/HostInfoStore.java @@ -53,7 +53,7 @@ public class HostInfoStore extends InfoStore { * Closes the writer. */ @Override - public void close() throws IOException { + public void close() throws Exception { mJsonWriter.endObject(); mJsonWriter.flush(); mJsonWriter.close(); diff --git a/common/util/src/com/android/compatibility/common/util/InfoStore.java b/common/util/src/com/android/compatibility/common/util/InfoStore.java index c5a411ea..f677c80c 100644 --- a/common/util/src/com/android/compatibility/common/util/InfoStore.java +++ b/common/util/src/com/android/compatibility/common/util/InfoStore.java @@ -16,10 +16,11 @@ package com.android.compatibility.common.util; import java.io.IOException; +import java.lang.AutoCloseable; import java.util.Arrays; import java.util.List; -public abstract class InfoStore { +public abstract class InfoStore implements AutoCloseable { protected static final int MAX_STRING_LENGTH = 1000; protected static final int MAX_ARRAY_LENGTH = 1000; @@ -33,7 +34,8 @@ public abstract class InfoStore { /** * Closes the writer. */ - abstract void close() throws IOException; + @Override + public abstract void close() throws Exception; /** * Start a new group of result. |