directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1054996 - in /directory/shared/branches/shared-AP/ldap/src: main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java test/java/org/apache/directory/shared/ldap/util/tree/DnNodeTest.java
Date Tue, 04 Jan 2011 12:34:53 GMT
Author: kayyagari
Date: Tue Jan  4 12:34:52 2011
New Revision: 1054996

URL: http://svn.apache.org/viewvc?rev=1054996&view=rev
Log:
o added move operation and tests

Modified:
    directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java
    directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/DnNodeTest.java

Modified: directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java?rev=1054996&r1=1054995&r2=1054996&view=diff
==============================================================================
--- directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java
(original)
+++ directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java
Tue Jan  4 12:34:52 2011
@@ -856,6 +856,63 @@ public class DnNode<N> implements Clonea
     
     
     /**
+     * move the DnNode's DN
+     *
+     * @param newParent the new parent DN
+     * @throws LdapException
+     */
+    public void move( DN newParent ) throws LdapException
+    {
+        DnNode<N> tmp = null;
+
+        DN tmpDn = null;
+       
+        // check if the new parent DN is child of the parent
+        if( newParent.isChildOf( parent.nodeDn ) )
+        {
+            tmp = parent;
+            tmpDn = parent.nodeDn;
+        }
+        
+        // if yes, then drill for the new parent node
+        if ( tmpDn != null )
+        {
+            int parentNodeSize = tmpDn.size();
+            int count = newParent.size() - parentNodeSize;
+            
+            while( count-- > 0 )
+            {
+                tmp = tmp.getChild( newParent.getRdn( parentNodeSize++ ) );
+            }
+        }
+
+        // if not, we have to traverse all the way up to the 
+        // root node and then find the new parent node
+        if ( tmp == null )
+        {
+            tmp = this;
+            while( tmp.parent != null )
+            {
+                tmp = tmp.parent;
+            }
+            
+            tmp = tmp.getNode( newParent );
+        }
+        
+        nodeDn = newParent.add( nodeRdn );
+        updateAfterModDn( nodeDn );
+        
+        if( parent != null )
+        {
+            parent.children.remove( nodeRdn );
+        }
+        
+        parent = tmp;
+        parent.children.put( nodeRdn, this );
+    }
+    
+    
+    /**
      * update the children's DN based on the new parent DN created
      * after a rename or move operation
      * 

Modified: directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/DnNodeTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/DnNodeTest.java?rev=1054996&r1=1054995&r2=1054996&view=diff
==============================================================================
--- directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/DnNodeTest.java
(original)
+++ directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/DnNodeTest.java
Tue Jan  4 12:34:52 2011
@@ -865,4 +865,69 @@ public class DnNodeTest
         
         assertNotNull( rootNode.getChild( newChildRdn ) );
     }
+
+
+    @Test
+    public void testMoveToAnAncestor() throws Exception
+    {
+        DnNode<DN> rootNode = new DnNode<DN>();
+        DN dn = new DN( "dc=vysper,dc=mina,dc=directory,dc=apache,dc=org" );
+        
+        rootNode.add( dn );
+
+        RDN minaRdn = new RDN( "dc=mina" );
+        DnNode<DN> apacheNode = rootNode.getChild( new RDN( "dc=org" ) ).getChild(
new RDN( "dc=apache" ) );
+        DnNode<DN> directoryNode = apacheNode.getChild( new RDN( "dc=directory" ) );

+        DnNode<DN> minaNode = directoryNode.getChild( minaRdn );
+        assertNotNull( minaNode );
+        assertEquals( directoryNode, minaNode.getParent() );
+        assertTrue( directoryNode.contains( minaRdn ) );
+        
+        DN newParent = new DN( "dc=apache,dc=org" );
+        minaNode.move( newParent );
+        
+        minaNode = apacheNode.getChild( minaRdn );
+        assertNotNull( minaNode );
+        assertNull( directoryNode.getChild( minaRdn ) );
+        assertNotNull( apacheNode.getChild( minaRdn ) );
+        assertFalse( directoryNode.contains( minaRdn ) );
+        assertTrue( apacheNode.contains( minaRdn ) );
+        
+        assertEquals( new DN( "dc=mina,dc=apache,dc=org" ), minaNode.getDn() );
+        assertEquals( new DN( "dc=vysper,dc=mina,dc=apache,dc=org" ), minaNode.getChild(
new RDN( "dc=vysper" ) ).getDn());
+    }
+
+    
+    @Test
+    public void testMoveToSiblingBranch() throws Exception
+    {
+        DnNode<DN> rootNode = new DnNode<DN>();
+        DN dn1 = new DN( "dc=vysper,dc=mina,dc=directory,dc=apache,dc=org" );
+        
+        DN dn2 = new DN( "dc=kayyagari,dc=apache,dc=org" );
+        rootNode.add( dn1 );
+        rootNode.add( dn2 );
+
+        RDN directoryRdn = new RDN( "dc=directory" );
+        
+        DnNode<DN> apacheNode = rootNode.getChild( new RDN( "dc=org" ) ).getChild(
new RDN( "dc=apache" ) );
+        DnNode<DN> directoryNode = apacheNode.getChild( new RDN( "dc=directory" ) );

+        assertNotNull( directoryNode );
+        assertEquals( apacheNode, directoryNode.getParent() );
+        assertTrue( apacheNode.contains( directoryRdn ) );
+        
+        directoryNode.move( dn2 );
+        
+        DnNode<DN> newParentNode = rootNode.getChild( new RDN( "dc=org" ) ).getChild(
new RDN( "dc=apache" ) ).getChild( new RDN( "dc=kayyagari" ) );
+        directoryNode = newParentNode.getChild( directoryRdn );
+        assertNotNull( directoryNode );
+        assertNull( apacheNode.getChild( directoryRdn ) );
+        assertNotNull( newParentNode.getChild( directoryRdn ) );
+        assertFalse( apacheNode.contains( directoryRdn ) );
+        assertTrue( newParentNode.contains( directoryRdn ) );
+        
+        assertEquals( new DN( "dc=directory,dc=kayyagari,dc=apache,dc=org" ), directoryNode.getDn()
);
+        assertEquals( new DN( "dc=mina,dc=directory,dc=kayyagari,dc=apache,dc=org" ), directoryNode.getChild(
new RDN( "dc=mina" ) ).getDn());
+        assertEquals( new DN( "dc=vysper,dc=mina,dc=directory,dc=kayyagari,dc=apache,dc=org"
), directoryNode.getChild( new RDN( "dc=mina" ) ).getChild( new RDN( "dc=vysper" ) ).getDn());
+    }
 }



Mime
View raw message