aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed
diff options
context:
space:
mode:
authorJulien Desprez <jdesprez@google.com>2016-11-15 15:07:52 +0000
committerJulien Desprez <jdesprez@google.com>2016-11-16 09:46:01 +0000
commite5c1f04436ada1994f5e5ea8970870e79045f4b2 (patch)
tree59414355d3fc181da53b19b54a4473cd8ed43014 /src/com/android/tradefed
parent90c7c06a80277ee5f69f027e54417f76a484a64f (diff)
downloadtradefederation-e5c1f04436ada1994f5e5ea8970870e79045f4b2.tar.gz
Add a toString() method to BuildInfo
Allow a standard printing of build info with the fields that are available. Bug: 32863157 Change-Id: Iaee90091b5096a6f41264bc80cafa59c28b007b3
Diffstat (limited to 'src/com/android/tradefed')
-rw-r--r--src/com/android/tradefed/build/BuildInfo.java19
-rw-r--r--src/com/android/tradefed/result/DeviceUnavailEmailResultReporter.java32
-rw-r--r--src/com/android/tradefed/result/EmailResultReporter.java9
3 files changed, 37 insertions, 23 deletions
diff --git a/src/com/android/tradefed/build/BuildInfo.java b/src/com/android/tradefed/build/BuildInfo.java
index 8365103ab..f48af7e85 100644
--- a/src/com/android/tradefed/build/BuildInfo.java
+++ b/src/com/android/tradefed/build/BuildInfo.java
@@ -21,6 +21,7 @@ import com.android.tradefed.util.FileUtil;
import com.android.tradefed.util.MultiMap;
import com.android.tradefed.util.UniqueMultiMap;
+import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import java.io.File;
@@ -34,6 +35,8 @@ import java.util.Map;
* with a {@link ITestDevice}.
*/
public class BuildInfo implements IBuildInfo {
+ private static final String BUILD_ALIAS_KEY = "build_alias";
+
private String mBuildId = UNKNOWN_BUILD_ID;
private String mTestTag = "stub";
private String mBuildTargetName = "stub";
@@ -346,4 +349,20 @@ public class BuildInfo implements IBuildInfo {
Objects.equal(mTestTag, other.mTestTag) &&
Objects.equal(mDeviceSerial, other.mDeviceSerial);
}
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this.getClass())
+ .omitNullValues()
+ .add("build_alias", getBuildAttributes().get(BUILD_ALIAS_KEY))
+ .add("bid", mBuildId)
+ .add("target", mBuildTargetName)
+ .add("build_flavor", mBuildFlavor)
+ .add("branch", mBuildBranch)
+ .add("serial", mDeviceSerial)
+ .toString();
+ }
}
diff --git a/src/com/android/tradefed/result/DeviceUnavailEmailResultReporter.java b/src/com/android/tradefed/result/DeviceUnavailEmailResultReporter.java
index 42a5236aa..074d81c86 100644
--- a/src/com/android/tradefed/result/DeviceUnavailEmailResultReporter.java
+++ b/src/com/android/tradefed/result/DeviceUnavailEmailResultReporter.java
@@ -20,6 +20,8 @@ import com.android.tradefed.config.OptionClass;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.log.LogUtil.CLog;
+import com.google.common.annotations.VisibleForTesting;
+
import java.net.InetAddress;
import java.net.UnknownHostException;
@@ -42,26 +44,26 @@ public class DeviceUnavailEmailResultReporter extends EmailResultReporter {
StringBuilder subj = new StringBuilder();
subj.append("Device unavailable ");
for (IBuildInfo build : getInvocationContext().getBuildInfos()) {
- String buildAlias = build.getBuildAttributes().get("build_alias");
- if (buildAlias == null){
- //If build alias is null, use the build id instead.
- buildAlias = build.getBuildId();
- }
- subj.append(String.format("{%s %s %s} ", build.getBuildFlavor(),
- buildAlias, build.getDeviceSerial()));
+ subj.append(build.toString());
}
+ subj.append(" ");
+ subj.append(String.format("hostname %s", getHostname()));
+
+ // Sample email subject: Device unavailable: mantaray-user JDQ39
+ // 015d172c980c2208 atl-034.mtv.corp.google.com
+ return subj.toString();
+ }
- String hostName = null;
+ /**
+ * Fetch the hostname and returns it, or returns "Unknown" if an error occurs.
+ */
+ @VisibleForTesting
+ String getHostname() {
try {
- hostName = InetAddress.getLocalHost().getHostName();
+ return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
- hostName = "Unknown";
CLog.e(e);
+ return "Unknown";
}
- subj.append(String.format("hostname %s", hostName));
-
- // Sample email subject: Device unavailable: mantaray-user JDQ39
- // 015d172c980c2208 atl-034.mtv.corp.google.com
- return subj.toString();
}
}
diff --git a/src/com/android/tradefed/result/EmailResultReporter.java b/src/com/android/tradefed/result/EmailResultReporter.java
index 8ef7eb70d..496e91764 100644
--- a/src/com/android/tradefed/result/EmailResultReporter.java
+++ b/src/com/android/tradefed/result/EmailResultReporter.java
@@ -153,14 +153,7 @@ public class EmailResultReporter extends CollectingTestListener implements
subj.append(" on ");
for (IBuildInfo build : context.getBuildInfos()) {
- subj.append("{");
- appendUnlessNull(subj, build.getBuildFlavor());
- appendUnlessNull(subj, build.getBuildBranch());
- if (!appendUnlessNull(subj, build.getBuildAttributes().get("build_alias"))) {
- subj.append("build ");
- subj.append(build.getBuildId());
- }
- subj.append("}");
+ subj.append(build.toString());
}
subj.append(": ");