directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Enrique Rodriguez <enriqu...@gmail.com>
Subject Re: startup and shutdown of apache 0.9.2 programmatically
Date Wed, 28 Sep 2005 17:28:41 GMT
Gianmaria Clerici wrote:
> I am trying to start and stop apache 0.9.2 from my JUnit tests.
> 
> This is the code I have.

At the risk of not directly answering your question and just confusing 
you, here's what *I* do.  For unit tests in the protocol-providers 
(which is the same as ApacheDS in "embedded mode") I use 
CoreContextFactory.  With CoreContextFactory you are firing up the 
backend without the LDAP or Kerberos protocols and you can configure 
everything in Java, so, in your case, you'd also remove any deps on the 
Spring config and XML loading.  Of course, if you really need the 
Directory Server and not just the directory core, then the below methods 
don't apply.

The setUp line "load( ctx, "kerberos.ldif" );" is my own utility method 
for loading ldifs.  Similarly, "doDelete()" is my own 10 line recursive 
delete dir (not commons FileUtils).

So, each setUp call results in a db and 'example' partition which is 
loaded fresh with kerberos principals from my ldif.  I can then test 
against 'ctx' in my testXxx() methods.

HTH:

     public void setUp() throws Exception
     {
         Hashtable env = new Hashtable( setUpPartition() );

         env.put( Context.PROVIDER_URL, "dc=example, dc=com" );
         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
         env.put( Context.SECURITY_CREDENTIALS, "secret" );
         env.put( Context.INITIAL_CONTEXT_FACTORY, 
"org.apache.ldap.server.jndi.CoreContextFactory" );

         factory = new CoreContextFactory();

         ctx = (DirContext) factory.getInitialContext( env );

         load( ctx, "kerberos.ldif" );
     }

     private Hashtable setUpPartition()
     {
         MutableStartupConfiguration config = new 
MutableStartupConfiguration();

         MutableContextPartitionConfiguration partConfig = new 
MutableContextPartitionConfiguration();
         partConfig.setName( "example" );

         HashSet indices = new HashSet();
         indices.add( "dc" );
         indices.add( "ou" );
         indices.add( "objectClass" );
         indices.add( "krb5PrincipalName" );
         indices.add( "uid" );
         partConfig.setIndexedAttributes( indices );

         partConfig.setSuffix( "dc=example, dc=com" );

         LockableAttributesImpl attrs = new LockableAttributesImpl();
         LockableAttributeImpl attr = new LockableAttributeImpl( 
"objectClass" );
         attr.add( "top" );
         attr.add( "domain" );
         attrs.put( attr );
         attrs.put( "dc", "example" );
         partConfig.setContextEntry( attrs );

         Set schemas = new HashSet();
         schemas.add( new CoreSchema() );
         schemas.add( new CosineSchema() );
         schemas.add( new ApacheSchema() );
         schemas.add( new InetorgpersonSchema() );
         schemas.add( new Krb5kdcSchema() );
         schemas.add( new SystemSchema() );
         schemas.add( new ApachednsSchema() );
         config.setBootstrapSchemas( schemas );
         config.setContextPartitionConfigurations( 
Collections.singleton( partConfig ) );

         return config.toJndiEnvironment();
     }

     public void tearDown() throws Exception
     {
         Hashtable env = new Hashtable();

         env.put( Context.PROVIDER_URL, "ou=system" );
         env.put( Context.INITIAL_CONTEXT_FACTORY, 
CoreContextFactory.class.getName() );
         env.putAll( new ShutdownConfiguration().toJndiEnvironment() );
         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
         env.put( Context.SECURITY_CREDENTIALS, "secret" );
         env.put( Context.SECURITY_AUTHENTICATION, "simple" );

         new InitialContext( env );

         context = null;
         ldifPath = null;
         loadClass = null;
         configuration = new MutableStartupConfiguration();

         doDelete( configuration.getWorkingDirectory() );
     }

Mime
View raw message