directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r593845 - in /directory/shared/branches/bigbang/ldap/src: main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
Date Sun, 11 Nov 2007 04:48:18 GMT
Author: akarasulu
Date: Sat Nov 10 20:48:17 2007
New Revision: 593845

URL: http://svn.apache.org/viewvc?rev=593845&view=rev
Log:
changes to reverse LDIF generation ...

 o removed the use of request objects in various reverseXXX methods
 o fixed tests to accomodate the changes
 o fixed what looked like a bug with hardcoding of deleteOldRdn property
   in LdifUtils.reverseModifyDN() 


Modified:
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
    directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java?rev=593845&r1=593844&r2=593845&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
(original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
Sat Nov 10 20:48:17 2007
@@ -19,9 +19,12 @@
  */
 package org.apache.directory.shared.ldap.ldif;
 
-import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
-import java.util.List;
+import org.apache.directory.shared.ldap.message.AttributeImpl;
+import org.apache.directory.shared.ldap.message.ModificationItemImpl;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.apache.directory.shared.ldap.name.Rdn;
+import org.apache.directory.shared.ldap.util.Base64;
+import org.apache.directory.shared.ldap.util.StringTools;
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
@@ -29,15 +32,9 @@
 import javax.naming.directory.Attributes;
 import javax.naming.directory.DirContext;
 import javax.naming.directory.ModificationItem;
-
-import org.apache.directory.shared.ldap.message.AddRequest;
-import org.apache.directory.shared.ldap.message.AttributeImpl;
-import org.apache.directory.shared.ldap.message.ModificationItemImpl;
-import org.apache.directory.shared.ldap.message.ModifyDnRequest;
-import org.apache.directory.shared.ldap.message.ModifyRequest;
-import org.apache.directory.shared.ldap.name.LdapDN;
-import org.apache.directory.shared.ldap.util.Base64;
-import org.apache.directory.shared.ldap.util.StringTools;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
 
 
 /**
@@ -136,7 +133,6 @@
     /**
      * Convert an Attributes as LDIF
      * @param attrs the Attributes to convert
-     * @param length the expectend line length
      * @return the corresponding LDIF code as a String
      * @throws NamingException If a naming exception is encountered.
      */
@@ -457,16 +453,15 @@
      * Compute a reverse LDIF of an AddRequest. It's simply a delete request
      * of the added entry
      *
-     * @param addRequest The added entry
-     * @return A reverse LDIF
+     * @param dn the dn of the added entry
+     * @return a reverse LDIF
      * @throws NamingException If something went wrong
      */
-    public static Entry reverseAdd( AddRequest addRequest) throws NamingException
+    public static Entry reverseAdd( LdapDN dn ) throws NamingException
     {
         Entry entry = new Entry();
         entry.setChangeType( ChangeType.Delete );
-        entry.setDn( addRequest.getEntry().getUpName() );
-
+        entry.setDn( dn.getUpName() );
         return entry;
     }
 
@@ -505,36 +500,37 @@
      *  - a new superior, no deleteOldRdn 
      *  - a new superior, deleteOldRdn set to true
      *
-     * @param modifyDn The modifyRequest
+     * @param newSuperiorDn the new superior dn if this is a move, otherwise null
+     * @param modifiedDn the dn of the entry being modified
+     * @param newRdn the new rdn to use
+     * @param deleteOldRdn true if deleting old rdn, false if leaving it
      * @return A reverse LDIF
      * @throws NamingException If something went wrong
      */
-    public static Entry reverseModifyDN( ModifyDnRequest modifyDn ) throws NamingException
+    public static Entry reverseModifyDN( LdapDN newSuperiorDn, LdapDN modifiedDn, Rdn newRdn,
boolean deleteOldRdn )
+            throws NamingException
     {
         Entry entry = new Entry();
-        
         LdapDN newDN = null;
-        
-        if ( modifyDn.getNewSuperior() != null )
+
+        if ( newSuperiorDn != null )
         {
-            newDN = modifyDn.getNewSuperior();
-            String newSuperior = ( (LdapDN)modifyDn.getName().getPrefix( modifyDn.getName().size()
- 1 ) ).getUpName();
+            newDN = newSuperiorDn;
+            String newSuperior = ( ( LdapDN ) modifiedDn.getPrefix( modifiedDn.size() - 1
) ).getUpName();
             String trimmedSuperior = StringTools.trim( newSuperior );
             entry.setNewSuperior( trimmedSuperior );
         }
         else
         {
-            newDN = (LdapDN)modifyDn.getName().getPrefix( modifyDn.getName().size() - 1 );
+            newDN = ( LdapDN ) modifiedDn.getPrefix( modifiedDn.size() - 1 );
         }
         
-        newDN.add( modifyDn.getNewRdn() );
+        newDN.add( newRdn );
         
         entry.setDn( newDN.getUpName() );
         entry.setChangeType( ChangeType.ModDn );
-        entry.setDeleteOldRdn( true );
-        
-        
-        entry.setNewRdn( modifyDn.getName().getRdn().getUpName() );
+        entry.setDeleteOldRdn( deleteOldRdn );
+        entry.setNewRdn( modifiedDn.getRdn().getUpName() );
         
         return entry;
     }
@@ -552,24 +548,26 @@
      * the initials modifications {A, B, C}, the reversed modifications will
      * be ordered like {C, B, A}), we will change the modifications order. 
      *
