001: /* LdapFixture.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.fixture.ldap;
022:
023: import java.util.Iterator;
024:
025: import net.sf.ldaptemplate.LdapOperations;
026:
027: import org.apache.commons.collections.iterators.FilterIterator;
028: import org.ddsteps.dataset.DataRow;
029: import org.ddsteps.dataset.DataSet;
030: import org.ddsteps.dataset.DataTable;
031: import org.ddsteps.dataset.decorator.TableNameToTablePredicateAdapter;
032: import org.ddsteps.dataset.decorator.util.StringInArrayPredicate;
033: import org.ddsteps.fixture.Fixture;
034: import org.ddsteps.fixture.ldap.support.LdapFixtureOperation;
035: import org.ddsteps.fixture.ldap.support.ReverseIterator;
036:
037: /**
038: * Fixture for LDAP. Works against a LdapOperations instance (LdapTemplate).
039: *
040: * Since we don't have any transactions here in the LDAP world the table order
041: * is significant; an entry of a specific type to be created may depend on a
042: * previously created one of another type. Tables are set up in the orderthey
043: * are defined in the data sheet. Similarly, when tearing down the fixture it
044: * might not be possible to remove an entry that has sub-entries also contained
045: * in the fixture, so this needs to be done in the reverse order.
046: *
047: * @author Mattias Arthursson
048: */
049: public class LdapFixture implements Fixture {
050:
051: private static final long serialVersionUID = 275497968628676690L;
052:
053: private DataSet dataSet;
054:
055: private LdapOperations ldapOperations;
056:
057: private String[] tableNames;
058:
059: LdapFixtureOperation getSetupLdapFixtureOperation() {
060: return LdapFixtureOperation.LDAP_FIXTURE_OPERATION_SET_UP;
061: }
062:
063: LdapFixtureOperation getTeardownLdapFixtureOperation() {
064: return LdapFixtureOperation.LDAP_FIXTURE_OPERATION_TEAR_DOWN;
065: }
066:
067: /**
068: * Construct a new LdapFixture.
069: *
070: * @param dataSet
071: * the DataSet to operate on
072: * @param ldapOperations
073: * the LdapOperations instance to work on.
074: */
075: public LdapFixture(DataSet dataSet, LdapOperations ldapOperations) {
076: this .dataSet = dataSet;
077: this .ldapOperations = ldapOperations;
078: }
079:
080: /*
081: * (non-Javadoc)
082: *
083: * @see org.ddsteps.fixture.Fixture#setUp()
084: */
085: public void setUp() throws Exception {
086: handleDataSet(getTableIterator(),
087: getSetupLdapFixtureOperation());
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.ddsteps.fixture.Fixture#tearDown()
094: */
095: public void tearDown() throws Exception {
096: // Tear down the elements in reverse order.
097: ReverseIterator reverseIterator = new ReverseIterator(
098: getTableIterator());
099: handleDataSet(reverseIterator,
100: getTeardownLdapFixtureOperation());
101: }
102:
103: protected void handleDataSet(Iterator tableIterator,
104: LdapFixtureOperation operation) {
105: while (tableIterator.hasNext()) {
106: DataTable table = (DataTable) tableIterator.next();
107: handleTable(table, operation);
108: }
109: }
110:
111: protected void handleTable(DataTable table,
112: LdapFixtureOperation operation) {
113: for (Iterator iter = table.rowIterator(); iter.hasNext();) {
114: DataRow row = (DataRow) iter.next();
115: operation.handleRow(ldapOperations, row);
116: }
117: }
118:
119: protected Iterator getTableIterator() {
120: if (tableNames != null && tableNames.length != 0) {
121: return new FilterIterator(dataSet.tableIterator(),
122: new TableNameToTablePredicateAdapter(
123: new StringInArrayPredicate(tableNames)));
124: } else {
125: return dataSet.tableIterator();
126: }
127: }
128:
129: public void setTableNames(String[] tableNames) {
130: this.tableNames = tableNames;
131: }
132: }
|