# HG changeset patch # User Dirk Olmes # Date 1477409992 0 # Node ID 99472565ba0bcae4784744ee79027b8a4c58f308 # Parent 886a28c5898cf0b55ea505b8cac6ebcb14d05fb4 Extract the code to create a new user to the User object diff -r 886a28c5898c -r 99472565ba0b jndi/src/test/java/de/codedo/jndi/JndiTestCase.java --- a/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java Tue Oct 25 15:21:13 2016 +0000 +++ b/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java Tue Oct 25 15:39:52 2016 +0000 @@ -13,35 +13,6 @@ @Ignore @Test - public void connectToJndi() throws Exception - { - DirContext context = createContext(); - - NamingEnumeration enumeration = context.list(USER_BASE); - while (enumeration.hasMore()) - { - NameClassPair pair = enumeration.next(); - - String dn = pair.getName() + "," + USER_BASE; - LdapContext ldapContext = (LdapContext) context.lookup(dn); - System.out.println(ldapContext); - - /* - Attributes attributes = context.getAttributes(dn); - NamingEnumeration attributesEnum = attributes.getAll(); - while (attributesEnum.hasMore()) - { - Attribute attribute = (Attribute)attributesEnum.next(); - System.out.println(attribute.getID()); - } - */ - } - - context.close(); - } - - @Ignore - @Test public void createInLdap() throws Exception { DirContext context = createContext(); @@ -69,6 +40,16 @@ } @Test + public void createInLdap2() throws Exception + { + DirContext context = createContext(); + User user = new User("psb", "Fernand", "Pirschelbär"); + user.create(context); + context.close(); + } + + @Ignore + @Test public void findHighestGroupNumber() throws Exception { DirContext context = createContext(); diff -r 886a28c5898c -r 99472565ba0b jndi/src/test/java/de/codedo/jndi/User.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jndi/src/test/java/de/codedo/jndi/User.java Tue Oct 25 15:39:52 2016 +0000 @@ -0,0 +1,48 @@ +package de.codedo.jndi; + +import javax.naming.*; +import javax.naming.directory.*; + +class User extends Object +{ + private static final String USER_BASE = "ou=users,dc=ldap,dc=example,dc=org"; + + private Attribute _cn; + private Attribute _uid; + private Attribute _sn; + private Attribute _givenName; + + User(String login, String givenName, String surname) + { + super(); + _uid = new BasicAttribute("uid", login); + _cn = new BasicAttribute("cn", login); + _sn = new BasicAttribute("sn", surname); + _givenName = new BasicAttribute("givenName", givenName); + } + + public void create(DirContext context) throws NamingException + { + Attribute objectClass = createObjectClass(); + + Attributes attributes = new BasicAttributes(); + attributes.put(objectClass); + attributes.put(_cn); + attributes.put(_uid); + attributes.put(_sn); + attributes.put(_givenName); + + String dn = "uid=" + _uid.get() + "," + USER_BASE; + context.createSubcontext(dn, attributes); + } + + private Attribute createObjectClass() + { + Attribute objectClass = new BasicAttribute("objectClass"); + objectClass.add("inetOrgPerson"); + objectClass.add("organizationalPerson"); + objectClass.add("person"); + objectClass.add("top"); + return objectClass; + } +}