mirror of
https://github.com/revanced/jadb.git
synced 2025-02-14 19:16:49 +01:00
vidstige/jadb#38 pull-request fixes
This commit is contained in:
parent
5b66a90c3b
commit
f4c44d6e3f
@ -74,37 +74,6 @@ public class JadbDevice {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getprop() throws IOException, JadbException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(executeShell("getprop")));
|
||||
return parseProp(bufferedReader);
|
||||
}
|
||||
|
||||
//@VisibleForTesting
|
||||
private Map<String, String> parseProp(BufferedReader bufferedReader) throws IOException {
|
||||
final Pattern pattern = Pattern.compile("^\\[(.*)\\]:.\\[(.*)\\]");
|
||||
|
||||
HashMap<String, String> result = new HashMap<>();
|
||||
|
||||
String line;
|
||||
Matcher matcher = pattern.matcher("");
|
||||
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
matcher.reset(line);
|
||||
|
||||
if(matcher.find()) {
|
||||
if(matcher.groupCount() < 2) {
|
||||
System.err.println("Property line: " + line +" does not match patter. Ignoring");
|
||||
continue;
|
||||
}
|
||||
String key = matcher.group(1);
|
||||
String value = matcher.group(2);
|
||||
result.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
||||
Transport transport = getTransport();
|
||||
SyncTransport sync = transport.startSync();
|
||||
|
53
src/se/vidstige/jadb/managers/PropertyManager.java
Normal file
53
src/se/vidstige/jadb/managers/PropertyManager.java
Normal file
@ -0,0 +1,53 @@
|
||||
package se.vidstige.jadb.managers;
|
||||
|
||||
import se.vidstige.jadb.JadbDevice;
|
||||
import se.vidstige.jadb.JadbException;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A class which works with properties, uses getprop and setprop methods of android shell
|
||||
*/
|
||||
public class PropertyManager {
|
||||
private final JadbDevice device;
|
||||
|
||||
public PropertyManager(JadbDevice device) {
|
||||
this.device = device;
|
||||
}
|
||||
|
||||
public Map<String, String> getprop() throws IOException, JadbException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(device.executeShell("getprop")));
|
||||
return parseProp(bufferedReader);
|
||||
}
|
||||
|
||||
private Map<String, String> parseProp(BufferedReader bufferedReader) throws IOException {
|
||||
final Pattern pattern = Pattern.compile("^\\[([a-zA-Z0-9_.-]*)\\]:.\\[([a-zA-Z0-9_.-]*)\\]");
|
||||
|
||||
HashMap<String, String> result = new HashMap<>();
|
||||
|
||||
String line;
|
||||
Matcher matcher = pattern.matcher("");
|
||||
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
matcher.reset(line);
|
||||
|
||||
if (matcher.find()) {
|
||||
if (matcher.groupCount() < 2) {
|
||||
System.err.println("Property line: " + line + " does not match patter. Ignoring");
|
||||
continue;
|
||||
}
|
||||
String key = matcher.group(1);
|
||||
String value = matcher.group(2);
|
||||
result.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package se.vidstige.jadb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class FakeJadbDevice extends JadbDevice {
|
||||
private final List<InputStream> executeShellResponse;
|
||||
|
||||
public FakeJadbDevice(String serial, String type, ITransportFactory tFactory, List<InputStream> executeShellResponse) {
|
||||
super(serial, type, tFactory);
|
||||
this.executeShellResponse = executeShellResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream executeShell(String command, String... args) throws IOException, JadbException {
|
||||
return executeShellResponse.get(0);
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package se.vidstige.jadb.test.unit;
|
||||
|
||||
import org.junit.Test;
|
||||
import se.vidstige.jadb.FakeJadbDevice;
|
||||
import se.vidstige.jadb.ITransportFactory;
|
||||
import se.vidstige.jadb.JadbDevice;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class JadbDeviceTest {
|
||||
@Test
|
||||
public void getprop() throws Exception {
|
||||
final String key1 = "bluetooth.hciattach";
|
||||
final String value1 = "true";
|
||||
|
||||
final String key2 = "bluetooth.status";
|
||||
final String value2 = "off";
|
||||
|
||||
String response = "[" + key1 + "]: [" + value1 + "]\n" +
|
||||
"[" + key2 + "]: [" + value2 + "]\n";
|
||||
|
||||
List<InputStream> executeShellResponse = Collections.singletonList((InputStream) new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
|
||||
Map<String, String> props = new FakeJadbDevice("serial", "type", null, executeShellResponse).getprop();
|
||||
|
||||
assertNotNull(props.get(key1));
|
||||
assertEquals(props.get(key1), value1);
|
||||
|
||||
assertNotNull(props.get(key2));
|
||||
assertEquals(props.get(key2), value2);
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ import se.vidstige.jadb.JadbConnection;
|
||||
import se.vidstige.jadb.JadbDevice;
|
||||
import se.vidstige.jadb.JadbException;
|
||||
import se.vidstige.jadb.RemoteFile;
|
||||
import se.vidstige.jadb.managers.PropertyManager;
|
||||
import se.vidstige.jadb.test.fakes.FakeAdbServer;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@ -99,7 +100,7 @@ public class MockedTestCases {
|
||||
server.add("serial-123");
|
||||
server.expectShell("serial-123", "getprop").returns("[] = nope\nx\n(");
|
||||
JadbDevice device = connection.getDevices().get(0);
|
||||
Map<String, String> x = device.getprop();
|
||||
Map<String, String> x = new PropertyManager(device).getprop();
|
||||
Assert.assertEquals(0, x.size());
|
||||
}
|
||||
|
||||
|
85
test/se/vidstige/jadb/test/unit/PropertyManagerTest.java
Normal file
85
test/se/vidstige/jadb/test/unit/PropertyManagerTest.java
Normal file
@ -0,0 +1,85 @@
|
||||
package se.vidstige.jadb.test.unit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import se.vidstige.jadb.JadbConnection;
|
||||
import se.vidstige.jadb.JadbDevice;
|
||||
import se.vidstige.jadb.managers.PropertyManager;
|
||||
import se.vidstige.jadb.test.fakes.FakeAdbServer;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class PropertyManagerTest {
|
||||
private FakeAdbServer server;
|
||||
private JadbConnection connection;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new FakeAdbServer(15037);
|
||||
server.start();
|
||||
connection = new JadbConnection("localhost", 15037);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPropsStandardFormat() throws Exception {
|
||||
final String key1 = "bluetooth.hciattach";
|
||||
final String value1 = "true";
|
||||
|
||||
final String key2 = "bluetooth.status";
|
||||
final String value2 = "off";
|
||||
|
||||
String format = "[%s]: [%s] \n";
|
||||
|
||||
String response = String.format(format, key1, value1) + String.format(format, key2, value2);
|
||||
|
||||
|
||||
server.add("serial-123");
|
||||
server.expectShell("serial-123", "getprop").returns(response);
|
||||
JadbDevice device = connection.getDevices().get(0);
|
||||
Map<String, String> props = new PropertyManager(device).getprop();
|
||||
|
||||
assertNotNull(props.get(key1));
|
||||
assertEquals(props.get(key1), value1);
|
||||
|
||||
assertNotNull(props.get(key2));
|
||||
assertEquals(props.get(key2), value2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPropsMalformedString() throws Exception {
|
||||
final String key1 = "bluetooth.hciattach";
|
||||
final String value1 = "true";
|
||||
|
||||
final String key2 = "bluetooth.status";
|
||||
final String value2 = "off";
|
||||
|
||||
String format = "[%s]: [%s] \n";
|
||||
|
||||
String response1 = String.format(format, key1, value1) + "[malformed]" + String.format(format, key2, value2);
|
||||
String response2 = String.format(format, key1, value1) + "[malformed]\n" + String.format(format, key2, value2);
|
||||
|
||||
|
||||
server.add("serial-123");
|
||||
JadbDevice device = connection.getDevices().get(0);
|
||||
server.expectShell("serial-123", "getprop").returns(response1);
|
||||
|
||||
//Test1
|
||||
Map<String, String> props1 = new PropertyManager(device).getprop();
|
||||
|
||||
assertNotNull(props1.get(key1));
|
||||
assertEquals(props1.get(key1), value1);
|
||||
|
||||
//Test2
|
||||
server.expectShell("serial-123", "getprop").returns(response2);
|
||||
Map<String, String> props2 = new PropertyManager(device).getprop();
|
||||
|
||||
assertNotNull(props2.get(key1));
|
||||
assertEquals(props2.get(key1), value1);
|
||||
|
||||
assertNotNull(props2.get(key2));
|
||||
assertEquals(props2.get(key2), value2);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user