You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2018/01/23 07:20:56 UTC

[directory-server] 01/05: o Added a test for PersisteSearch that uses the LDAP API o Fixed a typo

This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-server.git

commit b18ea82d406519a8ba75db9bc06519d31a643ff1
Author: Emmanuel Lécharny <el...@symas.com>
AuthorDate: Fri Dec 29 00:52:41 2017 +0100

    o Added a test for PersisteSearch that uses the LDAP API
    o Fixed a typo
---
 .../{PagedSearchIT.java => PagedSearchApiIT.java}  | 806 +++++++++++----------
 .../server/operations/search/PagedSearchIT.java    |   2 +-
 2 files changed, 431 insertions(+), 377 deletions(-)

diff --git a/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchIT.java b/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchApiIT.java
similarity index 51%
copy from server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchIT.java
copy to server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchApiIT.java
index bc012de..94e0a0f 100644
--- a/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchIT.java
+++ b/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchApiIT.java
@@ -20,30 +20,26 @@
 package org.apache.directory.server.operations.search;
 
 
-import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredContext;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
-import javax.naming.SizeLimitExceededException;
-import javax.naming.directory.DirContext;
-import javax.naming.directory.SearchControls;
-import javax.naming.directory.SearchResult;
-import javax.naming.ldap.LdapContext;
-import javax.naming.ldap.PagedResultsResponseControl;
-
 import org.apache.directory.api.asn1.EncoderException;
 import org.apache.directory.api.ldap.codec.api.LdapApiService;
 import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
 import org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator;
+import org.apache.directory.api.ldap.model.cursor.CursorException;
 import org.apache.directory.api.ldap.model.cursor.EntryCursor;
+import org.apache.directory.api.ldap.model.cursor.SearchCursor;
 import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
 import org.apache.directory.api.ldap.model.message.Control;
 import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
 import org.apache.directory.api.ldap.model.message.SearchRequest;
@@ -51,11 +47,13 @@ import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
 import org.apache.directory.api.ldap.model.message.SearchResultDone;
 import org.apache.directory.api.ldap.model.message.SearchScope;
 import org.apache.directory.api.ldap.model.message.controls.PagedResults;
+import org.apache.directory.api.ldap.model.message.controls.PagedResultsImpl;
 import org.apache.directory.api.ldap.model.name.Dn;
 import org.apache.directory.api.ldap.util.JndiUtils;
 import org.apache.directory.api.util.Network;
 import org.apache.directory.api.util.Strings;
 import org.apache.directory.ldap.client.api.EntryCursorImpl;
+import org.apache.directory.ldap.client.api.LdapConnection;
 import org.apache.directory.ldap.client.api.LdapNetworkConnection;
 import org.apache.directory.server.annotations.CreateLdapServer;
 import org.apache.directory.server.annotations.CreateTransport;
