/**
 *
 * Copyright 2003-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.File;
import java.util.Properties;
import java.util.HashSet;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.ldap.server.configuration.MutableServerStartupConfiguration;
import org.apache.ldap.server.jndi.ServerContextFactory;


/**
 * LDAP server test. Inspired by org.apache.geronimo.directory.RunningTest and
 * org.apache.geronimo.directory.DirectoryGBean.
 */
public class MyApacheDSTest {

    private static final String AUTHENTICATION = "simple";
    private static final String PRINCIPAL = "uid=admin,ou=system";
    private static final String CREDENTIALS = "secret";
    private static final String WORK_DIR = "c:/temp";
    
    private Context context;

    public void setUp() throws Exception {
        log("Starting LDAP Directory service");

        MutableServerStartupConfiguration startup =
                new MutableServerStartupConfiguration();
 
        startup.setWorkingDirectory(new File(WORK_DIR));
        startup.setAllowAnonymousAccess(true);
        startup.setLdapPort(9389);
        startup.setEnableNetworking(true);

        Properties env = new Properties();
        env.putAll(startup.toJndiEnvironment());
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                ServerContextFactory.class.getName());

        String providerURL = "ou=system";
        env.put( Context.PROVIDER_URL, providerURL);
        env.put( Context.SECURITY_AUTHENTICATION, AUTHENTICATION);
        env.put( Context.SECURITY_PRINCIPAL, PRINCIPAL);
        env.put( Context.SECURITY_CREDENTIALS, CREDENTIALS);

        //Fire it up
        context = new InitialDirContext( env );
        log("LDAP Directory service started.");
    }

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

        log("in testMe()");
        env.put(Context.PROVIDER_URL, "ldap://localhost:9389");
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        //env.put( Context.SECURITY_AUTHENTICATION, "simple");
        env.put( Context.SECURITY_PRINCIPAL, PRINCIPAL);
        env.put( Context.SECURITY_CREDENTIALS, CREDENTIALS);
        DirContext ctx = new InitialDirContext(env);

        // Perform search using URL
        // NamingEnumeration answer = ctx.search(
        // "ldap://localhost:389/ou=system", "(uid=admin)", null);
        HashSet set = new HashSet();

        NamingEnumeration list = ctx.list("ou=system");
        log("exiting testMe");
    }

    private static void log(String s) {
        System.out.println(s);
    }

    public static void main(String argv[]) throws Exception {
        MyApacheDSTest test = new MyApacheDSTest();

        test.setUp();
        test.testMe();
        log("Test PASSED");
        System.exit(0);
    }
}
