directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r811938 - in /directory/apacheds/branches/apacheds-schema/ldif-partition/src: main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java test/java/org/apache/directory/server/core/partition/LdifPartitionTest.java
Date Sun, 06 Sep 2009 22:53:03 GMT
Author: elecharny
Date: Sun Sep  6 22:53:03 2009
New Revision: 811938

URL: http://svn.apache.org/viewvc?rev=811938&view=rev
Log:
o Added a test for searches
o Added some tests for deletion
o Fixed some bugs in the searches and deletion
o Cleaned up the directory at startup if we exit from tests brutally

Modified:
    directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
    directory/apacheds/branches/apacheds-schema/ldif-partition/src/test/java/org/apache/directory/server/core/partition/LdifPartitionTest.java

Modified: directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java?rev=811938&r1=811937&r2=811938&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
(original)
+++ directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
Sun Sep  6 22:53:03 2009
@@ -102,6 +102,10 @@
 
     /** The context entry */
     private ServerEntry contextEntry;
+    
+    /** Flags used for the getFile() method */
+    private static final boolean CREATE = Boolean.TRUE;
+    private static final boolean DELETE = Boolean.FALSE;
 
     private int ldifScanInterval;
 
@@ -262,9 +266,22 @@
 
         if ( entry != null )
         {
-            File file = getFile( entry.getDn() ).getParentFile();
-            boolean deleted = deleteFile( file );
-            LOG.warn( "deleted file {} {}", file.getAbsoluteFile(), deleted );
+            File ldifFile = getFile( entry.getDn(), DELETE );
+            
+            boolean deleted = deleteFile( ldifFile );
+
+            LOG.debug( "deleted file {} {}", ldifFile.getAbsoluteFile(), deleted );
+
+            // Delete the parent if there is no more children
+            File parentFile = ldifFile.getParentFile();
+            
+            if ( parentFile.listFiles().length == 0 )
+            {
+                deleteFile( parentFile );
+
+                LOG.debug( "deleted file {} {}", parentFile.getAbsoluteFile(), deleted );
+            }
+            
         }
 
     }