@@ -70,7 +68,7 @@ import org.junit.runner.RunWith;
 /**
  * Test the PagedSearchControl. The following tables covers all the
  * possible cases for both an admin and a simple user, combining the
- * Server SizeLimit (SL), the requested SizeLimit (SL) and the paged
+ * Server SizeLimit (SL), the request SizeLimit (RL) and the paged
  * size limit (PL). The 'X' column tells if we are supposed to receive
  * a SizeLimitExceededException.<br>
  * <br>
@@ -111,15 +109,13 @@ import org.junit.runner.RunWith;
  * |test23 | 0  | 9  | 5  | 2 ( 5 + 4 )         | 9  | Y |<br>
  * |test24 | 5  | 0  | 5  | 1 ( 5 )             | 5  | Y |<br>
  * |test25 | 0  | 5  | 5  | 1 ( 5 )             | 5  | Y |<br>
- * |test26 | 5  | 4  | 3  | 2 ( 3 + 1 )         | 4  | Y |<br>
- * |test27 | 4  | 5  | 3  | 2 ( 3 + 1 )         | 4  | Y |<br>
- * |test28 | 5  | 3  | 4  | 1 ( 3 )             | 3  | Y |<br>
- * |test29 | 5  | 4  | 3  | 2 ( 3 + 1 )         | 4  | Y |<br>
- * |test30 | 4  | 5  | 3  | 2 ( 3 + 1 )         | 4  | Y |<br>
- * |test31 | 4  | 3  | 5  | 1 ( 3 )             | 3  | Y |<br>
- * |test32 | 3  | 5  | 4  | 1 ( 3 )             | 3  | Y |<br>
- * |test33 | 3  | 4  | 5  | 1 ( 3 )             | 3  | Y |<br>
- * |test34 | 5  | 5  | 5  | 1 ( 5 )             | 5  | Y |<br>
+ * |test26 | 5  | 3  | 4  | 1 ( 3 )             | 3  | Y |<br>
+ * |test27 | 5  | 4  | 3  | 2 ( 3 + 1 )         | 4  | Y |<br>
+ * |test28 | 4  | 5  | 3  | 2 ( 3 + 1 )         | 4  | Y |<br>
+ * |test29 | 4  | 3  | 5  | 1 ( 3 )             | 3  | Y |<br>
+ * |test30 | 3  | 5  | 4  | 1 ( 3 )             | 3  | Y |<br>
+ * |test31 | 3  | 4  | 5  | 1 ( 3 )             | 3  | Y |<br>
+ * |test32 | 5  | 5  | 5  | 1 ( 5 )             | 5  | Y |<br>
  * +-------+----+----+----+---------------------+----+---+<br>
  *</pre>
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
@@ -205,52 +201,18 @@ import org.junit.runner.RunWith;
         "cn: user",
         "userPassword: secret",
         "sn: user" })
-public class PagedSearchIT extends AbstractLdapTestUnit
+public class PagedSearchApiIT extends AbstractLdapTestUnit
 {
     private LdapApiService codec = LdapApiServiceFactory.getSingleton();
 
 
     /**
-     * Create the searchControls with a paged size
-     * @throws EncoderException on codec failures
-     */
-    private SearchControls createSearchControls( DirContext ctx, int sizeLimit, int pagedSize )
-        throws NamingException, EncoderException
-    {
-        SearchControls controls = new SearchControls();
-        controls.setCountLimit( sizeLimit );
-        controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
-        PagedResultsDecorator pagedSearchControl = new PagedResultsDecorator( codec );
-        pagedSearchControl.setSize( pagedSize );
-
-        ( ( LdapContext ) ctx ).setRequestControls( JndiUtils.toJndiControls( codec, new Control[]
-            { pagedSearchControl } ) );
-
-        return controls;
-    }
-
-
-    /**
-     * Create the searchControls with a paged size
-     * @throws EncoderException on codec failures
-     */
-    private void createNextSearchControls( DirContext ctx, byte[] cookie, int pagedSize )
-        throws NamingException, EncoderException
-    {
-        PagedResultsDecorator pagedSearchControl = new PagedResultsDecorator( codec );
-        pagedSearchControl.setCookie( cookie );
-        pagedSearchControl.setSize( pagedSize );
-        ( ( LdapContext ) ctx ).setRequestControls( JndiUtils.toJndiControls( codec, new Control[]
-            { pagedSearchControl } ) );
-    }
-
-
-    /**
      * Check that we got the correct result set
+     * @throws LdapInvalidAttributeValueException 
      */
