directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fel...@apache.org
Subject svn commit: r945727 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/aci/UserClass.java test/java/org/apache/directory/shared/ldap/aci/NameTest.java
Date Tue, 18 May 2010 16:08:36 GMT
Author: felixk
Date: Tue May 18 16:08:35 2010
New Revision: 945727

URL: http://svn.apache.org/viewvc?rev=945727&view=rev
Log:
Implement hashCode

Added:
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/NameTest.java
  (with props)
Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/aci/UserClass.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/aci/UserClass.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/aci/UserClass.java?rev=945727&r1=945726&r2=945727&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/aci/UserClass.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/aci/UserClass.java
Tue May 18 16:08:35 2010
@@ -154,6 +154,10 @@ public abstract class UserClass implemen
         }
 
 
+        /**
+         * {@inheritDoc}
+         */
+        @Override
         public boolean equals( Object o )
         {
             if ( this == o )
@@ -176,6 +180,23 @@ public abstract class UserClass implemen
         }
 
 
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public int hashCode()
+        {
+            int result = 37;
+
+            for ( DN dn : this.names )
+            {
+                result = result * 17 + dn.hashCode();
+            }
+
+            return result;
+        }
+
+
         public String toString()
         {
             StringBuilder buffer = new StringBuilder();

Added: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/NameTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/NameTest.java?rev=945727&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/NameTest.java
(added)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/NameTest.java
Tue May 18 16:08:35 2010
@@ -0,0 +1,112 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.aci;
+
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.directory.shared.ldap.aci.UserClass.Name;
+import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
+import org.apache.directory.shared.ldap.name.DN;
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ * Unit tests class UserClass.Name.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class NameTest
+{
+    private Name nameInstanceA;
+    private Name nameInstanceB;
+    private Name nameInstanceC;
+    private Name nameInstanceD;
+
+
+    /**
+     * Initialize name instances
+     * 
+     * nameInstanceA is equal to nameInstanceB
+     * NameInstanceC is different
+     */
+    @Before
+    public void initNames() throws LdapInvalidDnException
+    {
+        Set<DN> dnSetA = new HashSet<DN>();
+        dnSetA.add( new DN( "a=aa" ) );
+        dnSetA.add( new DN( "b=bb" ) );
+
+        Set<DN> dnSetB = new HashSet<DN>();
+        dnSetB.add( new DN( "b=bb" ) );
+        dnSetB.add( new DN( "a=aa" ) );
+
+        Set<DN> dnSetC = new HashSet<DN>();
+        dnSetC.add( new DN( "a=aa" ) );
+        dnSetC.add( new DN( "b=bb" ) );
+
+        Set<DN> dnSetD = new HashSet<DN>();
+        dnSetD.add( new DN( "b=bb" ) );
+        dnSetD.add( new DN( "c=cc" ) );
+
+        nameInstanceA = new Name( dnSetA );
+        nameInstanceB = new Name( dnSetB );
+        nameInstanceC = new Name( dnSetC );
+        nameInstanceD = new Name( dnSetD );
+    }
+
+
+    /**
+     * Tests for equality.
+     */
+    @Test
+    public void testEqual() throws Exception
+    {
+        assertFalse( nameInstanceA.equals( null ) );
+        assertTrue( nameInstanceA.equals( nameInstanceA ) );
+        assertTrue( nameInstanceA.equals( nameInstanceB ) );
+        assertTrue( nameInstanceB.equals( nameInstanceA ) );
+        assertTrue( nameInstanceB.equals( nameInstanceC ) );
+        assertTrue( nameInstanceA.equals( nameInstanceC ) );
+        assertFalse( nameInstanceA.equals( nameInstanceD ) );
+        assertFalse( nameInstanceB.equals( nameInstanceD ) );
+    }
+
+
+    /**
+     * Tests for hashCode.
+     * 
+     * Only test hashCode for equal object as they must have an equal hashCode.
+     * For non equal object the hashCode can be equal.
+     */
+    @Test
+    public void testHashCode() throws Exception
+    {
+        assertTrue( nameInstanceA.hashCode() == nameInstanceB.hashCode() );
+        assertTrue( nameInstanceB.hashCode() == nameInstanceC.hashCode() );
+        assertTrue( nameInstanceA.hashCode() == nameInstanceC.hashCode() );
+    }
+}
\ No newline at end of file

Propchange: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/NameTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/NameTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message