/* * Copyright (c) 1995-2006 levigo holding gmbh. All Rights Reserved. * * This software is the proprietary information of levigo holding gmbh Use is * subject to license terms. */ package com.levigo.tcat.test.directory; import java.util.Hashtable; import java.util.LinkedList; import java.util.List; import javax.naming.Name; import javax.naming.NameClassPair; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.ldap.LdapName; import junit.framework.TestCase; import org.apache.log4j.Logger; /** * @author Jörg Henne */ public class TestHang extends TestCase { private class MyRunner extends Thread { public Exception yikes; /* * @see java.lang.Thread#run() */ @Override public void run() { try { createAndDelete(); } catch (Exception e) { yikes = e; } } } private static final Logger logger = Logger.getLogger(TestHang.class); int count; private Hashtable env; boolean keepLingeringSubcontexts = false; int objCounter; /** * @param name * @throws NamingException */ private void create(String name) throws NamingException { InitialDirContext ctx = new InitialDirContext(getEnv()); try { BasicAttributes a = new BasicAttributes(); BasicAttribute oc = new BasicAttribute("objectClass"); String ocs[] = {"top", "organizationalUnit"}; for (String c : ocs) oc.add(c); a.put("description", "Just a test"); a.put(oc); logger.info("create: " + name); if (keepLingeringSubcontexts) ctx.createSubcontext(name, a); // yes, I know, missing close(). else ctx.bind(name, null, a); } finally { ctx.close(); } } private void createAndDelete() throws NamingException { List toBeDeleted = new LinkedList(); for (int i = 0; i < 10; i++) { int n; synchronized (this) { n = count++; } String name = "ou=someTestOU" + n; create(name); toBeDeleted.add(name); } for (String name : toBeDeleted) { delete(name); } } /** * @param name * @throws NamingException */ private void delete(String name) throws NamingException { InitialDirContext ctx = new InitialDirContext(getEnv()); try { logger.info("delete: " + name); ctx.unbind(name); } finally { ctx.close(); } } private void deleteRecursively(DirContext ctx, Name name) throws NamingException { NamingEnumeration children = ctx.list(name); try { while (children.hasMore()) { NameClassPair child = children.next(); name.add(child.getName()); deleteRecursively(ctx, name); name.remove(name.size() - 1); } } finally { children.close(); } logger.info("del " + name); if (name.size() > 0) ctx.unbind(name); } /** * @return */ private Hashtable getEnv() { if (null == env) { env = new Hashtable(); env .put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); env.put("java.naming.provider.url", "ldap://localhost:389/ou=unittest,dc=ceptix,dc=com"); env.put("java.naming.security.authentication", "none"); env.put("com.sun.jndi.ldap.connect.pool", "true"); } return env; } /* * @see junit.framework.TestCase#setUp() */ @Override protected void setUp() throws Exception { super.setUp(); InitialDirContext ctx = new InitialDirContext(getEnv()); deleteRecursively(ctx, new LdapName("")); ctx.close(); keepLingeringSubcontexts = false; } public void testMultiThreaded() throws Exception { List threads = new LinkedList(); for (int i = 0; i < 10; i++) { MyRunner r = new MyRunner(); r.start(); threads.add(r); } for (MyRunner r : threads) { r.join(); if (r.yikes != null) throw r.yikes; } } public void xtestSingleThreaded() throws Exception { for (int i = 0; i < 100; i++) { createAndDelete(); } } public void xtestSingleThreadedKeepLingeringCtx() throws Exception { keepLingeringSubcontexts = true; for (int i = 0; i < 100; i++) { createAndDelete(); } } }