-    private void checkResults( List<SearchResult> results, int expectedSize ) throws NamingException
+    private void checkResults( List<Entry> entries, int expectedSize ) throws LdapInvalidAttributeValueException
     {
-        assertEquals( expectedSize, results.size() );
+        assertEquals( expectedSize, entries.size() );
         Set<String> expected = new HashSet<String>();
 
         for ( int i = 0; i < 10; i++ )
@@ -261,8 +223,8 @@ public class PagedSearchIT extends AbstractLdapTestUnit
         // check that we have correctly read all the entries
         for ( int i = 0; i < expectedSize; i++ )
         {
-            SearchResult entry = results.get( i );
-            String user = ( String ) entry.getAttributes().get( "cn" ).get();
+            Entry entry = entries.get( i );
+            String user = ( String ) entry.get( "cn" ).getString();
             assertTrue( expected.contains( user ) );
 
             expected.remove( user );
@@ -277,89 +239,65 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * reach a limit. It will check that we have got all the expected entries.
      * @throws EncoderException  on codec failures
      */
-    private void doLoop( DirContext ctx, SearchControls controls, int pagedSizeLimit,
-        int expectedLoop, int expectedNbEntries, boolean expectedException ) throws NamingException, EncoderException
+    private void doLoop( LdapConnection connection, long searchLimit, int pagedSizeLimit, 
+        int expectedLoop, int expectedNbEntries, boolean expectedException ) throws LdapException, IOException, CursorException
     {
         // Loop over all the elements
         int loop = 0;
         boolean hasSizeLimitException = false;
-        List<SearchResult> results = new ArrayList<SearchResult>();
+        List<Entry> results = new ArrayList<>();
+
+        SearchRequest searchRequest = new SearchRequestImpl();
+        searchRequest.setBase( new Dn( "dc=users,ou=system" ) );
+        searchRequest.setFilter( "(cn=*)" );
+        searchRequest.setScope( SearchScope.SUBTREE );
+        searchRequest.setSizeLimit( searchLimit );
+
+        PagedResults pagedControl = new PagedResultsImpl();
+        pagedControl.setSize( pagedSizeLimit );
 
         while ( true )
         {
             loop++;
-            NamingEnumeration<SearchResult> list = null;
+            searchRequest.addControl( pagedControl );
 
-            try
+            try ( SearchCursor cursor = connection.search( searchRequest ) )
             {
-                list = ctx.search( "dc=users,ou=system", "(cn=*)", controls );
-
-                while ( list.hasMore() )
+                while ( cursor.next() )
                 {
-                    SearchResult result = list.next();
+                    Entry result = cursor.getEntry();
                     results.add( result );
                 }
-            }
-            catch ( SizeLimitExceededException e )
-            {
-                hasSizeLimitException = true;
-                break;
-            }
-            finally
-            {
-                // Close the NamingEnumeration
-                if ( list != null )
-                {
-                    list.close();
-                }
-            }
-
-            // Now read the next ones
-            javax.naming.ldap.Control[] responseControls = ( ( LdapContext ) ctx ).getResponseControls();
 
-            PagedResultsResponseControl responseControl =
-                ( PagedResultsResponseControl ) responseControls[0];
-            assertEquals( 0, responseControl.getResultSize() );
+                // Now read the next ones
+                Map<String, Control> controls =  cursor.getSearchResultDone().getControls();
 
-            // check if this is over
-            byte[] cookie = responseControl.getCookie();
-
-            if ( Strings.isEmpty( cookie ) )
-            {
-                // If so, exit the loop
-                break;
+                if ( cursor.getSearchResultDone().getLdapResult().getResultCode() == ResultCodeEnum.SIZE_LIMIT_EXCEEDED )
+                {
+                    hasSizeLimitException = true;
+                    
+                    break;
+                }
+                
+                PagedResults responseControl = ( PagedResults ) controls.get( PagedResults.OID );
+                assertEquals( 0, responseControl.getSize() );
+
+                // check if this is over
+                byte[] cookie = responseControl.getCookie();
+            
+                if ( Strings.isEmpty( cookie ) )
+                {
+                    // If so, exit the loop
+                    break;
+                }
+                
+                // Prepare the next iteration
+                pagedControl.setCookie( cookie );
             }
-
-            // Prepare the next iteration
-            createNextSearchControls( ctx, responseControl.getCookie(), pagedSizeLimit );
         }
 
         assertEquals( expectedException, hasSizeLimitException );
         assertEquals( expectedLoop, loop );
-        checkResults( results, expectedNbEntries );
-
-        // And close the connection
-        closeConnection( ctx );
-    }
-
-
-    /**
-     * Close a connection, and wait a bit to be sure it's done
-     */
-    private void closeConnection( DirContext ctx ) throws NamingException
-    {
-        if ( ctx != null )
-        {
-            ctx.close();
-
-            try
-            {
-                Thread.sleep( 10 );
-            }
-            catch ( Exception e )
-            {
-            }
-        }
     }
 
 
@@ -375,10 +313,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest1() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 3 );
 
-        doLoop( ctx, controls, 3, 4, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 3, 4, 10, false );
+        }
     }
 
 
