Fix LinkProperties's equals() method.

LinkProperties's equals() method was broken by the addition of
stacked interfaces. The reason was that equals() was checking
the equality of mStackedInterfaces.keys(), which is just an
enumeration, instead of mStackedInterfaces.keySet(), which
actually contains the keys. The test was failing, but I didn't
notice.

Fix the bug and make the test check the objects more in depth
so it can give more detailed error messages when equals() fails.

Bug: 8276725
Change-Id: Ie990bd75f641c28e63e54d953dcd0f4de13f7c9f
This commit is contained in:
Lorenzo Colitti
2013-04-01 10:47:43 +09:00
parent 1c17853c0b
commit 89218e5e3f
2 changed files with 33 additions and 9 deletions

View File

@@ -378,7 +378,7 @@ public class LinkProperties implements Parcelable {
* @return {@code true} if both are identical, {@code false} otherwise. * @return {@code true} if both are identical, {@code false} otherwise.
*/ */
public boolean isIdenticalStackedLinks(LinkProperties target) { public boolean isIdenticalStackedLinks(LinkProperties target) {
if (!mStackedLinks.keys().equals(target.mStackedLinks.keys())) { if (!mStackedLinks.keySet().equals(target.mStackedLinks.keySet())) {
return false; return false;
} }
for (LinkProperties stacked : mStackedLinks.values()) { for (LinkProperties stacked : mStackedLinks.values()) {

View File

@@ -33,14 +33,41 @@ public class LinkPropertiesTest extends TestCase {
private static String GATEWAY2 = "69.78.8.1"; private static String GATEWAY2 = "69.78.8.1";
private static String NAME = "qmi0"; private static String NAME = "qmi0";
public void assertLinkPropertiesEqual(LinkProperties source, LinkProperties target) {
// Check implementation of equals(), element by element.
assertTrue(source.isIdenticalInterfaceName(target));
assertTrue(target.isIdenticalInterfaceName(source));
assertTrue(source.isIdenticalAddresses(target));
assertTrue(target.isIdenticalAddresses(source));
assertTrue(source.isIdenticalDnses(target));
assertTrue(target.isIdenticalDnses(source));
assertTrue(source.isIdenticalRoutes(target));
assertTrue(target.isIdenticalRoutes(source));
assertTrue(source.isIdenticalHttpProxy(target));
assertTrue(target.isIdenticalHttpProxy(source));
assertTrue(source.isIdenticalStackedLinks(target));
assertTrue(target.isIdenticalStackedLinks(source));
// Check result of equals().
assertTrue(source.equals(target));
assertTrue(target.equals(source));
// Check hashCode.
assertEquals(source.hashCode(), target.hashCode());
}
@SmallTest @SmallTest
public void testEqualsNull() { public void testEqualsNull() {
LinkProperties source = new LinkProperties(); LinkProperties source = new LinkProperties();
LinkProperties target = new LinkProperties(); LinkProperties target = new LinkProperties();
assertFalse(source == target); assertFalse(source == target);
assertTrue(source.equals(target)); assertLinkPropertiesEqual(source, target);
assertTrue(source.hashCode() == target.hashCode());
} }
@SmallTest @SmallTest
@@ -73,8 +100,7 @@ public class LinkPropertiesTest extends TestCase {
target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
assertTrue(source.equals(target)); assertLinkPropertiesEqual(source, target);
assertTrue(source.hashCode() == target.hashCode());
target.clear(); target.clear();
// change Interface Name // change Interface Name
@@ -163,8 +189,7 @@ public class LinkPropertiesTest extends TestCase {
target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
assertTrue(source.equals(target)); assertLinkPropertiesEqual(source, target);
assertTrue(source.hashCode() == target.hashCode());
} catch (Exception e) { } catch (Exception e) {
fail(); fail();
} }
@@ -191,8 +216,7 @@ public class LinkPropertiesTest extends TestCase {
target.addLinkAddress(new LinkAddress( target.addLinkAddress(new LinkAddress(
NetworkUtils.numericToInetAddress(ADDRV6), 128)); NetworkUtils.numericToInetAddress(ADDRV6), 128));
assertTrue(source.equals(target)); assertLinkPropertiesEqual(source, target);
assertTrue(source.hashCode() == target.hashCode());
} catch (Exception e) { } catch (Exception e) {
fail(); fail();
} }