diff --git a/src/main/java/com/bitbar/remotedevice/RemoteDeviceClientMain.java b/src/main/java/com/bitbar/remotedevice/RemoteDeviceClientMain.java index c905a27..4db8545 100644 --- a/src/main/java/com/bitbar/remotedevice/RemoteDeviceClientMain.java +++ b/src/main/java/com/bitbar/remotedevice/RemoteDeviceClientMain.java @@ -8,6 +8,7 @@ import com.bitbar.remotedevice.cli.RemoteDeviceClientCommandLineInterface; import com.bitbar.remotedevice.errors.PortNotFreeException; import com.bitbar.remotedevice.errors.RequiredParameterIsEmptyException; +import com.bitbar.remotedevice.errors.WrongParameterException; import com.bitbar.remotedevice.ios.RemoteIOSDeviceSession; import com.bitbar.remotedevice.websocket.WebsocketManager; import com.testdroid.api.APIException; @@ -53,21 +54,6 @@ public class RemoteDeviceClientMain { private static String ADB_VERSION; - static { - try { - ProcessBuilder ps = new ProcessBuilder("adb", "version"); - Process process = ps.start(); - String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8.name()); - ADB_VERSION = new ADBVersionParser().parse(output); - process.waitFor(); - } catch (IOException | InterruptedException exc) { - LOGGER.error("Error", exc); - System.exit(1); - } finally { - LOGGER.info(String.format("ADB version detected: %s", ADB_VERSION)); - } - } - private RemoteDeviceClientMain(CommandLine commandLine) throws RequiredParameterIsEmptyException, APIException { String cloudUrl = commandLine.getOptionValue(CommandLineParameter.CLOUD_URI.getArgument()); String apiKey = commandLine.getOptionValue(CommandLineParameter.API_KEY.getArgument()); @@ -98,7 +84,7 @@ public static void main(String[] args) { LOGGER.error(String.format("Unknown command: %s", command)); System.exit(1); } - } catch (ParseException | RequiredParameterIsEmptyException e) { + } catch (ParseException | RequiredParameterIsEmptyException | WrongParameterException e) { LOGGER.error(e.getMessage()); usage(); } catch (APIException e) { @@ -143,7 +129,9 @@ private void devices() { } private void connect(CommandLine commandLine) - throws RequiredParameterIsEmptyException, APIException { + throws RequiredParameterIsEmptyException, APIException, WrongParameterException { + ADB_VERSION = computeAdbVersion(commandLine); + LOGGER.info("ADB version detected: {}", ADB_VERSION); Long deviceModelId; try { deviceModelId = Long.parseLong(commandLine.getOptionValue(CommandLineParameter.DEVICE_MODEL_ID.getArgument())); @@ -294,4 +282,26 @@ private void finishDeviceSessions() { } } + private String computeAdbVersion(CommandLine commandLine) throws WrongParameterException { + ADBVersionParser adbVersionParser = new ADBVersionParser(); + String adbVersion = null; + if (commandLine.hasOption(CommandLineParameter.ADB_VERSION.getArgument())) { + adbVersion = commandLine.getOptionValue(CommandLineParameter.ADB_VERSION.getArgument()); + adbVersionParser.validate(adbVersion); + return adbVersion; + } else { + try { + ProcessBuilder ps = new ProcessBuilder("adb", "version"); + Process process = ps.start(); + String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8.name()); + adbVersion = new ADBVersionParser().parse(output); + process.waitFor(); + } catch (IOException | InterruptedException exc) { + LOGGER.error("Can't detect ADB version", exc); + System.exit(1); + } + return adbVersion; + } + } + } diff --git a/src/main/java/com/bitbar/remotedevice/android/ADBVersionParser.java b/src/main/java/com/bitbar/remotedevice/android/ADBVersionParser.java index 76fa71e..23d5132 100644 --- a/src/main/java/com/bitbar/remotedevice/android/ADBVersionParser.java +++ b/src/main/java/com/bitbar/remotedevice/android/ADBVersionParser.java @@ -1,5 +1,8 @@ package com.bitbar.remotedevice.android; +import com.bitbar.remotedevice.cli.CommandLineParameter; +import com.bitbar.remotedevice.errors.WrongParameterException; + import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -13,4 +16,10 @@ public String parse(String input) { Matcher matcher = ADB_VERSION_PATTERN.matcher(input); return matcher.find() ? matcher.group(1) : UNKNOWN_VERSION; } + + public void validate(String input) throws WrongParameterException { + if (!ADB_VERSION_PATTERN.matcher(input).matches()) { + throw new WrongParameterException(CommandLineParameter.ADB_VERSION); + } + } } diff --git a/src/main/java/com/bitbar/remotedevice/cli/CommandLineParameter.java b/src/main/java/com/bitbar/remotedevice/cli/CommandLineParameter.java index a186a7e..a244aa3 100644 --- a/src/main/java/com/bitbar/remotedevice/cli/CommandLineParameter.java +++ b/src/main/java/com/bitbar/remotedevice/cli/CommandLineParameter.java @@ -2,6 +2,7 @@ public enum CommandLineParameter { API_KEY("apikey", true, "API key for cloud user authentication"), + ADB_VERSION("adbversion", true, "requested version of adb in format x.y.x"), DEVICE_MODEL_ID("device", true, "device id to connect"), CLOUD_URI("cloudurl", true, "address of cloud service"); diff --git a/src/main/java/com/bitbar/remotedevice/errors/WrongParameterException.java b/src/main/java/com/bitbar/remotedevice/errors/WrongParameterException.java new file mode 100644 index 0000000..831c43e --- /dev/null +++ b/src/main/java/com/bitbar/remotedevice/errors/WrongParameterException.java @@ -0,0 +1,10 @@ +package com.bitbar.remotedevice.errors; + +import com.bitbar.remotedevice.cli.CommandLineParameter; + +public class WrongParameterException extends Exception { + + public WrongParameterException(CommandLineParameter parameter) { + super(String.format("Parameter \"%s\" is wrong!", parameter.getArgument())); + } +} diff --git a/src/test/java/com/bitbar/remotedevice/android/ADBVersionParserTest.java b/src/test/java/com/bitbar/remotedevice/android/ADBVersionParserTest.java index babd7fc..5ba6387 100644 --- a/src/test/java/com/bitbar/remotedevice/android/ADBVersionParserTest.java +++ b/src/test/java/com/bitbar/remotedevice/android/ADBVersionParserTest.java @@ -1,6 +1,8 @@ package com.bitbar.remotedevice.android; +import com.bitbar.remotedevice.errors.WrongParameterException; import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -10,11 +12,14 @@ import java.util.Collection; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; class ADBVersionParserTest { - private static Collection data() { + private static Collection parseData() { Object[][] data = new Object[][]{ {"1.0.32"}, {"1.0.41"} @@ -22,13 +27,37 @@ private static Collection data() { return Arrays.asList(data); } + private static Collection validateData() { + Object[][] data = new Object[][]{ + {EMPTY, "Parameter \"adbversion\" is wrong!"}, + {"1", "Parameter \"adbversion\" is wrong!"}, + {"1.2", "Parameter \"adbversion\" is wrong!"}, + {"1.2.3", null}, + {"1.2.3.4", "Parameter \"adbversion\" is wrong!"} + }; + return Arrays.asList(data); + } + @ParameterizedTest - @MethodSource("data") + @MethodSource("parseData") void parse(String version) throws IOException{ ADBVersionParser classUnderTest = new ADBVersionParser(); assertThat(classUnderTest.parse(getStringFromFile(version))).isEqualTo(version); } + @ParameterizedTest + @MethodSource("validateData") + void validate(String version, String error) { + ADBVersionParser classUnderTest = new ADBVersionParser(); + Executable executable = () -> classUnderTest.validate(version); + if (error == null) { + assertDoesNotThrow(executable); + } else { + Throwable throwable = assertThrows(WrongParameterException.class, executable); + assertThat(throwable.getMessage()).isEqualTo("Parameter \"adbversion\" is wrong!"); + } + } + private static String getStringFromFile(String version) throws IOException { try (InputStream inputStream = ADBVersionParserTest.class.getResourceAsStream("/adb-version-output/" + version)) { return IOUtils.toString(inputStream, UTF_8);