@Test
  public void oneLevelSearch() throws Exception {
    LdapSearch search =
        new LdapSearch(contextFactory)
            .setBaseDn("dc=example,dc=org")
            .setScope(SearchControls.ONELEVEL_SCOPE)
            .setRequest("(objectClass={0})")
            .setParameters("inetOrgPerson")
            .returns("cn");

    assertThat(search.getBaseDn(), is("dc=example,dc=org"));
    assertThat(search.getScope(), is(SearchControls.ONELEVEL_SCOPE));
    assertThat(search.getRequest(), is("(objectClass={0})"));
    assertThat(search.getParameters(), is(new String[] {"inetOrgPerson"}));
    assertThat(search.getReturningAttributes(), is(new String[] {"cn"}));
    assertThat(
        search.toString(),
        is(
            "LdapSearch{baseDn=dc=example,dc=org, scope=onelevel, request=(objectClass={0}), parameters=[inetOrgPerson], attributes=[cn]}"));
    assertThat(Iterators.size(Iterators.forEnumeration(search.find())), is(0));
    assertThat(search.findUnique(), nullValue());
  }
  @Test
  public void objectSearch() throws Exception {
    LdapSearch search =
        new LdapSearch(contextFactory)
            .setBaseDn("cn=bind,ou=users,dc=example,dc=org")
            .setScope(SearchControls.OBJECT_SCOPE)
            .setRequest("(objectClass={0})")
            .setParameters("uidObject")
            .returns("uid");

    assertThat(search.getBaseDn(), is("cn=bind,ou=users,dc=example,dc=org"));
    assertThat(search.getScope(), is(SearchControls.OBJECT_SCOPE));
    assertThat(search.getRequest(), is("(objectClass={0})"));
    assertThat(search.getParameters(), is(new String[] {"uidObject"}));
    assertThat(search.getReturningAttributes(), is(new String[] {"uid"}));
    assertThat(
        search.toString(),
        is(
            "LdapSearch{baseDn=cn=bind,ou=users,dc=example,dc=org, scope=object, request=(objectClass={0}), parameters=[uidObject], attributes=[uid]}"));
    assertThat(Iterators.size(Iterators.forEnumeration(search.find())), is(1));
    assertThat(search.findUnique(), not(nullValue()));
  }
  @Test
  public void subtreeSearch() throws Exception {
    LdapSearch search =
        new LdapSearch(contextFactory)
            .setBaseDn("dc=example,dc=org")
            .setRequest("(objectClass={0})")
            .setParameters("inetOrgPerson")
            .returns("objectClass");

    assertThat(search.getBaseDn(), is("dc=example,dc=org"));
    assertThat(search.getScope(), is(SearchControls.SUBTREE_SCOPE));
    assertThat(search.getRequest(), is("(objectClass={0})"));
    assertThat(search.getParameters(), is(new String[] {"inetOrgPerson"}));
    assertThat(search.getReturningAttributes(), is(new String[] {"objectClass"}));
    assertThat(
        search.toString(),
        is(
            "LdapSearch{baseDn=dc=example,dc=org, scope=subtree, request=(objectClass={0}), parameters=[inetOrgPerson], attributes=[objectClass]}"));
    assertThat(Iterators.size(Iterators.forEnumeration(search.find())), is(3));
    thrown.expect(NamingException.class);
    thrown.expectMessage("Non unique result for " + search.toString());
    search.findUnique();
  }