Moving of persons from country or company now works. Also added transactions.

This commit is contained in:
Mattias Arthursson
2007-05-12 12:58:54 +00:00
parent b406d83cac
commit 64b31b268f
17 changed files with 359 additions and 159 deletions

View File

@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<ivy-module version="1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.jayasoft.org/misc/ivy/samples/ivy.xsd">
<info organisation="org.springframework" module="spring-ldap-person-sample" />
<info organisation="org.springframework"
module="spring-ldap-person-sample" />
<configurations>
<conf name="default" extends="global" />
@@ -14,81 +16,106 @@
<dependencies defaultconf="global->default">
<!-- global dependencies -->
<dependency org="commons-logging" name="commons-logging" rev="1.0.4" />
<dependency org="commons-logging" name="commons-logging"
rev="1.0.4" />
<dependency org="log4j" name="log4j" rev="1.2.9" />
<dependency org="oro" name="oro" rev="2.0.8" />
<dependency org="taglibs" name="standard" rev="1.1.2" />
<dependency org="jstl" name="jstl" rev="1.1.2" />
<dependency org="org.springframework" name="spring-ldap" rev="latest.integration" conf="global->default;source->@" />
<dependency org="org.springframework" name="spring-web" rev="2.0.4" />
<dependency org="org.springframework" name="spring-webmvc" rev="2.0.4" />
<dependency org="org.springframework" name="spring-webflow" rev="1.0.2" />
<dependency org="org.springframework" name="spring-webflow-src" rev="1.0.2" conf="source->source">
<dependency org="org.springframework" name="spring-ldap"
rev="latest.integration" conf="global->default;source->@" />
<dependency org="org.springframework" name="spring-web"
rev="2.0.4" />
<dependency org="org.springframework" name="spring-webmvc"
rev="2.0.4" />
<dependency org="org.springframework" name="spring-webflow"
rev="1.0.2" />
<dependency org="org.springframework" name="spring-webflow-src"
rev="1.0.2" conf="source->source">
<artifact name="spring-webflow-src" type="zip" ext="zip" />
</dependency>
<dependency org="org.springframework" name="spring-binding" rev="1.0.2" />
<dependency org="org.springframework" name="spring-binding-src" rev="1.0.2" conf="source->source">
<dependency org="org.springframework" name="spring-binding"
rev="1.0.2" />
<dependency org="org.springframework" name="spring-binding-src"
rev="1.0.2" conf="source->source">
<artifact name="spring-binding-src" type="zip" ext="zip" />
</dependency>
<dependency org="ognl" name="ognl" rev="2.6.9" />
<dependency org="org.acegisecurity" name="acegi-security" rev="1.0.3" />
<dependency org="org.acegisecurity" name="acegi-security-src" rev="1.0.3" conf="source->source">
<artifact name="acegi-security-src" type="zip"/>
<dependency org="org.acegisecurity" name="acegi-security"
rev="1.0.3" />
<dependency org="org.acegisecurity" name="acegi-security-src"
rev="1.0.3" conf="source->source">
<artifact name="acegi-security-src" type="zip" />
</dependency>
<!-- kind of nasty, acegi needs some 1.2.7 stuff, so we override with newer! -->
<dependency org="org.springframework" name="spring-jdbc" rev="2.0.4" />
<dependency org="org.springframework" name="spring-remoting" rev="2.0.4" />
<dependency org="org.springframework" name="spring-support" rev="2.0.4" />
<dependency org="org.springframework" name="spring-aop"
rev="2.0.4" />
<dependency org="aopalliance" name="aopalliance" rev="1.0" />
<!-- kind of nasty, acegi needs some 1.2.7 stuff, so we override with newer! -->
<dependency org="org.springframework" name="spring-jdbc"
rev="2.0.4" />
<dependency org="org.springframework" name="spring-remoting"
rev="2.0.4" />
<dependency org="org.springframework" name="spring-support"
rev="2.0.4" />
<!-- dependencies for Apache Directory Server -->
<dependency org="org.apache.directory.server" name="apacheds-core" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-core-shared" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-server-jndi" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-server-main" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-protocol-shared" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-protocol-changepw" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-protocol-kerberos" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-kerberos-shared" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server" name="apacheds-protocol-ldap" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.directory.server"
name="apacheds-core" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-core-shared" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-server-jndi" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-server-main" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-protocol-shared" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-protocol-changepw" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-protocol-kerberos" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-kerberos-shared" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.server"
name="apacheds-protocol-ldap" rev="1.0.0" conf="global->default" />
<dependency org="org.apache.directory.shared" name="shared-asn1" rev="0.9.5.3"
conf="global->default"/>
<dependency org="org.apache.directory.shared" name="shared-asn1-codec" rev="0.9.5.3"
conf="global->default"/>
<dependency org="org.apache.directory.shared" name="shared-ldap" rev="0.9.5.3"
conf="global->default"/>
<dependency org="org.apache.directory.shared" name="shared-asn1"
rev="0.9.5.3" conf="global->default" />
<dependency org="org.apache.directory.shared"
name="shared-asn1-codec" rev="0.9.5.3" conf="global->default" />
<dependency org="org.apache.directory.shared" name="shared-ldap"
rev="0.9.5.3" conf="global->default" />
<dependency org="antlr" name="antlr" rev="2.7.2" conf="global->default" />
<dependency org="jdbm" name="jdbm" rev="1.0" conf="global->default" />
<dependency org="antlr" name="antlr" rev="2.7.2"
conf="global->default" />
<dependency org="jdbm" name="jdbm" rev="1.0"
conf="global->default" />
<dependency org="org.slf4j" name="slf4j-log4j12" rev="1.0"
conf="global->default" />
conf="global->default" />
<dependency org="org.apache.mina" name="mina-core" rev="1.0.0"
conf="global->default"/>
<dependency org="org.apache.mina" name="mina-filter-ssl" rev="1.0.0"
conf="global->default"/>
conf="global->default" />
<dependency org="org.apache.mina" name="mina-filter-ssl"
rev="1.0.0" conf="global->default" />
<!-- End dependencies for Apache Directory Server -->
<!-- build time only dependencies -->
<dependency org="javax.servlet" name="servlet-api" rev="2.4" conf="buildtime->default"/>
<dependency org="javax.servlet" name="servlet-api" rev="2.4"
conf="buildtime->default" />
<!-- test-time only dependencies -->
<dependency org="com.cenqua.clover" name="clover" rev="1.3.12" conf="test->default" />
<dependency org="org.springframework" name="spring-mock" rev="2.0.4"
conf="test->default" />
<dependency org="junit" name="junit" rev="3.8.1" conf="test->default" />
<dependency org="easymock" name="easymock" rev="1.2_Java1.3" conf="test->default"/>
<dependency org="gsbase" name="gsbase" rev="2.0.1" conf="test->default" />
<dependency org="com.cenqua.clover" name="clover" rev="1.3.12"
conf="test->default" />
<dependency org="org.springframework" name="spring-mock"
rev="2.0.4" conf="test->default" />
<dependency org="junit" name="junit" rev="3.8.1"
conf="test->default" />
<dependency org="easymock" name="easymock" rev="1.2_Java1.3"
conf="test->default" />
<dependency org="gsbase" name="gsbase" rev="2.0.1"
conf="test->default" />
</dependencies>
</ivy-module>

