changeset 24:f6e07a3d95a2

factor out the query for the highest group number into GroupQuery
author Dirk Olmes <dirk.olmes@codedo.de>
date Tue, 25 Oct 2016 15:48:15 +0000
parents 99472565ba0b
children ec5b9cbb518c
files jndi/src/test/java/de/codedo/jndi/GroupQuery.java jndi/src/test/java/de/codedo/jndi/JndiTestCase.java
diffstat 2 files changed, 53 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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<NameClassPair> 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);
+	}
+}
--- 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<String, Object> env = new Hashtable<>();