directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r650348 - in /directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src: main/java/org/apache/directory/server/xdbm/search/impl/ test/java/org/apache/directory/server/xdbm/search/impl/
Date Tue, 22 Apr 2008 01:31:12 GMT
Author: akarasulu
Date: Mon Apr 21 18:31:09 2008
New Revision: 650348

URL: http://svn.apache.org/viewvc?rev=650348&view=rev
Log:
100% code coverage for LessEq evaluator and cursor

Modified:
    directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqCursor.java
    directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqEvaluator.java
    directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/LessEqTest.java

Modified: directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqCursor.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqCursor.java?rev=650348&r1=650347&r2=650348&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqCursor.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqCursor.java
Mon Apr 21 18:31:09 2008
@@ -54,6 +54,13 @@
     /** NDN Cursor on all entries in  (set when no index on user attribute) */
     private final Cursor<IndexEntry<String,Attributes>> ndnIdxCursor;
 
+    /**
+     * Used to store indexEntry from ndnCandidate so it can be saved after
+     * call to evaluate() which changes the value so it's not referring to
+     * the NDN but to the value of the attribute instead.
+     */
+    IndexEntry<String, Attributes> ndnCandidate;
+
     /** used in both modes */
     private boolean available = false;
 
@@ -172,6 +179,7 @@
         else
         {
             ndnIdxCursor.beforeFirst();
+            ndnCandidate = null;
         }
 
         available = false;
@@ -189,6 +197,7 @@
         else
         {
             ndnIdxCursor.afterLast();
+            ndnCandidate = null;
         }
 
         available = false;
@@ -223,11 +232,15 @@
 
         while( ndnIdxCursor.previous() )
         {
-            IndexEntry<?,Attributes> candidate = ndnIdxCursor.get();
-            if ( lessEqEvaluator.evaluate( candidate ) )
+            ndnCandidate = ndnIdxCursor.get();
+            if ( lessEqEvaluator.evaluate( ndnCandidate ) )
             {
                  return available = true;
             }
+            else
+            {
+                ndnCandidate = null;
+            }
         }
 
         return available = false;
@@ -259,11 +272,15 @@
 
         while( ndnIdxCursor.next() )
         {
-            IndexEntry<?,Attributes> candidate = ndnIdxCursor.get();
-            if ( lessEqEvaluator.evaluate( candidate ) )
+            ndnCandidate = ndnIdxCursor.get();
+            if ( lessEqEvaluator.evaluate( ndnCandidate ) )
             {
                  return available = true;
             }
+            else
+            {
+                ndnCandidate = null;
+            }
         }
 
         return available = false;
@@ -284,7 +301,7 @@
 
         if ( available )
         {
-            return ndnIdxCursor.get();
+            return ndnCandidate;
         }
 
         throw new InvalidCursorPositionException( "Cursor has not been positioned yet." );
@@ -313,6 +330,7 @@
         else
         {
             ndnIdxCursor.close();
-        }
+            ndnCandidate = null;
+         }
     }
 }

Modified: directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqEvaluator.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqEvaluator.java?rev=650348&r1=650347&r2=650348&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqEvaluator.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/LessEqEvaluator.java
Mon Apr 21 18:31:09 2008
@@ -141,7 +141,7 @@
         Attribute attr = AttributeUtils.getAttribute( entry, type );
 
         // if the attribute does not exist just return false
-        if ( attr != null && evaluate( attr ) )
+        if ( attr != null && evaluate( ( IndexEntry<Object,Attributes> ) indexEntry,
attr ) )
         {
             return true;
         }
@@ -163,7 +163,7 @@
 
                 attr = AttributeUtils.getAttribute( entry, descendant );
 
-                if ( attr != null && evaluate( attr ) )
+                if ( attr != null && evaluate( ( IndexEntry<Object,Attributes>
) indexEntry, attr ) )
                 {
                     return true;
                 }
@@ -175,7 +175,7 @@
     }
 
 
