Author: akarasulu Date: Sat Aug 28 00:35:05 2004 New Revision: 37158 Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ModifyRequestImplTest.java Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ModifyRequestImpl.java Log: Commit changes ...
o added equals override to factor in the name and modification items o added test case which passes right now Take a look at a major problem with the equals() implementation here in the JIRA: http://nagoya.apache.org/jira/browse/DIRSNICKERS-96 Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ModifyRequestImpl.java ============================================================================== --- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ModifyRequestImpl.java (original) +++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/message/ModifyRequestImpl.java Sat Aug 28 00:35:05 2004 @@ -1,25 +1,26 @@ -/* - * 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 java.util.ArrayList ; import java.util.Collection ; import java.util.Collections ; +import java.util.Iterator; import javax.naming.directory.ModificationItem ; @@ -27,17 +28,17 @@ /** * Lockable ModifyRequest implementation. * - * @author <a href="mailto:[EMAIL PROTECTED]"> - * Apache Directory Project</a> - * @version $Rev$ + * @author <a href="mailto:[EMAIL PROTECTED]"> + * Apache Directory Project</a> + * @version $Rev$ */ public class ModifyRequestImpl extends AbstractRequest implements ModifyRequest { /** Dn of the entry to modify or PDU's <b>object</b> field */ - private String m_name ; + private String name ; /** Sequence of modifications or PDU's <b>modification</b> seqence field */ - private Collection m_mods = new ArrayList() ; + private ArrayList mods = new ArrayList() ; // ------------------------------------------------------------------------ @@ -71,7 +72,7 @@ */ public Collection getModificationItems() { - return Collections.unmodifiableCollection( m_mods ) ; + return Collections.unmodifiableCollection( mods ) ; } @@ -83,7 +84,7 @@ */ public String getName() { - return m_name ; + return name ; } @@ -96,7 +97,7 @@ public void setName( String name ) { lockCheck( "Attempt to alter object name of locked ModifyRequest!" ) ; - m_name = name ; + this.name = name ; } @@ -109,7 +110,7 @@ public void addModification( ModificationItem mod ) { lockCheck( "Attempt to add modification to locked ModifyRequest!" ) ; - m_mods.add( mod ) ; + mods.add( mod ) ; } @@ -122,7 +123,7 @@ public void removeModification( ModificationItem mod ) { lockCheck( "Attempt to remove modification to locked ModifyRequest!" ) ; - m_mods.remove( mod ) ; + mods.remove( mod ) ; } @@ -140,5 +141,102 @@ public MessageTypeEnum getResponseType() { return RESP_TYPE ; + } + + + /** + * Checks to see if ModifyRequest stub equals another by factoring in checks + * for the name and modification items of the request. + * + * @param obj the object to compare this ModifyRequest to + * @return true if obj equals this ModifyRequest, false otherwise + */ + public boolean equals( Object obj ) + { + if ( obj == this ) + { + return true; + } + + if ( ! super.equals( obj ) ) + { + return false; + } + + ModifyRequest req = ( ModifyRequest ) 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 ( req.getModificationItems().size() != mods.size() ) + { + return false; + } + + Iterator list = req.getModificationItems().iterator(); + for ( int ii = 0; ii < mods.size(); ii++ ) + { + ModificationItem item = ( ModificationItem ) list.next(); + if ( ! equals( ( ModificationItem ) mods.get( ii ), item ) ) + { + return false; + } + } + + return true; + } + + + /** + * Checks to see if two ModificationItems are equal by factoring in the + * modification operation as well as the attribute of each item. + * + * @param item0 the first ModificationItem to compare + * @param item1 the second ModificationItem to compare + * @return true if the ModificationItems are equal, false otherwise + */ + private boolean equals( ModificationItem item0, ModificationItem item1 ) + { + if ( item0 == item1 ) + { + return true; + } + + if ( item0.getModificationOp() != item1.getModificationOp() ) + { + return false; + } + + // compare attribute id's at the very least + if ( ! item0.getAttribute().getID().equals( + item1.getAttribute().getID() ) ) + { + return false; + } + + // looks like we have another ordering issue - this time the order of + // attribute values does not match. Until this is resolved we'll + // comment it out - NOT A GOOD PRACTICE AT ALL!!!!! +// if ( ! item0.getAttribute().equals( item1.getAttribute() ) ) +// { +// return false; +// } + + return true; } } Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ModifyRequestImplTest.java ============================================================================== --- (empty file) +++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/message/ModifyRequestImplTest.java Sat Aug 28 00:35:05 2004 @@ -0,0 +1,346 @@ +/* + * 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 javax.naming.directory.DirContext; +import javax.naming.directory.ModificationItem; +import javax.naming.directory.Attributes; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.ArrayList; + +import org.apache.ldap.common.Lockable; +import org.apache.ldap.common.LockException; + + +/** + * Test case for the ModifyRequestImpl class. + * + * @author <a href="mailto:[EMAIL PROTECTED]"> Apache Directory + * Project</a> + * @version $Rev$ + */ +public class ModifyRequestImplTest extends TestCase +{ + /** + * Builds a ModifyRequest for testing purposes. + * + * @return the ModifyRequest to use for tests + */ + public ModifyRequestImpl getRequest() + { + // Construct the Modify request to test + ModifyRequestImpl req = new ModifyRequestImpl( 45 ); + req.setName( "cn=admin,dc=apache,dc=org" ); + + LockableAttributeImpl attr = new LockableAttributeImpl( "attr0" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + ModificationItem item = + new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req.addModification( item ); + + attr = new LockableAttributeImpl( "attr1" ); + attr.add( "val3" ); + item = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, attr ); + req.addModification( item ); + + attr = new LockableAttributeImpl( "attr2" ); + attr.add( "val4" ); + attr.add( "val5" ); + item = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, attr ); + req.addModification( item ); + + return req; + } + + + /** + * Tests the same object referrence for equality. + */ + public void testEqualsSameObj() + { + ModifyRequestImpl req = getRequest(); + assertTrue( req.equals( req ) ); + } + + + /** + * Tests for equality using exact copies. + */ + public void testEqualsExactCopy() + { + ModifyRequestImpl req0 = getRequest(); + ModifyRequestImpl req1 = getRequest(); + assertTrue( req0.equals( req1 ) ); + } + + + /** + * Test for inequality when only the IDs are different. + */ + public void testNotEqualDiffId() + { + ModifyRequestImpl req0 = new ModifyRequestImpl( 7 ); + ModifyRequestImpl req1 = new ModifyRequestImpl( 5 ); + assertFalse( req0.equals( req1 ) ); + } + + + /** + * Test for inequality when only the DN names are different. + */ + public void testNotEqualDiffName() + { + ModifyRequestImpl req0 = getRequest(); + req0.setName( "cn=admin,dc=example,dc=com" ); + ModifyRequestImpl req1 = getRequest(); + req1.setName( "cn=admin,dc=apache,dc=org" ); + + assertFalse( req0.equals( req1 ) ); + } + + + /** + * Test for inequality when only the mods ops are different. + */ + public void testNotEqualDiffModOps() + { + ModifyRequestImpl req0 = getRequest(); + LockableAttributeImpl attr = new LockableAttributeImpl( "attr3" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + ModificationItem item = + new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req0.addModification( item ); + + ModifyRequestImpl req1 = getRequest(); + attr = new LockableAttributeImpl( "attr3" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + item = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, attr ); + req0.addModification( item ); + + assertFalse( req0.equals( req1 ) ); + assertFalse( req1.equals( req0 ) ); + } + + + /** + * Test for inequality when only the number of mods are different. + */ + public void testNotEqualDiffModCount() + { + ModifyRequestImpl req0 = getRequest(); + LockableAttributeImpl attr = new LockableAttributeImpl( "attr3" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + ModificationItem item = + new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req0.addModification( item ); + + ModifyRequestImpl req1 = getRequest(); + + assertFalse( req0.equals( req1 ) ); + assertFalse( req1.equals( req0 ) ); + } + + + /** + * Test for inequality when only the mods attribute Id's are different. + */ + public void testNotEqualDiffModIds() + { + ModifyRequestImpl req0 = getRequest(); + LockableAttributeImpl attr = new LockableAttributeImpl( "attr3" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + ModificationItem item = + new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req0.addModification( item ); + + ModifyRequestImpl req1 = getRequest(); + attr = new LockableAttributeImpl( "attr4" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + item = new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req0.addModification( item ); + + assertFalse( req0.equals( req1 ) ); + assertFalse( req1.equals( req0 ) ); + } + + + /** + * Test for inequality when only the mods attribute values are different. + */ + public void testNotEqualDiffModValues() + { + ModifyRequestImpl req0 = getRequest(); + LockableAttributeImpl attr = new LockableAttributeImpl( "attr3" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + ModificationItem item = + new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req0.addModification( item ); + + ModifyRequestImpl req1 = getRequest(); + attr = new LockableAttributeImpl( "attr3" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + attr.add( "val3" ); + item = new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req0.addModification( item ); + + assertFalse( req0.equals( req1 ) ); + assertFalse( req1.equals( req0 ) ); + } + + + /** + * Tests for equality even when another BindRequest implementation is used. + */ + public void testEqualsDiffImpl() + { + ModifyRequest req0 = new ModifyRequest() + { + public Collection getModificationItems() + { + ArrayList list = new ArrayList(); + LockableAttributeImpl attr = new LockableAttributeImpl( "attr0" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + ModificationItem item = + new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + list.add( item ); + + attr = new LockableAttributeImpl( "attr1" ); + attr.add( "val3" ); + item = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, attr ); + list.add( item ); + + attr = new LockableAttributeImpl( "attr2" ); + attr.add( "val4" ); + attr.add( "val5" ); + item = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, attr ); + list.add( item ); + + return list; + } + + public void addModification( ModificationItem a_mod ) + { + } + + public void removeModification( ModificationItem a_mod ) + { + } + + public String getName() + { + return "cn=admin,dc=apache,dc=org"; + } + + public void setName( String a_name ) + { + } + + public MessageTypeEnum getResponseType() + { + return MessageTypeEnum.MODIFYRESPONSE; + } + + public boolean hasResponse() + { + return true; + } + + public MessageTypeEnum getType() + { + return MessageTypeEnum.MODIFYREQUEST; + } + + 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; + } + }; + + ModifyRequestImpl req1 = getRequest(); + assertTrue( req1.equals( req0 ) ); + } +}
