Code example for SearchControls

Methods: setSearchScope

0
        Attributes read = ctx.getAttributes( "cn=foobar,ou=system" );
        assertNotNull( read );
        assertEquals( "3", read.get( "pwdLastSet" ).get() );
 
        // Now search for foobar with pwdLastSet value of 3 
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
        NamingEnumeration<SearchResult> results = ctx.search( "ou=system", "(pwdLastSet=3)", searchControls );
        assertTrue( results.hasMore() );
        SearchResult result = results.next();
        assertNotNull( result );
        assertEquals( "cn=foobar", result.getName() );
        Attributes attributes = result.getAttributes();
        assertEquals( "3", attributes.get( "pwdLastSet" ).get() );
        results.close();
 
        // Now search with bogus value for pwdLastSet 
        results = ctx.search( "ou=system", "(pwdLastSet=300)", searchControls );
        assertFalse( results.hasMore() );
        results.close();
    }