Emmanuel Lecharny wrote:
On 1/31/07, Pierre-Alain RIVIERE <[EMAIL PROTECTED]> wrote:
Anyway, that's always good for us :)
<snip/>
Hmmmm... Is it possible you copy/paste a full unit test so that I can
try it
on my computer ? If you want to know how we do our integration tests,
have a
look at :
http://svn.apache.org/viewvc/directory/apacheds/trunk/core-unit/src/test/java/org/apache/directory/server/core/jndi/SearchContextITest.java?revision=499512&view=markup
I've attached to the mail the AbstractTestCase for all my unit tests
that needs access to an LDAP server.
In fact, when I started working on ApacheDS integration in my unit test,
I've tried first to use the AbstractServerTest class coming from
server-unit. But at this moment I did not succeed to make my own
customization - setting up a new partition -. So I wrote this class. And
yes, it's pretty slow to process partition initialization and starting
up the server for each unit test.
I've also joined a test.
getUserGroups(LDAPUser) just generates a search filter like this
"(&(member=cn=Pierre-Alain
RIVIERE,ou=Peoples,ou=Paris,ou=Offices,dc=ippon,dc=fr)(objectClass=ipponGroup))"
or this
"(&(member=cn=Stéphane
DUPONT,ou=Peoples,ou=Paris,ou=Offices,dc=ippon,dc=fr)(objectClass=ipponGroup))"
and then pass it to
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results =
context.search(baseDN, searchFilter, controls);
where context is given by the IpponAbstractServerTest.
I've not really test if the things goes well if an ipponGroup
objectClass contains only member attribute's values with not problematic
characters (é for example), but from what I've seen with the debugger it
should be OK - when there's no problematic characters a String is passed
to DnNormalizer#normalize(Object) method, in the other case it's a byte[].
<snip/>
I can't guarantee it 100% without being able to reproduce the test by
myself. The best I can do is to test your test and see what can be the
problem. Sorry for the burden.
Btw, which version of the server are you currently using ?
I'm using 1.0.0 version available from the maven repository
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-core</artifactId>
<version>1.0.0</version>
</dependency>
package fr.ippon.admin.dao.ldap;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import org.apache.directory.server.configuration.MutableServerStartupConfiguration;
import org.apache.directory.server.core.DirectoryService;
import org.apache.directory.server.core.configuration.PartitionConfiguration;
import org.apache.directory.server.core.partition.Partition;
import org.apache.directory.server.core.partition.impl.btree.MutableBTreePartitionConfiguration;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
import org.apache.directory.server.jndi.ServerContextFactory;
import org.apache.directory.shared.ldap.exception.LdapConfigurationException;
import org.apache.directory.shared.ldap.ldif.Entry;
import org.apache.directory.shared.ldap.ldif.LdifReader;
import fr.ippon.admin.ldap.schema.IpponSchema;
/**
*
* @author pariviere
*/
public class IpponAbstractServerTest extends TestCase {
private MutableServerStartupConfiguration configuration =
new MutableServerStartupConfiguration();
private int port = 10389;
private DirectoryService service = DirectoryService.getInstance();
private LdapContext context = null;
private String workingDirPath = "target/apacheds/";
public IpponAbstractServerTest() {
try {
doDelete(new File(workingDirPath));
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
protected void setUp() throws Exception {
setUpServer();
setUpIpponPartition();
service.startup(new ServerContextFactory(), configuration.toJndiEnvironment());
setUpContext();
}
/**
*
* @throws Exception
*/
protected void loadDefaultData() throws Exception {
importLdif(new File("src/test/resources/ippon.ldif"));
}
/**
* Import a LDIF file into the directory
*
* @param ldif the LDIF file to import
* @throws Exception
*/
protected void importLdif(File ldif) throws Exception {
try {
Iterator iterator = new LdifReader(ldif);
while ( iterator.hasNext() ) {
Entry entry = (Entry) iterator.next();
String dn = entry.getDn();
context.createSubcontext(dn, entry.getAttributes());
}
} catch (Exception ex) {
String msg = "failed while trying to parse system ldif file";
NamingException ne = new LdapConfigurationException(msg);
ne.setRootCause(ex);
throw ne;
}
}
/**
* TODO bad algorithm
* @param base
* @throws Exception
*/
protected void deleteFrom(String base) throws Exception {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = context.search(base, "(objectclass=*)", controls);
Set<String> dnsToDelete = new HashSet<String>();
while (results.hasMore()) {
SearchResult result = results.next();
dnsToDelete.add(result.getName());
}
for(String dn : dnsToDelete) {
try {
context.destroySubcontext(dn);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* Setup the context that will be used by the test.
*
* @throws Exception
*/
protected void setUpContext() throws Exception {
Hashtable<String, String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName());
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
///
/// Don't understand why this line sucks with the embedded ApacheDS :
/// env.put(Context.PROVIDER_URL, "ldap://localhost:" + port +"/dc=ippon,dc=fr");
env.put(Context.PROVIDER_URL, "");
context = new InitialLdapContext(env, null);
}
protected LdapContext getContext() {
return context;
}
@SuppressWarnings("unchecked")
protected void setUpServer() throws Exception {
configuration.setWorkingDirectory(new File(workingDirPath));
configuration.setLdapPort(port);
configuration.setShutdownHookEnabled(true);
///
/// add definition of Ippon's objectClassess
///
Set schemas = configuration.getBootstrapSchemas();
schemas.add(new IpponSchema());
configuration.setBootstrapSchemas(schemas);
}
/**
* Add ippon partition - the one who contains ippon schema (dc=ippon,dc=fr) -
* to the ApacheDS instance
* @throws Exception
*/
protected void setUpIpponPartition() throws Exception {
MutableBTreePartitionConfiguration ipponConfiguration
= new MutableBTreePartitionConfiguration();
Partition partition = new JdbmPartition();
ipponConfiguration.setContextPartition(partition);
ipponConfiguration.setName("ippon");
ipponConfiguration.setSuffix("dc=ippon,dc=fr");
Attributes entry = new BasicAttributes();
entry.put("dn", "dc=ippon,dc=fr");
entry.put("objectClass", "top");
entry.put("objectClass", "domain");
entry.put("objectClass", "extensibleObject");
entry.put("dc", "ippon");
entry.put("o", "Ippon Technologies");
ipponConfiguration.setContextEntry(entry);
Set<PartitionConfiguration> configurations = new HashSet<PartitionConfiguration>(1);
configurations.add(ipponConfiguration);
configuration.setContextPartitionConfigurations(configurations);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
service.shutdown();
doDelete(configuration.getWorkingDirectory());
}
/**
* Delete files generated by ApacheDS
*
* @param wkdir
* @throws IOException
*/
protected void doDelete(File wkdir) throws IOException {
if (wkdir.exists()) {
FileUtils.deleteDirectory(wkdir);
}
if (wkdir.exists()) {
throw new IOException("Failed to delete: " + wkdir);
}
}
}
package fr.ippon.admin.dao.ldap;
import java.util.HashSet;
import java.util.Set;
import fr.ippon.admin.dao.ldap.util.LDAPDAOUtil;
import fr.ippon.admin.model.ldap.LDAPGroup;
import fr.ippon.admin.model.ldap.LDAPOffice;
import fr.ippon.admin.model.ldap.LDAPUser;
public class GroupLDAPDAOTest extends IpponAbstractServerTest {
GroupLDAPDAO groupDAO = null;
@Override
protected void setUp() throws Exception {
super.setUp();
loadDefaultData();
groupDAO = new GroupLDAPDAO();
LDAPDAOUtil serviceUtil = new LDAPDAOUtil();
serviceUtil.setContext(getContext());
groupDAO.setDaoUtil(serviceUtil);
}
public void testGetUserGroups() throws Exception {
LDAPUser referenceUser = new LDAPUser();
Set<String> mails = new HashSet<String>();
mails.add("[EMAIL PROTECTED]");
LDAPOffice paris = new LDAPOffice();
paris.setCity("Paris");
referenceUser.setOffice(paris);
referenceUser.setMails(mails);
referenceUser.setName("Pierre-Alain");
referenceUser.setSurname("RIVIERE");
referenceUser.setLogin("pariviere");
Set<LDAPGroup> groups = groupDAO.getUserGroups(referenceUser);
assertEquals(3, groups.size());
}
}