@@ -394,10 +335,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest2() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 5 );
 
-        doLoop( ctx, controls, 5, 2, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 5, 2, 10, false );
+        }
     }
 
 
@@ -413,10 +357,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest3() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 3 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 5 );
 
-        doLoop( ctx, controls, 5, 2, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 5, 2, 10, false );
+        }
     }
 
 
@@ -432,10 +379,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest4() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 3, 5 );
 
-        doLoop( ctx, controls, 5, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 3L, 5, 1, 3, true );
+        }
     }
 
 
@@ -451,10 +401,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest5() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 3 );
 
-        doLoop( ctx, controls, 3, 4, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 3, 4, 10, false );
+        }
     }
 
 
@@ -470,10 +423,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest6() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 9, 5 );
 
-        doLoop( ctx, controls, 5, 2, 9, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 9L, 5, 2, 9, true );
+        }
     }
 
 
@@ -489,10 +445,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest7() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 5 );
 
-        doLoop( ctx, controls, 5, 2, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 5, 2, 10, false );
+        }
     }
 
 
@@ -508,10 +467,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest8() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 5, 5 );
 
-        doLoop( ctx, controls, 5, 1, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 5, 1, 5, true );
+        }
     }
 
 
@@ -527,10 +489,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest9() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 4, 3 );
 
-        doLoop( ctx, controls, 3, 2, 4, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 4L, 3, 2, 4, true );
+        }
     }
 
 
@@ -546,10 +511,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest10() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 4 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 5, 3 );
 
-        doLoop( ctx, controls, 3, 2, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 3, 2, 5, true );
+        }
     }
 
 
@@ -565,10 +533,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest11() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 3, 4 );
 
-        doLoop( ctx, controls, 4, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 3L, 4, 1, 3, true );
+        }
     }
 
 
@@ -584,10 +555,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest12() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 4, 3 );
 
-        doLoop( ctx, controls, 3, 2, 4, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 4L, 3, 2, 4, true );
+        }
     }
 
 
@@ -603,10 +577,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest13() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 4 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 5, 3 );
 
-        doLoop( ctx, controls, 3, 2, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 3, 2, 5, true );
+        }
     }
 
 
@@ -622,10 +599,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest14() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 4 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 3, 5 );
 
-        doLoop( ctx, controls, 5, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 3L, 5, 1, 3, true );
+        }
     }
 
 
@@ -641,10 +621,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest15() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 3 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 5, 4 );
 
-        doLoop( ctx, controls, 4, 2, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 4, 2, 5, true );
+        }
     }
 
 
@@ -660,10 +643,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest16() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 3 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 4, 5 );
 
-        doLoop( ctx, controls, 5, 1, 4, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 4L, 5, 1, 4, true );
+        }
     }
 
 
@@ -679,10 +665,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest17() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer() );
-        SearchControls controls = createSearchControls( ctx, 5, 5 );
 
-        doLoop( ctx, controls, 5, 1, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "uid=admin,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 5, 1, 5, true );
+        }
     }
 
 
@@ -698,10 +687,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest18() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 3 );
 
-        doLoop( ctx, controls, 3, 4, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 3, 4, 10, false );
+        }
     }
 
 
@@ -717,10 +709,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest19() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 5 );
 
-        doLoop( ctx, controls, 5, 2, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 5, 2, 10, false );
+        }
     }
 
 
@@ -736,10 +731,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest20() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 3 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 5 );
 
