directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fel...@apache.org
Subject svn commit: r947341 - in /directory/apacheds/trunk/kerberos-shared/src: main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java test/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddressesTest.java
Date Sat, 22 May 2010 22:27:01 GMT
Author: felixk
Date: Sat May 22 22:27:01 2010
New Revision: 947341

URL: http://svn.apache.org/viewvc?rev=947341&view=rev
Log:
- Avoid explicit casting of (HostAddresses[])
- Fix equals for null value
- Override hashCode when overriding equals
- Adding simple testcases

Modified:
    directory/apacheds/trunk/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java
    directory/apacheds/trunk/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddressesTest.java

Modified: directory/apacheds/trunk/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java?rev=947341&r1=947340&r2=947341&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java
(original)
+++ directory/apacheds/trunk/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddresses.java
Sat May 22 22:27:01 2010
@@ -116,6 +116,24 @@ public class HostAddresses extends Abstr
 
 
     /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+        int hash = 37;
+        
+        if ( addresses != null )
+        {
+            hash = hash * 17 + addresses.size();
+            hash = 17 + addresses.hashCode();
+        }
+        
+        return hash;
+    }
+
+
+    /**
      * Returns true if two {@link HostAddresses} are equal.
      *
      * @param that
@@ -123,6 +141,10 @@ public class HostAddresses extends Abstr
      */
     public boolean equals( HostAddresses that )
     {
+        if( that == null ) {
+            return false;
+        }
+        
         if ( ( addresses == null && that.addresses != null )
             || ( addresses != null && that.addresses == null ) )
         {
@@ -136,12 +158,9 @@ public class HostAddresses extends Abstr
                 return false;
             }
 
-            HostAddress[] thisHostAddresses = ( HostAddress[] ) addresses.toArray();
-            HostAddress[] thatHostAddresses = ( HostAddress[] ) that.addresses.toArray();
-
-            for ( int i = 0; i < thisHostAddresses.length; i++ )
+            for ( int i = 0; i < addresses.size(); i++ )
             {
-                if ( !thisHostAddresses[i].equals( thatHostAddresses[i] ) )
+                if ( !addresses.get( i ).equals( that.addresses.get( i ) ) )
                 {
                     return false;
                 }
@@ -159,7 +178,7 @@ public class HostAddresses extends Abstr
      */
     public HostAddress[] getAddresses()
     {
-        return ( HostAddress[] ) addresses.toArray();
+        return addresses.toArray(new HostAddress[0]);
     }
 
 

Modified: directory/apacheds/trunk/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddressesTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddressesTest.java?rev=947341&r1=947340&r2=947341&view=diff
==============================================================================
--- directory/apacheds/trunk/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddressesTest.java
(original)
+++ directory/apacheds/trunk/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/HostAddressesTest.java
Sat May 22 22:27:01 2010
@@ -20,12 +20,17 @@
 package org.apache.directory.server.kerberos.shared.messages.value;
 
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 
 import org.apache.directory.server.kerberos.shared.messages.value.types.HostAddrType;
+import org.junit.Before;
 import org.junit.Test;
-import static org.junit.Assert.assertTrue;
 
 /**
  * Test the HostAddresses encoding and decoding
@@ -35,6 +40,47 @@ import static org.junit.Assert.assertTru
  */
 public class HostAddressesTest
 {
+    HostAddresses hostAddressesA;
+    HostAddresses hostAddressesACopy;
+    HostAddresses hostAddressesB;
+    HostAddresses hostAddressesC;
+    HostAddresses hostAddressesD;
+
+    
+    /**
+     * Initialize name instances
+     */
+    @Before
+    public void initNames() throws Exception
+    {
+        hostAddressesA = new HostAddresses ( new HostAddress[]
+            { new HostAddress( HostAddrType.ADDRTYPE_INET, new byte[]
+                { 0x01, 0x02, 0x03, 0x04 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x05, 0x06, 0x07, 0x08 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x09, 0x0A, 0x0B, 0x0C } ) } );
+        hostAddressesACopy = new HostAddresses ( new HostAddress[]
+            { new HostAddress( HostAddrType.ADDRTYPE_INET, new byte[]
+                { 0x01, 0x02, 0x03, 0x04 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x05, 0x06, 0x07, 0x08 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x09, 0x0A, 0x0B, 0x0C } ) } );
+        hostAddressesB = new HostAddresses ( new HostAddress[]
+            { new HostAddress( HostAddrType.ADDRTYPE_INET, new byte[]
+                { 0x01, 0x02, 0x03, 0x04 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x05, 0x06, 0x07, 0x08 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x09, 0x0A, 0x0B, 0x0C } ) } );
+        hostAddressesC = new HostAddresses ( new HostAddress[]
+            { new HostAddress( HostAddrType.ADDRTYPE_INET, new byte[]
+                { 0x01, 0x02, 0x03, 0x05 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x05, 0x06, 0x07, 0x08 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x09, 0x0A, 0x0B, 0x0C } ) } );
+        hostAddressesD = new HostAddresses ( new HostAddress[]
+            { new HostAddress( HostAddrType.ADDRTYPE_INET, new byte[]
+                { 0x01, 0x02, 0x03, 0x04 } ), new HostAddress( HostAddrType.ADDRTYPE_INET,
new byte[]
+                { 0x05, 0x06, 0x07, 0x08 } ), new HostAddress( HostAddrType.ADDRTYPE_INET6,
new byte[]
+                { 0x09, 0x0A, 0x0B, 0x0C } ) } );
+    }
+
+    
     @Test
     public void testEncodingHostAddressesIPOneAddresses() throws Exception
     {
@@ -119,4 +165,69 @@ public class HostAddressesTest
 
         assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
     }
+
+
+    @Test
+    public void testEqualsNull() throws Exception
+    {
+        assertFalse( hostAddressesA.equals( null ) );
+    }
+
+
+    @Test
+    public void testEqualsReflexive() throws Exception
+    {
+        assertTrue( hostAddressesA.equals( hostAddressesA ) );
+    }
+
+
+    @Test
+    public void testHashCodeReflexive() throws Exception
+    {
+        assertEquals( hostAddressesA.hashCode(), hostAddressesA.hashCode() );
+    }
+
+
+    @Test
+    public void testEqualsSymmetric() throws Exception
+    {
+        assertTrue( hostAddressesA.equals( hostAddressesACopy ) );
+        assertTrue( hostAddressesACopy.equals( hostAddressesA ) );
+    }
+
+
+    @Test
+    public void testHashCodeSymmetric() throws Exception
+    {
+        assertEquals( hostAddressesA.hashCode(), hostAddressesACopy.hashCode() );
+        assertEquals( hostAddressesACopy.hashCode(), hostAddressesA.hashCode() );
+    }
+
+
+    @Test
+    public void testEqualsTransitive() throws Exception
+    {
+        assertTrue( hostAddressesA.equals( hostAddressesACopy ) );
+        assertTrue( hostAddressesACopy.equals( hostAddressesB ) );
+        assertTrue( hostAddressesA.equals( hostAddressesB ) );
+    }
+
+
+    @Test
+    public void testHashCodeTransitive() throws Exception
+    {
+        assertEquals( hostAddressesA.hashCode(), hostAddressesACopy.hashCode() );
+        assertEquals( hostAddressesACopy.hashCode(), hostAddressesB.hashCode() );
+        assertEquals( hostAddressesA.hashCode(), hostAddressesB.hashCode() );
+    }
+
+
+    @Test
+    public void testNotEqualDiffValue() throws Exception
+    {
+        assertFalse( hostAddressesA.equals( hostAddressesC ) );
+        assertFalse( hostAddressesC.equals( hostAddressesA ) );
+        assertFalse( hostAddressesA.equals( hostAddressesD ) );
+        assertFalse( hostAddressesD.equals( hostAddressesA ) );
+    }
 }



Mime
View raw message