From db5d9e13bcd894015e8d234fe100b42de37b21fd Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 3 Oct 2016 15:41:08 +0300 Subject: [PATCH] vidstige/jadb#38 some more changes for PR --- .../jadb/test/unit/MockedTestCases.java | 9 -- .../jadb/test/unit/PropertyManagerTest.java | 104 ++++++++++-------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/test/se/vidstige/jadb/test/unit/MockedTestCases.java b/test/se/vidstige/jadb/test/unit/MockedTestCases.java index 37b6ec6..1063652 100644 --- a/test/se/vidstige/jadb/test/unit/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/unit/MockedTestCases.java @@ -95,15 +95,6 @@ public class MockedTestCases { device.executeShell("ls", "space file"); } - @Test - public void testGetProps() throws Exception { - server.add("serial-123"); - server.expectShell("serial-123", "getprop").returns("[] = nope\nx\n("); - JadbDevice device = connection.getDevices().get(0); - Map x = new PropertyManager(device).getprop(); - Assert.assertEquals(0, x.size()); - } - private long parseDate(String date) throws ParseException { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return dateFormat.parse(date).getTime(); diff --git a/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java b/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java index 11c6880..dadf17c 100644 --- a/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java +++ b/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java @@ -1,5 +1,6 @@ package se.vidstige.jadb.test.unit; +import org.junit.After; import org.junit.Before; import org.junit.Test; import se.vidstige.jadb.JadbConnection; @@ -7,79 +8,88 @@ 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; -import static org.junit.Assert.assertNotNull; 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 { - final String key1 = "bluetooth.hciattach"; - final String value1 = "true"; + //Arrange + Map expected = new HashMap<>(); + expected.put("bluetooth.hciattach", "true"); + expected.put("bluetooth.status", "off"); - final String key2 = "bluetooth.status"; - final String value2 = "off"; + String response = "[bluetooth.status]: [off] \n" + + "[bluetooth.hciattach]: [true]"; - String format = "[%s]: [%s] \n"; + server.expectShell(DEVICE_SERIAL, "getprop").returns(response); - String response = String.format(format, key1, value1) + String.format(format, key2, value2); + //Act + Map actual = new PropertyManager(device).getprop(); - - server.add("serial-123"); - server.expectShell("serial-123", "getprop").returns(response); - JadbDevice device = connection.getDevices().get(0); - Map props = new PropertyManager(device).getprop(); - - assertNotNull(props.get(key1)); - assertEquals(props.get(key1), value1); - - assertNotNull(props.get(key2)); - assertEquals(props.get(key2), value2); + //Assert + assertEquals(expected, actual); } @Test - public void testGetPropsMalformedString() throws Exception { - final String key1 = "bluetooth.hciattach"; - final String value1 = "true"; + public void testGetPropsMalformedIgnoredString() throws Exception { + //Arrange + Map expected = new HashMap<>(); + expected.put("bluetooth.hciattach", "true"); + expected.put("bluetooth.status", "off"); - final String key2 = "bluetooth.status"; - final String value2 = "off"; + String response = "[bluetooth.status]: [off]\n" + + "[malformed_line]\n" + + "[bluetooth.hciattach]: [true]"; - String format = "[%s]: [%s] \n"; + server.expectShell(DEVICE_SERIAL, "getprop").returns(response); - 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); + //Act + Map actual = new PropertyManager(device).getprop(); - - server.add("serial-123"); - JadbDevice device = connection.getDevices().get(0); - server.expectShell("serial-123", "getprop").returns(response1); - - //Test1 - Map props1 = new PropertyManager(device).getprop(); - - assertNotNull(props1.get(key1)); - assertEquals(props1.get(key1), value1); - - //Test2 - server.expectShell("serial-123", "getprop").returns(response2); - Map props2 = new PropertyManager(device).getprop(); - - assertNotNull(props2.get(key1)); - assertEquals(props2.get(key1), value1); - - assertNotNull(props2.get(key2)); - assertEquals(props2.get(key2), value2); + //Assert + assertEquals(expected, actual); } -} \ No newline at end of file + + @Test + public void testGetPropsMalformedNotUsedString() throws Exception { + //Arrange + Map 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 actual = new PropertyManager(device).getprop(); + + //Assert + assertEquals(expected, actual); + } +}