-        doLoop( ctx, controls, 5, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 5, 1, 3, true );
+        }
     }
 
 
@@ -755,10 +753,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest21() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 3, 5 );
 
-        doLoop( ctx, controls, 5, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 3L, 5, 1, 3, true );
+        }
     }
 
 
@@ -774,10 +775,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest22() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 3 );
 
-        doLoop( ctx, controls, 3, 2, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 3, 2, 5, true );
+        }
     }
 
 
@@ -793,10 +797,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest23() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 9, 5 );
 
-        doLoop( ctx, controls, 5, 2, 9, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 9L, 5, 2, 9, true );
+        }
     }
 
 
@@ -812,10 +819,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchtest24() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 5 );
 
-        doLoop( ctx, controls, 5, 1, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, 5, 1, 5, true );
+        }
     }
 
 
@@ -831,48 +841,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchTest25() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 5, 5 );
-
-        doLoop( ctx, controls, 5, 1, 5, true );
-    }
-
 
-    /**
-     * Admin = no <br>
-     * SL = 5<br>
-     * RL = 4<br>
-     * PL = 3<br>
-     * expected exception : yes<br>
-     * expected number of entries returned : 2 ( 3 + 1 )<br>
-     */
-    @Test
-    public void testPagedSearchTest26() throws Exception
-    {
-        getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 4, 3 );
-
-        doLoop( ctx, controls, 3, 2, 4, true );
-    }
-
-
-    /**
-     * Admin = no <br>
-     * SL = 4<br>
-     * RL = 5<br>
-     * PL = 3<br>
-     * expected exception : yes<br>
-     * expected number of entries returned : 4 ( 3 + 1 )<br>
-     */
-    @Test
-    public void testPagedSearchtest27() throws Exception
-    {
-        getLdapServer().setMaxSizeLimit( 4 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 5, 3 );
-
-        doLoop( ctx, controls, 3, 2, 4, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 5, 1, 5, true );
+        }
     }
 
 
@@ -885,13 +860,16 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * expected number of entries returned : 3<br>
      */
     @Test
-    public void testPagedSearchtest28() throws Exception
+    public void testPagedSearchtest26() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 3, 4 );
 
-        doLoop( ctx, controls, 4, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 3L, 4, 1, 3, true );
+        }
     }
 
 
@@ -904,13 +882,16 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * expected number of entries returned : 4 ( 3 + 1 )<br>
      */
     @Test
-    public void testPagedSearchtest29() throws Exception
+    public void testPagedSearchtest27() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 4, 3 );
 
-        doLoop( ctx, controls, 3, 2, 4, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 4L, 3, 2, 4, true );
+        }
     }
 
 
@@ -923,13 +904,16 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * expected number of entries returned : 4 ( 3 + 1 )<br>
      */
     @Test
-    public void testPagedSearchtest30() throws Exception
+    public void testPagedSearchtest28() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 4 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 5, 3 );
 
-        doLoop( ctx, controls, 3, 2, 4, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 3, 2, 4, true );
+        }
     }
 
 
@@ -942,13 +926,16 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * expected number of entries returned : 3 <br>
      */
     @Test
-    public void testPagedSearchtest31() throws Exception
+    public void testPagedSearchtest29() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 4 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 3, 5 );
 
-        doLoop( ctx, controls, 3, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 3L, 5, 1, 3, true );
+        }
     }
 
 
@@ -961,13 +948,16 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * expected number of entries returned : 3 <br>
      */
     @Test
-    public void testPagedSearchtest32() throws Exception
+    public void testPagedSearchtest30() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 3 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 5, 4 );
 
-        doLoop( ctx, controls, 3, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 4, 1, 3, true );
+        }
     }
 
 
@@ -980,13 +970,16 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * expected number of entries returned : 3 <br>
      */
     @Test
-    public void testPagedSearchtest33() throws Exception
+    public void testPagedSearchtest31() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 3 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 4, 5 );
 
-        doLoop( ctx, controls, 3, 1, 3, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 4L, 5, 1, 3, true );
+        }
     }
 
 
