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 18:39:57 UTC

svn commit: rev 37088 - 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 09:39:56 2004
New Revision: 37088

Added:
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ExtendedResponseImplTest.java
Modified:
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ExtendedResponseImpl.java
Log:
Commit changes ...

 o added equals() override to ExtendedResponseImpl to take the response and
   the responseName (LDAPOID) into account
 o created and passed test case 


Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ExtendedResponseImpl.java
==============================================================================
--- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ExtendedResponseImpl.java	(original)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ExtendedResponseImpl.java	Thu Aug 26 09:39:56 2004
@@ -1,36 +1,38 @@
-/*
- *   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.
- *
- */
+/*
+ *   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 org.apache.commons.lang.ArrayUtils;
+
+
 /**
  * Lockable ExtendedResponse implementation
  * 
- * @author <a href="mailto:directory-dev@incubator.apache.org">
- * Apache Directory Project</a>
+ * @author <a href="mailto:directory-dev@incubator.apache.org">
+ * Apache Directory Project</a>
  * @version $Rev$
  */
 public class ExtendedResponseImpl
     extends AbstractResultResponse implements ExtendedResponse
 {
     /** Object identifier for the extended response */
-    private String m_oid ;
+    private String oid ;
     /** Values encoded in the extended response payload */
-    private byte [] m_value ;
+    private byte [] value ;
 
 
     // ------------------------------------------------------------------------
@@ -61,7 +63,7 @@
      */
     public byte [] getResponse()
     {
-        return m_value ;
+        return value ;
     }
 
 
@@ -74,7 +76,7 @@
     {
         lockCheck(
             "Attempt to alter response value in locked ExtendedResponse!" ) ;
-        m_value = value ;
+        this.value = value ;
     }
 
 
@@ -86,7 +88,7 @@
      */
     public String getResponseName()
     {
-        return m_oid ;
+        return oid ;
     }
 
 
@@ -100,6 +102,66 @@
     {
         lockCheck(
             "Attempt to alter responseName in locked ExtendedResponse!" ) ;
-        m_oid = oid ;
+        this.oid = oid ;
+    }
+
+
+    /**
+     * Checks to see if an object equals this ExtendedRequest.
+     *
+     * @param obj the object to be checked for equality
+     * @return true if the obj equals this ExtendedRequest, false otherwise
+     */
+    public boolean equals( Object obj )
+    {
+        if ( obj == this )
+        {
+            return true;
+        }
+
+        if ( ! super.equals( obj ) )
+        {
+            return false;
+        }
+
+        ExtendedResponse resp = ( ExtendedResponse ) obj;
+
+        if ( oid != null && resp.getResponseName() == null )
+        {
+            return false;
+        }
+
+        if ( oid == null && resp.getResponseName() != null )
+        {
+            return false;
+        }
+
+        if ( oid != null && resp.getResponseName() != null )
+        {
+            if ( ! oid.equals( resp.getResponseName() ) )
+            {
+                return false;
+            }
+        }
+
+        if ( value != null && resp.getResponse() == null )
+        {
+            return false;
+        }
+
+        if ( value == null && resp.getResponse() != null )
+        {
+            return false;
+        }
+
+        if ( value != null && resp.getResponse() != null )
+        {
+            if ( ! ArrayUtils.isEquals( value, resp.getResponse() ) )
+            {
+                return false;
+            }
+        }
+
+        return true;
     }
 }

Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ExtendedResponseImplTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ExtendedResponseImplTest.java	Thu Aug 26 09:39:56 2004
@@ -0,0 +1,232 @@
+/*
+ *   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 ExtendedResponseImpl class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a>
+ * @version $Rev$
+ */
+public class ExtendedResponseImplTest extends TestCase
+{
+    /**
+     * Creates and populates a ExtendedResponseImpl stub for testing purposes.
+     *
+     * @return a populated ExtendedResponseImpl stub
+     */
+    private ExtendedResponseImpl createStub()
+    {
+        // Construct the Search response to test with results and referrals
+        ExtendedResponseImpl response = new ExtendedResponseImpl( 45 );
+        response.setResponse( "Hello World!".getBytes() );
+        response.setResponseName( "1.1.1.1" );
+        LdapResultImpl result = new LdapResultImpl( response );
+        response.setLdapResult( result );
+        result.setMatchedDn( "dc=example,dc=com" );
+        result.setResultCode( ResultCodeEnum.SUCCESS );
+        ReferralImpl refs = new ReferralImpl( result );
+        refs.addLdapUrl( "ldap://someserver.com" );
+        refs.addLdapUrl( "ldap://apache.org" );
+        refs.addLdapUrl( "ldap://another.net" );
+        result.setReferral( refs );
+        return response;
+    }
+
+
+    /**
+     * Tests for equality using the same object.
+     */
+    public void testEqualsSameObj()
+    {
+        ExtendedResponseImpl resp = createStub();
+        assertTrue( resp.equals( resp ) );
+    }
+
+
+    /**
+     * Tests for equality using an exact copy.
+     */
+    public void testEqualsExactCopy()
+    {
+        ExtendedResponseImpl resp0 = createStub();
+        ExtendedResponseImpl resp1 = createStub();
+        assertTrue( resp0.equals( resp1 ) );
+        assertTrue( resp1.equals( resp0 ) );
+    }
+
+
+    /**
+     * Tests for equality using different stub implementations.
+     */
+    public void testEqualsDiffImpl()
+    {
+        ExtendedResponseImpl resp0 = createStub();
+        ExtendedResponse resp1 = new ExtendedResponse()
+        {
+            public String getResponseName()
+            {
+                return "1.1.1.1";
+            }
+
+            public void setResponseName( String a_oid )
+            {
+            }
+
+            public byte[] getResponse()
+            {
+                return "Hello World!".getBytes();
+            }
+
+            public void setResponse( byte[] a_value )
+            {
+            }
+
+            public LdapResult getLdapResult()
+            {
+                LdapResultImpl result = new LdapResultImpl( this );
+                result.setMatchedDn( "dc=example,dc=com" );
+                result.setResultCode( ResultCodeEnum.SUCCESS );
+                ReferralImpl refs = new ReferralImpl( result );
+                refs.addLdapUrl( "ldap://someserver.com" );
+                refs.addLdapUrl( "ldap://apache.org" );
+                refs.addLdapUrl( "ldap://another.net" );
+                result.setReferral( refs );
+
+                return result;
+            }
+
+            public void setLdapResult( LdapResult a_result )
+            {
+            }
+
+            public MessageTypeEnum getType()
+            {
+                return MessageTypeEnum.EXTENDEDRESP;
+            }
+
+            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 45;
+            }
+
+            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;
+            }
+        };
+
+        assertTrue( resp0.equals( resp1 ) );
+        assertFalse( resp1.equals( resp0 ) );
+    }
+
+
+    /**
+     * Tests inequality when messageIds are different.
+     */
+    public void testNotEqualsDiffIds()
+    {
+        ExtendedResponseImpl resp0 = new ExtendedResponseImpl( 3 );
+        ExtendedResponseImpl resp1 = new ExtendedResponseImpl( 4 );
+
+        assertFalse( resp0.equals( resp1 ) );
+        assertFalse( resp1.equals( resp0 ) );
+    }
+
+
+    /**
+     * Tests inequality when responseNames are different.
+     */
+    public void testNotEqualsDiffNames()
+    {
+        ExtendedResponseImpl resp0 = createStub();
+        resp0.setResponseName( "1.2.3.4" );
+        ExtendedResponseImpl resp1 = createStub();
+        resp1.setResponseName( "1.2.3.4.5" );
+
+        assertFalse( resp0.equals( resp1 ) );
+        assertFalse( resp1.equals( resp0 ) );
+    }
+
+
+    /**
+     * Tests inequality when responses are different.
+     */
+    public void testNotEqualsDiffResponses()
+    {
+        ExtendedResponseImpl resp0 = createStub();
+        resp0.setResponse( "abc".getBytes() );
+        ExtendedResponseImpl resp1 = createStub();
+        resp1.setResponse( "123".getBytes() );
+
+        assertFalse( resp0.equals( resp1 ) );
+        assertFalse( resp1.equals( resp0 ) );
+    }
+}