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 )
{
loop++;
EntryCursor cursor = null;
try
{
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() )
{
Entry result = cursor.get();
results.add( result );
++i;
}
SearchResultDone result = cursor.getSearchResultDone();
pagedSearchControl = ( PagedResults ) result.getControl( PagedResults.OID );
if ( result.getLdapResult().getResultCode() == ResultCodeEnum.UNWILLING_TO_PERFORM )
{
hasUnwillingToPerform = true;
break;
}
}
finally
{
if ( cursor != null )
{
cursor.close();
}
}
// Now read the next ones
assertEquals( 0, pagedSearchControl.getSize() );
// check if this is over
byte[] cookie = pagedSearchControl.getCookie();
if ( Strings.isEmpty( cookie ) )
{
// If so, exit the loop
break;
}
// Prepare the next iteration, sending a bad cookie
pagedSearchControl.setCookie( "test".getBytes( "UTF-8" ) );
pagedSearchControl.setSize( 3 );
}
assertTrue( hasUnwillingToPerform );
// Cleanup the session