@@ -999,13 +992,16 @@ public class PagedSearchIT extends AbstractLdapTestUnit
      * expected number of entries returned : 5 <br>
      */
     @Test
-    public void testPagedSearchtest34() throws Exception
+    public void testPagedSearchtest32() throws Exception
     {
         getLdapServer().setMaxSizeLimit( 5 );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, 5, 5 );
 
-        doLoop( ctx, controls, 5, 1, 5, true );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+            
+            doLoop( connection, 5L, 5, 1, 5, true );
+        }
     }
 
 
@@ -1021,10 +1017,13 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchWithNegativePL() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, -2 );
 
-        doLoop( ctx, controls, -2, 1, 10, false );
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+    
+            doLoop( connection, LdapServer.NO_SIZE_LIMIT, -2, 1, 10, false );
+        }
     }
 
 
@@ -1034,85 +1033,131 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     @Test
     public void testPagedSearchWrongCookie() throws Exception
     {
-        LdapNetworkConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() );
-        connection.bind( "uid=admin,ou=system", "secret" );
-
-        SearchControls controls = new SearchControls();
-        controls.setCountLimit( ( int ) LdapServer.NO_SIZE_LIMIT );
-        controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
-        PagedResults pagedSearchControl = new PagedResultsDecorator( codec );
-        pagedSearchControl.setSize( 3 );
-
-        // Loop over all the elements
-        int loop = 0;
-        List<Entry> results = new ArrayList<Entry>();
-        boolean hasUnwillingToPerform = false;
-
-        while ( true )
+        try ( LdapNetworkConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
         {
-            loop++;
-
-            EntryCursor cursor = null;
-
-            try
+            connection.bind( "uid=admin,ou=system", "secret" );
+    
+            PagedResults pagedSearchControl = new PagedResultsDecorator( codec );
+            pagedSearchControl.setSize( 3 );
+    
+            SearchRequest searchRequest = new SearchRequestImpl();
+            searchRequest.setBase( new Dn( "ou=system" ) );
+            searchRequest.setFilter( "(ObjectClass=*)" );
+            searchRequest.setScope( SearchScope.SUBTREE );
+            searchRequest.addAttributes( "*" );
+    
+            // Loop over all the elements
+            List<Entry> results = new ArrayList<>();
+            boolean hasUnwillingToPerform = false;
+    
+            while ( true )
             {
-                SearchRequest searchRequest = new SearchRequestImpl();
-                searchRequest.setBase( new Dn( "ou=system" ) );
-                searchRequest.setFilter( "(ObjectClass=*)" );
-                searchRequest.setScope( SearchScope.SUBTREE );
-                searchRequest.addAttributes( "*" );
                 searchRequest.addControl( pagedSearchControl );
 
-                cursor = new EntryCursorImpl( connection.search( searchRequest ) );
-
-                int i = 0;
-
-                while ( cursor.next() )
+                try ( EntryCursor cursor = new EntryCursorImpl( connection.search( searchRequest ) ) )
                 {
-                    Entry result = cursor.get();
-                    results.add( result );
-                    ++i;
+    
+                    while ( cursor.next() )
+                    {
+                        Entry result = cursor.get();
+                        results.add( result );
+                    }
+    
+                    SearchResultDone result = cursor.getSearchResultDone();
+                    pagedSearchControl = ( PagedResults ) result.getControl( PagedResults.OID );
+    
+                    if ( result.getLdapResult().getResultCode() == ResultCodeEnum.UNWILLING_TO_PERFORM )
+                    {
+                        hasUnwillingToPerform = true;
+                        
+                        break;
+                    }
                 }
-
-                SearchResultDone result = cursor.getSearchResultDone();
-                pagedSearchControl = ( PagedResults ) result.getControl( PagedResults.OID );
-
-                if ( result.getLdapResult().getResultCode() == ResultCodeEnum.UNWILLING_TO_PERFORM )
+    
+                // Now read the next ones
+                assertEquals( 0, pagedSearchControl.getSize() );
+    
+                // check if this is over
+                byte[] cookie = pagedSearchControl.getCookie();
+    
+                if ( Strings.isEmpty( cookie ) )
                 {
-                    hasUnwillingToPerform = true;
+                    // If so, exit the loop
                     break;
                 }
+    
+                // Prepare the next iteration, sending a bad cookie
+                pagedSearchControl.setCookie( "test".getBytes( "UTF-8" ) );
+                pagedSearchControl.setSize( 3 );
             }
-            finally
-            {
-                if ( cursor != null )
-                {
-                    cursor.close();
-                }
-            }
-
-            // Now read the next ones
-            assertEquals( 0, pagedSearchControl.getSize() );
+    
+            assertTrue( hasUnwillingToPerform );
+        }
+    }
 