-    private boolean evaluate( Attribute attribute ) throws Exception
+    private boolean evaluate( IndexEntry<Object,Attributes> indexEntry, Attribute attribute
) throws Exception
     {
         /*
          * Cycle through the attribute values testing normalized version
@@ -193,6 +193,7 @@
             //noinspection unchecked
             if ( comparator.compare( value, node.getValue() ) <= 0 )
             {
+                indexEntry.setValue( value );
                 values.close();
                 return true;
             }

Modified: directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/LessEqTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/LessEqTest.java?rev=650348&r1=650347&r2=650348&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/LessEqTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/LessEqTest.java
Mon Apr 21 18:31:09 2008
@@ -30,6 +30,7 @@
 import org.apache.directory.server.schema.SerializableComparator;
 import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmStore;
 import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex;
+import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
 import org.apache.directory.shared.ldap.constants.SchemaConstants;
 import org.apache.directory.shared.ldap.filter.LessEqNode;
 import org.apache.directory.shared.ldap.schema.*;
@@ -41,6 +42,7 @@
 import org.junit.Test;import static org.junit.Assert.assertTrue;import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
 
 import javax.naming.directory.Attributes;
 import javax.naming.NamingException;
@@ -132,6 +134,432 @@
 
         wkdir = null;
     }
+
+
+    @Test
+    public void testCursorIndexed() throws Exception
+    {
+        LessEqNode node = new LessEqNode( SchemaConstants.POSTALCODE_AT_OID, "3" );
+        LessEqEvaluator evaluator = new LessEqEvaluator( node, store, registries );
+        LessEqCursor cursor = new LessEqCursor( store, evaluator );
+        assertNotNull( cursor );
+        assertFalse( cursor.available() );
+        assertTrue( cursor.isElementReused() );
+        assertFalse( cursor.isClosed() );
+
+        // ---------- test bad get() ----------
+
+        try { cursor.get(); fail(); }
+        catch( InvalidCursorPositionException e ) {}
+
+        // ---------- test beforeFirst() ----------
+
+        cursor.beforeFirst();
+        assertFalse( cursor.available() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertFalse( cursor.next() );
+        assertFalse( cursor.available() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        // ---------- test first() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+
+        cursor.first();
+
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertFalse( cursor.next() );
+        assertFalse( cursor.available() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        // ---------- test afterLast() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+
+        cursor.afterLast();
+        assertFalse( cursor.available() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertFalse( cursor.previous() );
+        assertFalse( cursor.available() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        // ---------- test last() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+
+        cursor.last();
+
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertFalse( cursor.previous() );
+        assertFalse( cursor.available() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        // ---------- test before() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+        ForwardIndexEntry<String,Attributes> indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "2" );
+
+        assertFalse( cursor.available() );
+        cursor.before( indexEntry );
+        assertFalse( cursor.available() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertFalse( cursor.next() );
+        assertFalse( cursor.available() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        cursor = new LessEqCursor( store, evaluator );
+        indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "7" );
+        cursor.before( indexEntry );
+        assertFalse( cursor.available() );
+        assertTrue( cursor.previous() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+        cursor.close();
+
+        cursor = new LessEqCursor( store, evaluator );
+        indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "3" );
+        cursor.before( indexEntry );
+        assertFalse( cursor.available() );
+        assertTrue( cursor.next() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+        cursor.close();
+
+        // ---------- test after() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+        indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "1" );
+
+        assertFalse( cursor.available() );
+        cursor.after( indexEntry );
+        assertFalse( cursor.available() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertFalse( cursor.next() );
+        assertFalse( cursor.available() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        cursor = new LessEqCursor( store, evaluator );
+        indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "7" );
+        cursor.after( indexEntry );
+        assertFalse( cursor.available() );
+        assertTrue( cursor.previous() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+        cursor.close();
+
+        cursor = new LessEqCursor( store, evaluator );
+        indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "3" );
+        cursor.after( indexEntry );
+        assertFalse( cursor.available() );
+        assertTrue( cursor.previous() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+        cursor.close();
+    }
+
+
+    @Test
+    public void testCursorNotIndexed() throws Exception
+    {
+        LessEqNode node = new LessEqNode( SchemaConstants.POSTOFFICEBOX_AT_OID, "3" );
+        LessEqEvaluator evaluator = new LessEqEvaluator( node, store, registries );
+        LessEqCursor cursor = new LessEqCursor( store, evaluator );
+        assertNotNull( cursor );
+        assertFalse( cursor.available() );
+        assertTrue( cursor.isElementReused() );
+        assertFalse( cursor.isClosed() );
+
+        // ---------- test bad get() ----------
+
+        try { cursor.get(); fail(); }
+        catch( InvalidCursorPositionException e ) {}
+
+        // ---------- test beforeFirst() ----------
+
+        cursor.beforeFirst();
+        assertFalse( cursor.available() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertFalse( cursor.next() );
+        assertFalse( cursor.available() );
+        assertFalse( cursor.isClosed() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        // ---------- test beforeFirst() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+        cursor.first();
+
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.next() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertFalse( cursor.next() );
+        assertFalse( cursor.available() );
+        assertFalse( cursor.isClosed() );
+        cursor.close();
+        assertTrue( cursor.isClosed() );
+
+        // ---------- test afterLast() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+        cursor.afterLast();
+        assertFalse( cursor.available() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertFalse( cursor.previous() );
+        assertFalse( cursor.available() );
+
+        // ---------- test last() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+        cursor.last();
+
+        assertTrue( cursor.available() );
+        assertEquals( 5L, ( long ) cursor.get().getId() );
+        assertEquals( "3", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 2L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 4L, ( long ) cursor.get().getId() );
+        assertEquals( "2", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 3L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertTrue( cursor.previous() );
+        assertTrue( cursor.available() );
+        assertEquals( 1L, ( long ) cursor.get().getId() );
+        assertEquals( "1", cursor.get().getValue() );
+
+        assertFalse( cursor.previous() );
+        assertFalse( cursor.available() );
+
+        // ---------- test before() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+        ForwardIndexEntry<String,Attributes> indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "2" );
+        try { cursor.before( indexEntry ); fail( "Should never get here." );}
+        catch ( UnsupportedOperationException e ) {}
+
+        // ---------- test after() ----------
+
+        cursor = new LessEqCursor( store, evaluator );
+        indexEntry = new ForwardIndexEntry<String,Attributes>();
+        indexEntry.setValue( "2" );
+        try { cursor.after( indexEntry ); fail( "Should never get here." );}
+        catch ( UnsupportedOperationException e ) {}
+    }
+
+
+    // -----------------------------------------------------------------------
+    // Evaluator Test Cases
+    // -----------------------------------------------------------------------
 
 
     @Test



Mime
View raw message