Description
package org.apache.tuscany.das.ldap.prototype.learning;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import org.apache.directory.server.core.configuration.Configuration;
import org.apache.directory.server.core.configuration.MutablePartitionConfiguration;
import org.apache.directory.server.core.configuration.MutableStartupConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import junit.framework.TestCase;
public class LearningTest extends TestCase
{
private static final Logger log = LoggerFactory.getLogger( "partition" );
// Create the mutable configuration
MutableStartupConfiguration configuration = new MutableStartupConfiguration();
// Create the partition configuration to fill in
MutablePartitionConfiguration partition = new MutablePartitionConfiguration();
public void testDynamicPartitioning()
throws NamingException
{
// Get the start time
long startTime = System.currentTimeMillis();
partition.setName( "das" );
partition.setSuffix( "ou=das" );
Attributes suffixAttributes = new BasicAttributes();
suffixAttributes.put( "objectClass", "top");
suffixAttributes.get( "objectClass" ).add( "organizationalUnit" );
suffixAttributes.put( "ou", "das" );
partition.setContextEntry( suffixAttributes );
// Add the new partition configuration to the configuration bean
Set partitions = new HashSet();
partitions.add( partition );
configuration.setContextPartitionConfigurations( partitions );
// ------------------------------------------------------------
// Setup environment using the configuration with extra partition
// ------------------------------------------------------------
Hashtable env = new Hashtable();
// Notice that the provider URL is not really a URL but a DN
// This time instead of ou=system we're accessing the apachecon partition
env.put( Context.PROVIDER_URL, "ou=das" );
// User the ApacheDS core InitialContextFactory implementation
env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.directory.server.core.jndi.CoreContextFactory" );
// Fixed uber admin user is always present and initial password is set to "secret"
env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
env.put( Context.SECURITY_CREDENTIALS, "secret" );
env.put( Context.SECURITY_AUTHENTICATION, "simple" );
// Put extended configuration into environment
env.put( Configuration.JNDI_KEY, configuration );
// Startup the server core by getting the first InitialDirContext.
DirContext ictx = new InitialDirContext( env );
ictx.destroySubcontext( "cn=test" );
ictx.createSubcontext( "cn=test" );
System.out.println( "Embedded ApacheDS started in " + ( System.currentTimeMillis() - startTime ) + " milliseconds" );
DirContext jenny = null;
try
catch ( NameNotFoundException e2 )
{ // Create a new entry under ou=system since jenny does not // already exist. System.out.println( "jenny does not exist" ); Attributes jennyAttributes = new BasicAttributes( "objectClass", "top", true ); jennyAttributes.get( "objectClass" ).add( "person" ); jennyAttributes.put( "sn", "Tutone" ); jennyAttributes.put( "cn", "Jenny Tutone" ); jennyAttributes.put( "telephoneNumber", "867-5309" ); jenny = ( DirContext ) ictx.createSubcontext( "cn=Jenny Tutone", jennyAttributes ); System.out.println( "created " + jenny.getNameInNamespace() ); } Attribute number = jenny.getAttributes( "" ).get( "telephoneNumber" );
System.out.println( "Jenny's phone number is " + number.get() );
}
private DirContext connectWithApacheContextFactory() throws NamingException
{ Hashtable<String,String> env = new Hashtable<String, String>(); env.put( "java.naming.factory.initial", "org.apache.directory.server.core.jndi.CoreContextFactory" ); env.put( "java.naming.provider.url", "ldap://localhost:10389/ou=das"); env.put( "java.naming.security.authentication", "simple"); env.put( "java.naming.security.principal", "uid=admin,ou=system" ); env.put( "java.naming.security.credentials", "secret" ); return new InitialDirContext(env); }private DirContext connectWithSunContextFactory() throws NamingException
{ Hashtable<String,String> env = new Hashtable<String, String>(); env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" ); env.put( "java.naming.provider.url", "ldap://localhost:10389/ou=das"); env.put( "java.naming.security.authentication", "simple"); env.put( "java.naming.security.principal", "uid=admin,ou=system" ); env.put( "java.naming.security.credentials", "secret" ); return new InitialDirContext(env); } public void testHotPartitionConnectWithApacheContextFactory()
throws NamingException
{
DirContext directoryContext = null;
try
catch (Exception e)
{
}
assertNull(directoryContext);
}
public void testHotPartitionConnectWithSunContextFactory()
throws NamingException
public void testHotPartitionEntryCreation()
throws NamingException
{
DirContext directoryContext = null;
directoryContext = connectWithSunContextFactory();
boolean exceptionOccured = false;
try
catch (Exception e)
{ exceptionOccured = true; } assertTrue(exceptionOccured);
directoryContext.close();
}
}