-            // check if this is over
-            byte[] cookie = pagedSearchControl.getCookie();
 
-            if ( Strings.isEmpty( cookie ) )
+    /**
+     * Do a test with a paged search, reading all the entries
+     */
+    @Test
+    public void testPagedSearchAll() throws Exception
+    {
+        getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
+        
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
+        {
+            connection.bind( "cn=user,ou=system", "secret" );
+
+            // Create the control, and tell it we want 4 entries for every call
+            PagedResults pagedControl = new PagedResultsImpl();
+            pagedControl.setSize( 4 );
+        
+            // Read all the elements
+            List<Entry> results = new ArrayList<>();
+
+            // Create the SearchRequest
+            SearchRequest searchRequest = new SearchRequestImpl();
+            searchRequest.setBase( new Dn( "dc=users,ou=system" ) );
+            searchRequest.setFilter( "(cn=*)" );
+            searchRequest.setScope( SearchScope.SUBTREE );
+        
+            while ( true )
             {
-                // If so, exit the loop
-                break;
+                // Add the PagedSearch control to teh SearchRequest
+                searchRequest.addControl( pagedControl );
+                    
+                // Do the search now
+                try ( SearchCursor cursor = connection.search( searchRequest ) )
+                {
+                    // Loop on all teh entries we got back (Should be 4, or less)
+                    while ( cursor.next() )
+                    {
+                        Entry result = cursor.getEntry();
+                        results.add( result );
+                    }
+            
+                    // Now check the returned controls
+                    Map<String, Control> controls =  cursor.getSearchResultDone().getControls();
+    
+                    // We should get a PagedResult response
+                    PagedResults responseControl = ( PagedResults ) controls.get( PagedResults.OID );
+    
+                    // check if this is over, ie teh cookie is empty
+                    byte[] cookie = responseControl.getCookie();
+                    
+                    if ( Strings.isEmpty( cookie ) )
+                    {
+                        // Ok, we are done
+                        break;
+                    }
+    
+                    // Prepare the next iteration, sending a bad cookie
+                    pagedControl.setCookie( cookie );
+                }
             }
-
-            // Prepare the next iteration, sending a bad cookie
-            pagedSearchControl.setCookie( "test".getBytes( "UTF-8" ) );
-            pagedSearchControl.setSize( 3 );
+            
+            assertEquals( 10, results.size() );
         }
-
-        assertTrue( hasUnwillingToPerform );
-
-        // Cleanup the session
-        connection.unBind();
-        connection.close();
     }
 
 
