directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fel...@apache.org
Subject svn commit: r946688 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/aci/UserClass.java test/java/org/apache/directory/shared/ldap/aci/UserClass_SubtreeTest.java
Date Thu, 20 May 2010 16:21:48 GMT
Author: felixk
Date: Thu May 20 16:21:48 2010
New Revision: 946688

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

Some tests are ignored because I'm not sure if the equals method really does what is wanted:
A Collection is compared by collection1.equals(collection2) which is "false" in almost every
case as a reference comparision is done [1].
I'm not sure if this is really wanted. If so, the tests annotated with @ignore can be removed.
Otherwise a fix for the related equals method is needed.

[1] http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#equals%28java.lang.Object%29

Added:
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/UserClass_SubtreeTest.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=946688&r1=946687&r2=946688&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
Thu May 20 16:21:48 2010
@@ -39,6 +39,8 @@ import org.apache.directory.shared.ldap.
  */
 public abstract class UserClass implements Serializable
 {
+    private static final long serialVersionUID = -123919984184219893L;
+
     /**
      * Every directory user (with possible requirements for
      * authenticationLevel).
@@ -131,6 +133,7 @@ public abstract class UserClass implemen
      */
     private static abstract class NamedUserClass extends UserClass
     {
+        private static final long serialVersionUID = 8571875984468893621L;
         protected final Set<DN> names;
 
 
@@ -321,6 +324,23 @@ public abstract class UserClass implemen
         }
 
 
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public int hashCode()
+        {
+            int hash = 37;
+            hash = hash * 17 + subtreeSpecifications.hashCode();
+
+            return hash;
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
         public boolean equals( Object o )
         {
             if ( this == o )

Added: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/UserClass_SubtreeTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/UserClass_SubtreeTest.java?rev=946688&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/UserClass_SubtreeTest.java
(added)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/aci/UserClass_SubtreeTest.java
Thu May 20 16:21:48 2010
@@ -0,0 +1,146 @@
+/*
+ *  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.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.directory.shared.ldap.aci.UserClass.Subtree;
+import org.apache.directory.shared.ldap.subtree.BaseSubtreeSpecification;
+import org.apache.directory.shared.ldap.subtree.SubtreeSpecification;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+
+/**
+ * Unit tests class UserClass.Subtree.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class UserClass_SubtreeTest
+{
+    Subtree subtreeA;
+    Subtree subtreeACopy;
+    Subtree subtreeB;
+    Subtree subtreeC;
+
+
+    /**
+     * Initialize name instances
+     */
+    @Before
+    public void initNames() throws Exception
+    {
+        SubtreeSpecification subtreeSpecA = new BaseSubtreeSpecification();
+        SubtreeSpecification subtreeSpecB = new BaseSubtreeSpecification( );
+        SubtreeSpecification subtreeSpecC = new BaseSubtreeSpecification( );
+
+        Collection<SubtreeSpecification> colA = new ArrayList<SubtreeSpecification>();
+        colA.add( subtreeSpecA );
+        colA.add( subtreeSpecB );
+        colA.add( subtreeSpecC );
+        Collection<SubtreeSpecification> colB = new ArrayList<SubtreeSpecification>();
+        colB.add( subtreeSpecA );
+        colB.add( subtreeSpecB );
+        colB.add( subtreeSpecC );
+        Collection<SubtreeSpecification> colC = new ArrayList<SubtreeSpecification>();
+        colC.add( subtreeSpecB );
+        colC.add( subtreeSpecC );
+        colC.add( subtreeSpecA );
+
+        subtreeA = new Subtree( colA );
+        subtreeACopy = new Subtree( colA );
+        subtreeB = new Subtree( colB );
+        subtreeC = new Subtree( colC );
+    }
+
+
+    @Test
+    public void testEqualsNull() throws Exception
+    {
+        assertFalse( subtreeA.equals( null ) );
+    }
+
+
+    @Test
+    public void testEqualsReflexive() throws Exception
+    {
+        assertEquals( subtreeA, subtreeA );
+    }
+
+
+    @Test
+    public void testHashCodeReflexive() throws Exception
+    {
+        assertEquals( subtreeA.hashCode(), subtreeA.hashCode() );
+    }
+
+
+    @Test
+    @Ignore
+    public void testEqualsSymmetric() throws Exception
+    {
+        assertEquals( subtreeA, subtreeACopy );
+        assertEquals( subtreeACopy, subtreeA );
+    }
+
+
+    @Test
+    @Ignore
+    public void testHashCodeSymmetric() throws Exception
+    {
+        assertEquals( subtreeA.hashCode(), subtreeACopy.hashCode() );
+        assertEquals( subtreeACopy.hashCode(), subtreeA.hashCode() );
+    }
+
+
+    @Test
+    @Ignore
+    public void testEqualsTransitive() throws Exception
+    {
+        assertEquals( subtreeA, subtreeACopy );
+        assertEquals( subtreeACopy, subtreeB );
+        assertEquals( subtreeA, subtreeB );
+    }
+
+
+    @Test
+    @Ignore
+    public void testHashCodeTransitive() throws Exception
+    {
+        assertEquals( subtreeA.hashCode(), subtreeACopy.hashCode() );
+        assertEquals( subtreeACopy.hashCode(), subtreeB.hashCode() );
+        assertEquals( subtreeA.hashCode(), subtreeB.hashCode() );
+    }
+
+
+    @Test
+    public void testNotEqualDiffValue() throws Exception
+    {
+        assertFalse( subtreeA.equals( subtreeC ) );
+        assertFalse( subtreeC.equals( subtreeA ) );
+    }
+}

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

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



Mime
View raw message