-     * @param modifyRequest The modify request
+     * @param dn the dn of the modified entry
+     * @param forwardModifications the modification items for the forward change
      * @param modifiedEntry The modified entry. Necessary for the destructive modifications
      * @return A reversed LDIF 
      * @throws NamingException If something went wrong
      */
-    public static Entry reverseModify( ModifyRequest modifyRequest, Attributes modifiedEntry
) throws NamingException
+    public static Entry reverseModify( LdapDN dn, List<ModificationItemImpl> forwardModifications,
+                                       Attributes modifiedEntry ) throws NamingException
     {
         Entry entry = new Entry();
         entry.setChangeType( ChangeType.Modify );
         
-        entry.setDn( modifyRequest.getName().getUpName() );
+        entry.setDn( dn.getUpName() );
         
         // As the reversed modifications should be pushed in reversed order,
         // we create a list to temporarily store the modifications.
         List<ModificationItemImpl> reverseModifications = new ArrayList<ModificationItemImpl>();
         
         // Loop through all the modifications
-        for ( ModificationItem modification:modifyRequest.getModificationItems() )
+        for ( ModificationItem modification : forwardModifications )
         {
             switch ( modification.getModificationOp() )
             {

Modified: directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java?rev=593845&r1=593844&r2=593845&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
(original)
+++ directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
Sat Nov 10 20:48:17 2007
@@ -19,35 +19,16 @@
  */
 package org.apache.directory.shared.ldap.ldif;
 
-import java.util.List;
-
-import javax.naming.NamingException;
-import javax.naming.directory.Attribute;
-import javax.naming.directory.Attributes;
-import javax.naming.directory.BasicAttribute;
-import javax.naming.directory.BasicAttributes;
-import javax.naming.directory.DirContext;
-import javax.naming.directory.ModificationItem;
-
-import org.apache.directory.shared.ldap.message.AddRequest;
-import org.apache.directory.shared.ldap.message.AddRequestImpl;
-import org.apache.directory.shared.ldap.message.AttributeImpl;
-import org.apache.directory.shared.ldap.message.AttributesImpl;
-import org.apache.directory.shared.ldap.message.ModificationItemImpl;
-import org.apache.directory.shared.ldap.message.ModifyDnRequest;
-import org.apache.directory.shared.ldap.message.ModifyDnRequestImpl;
-import org.apache.directory.shared.ldap.message.ModifyRequest;
-import org.apache.directory.shared.ldap.message.ModifyRequestImpl;
+import org.apache.directory.shared.ldap.message.*;
 import org.apache.directory.shared.ldap.name.LdapDN;
 import org.apache.directory.shared.ldap.name.Rdn;
+import static org.junit.Assert.*;
 import org.junit.Test;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import javax.naming.NamingException;
+import javax.naming.directory.*;
+import java.util.Collections;
+import java.util.List;
 
 
 /**
@@ -379,16 +360,8 @@
     @Test
     public void testReverseAdd() throws NamingException
     {
-        AddRequest addRequest = new AddRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "dc=apache, dc=com" );
-        
-        addRequest.setEntry( dn );
-        
-        Attributes attributes = new AttributesImpl( "dc", "apache" );
-        addRequest.setAttributes( attributes );
-        
-        Entry reversed = LdifUtils.reverseAdd( addRequest );
+        Entry reversed = LdifUtils.reverseAdd( dn );
         
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
@@ -405,17 +378,8 @@
     @Test
     public void testReverseAddBase64DN() throws NamingException
     {
-        AddRequest addRequest = new AddRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "dc=Emmanuel L\u00c9charny" );
-        
-        addRequest.setEntry( dn );
-        
-        Attributes attributes = new AttributesImpl( "dc", "test" );
-        addRequest.setAttributes( attributes );
-        
-        Entry reversed = LdifUtils.reverseAdd( addRequest );
-        
+        Entry reversed = LdifUtils.reverseAdd( dn );
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
         assertEquals( ChangeType.Delete, reversed.getChangeType() );
@@ -462,15 +426,8 @@
     public void testReverseModifyDNNoDeleteOldRdnNoSuperior() throws NamingException
     {
         LdapDN dnModified = new LdapDN( "cn=joe, dc=example, dc=com" );
-        
-        ModifyDnRequest modifyDn = new ModifyDnRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, dc=example, dc=com" );
-
-        modifyDn.setName( dn );
-        modifyDn.setNewRdn( new Rdn( "cn=joe" ) );
-        
-        Entry reversed = LdifUtils.reverseModifyDN( modifyDn );
+        Entry reversed = LdifUtils.reverseModifyDN( null, dn, new Rdn( "cn=joe" ), true );
 
         assertNotNull( reversed );
         assertEquals( dnModified.getUpName(), reversed.getDn() );
@@ -489,16 +446,9 @@
     public void testReverseModifyDNDeleteOldRdnNoSuperior() throws NamingException
     {
         LdapDN dnModified = new LdapDN( "cn=joe, dc=example, dc=com" );
-        
-        ModifyDnRequest modifyDn = new ModifyDnRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, dc=example, dc=com" );
 
-        modifyDn.setName( dn );
-        modifyDn.setNewRdn( new Rdn( "cn=joe" ) );
-        modifyDn.setDeleteOldRdn( true );
-        
-        Entry reversed = LdifUtils.reverseModifyDN( modifyDn );
+        Entry reversed = LdifUtils.reverseModifyDN( null, dn, new Rdn( "cn=joe" ), true );
 
         assertNotNull( reversed );
         assertEquals( dnModified.getUpName(), reversed.getDn() );
@@ -517,17 +467,9 @@
     public void testReverseModifyDNNoDeleteOldRdnSuperior() throws NamingException
     {
         LdapDN dnModified = new LdapDN( "cn=joe,ou=system" );
-        
-        ModifyDnRequest modifyDn = new ModifyDnRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, dc=example, dc=com" );
 
-        modifyDn.setName( dn );
-        modifyDn.setNewRdn( new Rdn( "cn=joe" ) );
-        modifyDn.setNewSuperior( new LdapDN( "ou=system" ) );
-        modifyDn.setDeleteOldRdn( false );
-        
-        Entry reversed = LdifUtils.reverseModifyDN( modifyDn );
+        Entry reversed = LdifUtils.reverseModifyDN( new LdapDN( "ou=system" ), dn, new Rdn(
"cn=joe" ), true );
 
         assertNotNull( reversed );
         assertEquals( dnModified.getUpName(), reversed.getDn() );
@@ -547,17 +489,9 @@
     public void testReverseModifyDNDeleteOldRdnSuperior() throws NamingException
     {
         LdapDN dnModified = new LdapDN( "cn=joe,ou=system" );
-        
-        ModifyDnRequest modifyDn = new ModifyDnRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, dc=example, dc=com" );
 
-        modifyDn.setName( dn );
-        modifyDn.setNewRdn( new Rdn( "cn=joe" ) );
-        modifyDn.setNewSuperior( new LdapDN( "ou=system" ) );
-        modifyDn.setDeleteOldRdn( true );
-        
-        Entry reversed = LdifUtils.reverseModifyDN( modifyDn );
+        Entry reversed = LdifUtils.reverseModifyDN( new LdapDN( "ou=system" ), dn, new Rdn(
"cn=joe" ), true );
 
         assertNotNull( reversed );
         assertEquals( dnModified.getUpName(), reversed.getDn() );
@@ -584,17 +518,13 @@
         ou.add( "acme corp" );
         modifiedEntry.put( ou );
         
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl(
             DirContext.ADD_ATTRIBUTE, 
             new AttributeImpl( "ou", "BigCompany inc." ) );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
 
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
@@ -625,17 +555,13 @@
     {
         Attributes modifiedEntry = buildEntry();
         
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl(
             DirContext.ADD_ATTRIBUTE, 
             new AttributeImpl( "ou", "BigCompany inc." ) );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
 
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
@@ -666,17 +592,13 @@
     {
         Attributes modifiedEntry = buildEntry();
         
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl(
             DirContext.ADD_ATTRIBUTE, 
             new AttributeImpl( "cn", "test" ) );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
 
         assertNull( reversed );
     }
@@ -695,19 +617,14 @@
         ou.add( "acme corp" );
         modifiedEntry.put( ou );
 
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
 
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl( 
             DirContext.REMOVE_ATTRIBUTE, 
             new AttributeImpl( "ou", "acme corp" ) );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
-
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
 
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
@@ -745,18 +662,15 @@
         ou.add( "acme corp" );
         modifiedEntry.put( ou );
 
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
 
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl(
             DirContext.REMOVE_ATTRIBUTE, 
             new AttributeImpl( "ou" ) );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
+
 
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
@@ -794,17 +708,14 @@
         ou.add( "acme corp" );
         modifiedEntry.put( ou );
         
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
         
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl(
             DirContext.REMOVE_ATTRIBUTE, ou );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn, 
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
+
 
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
@@ -844,19 +755,16 @@
 
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
 
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         Attribute ouModified = new AttributeImpl( "ou" );
         ouModified.add( "directory" );
         ouModified.add( "BigCompany inc." );
         
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl(
             DirContext.REPLACE_ATTRIBUTE, ouModified );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
+
 
 
         assertNotNull( reversed );
@@ -888,8 +796,6 @@
     {
         Attributes modifiedEntry = buildEntry();
         
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
         
         Attribute newOu = new AttributeImpl( "ou" );
@@ -897,13 +803,11 @@
         newOu.add( "acme corp" );
 
         
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl(
             DirContext.REPLACE_ATTRIBUTE, newOu );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
 
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );
@@ -941,17 +845,13 @@
         ou.add( "acme corp" );
         modifiedEntry.put( ou );
         
-        ModifyRequest modify = new ModifyRequestImpl( 1 );
-        
         LdapDN dn = new LdapDN( "cn=test, ou=system" );
         
-        ModificationItem mod = new ModificationItemImpl( 
+        ModificationItemImpl mod = new ModificationItemImpl( 
             DirContext.REPLACE_ATTRIBUTE, new AttributeImpl( "ou" ) );
 
-        modify.setName( dn );
-        modify.addModification( mod );
-        
-        Entry reversed = LdifUtils.reverseModify( modify, modifiedEntry );
+        Entry reversed = LdifUtils.reverseModify( dn,
+                Collections.<ModificationItemImpl>singletonList( mod ), modifiedEntry
);
 
         assertNotNull( reversed );
         assertEquals( dn.getUpName(), reversed.getDn() );



Mime
View raw message