You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/08/28 06:02:28 UTC

svn commit: rev 37152 - in incubator/directory/ldap/trunk/common/src: java/org/apache/ldap/common/message test/org/apache/ldap/common/message

Author: akarasulu
Date: Fri Aug 27 21:02:27 2004
New Revision: 37152

Added:
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/SearchResponseReferenceImplTest.java
Modified:
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/SearchResponseReferenceImpl.java
Log:
Added equals() method override to take the referrals into account.  Added and 
passed unit test case for this class - namely for testing equals().


Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/SearchResponseReferenceImpl.java
==============================================================================
--- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/SearchResponseReferenceImpl.java	(original)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/SearchResponseReferenceImpl.java	Fri Aug 27 21:02:27 2004
@@ -75,4 +75,46 @@
             "Attempt to alter referrals of a locked SearchRequestReference!" ) ;
         m_referral = a_referral ;
     }
+
+
+    /**
+     * Checks to see if an object is equal to this SearchResponseReference stub.
+     *
+     * @param obj the object to compare to this response stub
+     * @return true if the objects are equivalent false otherwise
+     */
+    public boolean equals( Object obj )
+    {
+        if ( obj == this )
+        {
+            return true;
+        }
+
+        if ( ! super.equals( obj ) )
+        {
+            return false;
+        }
+
+        SearchResponseReference resp = ( SearchResponseReference ) obj;
+
+        if ( m_referral != null && resp.getReferral() == null )
+        {
+            return false;
+        }
+
+        if ( m_referral == null && resp.getReferral() != null )
+        {
+            return false;
+        }
+
+        if ( m_referral != null && resp.getReferral() != null )
+        {
+            if ( ! m_referral.equals( resp.getReferral() ) )
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
 }

Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/SearchResponseReferenceImplTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/SearchResponseReferenceImplTest.java	Fri Aug 27 21:02:27 2004
@@ -0,0 +1,185 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.ldap.common.message;
+
+
+import junit.framework.TestCase;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.ldap.common.LockException;
+import org.apache.ldap.common.Lockable;
+
+
+/**
+ * TestCase for the SearchResponseReferenceImpl class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a>
+ * @version $Rev$
+ */
+public class SearchResponseReferenceImplTest extends TestCase
+{
+    /**
+     * Creates a baseline referral to test with and adds it to the supplied
+     * response object.
+     *
+     * @param resp the parent lockable
+     * @return the newly created referral for testing
+     */
+    public Referral getReferral( SearchResponseReference resp )
+    {
+        ReferralImpl ref = new ReferralImpl( resp );
+        resp.setReferral( ref );
+        ref.addLdapUrl( "http://apache.org???" );
+        ref.addLdapUrl( "http://mit.edu???" );
+        ref.addLdapUrl( "http://abc.com???" );
+        return ref;
+    }
+
+    /**
+     * Tests for equality when the same object referrence is used.
+     */
+    public void testEqualsSameObject()
+    {
+        SearchResponseReferenceImpl resp = new SearchResponseReferenceImpl( 5 );
+        getReferral( resp );
+        assertTrue( "the same object should be equal", resp.equals( resp ) );
+    }
+
+
+    /**
+     * Tests for equality when an exact copy is compared.
+     */
+    public void testEqualsExactCopy()
+    {
+        SearchResponseReferenceImpl resp0 = new SearchResponseReferenceImpl( 5 );
+        getReferral( resp0 );
+        SearchResponseReferenceImpl resp1 = new SearchResponseReferenceImpl( 5 );
+        getReferral( resp1 );
+
+        assertTrue( "exact copies should be equal", resp0.equals( resp1 ) );
+        assertTrue( "exact copies should be equal", resp1.equals( resp0 ) );
+    }
+
+
+    /**
+     * Tests for equality when a different implementation is used.
+     */
+    public void testEqualsDiffImpl()
+    {
+        SearchResponseReference resp0 = new SearchResponseReference()
+        {
+            public Referral getReferral()
+            {
+                return SearchResponseReferenceImplTest.this.getReferral( this );
+            }
+
+            public void setReferral( Referral a_referral )
+            {
+            }
+
+            public MessageTypeEnum getType()
+            {
+                return MessageTypeEnum.SEARCHRESREF;
+            }
+
+            public Collection getControls()
+            {
+                return Collections.EMPTY_LIST;
+            }
+
+            public void add( Control control ) throws MessageException
+            {
+            }
+
+            public void remove( Control control ) throws MessageException
+            {
+            }
+
+            public int getMessageId()
+            {
+                return 5;
+            }
+
+            public Object get( Object key )
+            {
+                return null;
+            }
+
+            public Object put( Object key, Object value )
+            {
+                return null;
+            }
+
+            public Lockable getParent()
+            {
+                return null;
+            }
+
+            public boolean isLocked()
+            {
+                return false;
+            }
+
+            public boolean getLocked()
+            {
+                return false;
+            }
+
+            public void setLocked( boolean isLocked ) throws LockException
+            {
+            }
+
+            public boolean isUnlockable()
+            {
+                return false;
+            }
+        };
+
+        SearchResponseReferenceImpl resp1 =
+                new SearchResponseReferenceImpl( 5 );
+        getReferral( resp1 );
+
+        assertFalse( "using Object.equal() should NOT be equal",
+                resp0.equals( resp1 ) );
+        assertTrue( "same but different implementations should be equal",
+                resp1.equals( resp0 ) );
+    }
+
+
+    /**
+     * Tests for inequality when the urls are not the same.
+     */
+    public void testNotEqualDiffUrls()
+    {
+        SearchResponseReferenceImpl resp0 =
+                new SearchResponseReferenceImpl( 5 );
+        getReferral( resp0 );
+        SearchResponseReferenceImpl resp1 =
+                new SearchResponseReferenceImpl( 5 );
+        getReferral( resp1 );
+        resp1.getReferral().addLdapUrl( "ldap://asdf.com???" );
+
+        assertFalse( "different urls should not be equal",
+                resp1.equals( resp0 ) );
+        assertFalse( "different urls should not be equal",
+                resp0.equals( resp1 ) );
+    }
+
+}