# HG changeset patch # User Dirk Olmes # Date 1477410495 0 # Node ID f6e07a3d95a24d35c1faabb13ac5524aae12bcea # Parent 99472565ba0bcae4784744ee79027b8a4c58f308 factor out the query for the highest group number into GroupQuery diff -r 99472565ba0b -r f6e07a3d95a2 jndi/src/test/java/de/codedo/jndi/GroupQuery.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jndi/src/test/java/de/codedo/jndi/GroupQuery.java Tue Oct 25 15:48:15 2016 +0000 @@ -0,0 +1,41 @@ +package de.codedo.jndi; + +import javax.naming.*; +import javax.naming.directory.*; + +class GroupQuery extends Object +{ + private static final String GROUP_BASE = "ou=groups,dc=ldap,dc=example,dc=org"; + + private final DirContext _context; + + GroupQuery(DirContext context) + { + super(); + _context = context; + } + + public int getHighestGroupNumber() throws NamingException + { + int highest = 0; + + NamingEnumeration groupEnum = _context.list(GROUP_BASE); + while (groupEnum.hasMore()) + { + NameClassPair pair = groupEnum.next(); + int groupNumber = getGroupNumber(pair.getName()); + highest = Math.max(highest, groupNumber); + } + return highest; + } + + private int getGroupNumber(String groupCn) throws NamingException + { + String dn = groupCn + "," + GROUP_BASE; + Attributes attributes = _context.getAttributes(dn); + + Attribute gidNumber = attributes.get("gidNumber"); + String value = gidNumber.get().toString(); + return Integer.valueOf(value); + } +} diff -r 99472565ba0b -r f6e07a3d95a2 jndi/src/test/java/de/codedo/jndi/JndiTestCase.java --- a/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java Tue Oct 25 15:39:52 2016 +0000 +++ b/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java Tue Oct 25 15:48:15 2016 +0000 @@ -39,6 +39,7 @@ context.close(); } + @Ignore @Test public void createInLdap2() throws Exception { @@ -79,6 +80,17 @@ return Integer.valueOf(value); } + @Test + public void findHighestGroupNumber2() throws Exception + { + DirContext context = createContext(); + GroupQuery query = new GroupQuery(context); + int highest = query.getHighestGroupNumber(); + Assert.assertEquals(10002, highest); + + context.close(); + } + private DirContext createContext() throws Exception { Hashtable env = new Hashtable<>();