01: /* TeardownLdapFixtureOperation.java
02: *
03: * DDSteps - Data Driven JUnit Test Steps
04: * Copyright (C) 2005 Jayway AB
05: * www.ddsteps.org
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License version 2.1 as published by the Free Software Foundation.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, visit
18: * http://www.opensource.org/licenses/lgpl-license.php
19: */
20:
21: package org.ddsteps.fixture.ldap.support;
22:
23: import java.util.Iterator;
24:
25: import net.sf.ldaptemplate.BadLdapGrammarException;
26: import net.sf.ldaptemplate.EntryNotFoundException;
27: import net.sf.ldaptemplate.LdapOperations;
28: import net.sf.ldaptemplate.support.DistinguishedName;
29:
30: import org.apache.commons.lang.StringUtils;
31: import org.apache.commons.logging.Log;
32: import org.apache.commons.logging.LogFactory;
33: import org.ddsteps.dataset.DataRow;
34: import org.ddsteps.dataset.DataValue;
35: import org.springframework.dao.DataIntegrityViolationException;
36:
37: /**
38: * LdapFixtureOperation for removing data. If a requested row is not found when
39: * trying to remove it, the exception is ignored. However, malformed DN:s are
40: * not tolerated.
41: *
42: * @author Mattias Arthursson
43: */
44: public class TeardownLdapFixtureOperation extends
45: AbstractLdapFixtureOperation {
46:
47: private static Log log = LogFactory
48: .getLog(TeardownLdapFixtureOperation.class);
49:
50: /*
51: * (non-Javadoc)
52: *
53: * @see org.ddsteps.fixture.ldap.LdapFixtureOperation#handleRow(net.sf.ldaptemplate.LdapOperations,
54: * org.ddsteps.dataset.DataRow)
55: */
56: public void handleRow(LdapOperations ldapOperations, DataRow dataRow) {
57: for (Iterator iter = dataRow.iterator(); iter.hasNext();) {
58: DataValue dataValue = (DataValue) iter.next();
59: if (StringUtils.equals(dataValue.getName(),
60: getDnColumnName())) {
61: Object value = dataValue.getValue();
62: log.info("Removing entry " + value);
63: if (value instanceof String) {
64: try {
65: ldapOperations.unbind(new DistinguishedName(
66: (String) value));
67: } catch (EntryNotFoundException e) {
68: log.info("Entry with DN " + value
69: + " not found");
70: }
71: return;
72: } else {
73: throw new BadLdapGrammarException(
74: "Invalid Distinguished Name: "
75: + "Must be a valid LDAP DN but was :"
76: + value);
77: }
78: }
79: }
80:
81: // If we get here, no DN was specified.
82: throw new DataIntegrityViolationException(
83: "A Distinguished Name must be supplied in column '"
84: + getDnColumnName() + "'");
85: }
86: }
|