001: /* AbstractLdapValidator.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: * www.ddsteps.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License version 2.1 as published by the Free Software Foundation.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, visit
018: * http://www.opensource.org/licenses/lgpl-license.php
019: */
020:
021: package org.ddsteps.step.ldap;
022:
023: import javax.naming.NamingException;
024: import javax.naming.directory.Attributes;
025:
026: import junit.framework.AssertionFailedError;
027: import net.sf.ldaptemplate.AttributesMapper;
028: import net.sf.ldaptemplate.EntryNotFoundException;
029: import net.sf.ldaptemplate.LdapOperations;
030: import net.sf.ldaptemplate.support.DistinguishedName;
031:
032: import org.apache.commons.lang.StringUtils;
033: import org.ddsteps.step.TestStep;
034:
035: /**
036: * Provides LDAP lookup functionality. Subclasses must implement
037: * {@link #validateAttributes(Attributes)} in which the attributes of the
038: * specified DN should be matched against properties of the implementing class.
039: *
040: * @author Mattias Arthursson
041: */
042: public abstract class AbstractLdapValidator implements TestStep,
043: AttributesMapper {
044:
045: private LdapOperations ldapOperations;
046:
047: private String dn;
048:
049: public AbstractLdapValidator(LdapOperations ldapOperations) {
050: this .ldapOperations = ldapOperations;
051: }
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see org.ddsteps.step.TestStep#runStep()
057: */
058: public void runStep() throws Exception {
059: if (StringUtils.isBlank(dn)) {
060: throw new AssertionFailedError("Property DN must be set");
061: }
062: try {
063: ldapOperations.lookup(new DistinguishedName(dn), this );
064: } catch (EntryNotFoundException e) {
065: throw new AssertionFailedError("DN '" + dn
066: + "' could not be found");
067: }
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see net.sf.ldaptemplate.AttributesMapper#mapFromAttributes(javax.naming.directory.Attributes)
074: */
075: public Object mapFromAttributes(Attributes attributes)
076: throws NamingException {
077: validateAttributes(attributes);
078: return null;
079: }
080:
081: /**
082: * Validate the supplied Attributes against properties of the implementing
083: * class.
084: *
085: * @param attributes
086: * the Attributes to validate
087: * @throws NamingException
088: * If any NamingException occurs during validation.
089: */
090: protected abstract void validateAttributes(Attributes attributes)
091: throws NamingException;
092:
093: /**
094: * Set the DN of the entry to be validated.
095: *
096: * @param dn
097: * the DN of the entry to be validated.
098: */
099: public void setDn(String dn) {
100: this.dn = dn;
101: }
102: }
|