View File

@@ -79,4 +79,8 @@ public class DummyGroupDao implements GroupDao {
Group actualGroup = findByPrimaryKey(group.getName());
actualGroup.setMembers(group.getMembers());
}
public void updateMemberDn(String originalDn, String newDn) {
throw new UnsupportedOperationException("Not implemented");
}
}

View File

@@ -76,19 +76,29 @@ public class DummyPersonDao implements PersonDao {
return new LinkedList(PERSONS.entrySet());
}
public Person findByPrimaryKey(String country, String company,
public Person findByPrimaryKey(String dn) {
log.info("findByPrimaryKey");
if (PERSONS.get(dn) != null) {
return (Person) PERSONS.get(dn);
} else {
throw new NameNotFoundException("Could not find person with dn '"
+ dn + "'");
}
}
public Person findByPrimaryKeyData(String country, String company,
String fullname) {
log.info("findByPrimaryKey");
if (PERSONS.get(fullname) != null) {
return (Person) PERSONS.get(fullname);
} else {
throw new NameNotFoundException(
"Could not find person with name '" + fullname + "'");
throw new NameNotFoundException("Could not find person with name '"
+ fullname + "'");
}
}
public void update(Person person) {
Person actualPerson = findByPrimaryKey(null, null, person.getFullName());
Person actualPerson = findByPrimaryKey(person.getFullName());
actualPerson.setCompany(person.getCompany());
actualPerson.setCountry(person.getCountry());
actualPerson.setDescription(person.getDescription());

View File

@@ -37,4 +37,14 @@ public interface GroupDao {
public List findAll();
public List find(SearchCriteria criteria);
/**
* Update all groups referring to the original DN with the new DN value.
*
* @param originalDn
* the original DN.
* @param newDn
* the new DN.
*/
public void updateMemberDn(String originalDn, String newDn);
}

View File

@@ -15,8 +15,13 @@
*/
package org.springframework.ldap.samples.person.dao;
import java.util.Iterator;
import java.util.List;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
@@ -127,4 +132,34 @@ public class GroupDaoImpl implements GroupDao {
public void setLdapOperations(LdapOperations ldapOperations) {
this.ldapOperations = ldapOperations;
}
public void updateMemberDn(String originalDn, String newDn) {
BasicAttribute removeAttribute = new BasicAttribute("uniqueMember",
originalDn);
BasicAttribute addAttribute = new BasicAttribute("uniqueMember",
newDn);
ModificationItem[] modificationItems = new ModificationItem[2];
modificationItems[0] = new ModificationItem(
DirContext.REMOVE_ATTRIBUTE, removeAttribute);
modificationItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
addAttribute);
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "groupOfUniqueNames"));
filter.and(new EqualsFilter("uniqueMember", originalDn));
List list = ldapOperations.search(DistinguishedName.EMPTY_PATH, filter
.encode(), new ContextMapper() {
public Object mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
return adapter.getNameInNamespace();
}
});
for (Iterator iter = list.iterator(); iter.hasNext();) {
String dn = (String) iter.next();
ldapOperations.modifyAttributes(dn, modificationItems);
}
}
}

