controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
NamingEnumeration<SearchResult> list = ctx.search( "ou=system", "(objectClass=*)", controls );
Map<String, SearchResult> results = new HashMap<String, SearchResult>();
while ( list.hasMore() )
{
SearchResult result = list.next();
results.put( result.getName(), result );
}
assertNotNull( results.get( "ou=users" ) );
// -------------------------------------------------------------------
// Now we will throw exceptions when searching for referrals
// -------------------------------------------------------------------
ctx.addToEnvironment( Context.REFERRAL, "throw" );
list = ctx.search( "ou=system", "(objectClass=*)", controls );
results = new HashMap<String, SearchResult>();
try
{
while ( list.hasMore() )
{
SearchResult result = list.next();
results.put( result.getName(), result );
}
}
catch ( ReferralException e )
{
// As we use the uuidIndex the order of search continuations returned by
// the server is not deterministic. So we collect all referrals first into
// an hashset and check afterwards if the expected URLs are included.
Set<Object> s = new HashSet<Object>();
s.add( e.getReferralInfo() );
while ( e.skipReferral() )
{
try
{
Context ctx2 = e.getReferralContext();
ctx2.list( "" );
}
catch ( NamingException ne )
{
if ( ne instanceof ReferralException )
{
e = ( ReferralException ) ne;
s.add( e.getReferralInfo() );
}
else
{
break;
}
}
}
assertEquals( 5, s.size() );
assertTrue( s.contains( "ldap://fermi:10389/ou=users,ou=system??sub" ) );
assertTrue( s.contains( "ldap://hertz:10389/ou=users,dc=example,dc=com??sub" ) );
assertTrue( s.contains( "ldap://maxwell:10389/ou=users,ou=system??sub" ) );
}
assertNull( results.get( "ou=remoteusers" ) );
list.close();
// try again but this time with single level scope
controls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
list = ctx.search( "ou=system", "(objectClass=*)", controls );
results = new HashMap<String, SearchResult>();
try
{
while ( list.hasMore() )
{
SearchResult result = list.next();
results.put( result.getName(), result );
}
}
catch ( ReferralException e )
{
assertEquals( "ldap://fermi:10389/ou=users,ou=system??base", e.getReferralInfo() );