directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fel...@apache.org
Subject svn commit: r946731 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/schema/ObjectClass.java test/java/org/apache/directory/shared/ldap/schema/ObjectClassTest.java
Date Thu, 20 May 2010 18:02:46 GMT
Author: felixk
Date: Thu May 20 18:02:45 2010
New Revision: 946731

URL: http://svn.apache.org/viewvc?rev=946731&view=rev
Log:
- Override hashCode when overriding equals
- Adding simple testcases

Added:
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/ObjectClassTest.java
  (with props)
Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/ObjectClass.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/ObjectClass.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/ObjectClass.java?rev=946731&r1=946730&r2=946731&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/ObjectClass.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/ObjectClass.java
Thu May 20 18:02:45 2010
@@ -903,8 +903,49 @@ public class ObjectClass extends Abstrac
 
 
     /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+        int hash = 37;
+
+        hash = hash * 17 + super.hashCode();
+        hash = hash * 17 + objectClassType.hashCode();
+        hash = hash * 17 + superiorOids.size();
+        // Order doesn't matters, thus just add the hashCode
+        for ( String oid : superiorOids )
+        {
+            hash = hash + oid.hashCode();
+        }
+        hash = hash * 17 + superiors.size();
+        // Order doesn't matters, thus just add the hashCode
+        for ( ObjectClass oid : superiors )
+        {
+            hash = hash + oid.hashCode();
+        }
+        hash = hash * 17 + mayAttributeTypeOids.size();
+        // Order doesn't matters, thus just add the hashCode
+        for ( String oid : mayAttributeTypeOids )
+        {
+            hash = hash + oid.hashCode();
+        }
+        hash = hash * 17 + mayAttributeTypes.size();
+        // Order doesn't matters, thus just add the hashCode
+        for ( AttributeType oid : mayAttributeTypes )
+        {
+            hash = hash + oid.hashCode();
+        }
+        hash = hash * 17 + superiorOids.hashCode();
+
+        return hash;
+    }
+
+
+    /**
      * @see Object#equals(Object)
      */
+    @Override
     public boolean equals( Object o )
     {
         if ( !super.equals( o ) )

Added: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/ObjectClassTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/ObjectClassTest.java?rev=946731&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/ObjectClassTest.java
(added)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/ObjectClassTest.java
Thu May 20 18:02:45 2010
@@ -0,0 +1,119 @@
+/*
+ *  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.schema;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ * Unit tests class ObjectClass.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ObjectClassTest
+{
+    ObjectClass objectClassA;
+    ObjectClass objectClassACopy;
+    ObjectClass objectClassB;
+    ObjectClass objectClassC;
+
+
+    /**
+     * Initialize name instances
+     */
+    @Before
+    public void initNames() throws Exception
+    {
+        // TODO Create ObjectClasses with more meaningful constructor arguments
+        objectClassA = new ObjectClass( "aa" );
+        objectClassACopy = new ObjectClass( "aa" );
+        objectClassB = new ObjectClass( "aa" );
+        objectClassC = new ObjectClass( "cc" );
+    }
+
+
+    @Test
+    public void testEqualsNull() throws Exception
+    {
+        assertFalse( objectClassA.equals( null ) );
+    }
+
+
+    @Test
+    public void testEqualsReflexive() throws Exception
+    {
+        assertEquals( objectClassA, objectClassA );
+    }
+
+
+    @Test
+    public void testHashCodeReflexive() throws Exception
+    {
+        assertEquals( objectClassA.hashCode(), objectClassA.hashCode() );
+    }
+
+
+    @Test
+    public void testEqualsSymmetric() throws Exception
+    {
+        assertEquals( objectClassA, objectClassACopy );
+        assertEquals( objectClassACopy, objectClassA );
+    }
+
+
+    @Test
+    public void testHashCodeSymmetric() throws Exception
+    {
+        assertEquals( objectClassA.hashCode(), objectClassACopy.hashCode() );
+        assertEquals( objectClassACopy.hashCode(), objectClassA.hashCode() );
+    }
+
+
+    @Test
+    public void testEqualsTransitive() throws Exception
+    {
+        assertEquals( objectClassA, objectClassACopy );
+        assertEquals( objectClassACopy, objectClassB );
+        assertEquals( objectClassA, objectClassB );
+    }
+
+
+    @Test
+    public void testHashCodeTransitive() throws Exception
+    {
+        assertEquals( objectClassA.hashCode(), objectClassACopy.hashCode() );
+        assertEquals( objectClassACopy.hashCode(), objectClassB.hashCode() );
+        assertEquals( objectClassA.hashCode(), objectClassB.hashCode() );
+    }
+
+
+    @Test
+    public void testNotEqualDiffValue() throws Exception
+    {
+        assertFalse( objectClassA.equals( objectClassC ) );
+        assertFalse( objectClassC.equals( objectClassA ) );
+    }
+}

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

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



Mime
View raw message