From 35fa1c560810e5e7e251e1bc920907899b084947 Mon Sep 17 00:00:00 2001 From: EmanuelAry Date: Mon, 15 Jun 2026 18:43:45 -0300 Subject: [PATCH 1/2] refactor: Isolate status logic and remove magic numbers Extract status code mapping to a constant map (STATUS_CODE_MAP), create dedicated methods statusFromCode() and applyStatus() to handle status code parsing and callback invocation. Also extract content extraction to a reusable extractContent() method. This eliminates the long switch with hardcoded integers and makes the STATUS_CODE handling self-documenting and easier to extend. --- .../InstrumentationTestRunnerProcessor.java | 63 +++++++++++-------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java b/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java index c75f477e9..53d055a5e 100644 --- a/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java +++ b/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java @@ -24,6 +24,8 @@ import com.google.common.io.LineProcessor; import java.util.List; import java.util.logging.Logger; +import java.util.Map; + /** * {@link LineProcessor} for instrumentation test runner output. @@ -123,36 +125,15 @@ public boolean processLine(String line) { } if (line.startsWith(STATUS_CODE)) { - String statusCode = line.replace(STATUS_CODE, "").trim(); - int statusInt = Integer.parseInt(statusCode); - + String statusCode = extractContent(line, STATUS_CODE).trim(); + int code = Integer.parseInt(statusCode); + applyStatus(code); try { - switch (statusInt) { - case 1: - currentTest.setStatus(Status.STARTED); - onTestStart(currentTest.build()); - break; - case 0: - currentTest.setStatus(Status.PASSED); - break; - case -1: - currentTest.setStatus(Status.ERROR); - break; - case -2: - currentTest.setStatus(Status.FAILED); - break; - case -4: - currentTest.setStatus(Status.ASSUMPTION_FAILURE); - break; - default: - throw new IllegalArgumentException( - String.format("Illegal test instrumentation code: \"%s\"", statusCode)); - } + // A chamada onTestStart, se necessária, já foi feita em applyStatus } finally { onTestFinished(currentTest.build()); currentTest = null; } - return true; } @@ -190,6 +171,38 @@ public boolean processLine(String line) { return true; } + private static final Map STATUS_CODE_MAP = Map.of( + 1, Status.STARTED, + 0, Status.PASSED, + -1, Status.ERROR, + -2, Status.FAILED, + -4, Status.ASSUMPTION_FAILURE + ); + + private Status statusFromCode(int code) { + Status status = STATUS_CODE_MAP.get(code); + if (status == null) { + throw new IllegalArgumentException( + "Illegal test instrumentation code: \"" + code + "\""); + } + return status; +} + + private void applyStatus(int code) { + Status status = statusFromCode(code); + currentTest.setStatus(status); + if (status == Status.STARTED) { + onTestStart(currentTest.build()); + } + } + /** + * Returns the content of {@code line} after removing the expected {@code prefix}. + * Assumes the line starts with the given prefix. + */ + private String extractContent(String line, String prefix) { + return line.substring(prefix.length()); + } + private void onTestFinished(ExecutedTest executedTest) { executedTests.add(executedTest); eventBus.post(executedTest); From bc70221fd18b53f6ec39f0025692457ff64015e3 Mon Sep 17 00:00:00 2001 From: EmanuelAry Date: Mon, 15 Jun 2026 18:57:16 -0300 Subject: [PATCH 2/2] refactor: Isolate status logic and remove magic numbers Extract status code mapping to a constant map (STATUS_CODE_MAP), create dedicated methods statusFromCode() and applyStatus() to handle status code parsing and callback invocation. Also extract content extraction to a reusable extractContent() method. This eliminates the long switch with hardcoded integers and makes the STATUS_CODE handling self-documenting and easier to extend. --- .../testing/broker/InstrumentationTestRunnerProcessor.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java b/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java index 53d055a5e..293019f7d 100644 --- a/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java +++ b/tools/device_broker/java/com/google/android/apps/common/testing/broker/InstrumentationTestRunnerProcessor.java @@ -127,9 +127,8 @@ public boolean processLine(String line) { if (line.startsWith(STATUS_CODE)) { String statusCode = extractContent(line, STATUS_CODE).trim(); int code = Integer.parseInt(statusCode); - applyStatus(code); try { - // A chamada onTestStart, se necessária, já foi feita em applyStatus + applyStatus(code); } finally { onTestFinished(currentTest.build()); currentTest = null;