View File

@@ -15,13 +15,10 @@
*/
package org.springframework.ldap.samples.person.dao;
import javax.naming.NamingException;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.samples.person.domain.Person;
import org.springframework.ldap.support.LdapUtils;
/**
* Maps from DirContextOperations (DirContextAdapters, really) to Person
@@ -39,13 +36,11 @@ public class PersonContextMapper implements ContextMapper {
DirContextOperations dirContext = (DirContextOperations) ctx;
DistinguishedName dn = new DistinguishedName(dirContext.getDn());
String fullDn;
try {
fullDn = dirContext.getNameInNamespace();
} catch (NamingException e) {
throw LdapUtils.convertLdapException(e);
}
fullDn = dirContext.getNameInNamespace();
Person person = new Person();
person.setDn(fullDn);
person.setPrimaryKey(dirContext.getDn().toString());
person.setCountry(dn.getLdapRdn(0).getComponent().getValue());
person.setCompany(dn.getLdapRdn(1).getComponent().getValue());
person.setFullName(dirContext.getStringAttribute("cn"));

View File

@@ -20,7 +20,6 @@ import java.util.List;
import org.springframework.ldap.samples.person.domain.Person;
import org.springframework.ldap.samples.person.domain.SearchCriteria;
/**
* Data Access Object interface for the Person entity.
*
@@ -33,10 +32,13 @@ public interface PersonDao {
public void update(Person person);
public void delete(Person person);
public Person findByPrimaryKey(String country, String company, String fullname);
public Person findByPrimaryKey(String dn);
public Person findByPrimaryKeyData(String country, String company,
String fullname);
public List findAll();
public List find(SearchCriteria criteria);
}

View File

@@ -17,6 +17,8 @@ package org.springframework.ldap.samples.person.dao;
import java.util.List;
import javax.naming.Name;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
@@ -79,14 +81,27 @@ public class PersonDaoImpl implements PersonDao {
}
public void update(Person person) {
DistinguishedName originalDn = new DistinguishedName(person
.getPrimaryKey());
DistinguishedName newDn = buildDn(person);
if (!originalDn.equals(newDn)) {
ldapOperations.rename(originalDn, newDn);
}
DirContextOperations ctx = (DirContextOperations) ldapOperations
.lookup(buildDn(person));
ldapOperations.modifyAttributes(buildDn(person), setAttributes(ctx,
person).getModificationItems());
.lookup(newDn);
ldapOperations.modifyAttributes(newDn, setAttributes(ctx, person)
.getModificationItems());
if (!originalDn.equals(newDn)) {
person.setDn(ctx.getNameInNamespace());
person.setPrimaryKey(ctx.getDn().toString());
}
}
public void delete(Person person) {
ldapOperations.unbind(buildDn(person));
ldapOperations.unbind(person.getPrimaryKey());
}
public List findAll() {
@@ -95,9 +110,8 @@ public class PersonDaoImpl implements PersonDao {
.encode(), getContextMapper());
}
public Person findByPrimaryKey(String country, String company,
public Person findByPrimaryKeyData(String country, String company,
String fullname) {
DistinguishedName dn = new DistinguishedName();
dn.add("c", country);
dn.add("ou", company);
@@ -106,6 +120,10 @@ public class PersonDaoImpl implements PersonDao {
return (Person) ldapOperations.lookup(dn, getContextMapper());
}
public Person findByPrimaryKey(String dn) {
return (Person) ldapOperations.lookup(dn, getContextMapper());
}
public List find(SearchCriteria criteria) {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", "person"));
@@ -117,4 +135,5 @@ public class PersonDaoImpl implements PersonDao {
public void setLdapOperations(LdapOperations ldapOperations) {
this.ldapOperations = ldapOperations;
}
}

View File

@@ -127,10 +127,28 @@ public class TraditionalPersonDaoImpl implements PersonDao {
* @see org.springframework.ldap.samples.person.dao.PersonDao#findByPrimaryKey(java.lang.String,
* java.lang.String, java.lang.String)
*/
public Person findByPrimaryKey(String country, String company,
String fullname) {
public Person findByPrimaryKey(String dn) {
DirContext ctx = createContext();
try {
Attributes attributes = ctx.getAttributes(dn);
return mapToPerson(dn, attributes);
} catch (NamingException e) {
throw new RuntimeException(e);
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
// Never mind this.
}
}
}
}
public Person findByPrimaryKeyData(String country, String company,
String fullname) {
DirContext ctx = createContext();
String dn = buildDn(country, company, fullname);
try {
Attributes attributes = ctx.getAttributes(dn);
@@ -225,4 +243,5 @@ public class TraditionalPersonDaoImpl implements PersonDao {
public void setBase(String base) {
this.base = base;
}
}

View File

@@ -31,11 +31,17 @@ public class Person implements Serializable {
private static final long serialVersionUID = -8953862790009843528L;
/**
* Needed when adding persons to groups, since groups require
* full distinguished names of their members.
* Needed when adding persons to groups, since groups require full
* distinguished names of their members.
*/
private String dn;
/**
* The primary key - that is the distinguished name of the person in the
* current namespace.
*/
private String primaryKey;
private String fullName;
private String lastName;
@@ -114,8 +120,16 @@ public class Person implements Serializable {
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
public String toString() {
return new ToStringCreator(this).toString();
}
public String getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(String primaryKey) {
this.primaryKey = primaryKey;
}
}

View File

@@ -17,6 +17,8 @@ package org.springframework.ldap.samples.person.service;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.ldap.samples.person.dao.GroupDao;
import org.springframework.ldap.samples.person.dao.PersonDao;
import org.springframework.ldap.samples.person.domain.Person;
import org.springframework.ldap.samples.person.domain.SearchCriteria;
@@ -31,6 +33,8 @@ public class PersonServiceImpl implements PersonService {
private PersonDao personDao;
private GroupDao groupDao;
/*
* @see org.springframework.ldap.samples.person.service.PersonService#create(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
@@ -53,7 +57,18 @@ public class PersonServiceImpl implements PersonService {
* @see org.springframework.ldap.samples.person.service.PersonService#update(org.springframework.ldap.samples.person.domain.Person)
*/
public void update(Person person) {
String originalDn = person.getDn();
personDao.update(person);
String newDn = person.getDn();
// If the DN has changed (i.e. if the user has been moved in the tree,
// also modify all referring groups.
// Unfortunately this doesn't work with the current version of ApacheDS,
// as the format of the DN produced by DistinguishedName is different
// from the one present in the DB (and recognized by acegi).
if (!StringUtils.equals(originalDn, newDn)) {
groupDao.updateMemberDn(originalDn, newDn);
}
}
public void delete(Person person) {
@@ -65,7 +80,7 @@ public class PersonServiceImpl implements PersonService {
* java.lang.String, java.lang.String)
*/
public Person findByPrimaryKey(String country, String company, String name) {
return personDao.findByPrimaryKey(country, company, name);
return personDao.findByPrimaryKeyData(country, company, name);
}
/*
@@ -85,4 +100,8 @@ public class PersonServiceImpl implements PersonService {
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void setGroupDao(GroupDao groupDao) {
this.groupDao = groupDao;
}
}

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="apacheDsContext.xml"/>
<import resource="apacheDsContext.xml" />
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
@@ -12,32 +12,42 @@
<!-- Authenticate using a fixed user -->
<!--
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource" >
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource" >
<property name="url" value="${url}" />
<property name="base" value="${base}" />
<property name="userDn" value="${userDn}" />
<property name="password" value="${password}" />
<property name="dirObjectFactory" value="org.springframework.ldap.core.support.DefaultDirObjectFactory" />
</bean>
-->
</bean>
-->
<!-- Authenticate using the currently logged in user, retrieved by Acegi Security -->
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource" >
<bean id="contextSourceTarget"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="${url}" />
<property name="base" value="${base}" />
<property name="authenticationSource" ref="acegiAuthenticationSource" />
<property name="dirObjectFactory" value="org.springframework.ldap.core.support.DefaultDirObjectFactory" />
<!--
Disable pooling if you want to test changing the password after a user has logged in,
perhaps in order to verify that AcegiAuthenticationSource works correctly.
<property name="authenticationSource"
ref="acegiAuthenticationSource" />
<property name="dirObjectFactory"
value="org.springframework.ldap.core.support.DefaultDirObjectFactory" />
<property name="pooled" value="false" />
-->
</bean>
<bean id="acegiAuthenticationSource" class="org.springframework.ldap.authentication.AcegiAuthenticationSource" />
<bean id="ldapOperations" class="org.springframework.ldap.core.LdapTemplate">
<bean id="contextSource"
class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy">
<constructor-arg ref="contextSourceTarget" />
</bean>
<bean id="transactionManager"
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager">
<property name="contextSource" ref="contextSource" />
</bean>
<bean id="acegiAuthenticationSource"
class="org.springframework.ldap.authentication.AcegiAuthenticationSource" />
<bean id="ldapOperations"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
@@ -54,9 +64,11 @@
<property name="base" value="${base}" />
</bean>
<bean id="dummyPersonDao"
<!--
<bean id="dummyPersonDao"
class="org.springframework.ldap.samples.person.dao.DummyPersonDao">
</bean>
</bean>
-->
<bean id="dummyGroupDao"
class="org.springframework.ldap.samples.person.dao.DummyGroupDao">
@@ -67,22 +79,34 @@
<property name="ldapOperations" ref="ldapOperations" />
</bean>
<bean id="personService"
<bean id="personServiceTarget"
class="org.springframework.ldap.samples.person.service.PersonServiceImpl">
<property name="personDao" ref="personDao" />
<property name="groupDao" ref="groupDao" />
<!-- Replace with this to use the dummy or the traditional implementation -->
<!--
<property name="personDao" ref="dummyPersonDao" />
<property name="personDao" ref="traditionalPersonDao" />
<property name="personDao" ref="dummyPersonDao" />
<property name="personDao" ref="traditionalPersonDao" />
-->
</bean>
<bean name="personService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="personServiceTarget" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
<bean id="groupService"
class="org.springframework.ldap.samples.person.service.GroupServiceImpl">
<property name="groupDao" ref="groupDao" />
<!-- Replace with this to use the dummy implementation -->
<!--
<property name="groupDao" ref="dummyGroupDao" />
<property name="groupDao" ref="dummyGroupDao" />
-->
</bean>
</beans>

View File

@@ -10,37 +10,29 @@
<input-attribute name="country"/>
</input-mapper>
<start-state idref="displayDetails" />
<start-state idref="init" />
<action-state id="init">
<!-- get a person to work with -->
<bean-action bean="personService" method="findByPrimaryKey">
<method-arguments>
<argument expression="flowScope.country" />
<argument expression="flowScope.company" />
<argument expression="flowScope.name" />
</method-arguments>
<method-result name="person" scope="flow"/>
</bean-action>
<transition to="displayDetails" />
</action-state>
<view-state id="displayDetails" view="showDetails">
<render-actions>
<!-- get a person to work with -->
<bean-action bean="personService" method="findByPrimaryKey">
<method-arguments>
<argument expression="flowScope.country" />
<argument expression="flowScope.company" />
<argument expression="flowScope.name" />
</method-arguments>
<method-result name="person" />
</bean-action>
</render-actions>
<transition on="back" to="finish" />
<transition on="edit" to="editDetails" />
</view-state>
<view-state id="editDetails" view="editDetails">
<render-actions>
<!-- get a person to work with -->
<bean-action bean="personService" method="findByPrimaryKey">
<method-arguments>
<argument expression="flowScope.country" />
<argument expression="flowScope.company" />
<argument expression="flowScope.name" />
</method-arguments>
<method-result name="person" scope="flow" />
</bean-action>
<!-- setup form with person -->
<action bean="editPersonFormAction" method="setupForm"/>
</render-actions>

View File

@@ -168,8 +168,9 @@ public class GroupDaoImplTest extends TestCase {
public void testFindAll() {
List expectedList = Collections.singletonList(null);
ldapOperationsControl.expectAndReturn(ldapOperationsMock.search(
DistinguishedName.EMPTY_PATH, "(objectclass=groupOfUniqueNames)",
contextMapperMock), expectedList);
DistinguishedName.EMPTY_PATH,
"(objectclass=groupOfUniqueNames)", contextMapperMock),
expectedList);
replay();
@@ -181,8 +182,7 @@ public class GroupDaoImplTest extends TestCase {
}
public void testFindByPrimaryKey() {
DistinguishedName dn = new DistinguishedName(
"cn=Some Group, ou=groups");
DistinguishedName dn = new DistinguishedName("cn=Some Group, ou=groups");
ldapOperationsControl.expectAndReturn(ldapOperationsMock.lookup(dn,
contextMapperMock), group);
@@ -200,8 +200,8 @@ public class GroupDaoImplTest extends TestCase {
List expectedList = Collections.singletonList(null);
ldapOperationsControl.expectAndReturn(ldapOperationsMock.search(
DistinguishedName.EMPTY_PATH,
"(&(objectclass=groupOfUniqueNames)(cn=*some*))", contextMapperMock),
expectedList);
"(&(objectclass=groupOfUniqueNames)(cn=*some*))",
contextMapperMock), expectedList);
replay();

View File

@@ -18,6 +18,7 @@ package org.springframework.ldap.samples.person.dao;
import java.util.Collections;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.ModificationItem;
import junit.framework.TestCase;
@@ -38,6 +39,16 @@ import org.springframework.ldap.samples.person.domain.SearchCriteria;
*/
public class PersonDaoImplTest extends TestCase {
private static final String DEFAULT_DN = "cn=some person, ou=Some Company, c=SE";
private static final DistinguishedName DEFAULT_DNAME = new DistinguishedName(
DEFAULT_DN);
private static final String MODIFIED_DN = "cn=some person, ou=Some Other Company, c=SE";
private static final DistinguishedName MODIFIED_DNAME = new DistinguishedName(
MODIFIED_DN);
private MockControl ldapOperationsControl;
private LdapOperations ldapOperationsMock;
@@ -68,6 +79,10 @@ public class PersonDaoImplTest extends TestCase {
contextMapperMock = (ContextMapper) contextMapperControl.getMock();
person = new Person();
person.setFullName("some person");
person.setCompany("Some company");
person.setCountry("SE");
person.setPrimaryKey(DEFAULT_DN);
tested = new PersonDaoImpl() {
DirContextOperations setAttributes(DirContextOperations adapter,
@@ -76,11 +91,6 @@ public class PersonDaoImplTest extends TestCase {
return dirContextOperationsMock;
}
DistinguishedName buildDn(Person p) {
assertSame(person, p);
return DistinguishedName.EMPTY_PATH;
}
ContextMapper getContextMapper() {
return contextMapperMock;
}
@@ -129,8 +139,7 @@ public class PersonDaoImplTest extends TestCase {
}
public void testCreate() {
ldapOperationsMock.bind(DistinguishedName.EMPTY_PATH,
dirContextOperationsMock, null);
ldapOperationsMock.bind(DEFAULT_DNAME, dirContextOperationsMock, null);
replay();
@@ -140,26 +149,49 @@ public class PersonDaoImplTest extends TestCase {
}
public void testUpdate() {
ldapOperationsControl
.expectAndReturn(ldapOperationsMock
.lookup(DistinguishedName.EMPTY_PATH),
dirContextOperationsMock);
ldapOperationsControl.expectAndReturn(ldapOperationsMock
.lookup(DEFAULT_DNAME), dirContextOperationsMock);
ModificationItem[] modificationItems = new ModificationItem[0];
dirContextOperationsControl.expectAndReturn(dirContextOperationsMock
.getModificationItems(), modificationItems);
ldapOperationsMock.modifyAttributes(DistinguishedName.EMPTY_PATH,
modificationItems);
ldapOperationsMock.modifyAttributes(DEFAULT_DNAME, modificationItems);
replay();
tested.update(person);
verify();
}
public void testUpdateWithChangedCompany() throws NamingException {
person.setCompany("Some Other Company");
ldapOperationsMock.rename(DEFAULT_DNAME, MODIFIED_DNAME);
ldapOperationsControl.expectAndReturn(ldapOperationsMock
.lookup(MODIFIED_DNAME), dirContextOperationsMock);
ModificationItem[] modificationItems = new ModificationItem[0];
dirContextOperationsControl.expectAndReturn(dirContextOperationsMock
.getModificationItems(), modificationItems);
ldapOperationsMock.modifyAttributes(MODIFIED_DNAME, modificationItems);
dirContextOperationsControl.expectAndReturn(dirContextOperationsMock
.getNameInNamespace(), "cn=new dn, dc=jayway, dc=se");
dirContextOperationsControl.expectAndReturn(dirContextOperationsMock
.getDn(), new DistinguishedName("cn=new dn"));
replay();
tested.update(person);
verify();
assertEquals("cn=new dn", person.getPrimaryKey());
assertEquals("cn=new dn, dc=jayway, dc=se", person.getDn());
}
public void testDelete() {
ldapOperationsMock.unbind(DistinguishedName.EMPTY_PATH);
ldapOperationsMock.unbind(DEFAULT_DN);
replay();
@@ -184,16 +216,14 @@ public class PersonDaoImplTest extends TestCase {
}
public void testFindByPrimaryKey() {
DistinguishedName dn = new DistinguishedName(
"cn=Some Person, ou=Some company, c=Sweden");
String dn = "cn=Some Person, ou=Some company, c=Sweden";
ldapOperationsControl.expectAndReturn(ldapOperationsMock.lookup(dn,
contextMapperMock), person);
replay();
Person result = tested.findByPrimaryKey("Sweden", "Some company",
"Some Person");
Person result = tested.findByPrimaryKey(dn);
verify();

View File

@@ -21,10 +21,10 @@ import java.util.List;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.samples.person.dao.GroupDao;
import org.springframework.ldap.samples.person.dao.PersonDao;
import org.springframework.ldap.samples.person.domain.Person;
import org.springframework.ldap.samples.person.domain.SearchCriteria;
import org.springframework.ldap.samples.person.service.PersonServiceImpl;
/**
* Unit tests for the PersonServiceImpl class.
@@ -116,7 +116,7 @@ public class PersonServiceImplTest extends TestCase {
public void testFindByPrimaryKey() {
Person person = new Person();
personDaoControl.expectAndReturn(personDaoMock.findByPrimaryKey(
personDaoControl.expectAndReturn(personDaoMock.findByPrimaryKeyData(
"Sweden", "Some company", "Some Person"), person);
replay();

View File

@@ -83,7 +83,7 @@ public class DetailFlowExecutionTest extends AbstractXmlFlowExecutionTests {
}
public void testEdit() {
expectFindByPrimaryKey(2);
expectFindByPrimaryKey();
replay();
startFlow(attributeMap);
@@ -96,7 +96,7 @@ public class DetailFlowExecutionTest extends AbstractXmlFlowExecutionTests {
}
public void testCancel() {
expectFindByPrimaryKey(3);
expectFindByPrimaryKey();
replay();
startFlow(attributeMap);
@@ -110,7 +110,7 @@ public class DetailFlowExecutionTest extends AbstractXmlFlowExecutionTests {
}
public void testSubmit() {
expectFindByPrimaryKey(3);
expectFindByPrimaryKey();
expectUpdate();
replay();