@@ -329,7 +346,7 @@
 
     private void entryMoved( LdapDN entryDn, Long entryId ) throws Exception
     {
-        File file = getFile( entryDn ).getParentFile();
+        File file = getFile( entryDn, DELETE ).getParentFile();
         boolean deleted = deleteFile( file );
         LOG.warn( "move operation: deleted file {} {}", file.getAbsoluteFile(), deleted );
 
@@ -411,7 +428,7 @@
     /**
      * Create the file name from the entry DN.
      */
-    private File getFile( LdapDN entryDn ) throws NamingException
+    private File getFile( LdapDN entryDn, boolean create ) throws NamingException
     {
         StringBuilder filePath = new StringBuilder();
         filePath.append( suffixDirectory ).append( File.separator );
@@ -430,7 +447,7 @@
         
         File dir = new File( parentDir );
         
-        if ( !dir.exists() )
+        if ( !dir.exists() && create )
         {
             // We have to create the entry if it does not have a parent
             dir.mkdir();
@@ -438,7 +455,7 @@
         
         File ldifFile = new File( parentDir + rdnFileName );
         
-        if ( ldifFile.exists() )
+        if ( ldifFile.exists() && create )
         {
             // The entry already exists
             throw new NamingException( "The entry already exsists" );
@@ -594,7 +611,7 @@
      */
     private void add( Entry entry ) throws Exception
     {
-        FileWriter fw = new FileWriter( getFile( entry.getDn() ) );
+        FileWriter fw = new FileWriter( getFile( entry.getDn(), CREATE ) );
         fw.write( LdifUtils.convertEntryToLdif( entry ) );
         fw.close();
     }

Modified: directory/apacheds/branches/apacheds-schema/ldif-partition/src/test/java/org/apache/directory/server/core/partition/LdifPartitionTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/ldif-partition/src/test/java/org/apache/directory/server/core/partition/LdifPartitionTest.java?rev=811938&r1=811937&r2=811938&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/ldif-partition/src/test/java/org/apache/directory/server/core/partition/LdifPartitionTest.java
(original)
+++ directory/apacheds/branches/apacheds-schema/ldif-partition/src/test/java/org/apache/directory/server/core/partition/LdifPartitionTest.java
Sun Sep  6 22:53:03 2009
@@ -21,6 +21,8 @@
 package org.apache.directory.server.core.partition;
 
 import java.io.File;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.UUID;
 
 import javax.naming.NamingException;
@@ -33,11 +35,18 @@
 import org.apache.directory.server.core.entry.ClonedServerEntry;
 import org.apache.directory.server.core.entry.DefaultServerEntry;
 import org.apache.directory.server.core.entry.ServerEntry;
+import org.apache.directory.server.core.filtering.EntryFilteringCursor;
 import org.apache.directory.server.core.interceptor.context.AddOperationContext;
+import org.apache.directory.server.core.interceptor.context.DeleteOperationContext;
+import org.apache.directory.server.core.interceptor.context.SearchOperationContext;
 import org.apache.directory.server.core.partition.ldif.LdifPartition;
 import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
 import org.apache.directory.shared.ldap.constants.SchemaConstants;
 import org.apache.directory.shared.ldap.csn.CsnFactory;
+import org.apache.directory.shared.ldap.entry.Entry;
+import org.apache.directory.shared.ldap.filter.ExprNode;
+import org.apache.directory.shared.ldap.filter.FilterParser;
+import org.apache.directory.shared.ldap.filter.SearchScope;
 import org.apache.directory.shared.ldap.name.LdapDN;
 import org.apache.directory.shared.ldap.schema.SchemaUtils;
 import org.apache.directory.shared.ldap.schema.ldif.extractor.SchemaLdifExtractor;
@@ -47,6 +56,9 @@
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
@@ -90,6 +102,13 @@
         loader.loadAllEnabled( registries );
 
         defaultCSNFactory = new CsnFactory( 0 );
+        
+        wkdir = File.createTempFile( LdifPartitionTest.class.getSimpleName(), "db" );
+        wkdir.delete();
+        wkdir = new File( wkdir.getParentFile(), LdifPartitionTest.class.getSimpleName()
);
+        FileUtils.deleteDirectory( wkdir );
+        //wkdir.mkdirs();
+
     }
 
     
@@ -222,7 +241,6 @@
         {
             assertTrue( true );
         }
-        
 
         assertTrue( new File( wkdir, "ou=test,ou=system" ).exists() );
         assertTrue( new File( wkdir, "ou=test,ou=system.ldif" ).exists() );
@@ -231,4 +249,143 @@
         assertFalse( new File( wkdir, "ou=test,ou=system/dc=test/dc=test" ).exists() );
         assertTrue( new File( wkdir, "ou=test,ou=system/dc=test/dc=test.ldif" ).exists()
);
     }
+
+
+    //-------------------------------------------------------------------------
+    // Partition.delete() tests
+    //-------------------------------------------------------------------------
+    /**
+     * Test that we can delete an existing entry 
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testLdifDeleteExistingEntry() throws Exception
+    {
+        LdapDN adminDn = new LdapDN( "uid=admin,ou=system" ).normalize( registries.getAttributeTypeRegistry().getNormalizerMapping()
);
+        CoreSession session = new MockCoreSession( new LdapPrincipal( adminDn, AuthenticationLevel.STRONG
), new MockDirectoryService( 1 ) );
+        AddOperationContext addCtx = new AddOperationContext( session );
+        
+        ClonedServerEntry entry1 = createEntry( "dc=test,ou=test,ou=system" );
+        entry1.put( "ObjectClass", "top", "domain" );
+        entry1.put( "dc", "test" );
+        addCtx.setEntry( entry1 );
+        
+        partition.add( addCtx );
+
+        ClonedServerEntry entry2 = createEntry( "dc=test1,dc=test,ou=test,ou=system" );
+        entry2.put( "ObjectClass", "top", "domain" );
+        entry2.put( "dc", "test1" );
+        addCtx.setEntry( entry2 );
+        
+        partition.add( addCtx );
+        
+        ClonedServerEntry entry3 = createEntry( "dc=test2,dc=test,ou=test,ou=system" );
+        entry3.put( "ObjectClass", "top", "domain" );
+        entry3.put( "dc", "test2" );
+        addCtx.setEntry( entry3 );
+        
+        partition.add( addCtx );
+        
+        DeleteOperationContext delCtx = new DeleteOperationContext( session );
+
+        LdapDN dn = new LdapDN( "dc=test1,dc=test,ou=test,ou=system" );
+        dn.normalize( registries.getAttributeTypeRegistry().getNormalizerMapping() );
+        
+        delCtx.setDn( dn );
+        
+        partition.delete( delCtx );
+
+        assertTrue( new File( wkdir, "ou=test,ou=system" ).exists() );
+        assertTrue( new File( wkdir, "ou=test,ou=system.ldif" ).exists() );
+        assertTrue( new File( wkdir, "ou=test,ou=system/dc=test" ).exists() );
+        assertTrue( new File( wkdir, "ou=test,ou=system/dc=test.ldif" ).exists() );
+        assertFalse( new File( wkdir, "ou=test,ou=system/dc=test/dc=test1" ).exists() );
+        assertFalse( new File( wkdir, "ou=test,ou=system/dc=test/dc=test1.ldif" ).exists()
);
+        assertFalse( new File( wkdir, "ou=test,ou=system/dc=test/dc=test2" ).exists() );
+        assertTrue( new File( wkdir, "ou=test,ou=system/dc=test/dc=test2.ldif" ).exists()
);
+
+        dn = new LdapDN( "dc=test2,dc=test,ou=test,ou=system" );
+        dn.normalize( registries.getAttributeTypeRegistry().getNormalizerMapping() );
+        
+        delCtx.setDn( dn );
+        
+        partition.delete( delCtx );
+
+        assertTrue( new File( wkdir, "ou=test,ou=system" ).exists() );
+        assertTrue( new File( wkdir, "ou=test,ou=system.ldif" ).exists() );
+        assertFalse( new File( wkdir, "ou=test,ou=system/dc=test" ).exists() );
+        assertTrue( new File( wkdir, "ou=test,ou=system/dc=test.ldif" ).exists() );
+        assertFalse( new File( wkdir, "ou=test,ou=system/dc=test/dc=test2" ).exists() );
+        assertFalse( new File( wkdir, "ou=test,ou=system/dc=test/dc=test2.ldif" ).exists()
);
+    }
+    //-------------------------------------------------------------------------
+    // Partition.delete() tests
+    //-------------------------------------------------------------------------
+    /**
+     * Test that we can search for an existing entry 
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testLdifSearchExistingEntry() throws Exception
+    {
+        LdapDN adminDn = new LdapDN( "uid=admin,ou=system" ).normalize( registries.getAttributeTypeRegistry().getNormalizerMapping()
);
+        CoreSession session = new MockCoreSession( new LdapPrincipal( adminDn, AuthenticationLevel.STRONG
), new MockDirectoryService( 1 ) );
+        AddOperationContext addCtx = new AddOperationContext( session );
+        
+        ClonedServerEntry entry1 = createEntry( "dc=test,ou=test,ou=system" );
+        entry1.put( "ObjectClass", "top", "domain" );
+        entry1.put( "dc", "test" );
+        addCtx.setEntry( entry1 );
+        
+        partition.add( addCtx );
+
+        ClonedServerEntry entry2 = createEntry( "dc=test1,dc=test,ou=test,ou=system" );
+        entry2.put( "ObjectClass", "top", "domain" );
+        entry2.put( "dc", "test1" );
+        addCtx.setEntry( entry2 );
+        
+        partition.add( addCtx );
+        
+        ClonedServerEntry entry3 = createEntry( "dc=test2,dc=test,ou=test,ou=system" );
+        entry3.put( "ObjectClass", "top", "domain" );
+        entry3.put( "dc", "test2" );
+        addCtx.setEntry( entry3 );
+        
+        partition.add( addCtx );
+        
+        SearchOperationContext searchCtx = new SearchOperationContext( session );
+
+        LdapDN dn = new LdapDN( "dc=test,ou=test,ou=system" );
+        dn.normalize( registries.getAttributeTypeRegistry().getNormalizerMapping() );
+        searchCtx.setDn( dn );
+        ExprNode filter = FilterParser.parse( "(ObjectClass=domain)" );
+        searchCtx.setFilter( filter );
+        searchCtx.setScope( SearchScope.SUBTREE );
+        
+        EntryFilteringCursor cursor = partition.search( searchCtx );
+        
+        assertNotNull( cursor );
+        
+        Set<String> expectedDns = new HashSet<String>();
+        expectedDns.add( entry1.getDn().getNormName() );
+        expectedDns.add( entry2.getDn().getNormName() );
+        expectedDns.add( entry3.getDn().getNormName() );
+        
+        cursor.beforeFirst();
+        int nbRes = 0;
+        
+        while ( cursor.next() )
+        {
+            Entry entry = cursor.get();
+            assertNotNull( entry );
+            nbRes++;
+            
+            expectedDns.remove( entry.getDn().getNormName() );
+        }
+
+        assertEquals( 3, nbRes );
+        assertEquals( 0, expectedDns.size() );
+    }
 }



Mime
View raw message