From e40a0fd4332a5bbd6e8575b36074875f7580a7b9 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 22 Aug 2016 11:51:40 +0000
Subject: [PATCH] HostPort: added unit tests

---
 opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java |   56 +++++++++++++++++++++++++++-
 opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java     |    9 ++--
 2 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java b/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java
index fcf599b..322023d 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java
@@ -38,6 +38,8 @@
  * <p>
  * Due to the possibility of live network configuration changes, instances of
  * this class are not intended for caching and should be rebuilt on demand.
+ * <p>
+ * Note: this class has a natural ordering that is inconsistent with equals.
  */
 @org.opends.server.types.PublicAPI(
      stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
@@ -462,11 +464,8 @@
 
       // Get and compare addresses of RS1 and RS2
       // Normalize local addresses to null for fast comparison.
-      final InetAddress[] thisAddresses =
-          isLocalAddress() ? null : InetAddress.getAllByName(getHost());
-      final InetAddress[] otherAddresses =
-          other.isLocalAddress() ? null : InetAddress.getAllByName(other
-              .getHost());
+      final InetAddress[] thisAddresses = isLocalAddress() ? null : InetAddress.getAllByName(getHost());
+      final InetAddress[] otherAddresses = other.isLocalAddress() ? null : InetAddress.getAllByName(other.getHost());
 
       // Now compare addresses, if at least one match, this is the same server.
       if (thisAddresses == null && otherAddresses == null)
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java b/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java
index e03df59..a3c2ad4 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java
@@ -15,11 +15,14 @@
  */
 package org.opends.server.types;
 
+import static org.assertj.core.api.Assertions.*;
+import static org.opends.server.util.CollectionUtils.*;
+
+import java.util.Set;
+
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
-import static org.assertj.core.api.Assertions.*;
-
 @Test(groups = { "precommit", "types" }, sequential = true)
 @SuppressWarnings("javadoc")
 public class HostPortTest extends TypesTestCase
@@ -132,6 +135,7 @@
     final HostPort hp2 = new HostPort("home", 1);
     assertThat(hp1).isEqualTo(hp2);
     assertThat(hp1.hashCode()).isEqualTo(hp2.hashCode());
+    assertThat(hp1).isEqualByComparingTo(hp2);
   }
 
   @Test
@@ -163,6 +167,17 @@
     final HostPort hp2 = new HostPort(hostName, 389);
     assertThat(hp1).isEqualTo(hp2);
     assertThat(hp1.hashCode()).isEqualTo(hp2.hashCode());
+    assertThat(hp1).isEqualByComparingTo(hp2);
+  }
+
+  @Test
+  public void valueOfEqualsHashCodeDifferentHostNames()
+  {
+    final HostPort hp1 = HostPort.valueOf("127.0.0.1:389");
+    final HostPort hp2 = HostPort.valueOf("localhost:389");
+    assertThat(hp1).isEqualTo(hp2);
+    assertThat(hp1.hashCode()).isEqualTo(hp2.hashCode());
+    assertThat(hp1).isNotEqualByComparingTo(hp2);
   }
 
   @Test(expectedExceptions = IllegalArgumentException.class)
@@ -229,4 +244,41 @@
     HostPort hp2 = HostPort.allAddresses(1389);
     assertThat(hp1).isEqualTo(hp2);
   }
+
+  @Test
+  public void isEquivalentTo()
+  {
+    HostPort hp1 = HostPort.valueOf("localhost:389");
+    assertThat(hp1.isEquivalentTo(hp1)).isTrue();
+    HostPort hp2 = HostPort.valueOf("localhost:389");
+    assertThat(hp1.isEquivalentTo(hp2)).isTrue();
+
+    HostPort hpNull = new HostPort(null, 0);
+    assertThat(hp1.isEquivalentTo(hpNull)).isFalse();
+
+    HostPort nonLocalHp = HostPort.valueOf("example.org:389");
+    assertThat(nonLocalHp.isEquivalentTo(nonLocalHp)).isTrue();
+    assertThat(hp1.isEquivalentTo(nonLocalHp)).isFalse();
+    assertThat(nonLocalHp.isEquivalentTo(hp1)).isFalse();
+  }
+
+  @Test
+  public void isLocal()
+  {
+    HostPort hp1 = HostPort.valueOf("localhost:389");
+    assertThat(hp1.isLocalAddress()).isTrue();
+
+    HostPort nonLocalHp = HostPort.valueOf("example.org:389");
+    assertThat(nonLocalHp.isLocalAddress()).isFalse();
+  }
+
+  @Test
+  public void toLowerCaseStrings()
+  {
+    HostPort hp1 = HostPort.valueOf("localHost:389");
+    HostPort nonLocalHp = HostPort.valueOf("Example.org:389");
+
+    Set<String> sets = HostPort.toLowerCaseStrings(newHashSet(hp1, nonLocalHp));
+    assertThat(sets).containsOnly("localhost:389", "example.org:389");
+  }
 }

--
Gitblit v1.10.0