All Downloads are FREE. Search and download functionalities are using the official Maven repository.

iutest.net.sf.ldaptemplate.LdapTemplateBaseSuffixITest Maven / Gradle / Ivy

Go to download

Java framework to simplify LDAP operations, based on the pattern of Spring's JdbcTemplate. Relieves the users of the burden of looking up and closing contexts, looping through NamingEnumerations, encoding/decoding values and filters, and more.

There is a newer version: 1.0.2
Show newest version
/*
 * Copyright 2002-2005 the original author or authors.
 *
 * 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.
 */

package net.sf.ldaptemplate;

import net.sf.ldaptemplate.support.CountSearchResultCallbackHandler;
import net.sf.ldaptemplate.support.DirContextAdapter;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

/**
 * Tests to verify that setting a base suffix on the ContextSource (as defined
 * in ldapTemplateBaseSuffixTestContext.xml) works as expected.
 * 
 * NOTE: This test will not work under Java 1.4.1 or earlier.
 * 
 * @author Mattias Arthursson
 */
public class LdapTemplateBaseSuffixITest extends
        AbstractDependencyInjectionSpringContextTests {
    private LdapTemplate tested;

    protected String[] getConfigLocations() {
        return new String[] { "/conf/ldapTemplateBaseSuffixTestContext.xml" };
    }

    /**
     * This method depends on a DirObjectFactory ({@link net.sf.ldaptemplate.support.DefaultDirObjectFactory})
     * being set in the ContextSource.
     */
    public void testLookup_Plain() {
        DirContextAdapter result = (DirContextAdapter) tested
                .lookup("cn=Some Person2, ou=company1, c=Sweden");

        assertEquals("Some Person2", result.getStringAttribute("cn"));
        assertEquals("Person2", result.getStringAttribute("sn"));
        assertEquals("Sweden, Company1, Some Person2", result
                .getStringAttribute("description"));
        assertEquals("cn=Some Person2, ou=company1, c=Sweden", result.getDn()
                .toString());
    }
    
    public void testSearch_Plain(){
        CountSearchResultCallbackHandler handler = new CountSearchResultCallbackHandler();

        tested.search("", "(objectclass=person)", handler);
        assertEquals(5, handler.getNoOfRows());
    }

    public void testBindAndUnbind_Plain() {
        DirContextAdapter adapter = new DirContextAdapter();
        adapter.setAttributeValues("objectclass", new String[] { "top",
                "person" });
        adapter.setAttributeValue("cn", "Some Person4");
        adapter.setAttributeValue("sn", "Person4");
        tested.bind("cn=Some Person4, ou=company1, c=Sweden", adapter, null);

        DirContextAdapter result = (DirContextAdapter) tested
                .lookup("cn=Some Person4, ou=company1, c=Sweden");

        assertEquals("Some Person4", result.getStringAttribute("cn"));
        assertEquals("Person4", result.getStringAttribute("sn"));
        assertEquals("cn=Some Person4, ou=company1, c=Sweden", result.getDn()
                .toString());

        tested.unbind("cn=Some Person4, ou=company1, c=Sweden");
        try {
            tested.lookup("cn=Some Person4, ou=company1, c=Sweden");
            fail("EntryNotFoundException expected");
        } catch (EntryNotFoundException expected) {
            assertTrue(true);
        }
    }

    public void setTested(LdapTemplate tested) {
        this.tested = tested;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy