public static Iterator<?> toIterator(TemplateContext ctx, Object o) { if (o == null) { return IteratorUtils.NULL_ITERATOR; } if (o instanceof Iterator<?>) { return (Iterator<?>) o; } if (o instanceof Iterable<?>) { return ((Iterable<?>) o).iterator(); } if (o instanceof Map<?, ?>) { return IteratorUtils.toIterator((Map<?, ?>) o); } if (isString(ctx, o)) { return IteratorUtils.toIterator(toString(ctx, o)); } if (isArray(ctx, o)) { return IteratorUtils.toIteratorArray(o); } if (o instanceof Enumeration<?>) { return Iterators.forEnumeration((Enumeration<?>) o); } ctx.warn("Cannot iterate over object of type " + o.getClass()); return IteratorUtils.NULL_ITERATOR; }
@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 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 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(); }
public Iterator<TreeDataModelTuple> children() { Iterator<TreeNode> children = Iterators.forEnumeration((Enumeration<TreeNode>) getData().children()); return new IterableDataTuplesIterator(getRowKey(), children); }