### Question:
NameServerComparator implements Comparator<InetSocketAddress>, Serializable { @Override public int compare(InetSocketAddress addr1, InetSocketAddress addr2) { if (addr1.equals(addr2)) { return 0; } if (!addr1.isUnresolved() && !addr2.isUnresolved()) { if (addr1.getAddress().getClass() == addr2.getAddress().getClass()) { return 0; } return preferredAddressType.isAssignableFrom(addr1.getAddress().getClass()) ? -1 : 1; } if (addr1.isUnresolved() && addr2.isUnresolved()) { return 0; } return addr1.isUnresolved() ? 1 : -1; } NameServerComparator(Class<? extends InetAddress> preferredAddressType); @Override int compare(InetSocketAddress addr1, InetSocketAddress addr2);  }### Answer:
@Test public void testCompareResolvedOnly() { NameServerComparator comparator = new NameServerComparator(Inet4Address.class); int x = comparator.compare(IPV4ADDRESS1, IPV6ADDRESS1); int y = comparator.compare(IPV6ADDRESS1, IPV4ADDRESS1); assertEquals(-1, x); assertEquals(x, -y); assertEquals(0, comparator.compare(IPV4ADDRESS1, IPV4ADDRESS1)); assertEquals(0, comparator.compare(IPV6ADDRESS1, IPV6ADDRESS1)); }
@Test public void testCompareUnresolvedSimple() { NameServerComparator comparator = new NameServerComparator(Inet4Address.class); int x = comparator.compare(IPV4ADDRESS1, UNRESOLVED1); int y = comparator.compare(UNRESOLVED1, IPV4ADDRESS1); assertEquals(-1, x); assertEquals(x, -y); assertEquals(0, comparator.compare(IPV4ADDRESS1, IPV4ADDRESS1)); assertEquals(0, comparator.compare(UNRESOLVED1, UNRESOLVED1)); }
@Test public void testCompareUnresolvedOnly() { NameServerComparator comparator = new NameServerComparator(Inet4Address.class); int x = comparator.compare(UNRESOLVED1, UNRESOLVED2); int y = comparator.compare(UNRESOLVED2, UNRESOLVED1); assertEquals(0, x); assertEquals(x, -y); assertEquals(0, comparator.compare(UNRESOLVED1, UNRESOLVED1)); assertEquals(0, comparator.compare(UNRESOLVED2, UNRESOLVED2)); }