Merge pull request #39 from SKART1/IMP-38-getprop

Imp 38 getprop
This commit is contained in:
Samuel Carlsson 2016-10-03 17:57:17 +02:00 committed by GitHub
commit 70ea021bf0
4 changed files with 154 additions and 0 deletions

View File

@ -4,7 +4,11 @@ import se.vidstige.jadb.managers.Bash;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JadbDevice {
private final String serial;

View 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;
}
}

View File

@ -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;
@ -17,6 +18,7 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
public class MockedTestCases {

View File

@ -0,0 +1,95 @@
package se.vidstige.jadb.test.unit;
import org.junit.After;
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.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class PropertyManagerTest {
private final String DEVICE_SERIAL = "serial-123";
private FakeAdbServer server;
private JadbConnection connection;
private JadbDevice device;
@Before
public void setUp() throws Exception {
server = new FakeAdbServer(15037);
server.start();
server.add(DEVICE_SERIAL);
connection = new JadbConnection("localhost", 15037);
device = connection.getDevices().get(0);
}
@After
public void tearDown() throws Exception {
server.stop();
server.verifyExpectations();
}
@Test
public void testGetPropsStandardFormat() throws Exception {
//Arrange
Map<String, String> expected = new HashMap<>();
expected.put("bluetooth.hciattach", "true");
expected.put("bluetooth.status", "off");
String response = "[bluetooth.status]: [off] \n" +
"[bluetooth.hciattach]: [true]";
server.expectShell(DEVICE_SERIAL, "getprop").returns(response);
//Act
Map<String, String> actual = new PropertyManager(device).getprop();
//Assert
assertEquals(expected, actual);
}
@Test
public void testGetPropsMalformedIgnoredString() throws Exception {
//Arrange
Map<String, String> expected = new HashMap<>();
expected.put("bluetooth.hciattach", "true");
expected.put("bluetooth.status", "off");
String response = "[bluetooth.status]: [off]\n" +
"[malformed_line]\n" +
"[bluetooth.hciattach]: [true]";
server.expectShell(DEVICE_SERIAL, "getprop").returns(response);
//Act
Map<String, String> actual = new PropertyManager(device).getprop();
//Assert
assertEquals(expected, actual);
}
@Test
public void testGetPropsMalformedNotUsedString() throws Exception {
//Arrange
Map<String, String> expected = new HashMap<>();
expected.put("bluetooth.status", "off");
String response = "[bluetooth.status]: [off]\n" +
"malformed[bluetooth.hciattach]: [true]";
server.expectShell(DEVICE_SERIAL, "getprop").returns(response);
//Act
Map<String, String> actual = new PropertyManager(device).getprop();
//Assert
assertEquals(expected, actual);
}
}