Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from snieguu/master
Browse files Browse the repository at this point in the history
Allow define ADB version as commandline parameter
  • Loading branch information
lastverb authored Nov 4, 2019
2 parents 21c0b2d + f7e1381 commit f9bfcf0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
44 changes: 27 additions & 17 deletions src/main/java/com/bitbar/remotedevice/RemoteDeviceClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,25 +12,52 @@
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<Object[]> data() {
private static Collection<Object[]> parseData() {
Object[][] data = new Object[][]{
{"1.0.32"},
{"1.0.41"}
};
return Arrays.asList(data);
}

private static Collection<Object[]> 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);
Expand Down

0 comments on commit f9bfcf0

Please sign in to comment.