@@ -1124,55 +1169,64 @@ public class PagedSearchIT extends AbstractLdapTestUnit
     public void testPagedSearchModifyingPagedLimit() throws Exception
     {
         getLdapServer().setMaxSizeLimit( LdapServer.NO_SIZE_LIMIT );
-        DirContext ctx = getWiredContext( getLdapServer(), "cn=user,ou=system", "secret" );
-        SearchControls controls = createSearchControls( ctx, ( int ) LdapServer.NO_SIZE_LIMIT, 4 );
-
-        // Loop over all the elements
-        int loop = 0;
-        List<SearchResult> results = new ArrayList<SearchResult>();
-
-        // The expected size after each loop.
-        int[] expectedSize = new int[]
-            { 4, 7, 9, 10 };
-
-        while ( true )
+        
+        try ( LdapConnection connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() ) )
         {
-            loop++;
-
-            NamingEnumeration<SearchResult> list =
-                ctx.search( "dc=users,ou=system", "(cn=*)", controls );
-
-            while ( list.hasMore() )
+            connection.bind( "cn=user,ou=system", "secret" );
+            PagedResults pagedControl = new PagedResultsImpl();
+            pagedControl.setSize( 4 );
+    
+            // Loop over all the elements
+            int loop = 0;
+            List<Entry> results = new ArrayList<>();
+    
+            // The expected size after each loop.
+            int[] expectedSize = new int[] { 4, 7, 9, 10 };
+            SearchRequest searchRequest = new SearchRequestImpl();
+            searchRequest.setBase( new Dn( "dc=users,ou=system" ) );
+            searchRequest.setFilter( "(cn=*)" );
+            searchRequest.setScope( SearchScope.SUBTREE );
+    
+            while ( true )
             {
-                SearchResult result = list.next();
-                results.add( result );
-            }
-
-            list.close();
-
-            // Now read the next ones
-            javax.naming.ldap.Control[] responseControls = ( ( LdapContext ) ctx ).getResponseControls();
-
-            PagedResultsResponseControl responseControl =
-                ( PagedResultsResponseControl ) responseControls[0];
-            assertEquals( 0, responseControl.getResultSize() );
-
-            // check if this is over
-            byte[] cookie = responseControl.getCookie();
+                loop++;
+    
+                searchRequest.addControl( pagedControl );
+                
+                try ( SearchCursor cursor = connection.search( searchRequest ) )
+                {
+                    while ( cursor.next() )
+                    {
+                        Entry result = cursor.getEntry();
+                        results.add( result );
+                    }
+        
+                    // Now read the next ones
+                    Map<String, Control> controls =  cursor.getSearchResultDone().getControls();
+    
+                    PagedResults responseControl = ( PagedResults ) controls.get( PagedResults.OID );
+                    assertEquals( 0, responseControl.getSize() );
+    
+                    // check if this is over
+                    byte[] cookie = responseControl.getCookie();
+                
+                    if ( Strings.isEmpty( cookie ) )
+                    {
+                        // If so, exit the loop
+                        break;
+                    }
+        
+                    // Prepare the next iteration, sending a bad cookie
+                    pagedControl.setSize( 4 - loop );
+                    pagedControl.setCookie( cookie );
 
-            if ( Strings.isEmpty( cookie ) )
-            {
-                // If so, exit the loop
-                break;
+                }
+    
+                assertEquals( expectedSize[loop - 1], results.size() );
             }
-
-            // Prepare the next iteration, sending a bad cookie
-            createNextSearchControls( ctx, responseControl.getCookie(), 4 - loop );
-
-            assertEquals( expectedSize[loop - 1], results.size() );
+    
+            assertEquals( 4, loop );
+            checkResults( results, 10 );
         }
-
-        assertEquals( 4, loop );
-        checkResults( results, 10 );
     }
 }
\ No newline at end of file
diff --git a/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchIT.java b/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchIT.java
index bc012de..c28ea18 100644
--- a/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchIT.java
+++ b/server-integ/src/test/java/org/apache/directory/server/operations/search/PagedSearchIT.java
@@ -70,7 +70,7 @@ import org.junit.runner.RunWith;
 /**
  * Test the PagedSearchControl. The following tables covers all the
  * possible cases for both an admin and a simple user, combining the
- * Server SizeLimit (SL), the requested SizeLimit (SL) and the paged
+ * Server SizeLimit (SL), the requested SizeLimit (RL) and the paged
  * size limit (PL). The 'X' column tells if we are supposed to receive
  * a SizeLimitExceededException.<br>
  * <br>

-- 
To stop receiving notification emails like this one, please contact
elecharny@apache.org.