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/26 14:15:17 UTC

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

Author: akarasulu
Date: Thu Aug 26 05:15:17 2004
New Revision: 37074

Added:
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/CompareRequestImplTest.java   (contents, props changed)
Modified:
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/CompareRequestImpl.java
Log:
added equals() override in CompareRequestImpl along with unit tests

Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/CompareRequestImpl.java
==============================================================================
--- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/CompareRequestImpl.java	(original)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/CompareRequestImpl.java	Thu Aug 26 05:15:17 2004
@@ -28,11 +28,11 @@
     extends AbstractRequest implements CompareRequest
 {
     /** Distinguished name identifying the compared entry */
-    private String m_name ;
+    private String name ;
     /** The id of the attribute used in the comparison */
-    private String m_attrId ;
+    private String attrId ;
     /** The value of the attribute used in the comparison */
-    private String m_attrVal ;
+    private String attrVal ;
 
 
     // ------------------------------------------------------------------------
@@ -65,7 +65,7 @@
      */
     public String getName()
     {
-        return m_name ;
+        return name ;
     }
 
 
@@ -78,7 +78,7 @@
     public void setName( String name )
     {
         lockCheck( "Attempt to alter entry name on locked CompareRequest!" ) ;
-        m_name = name ;
+        this.name = name ;
     }
 
 
@@ -89,7 +89,7 @@
      */
     public String getAssertionValue()
     {
-        return m_attrVal ;
+        return attrVal ;
     }
 
 
@@ -102,7 +102,7 @@
     {
         super.lockCheck(
             "Attempt to alter attribute value of locked CompareRequest" ) ;
-        m_attrVal = attrVal ;
+        this.attrVal = attrVal ;
     }
 
 
@@ -113,7 +113,7 @@
      */
     public String getAttributeId()
     {
-        return m_attrId ;
+        return attrId ;
     }
 
 
@@ -126,7 +126,7 @@
     {
         super.lockCheck(
             "Attempt to alter attribute id of locked CompareRequest" ) ;
-        m_attrId = attrId ;
+        this.attrId = attrId ;
     }
 
 
@@ -144,5 +144,83 @@
     public MessageTypeEnum getResponseType()
     {
         return RESP_TYPE ;
+    }
+
+
+    /**
+     * Checks to see if an object is equivalent to this CompareRequest.
+     *
+     * @param obj the obj to compare with this CompareRequest
+     * @return true if the obj is equal to this request, false otherwise
+     */
+    public boolean equals( Object obj )
+    {
+        if ( obj == this )
+        {
+            return true;
+        }
+
+        if ( ! super.equals( obj ) )
+        {
+            return false;
+        }
+
+        CompareRequest req = ( CompareRequest ) obj;
+
+        if ( name != null && req.getName() == null )
+        {
+            return false;
+        }
+
+        if ( name == null && req.getName() != null )
+        {
+            return false;
+        }
+
+        if ( name != null && req.getName() != null )
+        {
+            if ( ! name.equals( req.getName() ) )
+            {
+                return false;
+            }
+        }
+
+        if ( attrId != null && req.getAttributeId() == null )
+        {
+            return false;
+        }
+
+        if ( attrId == null && req.getAttributeId() != null )
+        {
+            return false;
+        }
+
+        if ( attrId != null && req.getAttributeId() != null )
+        {
+            if ( ! attrId.equals( req.getAttributeId() ) )
+            {
+                return false;
+            }
+        }
+
+        if ( attrVal != null && req.getAssertionValue() == null )
+        {
+            return false;
+        }
+
+        if ( attrVal == null && req.getAssertionValue() != null )
+        {
+            return false;
+        }
+
+        if ( attrVal != null && req.getAssertionValue() != null )
+        {
+            if ( ! attrVal.equals( req.getAssertionValue() ) )
+            {
+                return false;
+            }
+        }
+
+        return true;
     }
 }

Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/CompareRequestImplTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/CompareRequestImplTest.java	Thu Aug 26 05:15:17 2004
@@ -0,0 +1,245 @@
+/*
+ *   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.Lockable;
+import org.apache.ldap.common.LockException;
+
+
+/**
+ * TestCase for the CompareRequestImpl class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class CompareRequestImplTest extends TestCase
+{
+    /**
+     * Tests the same object referrence for equality.
+     */
+    public void testEqualsSameObj()
+    {
+        CompareRequestImpl req = new CompareRequestImpl( 5 );
+        assertTrue( req.equals( req ) );
+    }
+
+
+    /**
+     * Tests for equality using exact copies.
+     */
+    public void testEqualsExactCopy()
+    {
+        CompareRequestImpl req0 = new CompareRequestImpl( 5 );
+        req0.setName( "cn=admin,dc=example,dc=com" );
+        req0.setAttributeId( "objectClass" );
+        req0.setAssertionValue( "top" );
+
+        CompareRequestImpl req1 = new CompareRequestImpl( 5 );
+        req1.setName( "cn=admin,dc=example,dc=com" );
+        req1.setAttributeId( "objectClass" );
+        req1.setAssertionValue( "top" );
+
+        assertTrue( req0.equals( req1 ) );
+        assertTrue( req1.equals( req0 ) );
+    }
+
+
+    /**
+     * Test for inequality when only the IDs are different.
+     */
+    public void testNotEqualDiffId()
+    {
+        CompareRequestImpl req0 = new CompareRequestImpl( 7 );
+        req0.setName( "cn=admin,dc=example,dc=com" );
+
+        CompareRequestImpl req1 = new CompareRequestImpl( 5 );
+        req1.setName( "cn=admin,dc=example,dc=com" );
+
+        assertFalse( req0.equals( req1 ) );
+        assertFalse( req1.equals( req0 ) );
+    }
+
+
+    /**
+     * Test for inequality when only the attributeIds are different.
+     */
+    public void testNotEqualDiffAttributeIds()
+    {
+        CompareRequestImpl req0 = new CompareRequestImpl( 5 );
+        req0.setName( "cn=admin,dc=apache,dc=org" );
+        req0.setAttributeId( "dc" );
+        req0.setAssertionValue( "apache.org" );
+
+        CompareRequestImpl req1 = new CompareRequestImpl( 5 );
+        req1.setName( "cn=admin,dc=apache,dc=org" );
+        req1.setAttributeId( "nisDomain" );
+        req1.setAssertionValue( "apache.org" );
+
+        assertFalse( req0.equals( req1 ) );
+        assertFalse( req1.equals( req0 ) );
+    }
+
+
+    /**
+     * Test for inequality when only the Assertion values are different.
+     */
+    public void testNotEqualDiffValue()
+    {
+        CompareRequestImpl req0 = new CompareRequestImpl( 5 );
+        req0.setName( "cn=admin,dc=apache,dc=org" );
+        req0.setAttributeId( "dc" );
+        req0.setAssertionValue( "apache.org" );
+
+        CompareRequestImpl req1 = new CompareRequestImpl( 5 );
+        req1.setName( "cn=admin,dc=apache,dc=org" );
+        req1.setAttributeId( "dc" );
+        req1.setAssertionValue( "nagoya.apache.org" );
+
+        assertFalse( req0.equals( req1 ) );
+        assertFalse( req1.equals( req0 ) );
+    }
+
+
+    /**
+     * Tests for equality even when another CompareRequest implementation is used.
+     */
+    public void testEqualsDiffImpl()
+    {
+        CompareRequest req0 = new CompareRequest()
+        {
+            public String getAssertionValue()
+            {
+                return null;
+            }
+
+            public void setAssertionValue( String a_value )
+            {
+
+            }
+
+            public String getAttributeId()
+            {
+                return null;
+            }
+
+            public void setAttributeId( String a_attrId )
+            {
+
+            }
+
+            public String getName()
+            {
+                return null;
+            }
+
+            public void setName( String a_name )
+            {
+            }
+
+            public boolean isVersion3()
+            {
+                return true;
+            }
+
+            public boolean getVersion3()
+            {
+                return true;
+            }
+
+            public void setVersion3( boolean a_isVersion3 )
+            {
+            }
+
+            public MessageTypeEnum getResponseType()
+            {
+                return MessageTypeEnum.COMPARERESPONSE;
+            }
+
+            public boolean hasResponse()
+            {
+                return true;
+            }
+
+            public MessageTypeEnum getType()
+            {
+                return MessageTypeEnum.COMPAREREQUEST;
+            }
+
+            public Collection getControls()
+            {
+                return Collections.EMPTY_LIST;
+            }
+
+            public void add( Control a_control ) throws MessageException
+            {
+            }
+
+            public void remove( Control a_control ) throws MessageException
+            {
+            }
+
+            public int getMessageId()
+            {
+                return 5;
+            }
+
+            public Object get( Object a_key )
+            {
+                return null;
+            }
+
+            public Object put( Object a_key, Object a_value )
+            {
+                return null;
+            }
+
+            public Lockable getParent()
+            {
+                return null;
+            }
+
+            public boolean isLocked()
+            {
+                return false;
+            }
+
+            public boolean getLocked()
+            {
+                return false;
+            }
+
+            public void setLocked( boolean a_isLocked ) throws LockException
+            {
+            }
+
+            public boolean isUnlockable()
+            {
+                return false;
+            }
+        };
+
+        CompareRequestImpl req1 = new CompareRequestImpl( 5 );
+        assertTrue( req1.equals( req0 ) );
+        assertFalse( req0.equals( req1 ) );
+    }
+}