Patch from Jasper on July 26.

This commit is contained in:
Ulrik Sandberg
2007-07-26 07:42:32 +00:00
parent 27544ae2fb
commit 2c72b14706
40 changed files with 96 additions and 1189 deletions

View File

@@ -139,7 +139,10 @@ public class LdapDaoITest extends AbstractLdapTemplateIntegrationTest
public void testFilterByBeanProperty()
{
ldapDao.create(testPerson);
List results = ldapDao.filterByBeanProperty(testPerson.getEmailAddress(), "EmailAddress", ITestPerson.class);
List results = ldapDao.filterByBeanProperty(
"emailAddress", testPerson.getEmailAddress(), ITestPerson.class);
LOGGER.debug("filterByBeanProperty returned: " + results.size() + " results.");
Assert.assertTrue("Filter should return collection containing more than one result. ",
results.size() > 0);

View File

@@ -21,10 +21,10 @@
<constructor-arg ref="contextSource"/>
</bean>
<bean id="typeConverter" singleton="true" class="org.springframework.ldap.odm.attributetypes.LdapTypeConverter"/>
<bean id="typeConverter" singleton="true" class="org.springframework.ldap.odm.typeconversion.LdapTypeConverter"/>
<bean id="referencedEntryEditorFactory" singleton="true"
class="org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory">
class="org.springframework.ldap.odm.typeconversion.ReferencedEntryEditorFactory">
<constructor-arg value="${base}" />
<constructor-arg ref="ldapTemplate"/>
</bean>

View File

@@ -12,12 +12,13 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
/**
* The <code>NamingAttribute</code> annotation identifies the name of the attribute that forms
* <p>The <code>NamingAttribute</code> annotation identifies the name of the attribute that forms
* the first part of a Distinguished Name. For example, in the Distinguished Name
* 'uid=x232, ou=people' the naming attribute is 'uid'. The <code>NamingAttribute</code> together
* with a <code>NamingSuffix</code> tell the Object Directory Mapper how to serialize a java object
* to and from an LDAP repository.
* Example: <pre>@NamingAttribute("uid")</pre>
* 'uid=x232, ou=people' the naming attribute is 'uid'.</p>
* <p> The <code>NamingAttribute</code> together with a {@link NamingSuffix} tell the
* {@link org.springframework.ldap.odm.mapping.ObjectDirectoryMapper} how to assemble a
* Distinguished Name for an object.</p>
* <p>Example: <pre>@NamingAttribute("uid")</pre></p>
*
* @see NamingSuffix
*/

View File

@@ -17,6 +17,10 @@ import java.lang.annotation.Target;
* <pre>@NamingSuffix({"ou=people", "dc=example", "dc=com"})</pre>
* will be persisted under the branch 'com/example/people' in the LDAP repository.
*
* <p> The <code>NamingSuffix</code> together with a {@link NamingAttribute} tell the
* {@link org.springframework.ldap.odm.mapping.ObjectDirectoryMapper} how to assemble a
* Distinguished Name for an object.</p>
*
* @see NamingAttribute
*/
@Documented

View File

@@ -1,99 +0,0 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ldap.core.DistinguishedName;
import javax.naming.ldap.LdapName;
import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* <p/>
* LdapTypeConverter is responsible for the conversion of LDAP attributes returned in String form
* to native java types and vice versa. It it leverages Spring's property editors, with
* some custom editors to:
* <li>
* <ul>convert generalized time strings to <code>java.util.Date.</code></ul>
* <ul>convert dn strings to <code>javax.naming.ldap.LdapName</code> or
* <code>org.springframework.ldap.core.DistinguishedName</code>.</ul>
* </li>
* </p>
* <p/>
* Additional custom editors may be created at runtime to if an Object Directory Map
* contains references to other mapped objects (eg. A role of type Role.class containing
* references to members of type Person.class)
* <p/>
* </p>
*/
public class LdapTypeConverter extends SimpleTypeConverter
{
private static final DateFormat GENERALIZED_TIME
= new SimpleDateFormat("yyyyMMddHHmmss.SSSZ");
public LdapTypeConverter()
{
super();
registerCustomEditor(Date.class, new CustomDateEditor(GENERALIZED_TIME, true));
registerCustomEditor(LdapName.class, new NameEditor(LdapName.class));
registerCustomEditor(DistinguishedName.class, new NameEditor(DistinguishedName.class));
}
/**
* Convert Object to String
*/
public String getAsText(Object value)
{
if (value instanceof String)
{
return (String) value;
}
PropertyEditor pe = propertyEditorFor(value.getClass());
if (pe != null)
{
pe.setValue(value);
return pe.getAsText();
}
else
{
return null;
}
}
/**
* Convert Object array to String array
*/
public String[] getAllAsText(Object[] values)
{
String[] textValues = new String[values.length];
for (int i = 0; i < values.length; i++)
{
textValues[i] = getAsText(values[i]);
}
return textValues;
}
private PropertyEditor propertyEditorFor(Class clazz)
{
PropertyEditor pe = findCustomEditor(clazz, null);
if (pe != null)
{
return pe;
}
else
{
pe = getDefaultEditor(clazz);
return pe;
}
}
}

View File

@@ -1,58 +0,0 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import org.springframework.ldap.core.DistinguishedName;
import javax.naming.ldap.LdapName;
import java.beans.PropertyEditorSupport;
/**
* NameEditor is responsible for converting Strings to the type LdapName or DistinguishedName.
* And, conversely LdapNames and DistinguishedNames to the type String.
*/
public class NameEditor extends PropertyEditorSupport
{
private Class conversionClass;
/**
* @param conversionClass <code>LdapName.class</code> for String <--> LdapName and
* <code>DistinguishedName.class</code> for String <--> DistinguishedName
*/
public NameEditor(Class conversionClass)
{
this.conversionClass = conversionClass;
if (!conversionClass.equals(DistinguishedName.class) && !conversionClass.equals(LdapName.class))
{
throw new IllegalArgumentException(
"NameEditor can only be created for LdapName or DistinguishedName");
}
}
/**
* Invokes toString() on the LdapName or DistinguishedName
*/
public String getAsText()
{
Object value = conversionClass.cast(getValue());
return value != null ? value.toString() : "";
}
/**
* Attempts to parse and String and return an LdapName or DistinguishedName
*/
public void setAsText(String text) throws IllegalArgumentException
{
try
{
setValue(conversionClass.getConstructor(String.class).newInstance(text));
}
catch (Exception e)
{
throw new IllegalArgumentException(e.getMessage(), e);
}
}
}

View File

@@ -1,65 +0,0 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import java.beans.PropertyEditorSupport;
/** ReferencedEntryEditor is responsible for converting references in an object
* directory map from distinguished name strings to the target type and vice versa.
*/
public class ReferencedEntryEditor extends PropertyEditorSupport
{
private DistinguishedName base;
private LdapTemplate ldapTemplate;
private ObjectDirectoryMapper objectDirectoryMapper;
public ReferencedEntryEditor(DistinguishedName baseDn,
LdapTemplate ldapTemplate,
ObjectDirectoryMapper objectDirectoryMapper)
{
this.base = baseDn;
this.ldapTemplate = ldapTemplate;
this.objectDirectoryMapper = objectDirectoryMapper;
}
/** Builds a distinguished name from the instance value */
public String getAsText()
{
try
{
DistinguishedName value = (DistinguishedName) base.clone();
value.append((DistinguishedName) objectDirectoryMapper.buildDn(getValue()));
return value.toString();
}
catch (MappingException e)
{
throw new RuntimeException(e.getMessage(), e);
}
}
/** Sets the value of the editor by performing a lookup and mapping the result
* to the target type using an object directory mapper.
* @param text The distinguished name of an ldap entry.
* @throws IllegalArgumentException
*/
public void setAsText(String text) throws IllegalArgumentException
{
DistinguishedName dn = new DistinguishedName(text);
if (dn.startsWith(base))
{
for (int i = 0; i < base.size(); i++)
{
dn.removeFirst();
}
}
setValue(ldapTemplate.lookup(dn, objectDirectoryMapper));
}
}

View File

@@ -1,78 +0,0 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapperFactory;
import java.util.HashMap;
import java.util.Map;
/**
* ReferencedEntryEditorFactory is a factory for assembling ReferencedEntryEditors.
*
* @see ReferencedEntryEditor
*/
public class ReferencedEntryEditorFactory
{
private static final Log LOGGER = LogFactory.getLog(ReferencedEntryEditorFactory.class);
private String base;
private ObjectDirectoryMapperFactory odmFactory;
private LdapTemplate ldapTemplate;
private Map<Class, ReferencedEntryEditor> referencedEntryEditors;
public ReferencedEntryEditorFactory(String base, LdapTemplate ldapTemplate)
{
this.base = base;
this.ldapTemplate = ldapTemplate;
this.referencedEntryEditors = new HashMap();
}
/**
* Attempts to build a ReferencedEntryEditor for the given type. If Object Directory
* Mapping for the given type is successful an editor is returned, otherwise a
* <code>MappingException</code> is thrown.
*
* @param clazz the type to build a ReferencedEntryEditor for.
* @return A ReferencedEntryEditor for the given type.
*
*/
public ReferencedEntryEditor referencedEntryEditorForClass(Class clazz)
throws MappingException
{
if (referencedEntryEditors.containsKey(clazz))
{
LOGGER.debug("Returning cached referenced entry editor for class: " + clazz.getSimpleName());
return referencedEntryEditors.get(clazz);
}
else
{
LOGGER.debug("Attempting to create a referenced entry editor for class: "
+ clazz.getSimpleName());
ObjectDirectoryMapper odm = odmFactory.objectDirectoryMapperForClass(clazz);
ReferencedEntryEditor referencedEntryEditor =
new ReferencedEntryEditor(new DistinguishedName(base), ldapTemplate, odm);
referencedEntryEditors.put(clazz, referencedEntryEditor);
return referencedEntryEditor;
}
}
/**
* @param mapperFactory the <code>ObjectDirectoryMapperFactory</code> to use when attempting
* to build a <code>ReferencedEntryEditor</code>.
*/
public void setObjectDirectoryMapperFactory(ObjectDirectoryMapperFactory
mapperFactory)
{
this.odmFactory = mapperFactory;
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import org.springframework.ldap.core.DistinguishedName;
import javax.naming.ldap.LdapName;
import java.util.Date;
/** This list of types supported for mapping between ldap attributes and bean properties. */
public enum ValidConversionType
{
BYTE_ARRAY(byte[].class),
BOOLEAN(Boolean.class),
STRING(String.class),
STRING_ARRAY(String[].class),
DATE(Date.class),
DATE_ARRAY(Date[].class),
LDAP_NAME(LdapName.class),
LDAP_NAME_ARRAY(LdapName[].class),
DISTINGUISHED_NAME(DistinguishedName.class),
DISTINGUISHED_NAME_ARRAY(DistinguishedName[].class),
INTEGER(Integer.class),
INTEGER_ARRAY(Integer[].class),
LONG(Long.class),
LONG_ARRAY(Long[].class);
private final Class clazz;
ValidConversionType(Class validType)
{
this.clazz = validType;
}
/** Returns the enumeration of types as a human-friendly string. */
public static String listTypes()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ValidConversionType.values().length; i++)
{
ValidConversionType validType = ValidConversionType.values()[i];
sb.append("\n");
sb.append(validType.clazz.getSimpleName());
if (i != ValidConversionType.values().length - 1)
{
sb.append(",");
}
}
return sb.toString();
}
/** Returns true if the argument is a member of this enumeration. */
public static boolean isValidConversionType(Class returnType)
{
for (ValidConversionType type : ValidConversionType.values())
{
if (returnType.equals(type.clazz))
{
return true;
}
}
return false;
}
}

View File

@@ -1,7 +0,0 @@
<html>
<body>
Contains support classes used to assemble an <code>LdapDao</code> implementation.
</body>
</html>

View File

@@ -6,7 +6,7 @@
package org.springframework.ldap.odm.dao;
/**
* Thrown by <code>LdapDao</code> under exceptional circumstances.
* Thrown by {@link LdapDao} under exceptional circumstances.
*
* @see LdapDao
*/

View File

@@ -8,7 +8,11 @@ package org.springframework.ldap.odm.dao;
import javax.naming.Name;
import java.util.List;
/** A realization of the Data Access Object (DAO) pattern using object directory mapping. */
/** A realization of the Data Access Object (DAO) pattern using object directory mapping.
*
* @see org.springframework.ldap.odm.mapping.ObjectDirectoryMap
* @see org.springframework.ldap.odm.mapping.ObjectDirectoryMapper
*/
public interface LdapDao
{
/** Persists the mapped object dirObject in the LDAP repository. */
@@ -39,5 +43,5 @@ public interface LdapDao
List findAll(Class ofType);
/** Search for entries in the repository and map results to class returnType. */
List filterByBeanProperty(String value, String beanPropertyName, Class returnType);
List filterByBeanProperty(String beanPropertyName, String value, Class returnType);
}

View File

@@ -13,6 +13,7 @@ import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapperFactory;
import org.springframework.util.StringUtils;
import javax.naming.Name;
import java.util.List;
@@ -136,14 +137,16 @@ public class LdapDaoImpl implements LdapDao
}
public List filterByBeanProperty(String value, String beanPropertyName, Class returnType)
public List filterByBeanProperty(String beanPropertyName, String value, Class returnType)
{
try
{
LOGGER.debug("Filtering on property: " + beanPropertyName + ", for value: " + value);
ObjectDirectoryMapper mapper = odmFactory.objectDirectoryMapperForClass(returnType);
String filter = mapper.getObjectDirectoryMap().attributeNameFor(beanPropertyName) + "=" + value;
List results = ldapTemplate.search(mapper.getObjectDirectoryMap().getNamingSuffix().toString(), filter, mapper);
String filter = mapper.getObjectDirectoryMap().attributeNameFor(beanPropertyName)
+ "=" + value;
List results = ldapTemplate.search(mapper.getObjectDirectoryMap().getNamingSuffix().toString(),
filter, mapper);
LOGGER.debug(results.size() + " results found for filter: " + filter);
return results;
}

View File

@@ -15,7 +15,7 @@ import java.util.Map;
import java.util.Set;
/**
* An abstract base class implementing an <code>ObjectDirectoryMap</code>. Actual parsing of
* An abstract base class implementing an {@link ObjectDirectoryMap}. Actual parsing of
* mapping information needs to be implemented in a concrete sub class.
*/
public abstract class AbstractObjectDirectoryMap implements ObjectDirectoryMap

View File

@@ -10,17 +10,17 @@ import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
/** An implementation of an <code>ObjectDirectoryMap</code> based on Annotations. A class that is
/**
* An implementation of an {@link ObjectDirectoryMap} based on Annotations. A class that is
* to be serialized to and from and LDAP repository must include the following annotations:
* <ul>
* <li>NamingAttribute</li>
* <li>ObjectClasses</li>
* <li>NamingSuffix</li>
* <li>DirAttribute</li>
* <li>One {@link NamingAttribute}.</li>
* <li>One {@link ObjectClasses}.</li>
* <li>One {@link NamingSuffix}.</li>
* <li>At least one {@link DirAttribute}.</li>
* </ul>
*
* @see org.springframework.ldap.odm.annotations.NamingAttribute
@@ -114,7 +114,7 @@ public class AnnotationObjectDirectoryMap extends AbstractObjectDirectoryMap
DirAttribute dirAttribute = field.getAnnotation(DirAttribute.class);
if (dirAttribute != null)
{
String beanPropertyName = StringUtils.capitalize(field.getName());
String beanPropertyName = field.getName();
String attributeName = dirAttribute.value().equals("") ? field.getName() : dirAttribute.value();
map(beanPropertyName, attributeName);
}

View File

@@ -5,7 +5,10 @@
*/
package org.springframework.ldap.odm.mapping;
/** Thrown when an attempt to create an Object Directory Map for a given class is unsuccessful. */
/** Thrown when an attempt to create an {@link ObjectDirectoryMap} for a given class is unsuccessful.
* May indicate semantic or syntax errors in the mapping information, or some other exceptional
* condition related to the creation of an {@link ObjectDirectoryMap}.
*/
public class MappingException extends Exception
{

View File

@@ -10,9 +10,8 @@ import org.springframework.ldap.core.DistinguishedName;
import java.util.Set;
/**
* <code>ObjectDirectoryMap</code> encapsulates the information required to serialize a java bean
* to and from an LDAP repository. An <code>ObjectDirectoryMapper</code> performs the
* serialization using this information.
* Encapsulates the information required to serialize a java object to and from an LDAP repository.
* An {@link ObjectDirectoryMapper} performs the serialization using this information.
*
* @see ObjectDirectoryMapper
*/

View File

@@ -11,27 +11,46 @@ import org.springframework.ldap.core.ContextMapper;
import javax.naming.Name;
/** An <code>ObjectDirectoryMapper</code> performs serialization between java beans and and
* an LDAP repository using the information in an <code>ObjectDirectoryMap</code>.
/**
* Performs serialization between java objects and and an LDAP repository using the information
* in an {@link ObjectDirectoryMap}.
*
* @see ObjectDirectoryMap
* @see ObjectDirectoryMap
*/
public interface ObjectDirectoryMapper extends ContextMapper, ContextAssembler
{
/** Map the supplied object to the specified context. */
/**
* Map the supplied object to the specified context, using the associated
* {@link ObjectDirectoryMap}.
*/
Object mapFromContext(Object ctx);
/** Map a single LDAP Context to an object. */
/**
* Map a single LDAP Context to an object using the associated {@link ObjectDirectoryMap}.
*/
void mapToContext(Object beanInstance, Object ctx);
/** Builds a DistinguishedName from an object instance. */
/**
* Builds a Distinguished Name from an object instance. Looks up the Naming Attribute and
* Naming Suffix from the {@link ObjectDirectoryMap}. Populates the Naming Attribute value
* with value on the beanInstance.
*
* @throws MappingException if the naming attribute value on the beanInstance is null.
*/
Name buildDn(Object beanInstance) throws MappingException;
/** Builds a DistinguishedName given the value of the naming attribute. */
/**
* Builds a Distinguished Name from the given <code>namingAttributeValue</code>.
* Looks up the Naming Attribute and Naming Suffix from the {@link ObjectDirectoryMap}.
* Populates the Naming Attribute value with the given value.
*
* @throws MappingException if the given value is null.
*/
Name buildDn(String namingAttributeValue) throws MappingException;
/** Returns the <code>ObjectDirectoryMap</code> that the <code>ObjectDirectoryMapper</code>
* corresponds to.
/**
* Returns the {@link ObjectDirectoryMap} associated with the given instance of the
* <code>ObjectDirectoryMapper</code>.
*/
ObjectDirectoryMap getObjectDirectoryMap();
}

View File

@@ -7,20 +7,20 @@ package org.springframework.ldap.odm.mapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.typeconversion.LdapTypeConverter;
import org.springframework.ldap.odm.typeconversion.ReferencedEntryEditorFactory;
import java.util.HashMap;
import java.util.Map;
/**
* <code>ObjectDirectoryMapperFactory</code> is a factory for assembling
* <code>ObjectDirectoryMappers</code>. It builds a registry of ObjectDirectoryMappers, such
* that the first request for a mapper of a given type results in the attempt to build one.
* Subsequent requests result in the return of a cached instance.
* A factory for assembling {@link ObjectDirectoryMapper ObjectDirectoryMappers}.
* It builds a registry such that the first request for an {@link ObjectDirectoryMapper}
* of a given type results in the attempt to build one. Subsequent requests return a
* cached instance.
*
* @see ObjectDirectoryMapper
* @see org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory
* @see org.springframework.ldap.odm.typeconversion.ReferencedEntryEditorFactory
*/
public class ObjectDirectoryMapperFactory
{
@@ -38,11 +38,11 @@ public class ObjectDirectoryMapperFactory
this.referencedEntryEditorFactory.setObjectDirectoryMapperFactory(this);
}
/** Attempts to return an ObjectDirectoryMapper for the given class. Upon the first encounter
* of the given class, mapping is attempted. If mapping is successful the mapper is returned.
* Subsequent requests for the given class return a cached mapper.
/** Attempts to return an {@link ObjectDirectoryMapper} for the given class. Upon the
* first encounter of the given class, mapping is attempted. If mapping is successful
* the mapper is returned. Subsequent requests for same class return a cached mapper.
* @param clazz
* @return ObjectDirectoryMapper
* @return An {@link ObjectDirectoryMapper} for the Class <code>clazz</code>
* @throws MappingException when the mapping information for the given class contains errors.
*/
public ObjectDirectoryMapper objectDirectoryMapperForClass(Class clazz)

View File

@@ -11,10 +11,11 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.attributetypes.ValidConversionType;
import org.springframework.ldap.odm.typeconversion.LdapTypeConverter;
import org.springframework.ldap.odm.typeconversion.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.typeconversion.ValidConversionType;
import org.springframework.ldap.odm.util.AttributeWrapper;
import org.springframework.util.StringUtils;
import javax.naming.Name;
import javax.naming.directory.Attribute;
@@ -23,7 +24,7 @@ import java.util.HashMap;
import java.util.Map;
/**
* An implemtation of the <code>ObjectDirectoryMapper</code> interface.
* An implemtation of the {@link ObjectDirectoryMapper} interface.
*/
public class ObjectDirectoryMapperImpl implements ObjectDirectoryMapper
{
@@ -184,9 +185,10 @@ public class ObjectDirectoryMapperImpl implements ObjectDirectoryMapper
{
try
{
Method getter = clazz.getMethod("get" + beanPropertyName);
String methodSuffix = StringUtils.capitalize(beanPropertyName);
Method getter = clazz.getMethod("get" + methodSuffix);
propertyGetters.put(beanPropertyName, getter);
Method setter = clazz.getMethod("set" + beanPropertyName, getter.getReturnType());
Method setter = clazz.getMethod("set" + methodSuffix, getter.getReturnType());
propertySetters.put(beanPropertyName, setter);
}
catch (NoSuchMethodException e)

View File

@@ -1,370 +0,0 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.ldap.core.DistinguishedName;
import org.testng.Assert;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Calendar;
public class LdapTypeConverterTest extends TestCase
{
private static final Log LOGGER = LogFactory.getLog(LdapTypeConverterTest.class);
private LdapTypeConverter typeConverter;
@Override
protected void setUp() throws Exception
{
super.setUp();
typeConverter = new LdapTypeConverter();
}
public void testConvertToByteArray()
{
byte[] objectToTranslate = "fred".getBytes();
try
{
byte[] translated = (byte[]) typeConverter.convertIfNecessary(
objectToTranslate, byte[].class);
Assert.assertTrue(Arrays.equals(objectToTranslate, translated),
"byte[] attributes should pass through unchanged.");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception: byte[] attributes should pass through unchanged.",
e);
}
}
public void testConvertToBoolean()
{
String objectToTranslate = "true";
try
{
Object translated = typeConverter.convertIfNecessary(
objectToTranslate, Boolean.class);
Assert
.assertEquals(true, translated,
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertFromBoolean()
{
boolean objectToTranslate = false;
try
{
Object translated = typeConverter.getAsText(objectToTranslate);
Assert.assertEquals(translated, "false",
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToString()
{
String objectToTranslate = "onetwothree";
try
{
Object translated = typeConverter.convertIfNecessary(
objectToTranslate, String.class);
Assert.assertEquals("onetwothree", translated,
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToStringArray()
{
String object1 = "onetwothree";
String object2 = "fourfivesix";
String object3 = "seveneightnine";
Object objectToTranslate = new String[]{object1, object2, object3};
try
{
Object[] translated = (Object[]) typeConverter.convertIfNecessary(
objectToTranslate, String[].class);
Assert.assertEquals("fourfivesix", translated[1],
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToDate() throws ParseException
{
String object1 = "19700101100000.000+1000";
try
{
Object translated = typeConverter.convertIfNecessary(object1,
Date.class);
Assert.assertEquals(translated, new Date(0L),
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertFromDate()
{
Date source = new Date(1184283822285L); //Friday July 13, 2007 9:43:42 AM GMT+1000
LOGGER.debug(typeConverter.getAsText(source));
Assert.assertTrue(typeConverter.getAsText(source)
.matches("20070713\\d\\d\\d\\d42.285\\+\\d\\d\\d\\d"));
}
public void testConvertToDateArray() throws ParseException
{
String object1 = "20071105093655.0+1000";
String object2 = "19700101100000.0+1000";
Object objectToTranslate = new String[]{object1, object2};
try
{
Object[] translated = (Object[]) typeConverter.convertIfNecessary(
objectToTranslate, Date[].class);
Assert.assertEquals(translated[1], new Date(0L),
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
e.printStackTrace();
Assert.fail("Unexpected exception during translation");
}
}
public void testConvertFromDateArray()
{
Date date1 = new Date(1184283822285L); //Friday July 13, 2007 9:43:42 AM + GMT+1000
Date date2 = new Date(0); //epoch + GMT + 1000
Date[] dates = new Date[]{date1, date2};
String[] converted = typeConverter.getAllAsText(dates);
Assert.assertTrue(converted[0].matches("20070713\\d\\d\\d\\d42.285\\+\\d\\d\\d\\d"));
Assert.assertTrue(converted[1].matches("19700101\\d\\d\\d\\d00.000\\+\\d\\d\\d\\d"));
}
public void testConvertToLdapName()
{
String object1 = "uid=amAdmin, ou = people, dc = myretsu,dc=com";
try
{
Object translated = typeConverter.convertIfNecessary(object1,
LdapName.class);
Assert.assertEquals(translated, new LdapName(object1),
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
catch (InvalidNameException e)
{
Assert.fail("Problem with test: Ldap name can't be parsed.", e);
}
}
public void testConvertToLdapNameArray()
{
String object1 = "uid=amAdmin, ou = people, dc = myretsu,dc=com";
String object2 = "uid=fred, ou = people, dc = myretsu,dc=com";
Object objectToTranslate = new String[]{object1, object2};
try
{
Object[] translated = (Object[]) typeConverter.convertIfNecessary(
objectToTranslate, LdapName[].class);
Assert.assertEquals(new LdapName(object2), translated[1],
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
catch (InvalidNameException e)
{
Assert.fail("Problem with test: Ldap name can't be parsed.", e);
}
}
public void testConvertToDistinguishedName()
{
String object1 = "uid=amAdmin, ou = people, dc = myretsu,dc=com";
try
{
Object translated = typeConverter.convertIfNecessary(object1,
DistinguishedName.class);
Assert.assertEquals(new DistinguishedName(object1), translated,
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToDistinguishedNameArray()
{
String object1 = "uid=amAdmin, ou = people, dc = myretsu,dc=com";
String object2 = "uid=fred, ou = people, dc = myretsu,dc=com";
Object objectToTranslate = new String[]{object1, object2};
try
{
Object[] translated = (Object[]) typeConverter.convertIfNecessary(
objectToTranslate, DistinguishedName[].class);
Assert.assertEquals(new DistinguishedName(object2), translated[1],
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToLong()
{
String object1 = "9887342";
try
{
Object translated = typeConverter.convertIfNecessary(object1,
Long.class);
Assert.assertEquals(9887342L, translated,
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToLongArray()
{
String object1 = "878787";
String object2 = "23948787";
Object objectToTranslate = new String[]{object1, object2};
try
{
Object[] translated = (Object[]) typeConverter.convertIfNecessary(
objectToTranslate, Long[].class);
Assert.assertEquals(23948787L, translated[1],
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToInteger()
{
String object1 = "9887342";
try
{
Object translated = typeConverter.convertIfNecessary(object1,
Integer.class);
Assert.assertEquals(9887342, translated,
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testConvertToIntegerArray()
{
String object1 = "878787";
String object2 = "23948787";
Object objectToTranslate = new String[]{object1, object2};
try
{
Object[] translated = (Object[]) typeConverter.convertIfNecessary(
objectToTranslate, Integer[].class);
Assert.assertEquals(23948787, translated[1],
"Unexpected translated value");
}
catch (TypeMismatchException e)
{
Assert.fail("Unexpected exception during translation", e);
}
}
public void testThrowsTypeMismatchExceptionWhenTypeTranslationFails()
{
String object1 = "asdlkjkalkjl";
Object[] translated;
try
{
translated = (Object[]) typeConverter.convertIfNecessary(object1,
LdapName.class);
Assert.fail("Should've thrown exception");
}
catch (TypeMismatchException e)
{
// Pass
}
}
public void testGetAsTextReturnsStringsUnchanged()
{
String string = "onetwothree123";
Assert.assertEquals(typeConverter.getAsText(string), "onetwothree123");
}
public void testGetAsTextReturnsNullWhenNoPropertyEditorRegistered()
{
Assert.assertNull(typeConverter.getAsText(new Foo("fooString")));
}
//Something there'll definitely be no property editor registered for
private class Foo
{
private String fooString;
public Foo(String fooString)
{
this.fooString = fooString;
}
}
}

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.springframework.ldap.core.DistinguishedName;
public class NameEditorTest extends TestCase
{
public void testThrowsExceptionWhenConstructorArgumentNotAName()
{
try
{
NameEditor nameEditor = new NameEditor(String.class);
fail("Should've thrown exception.");
}
catch (IllegalArgumentException e)
{
Assert.assertEquals(e.getMessage(),
"NameEditor can only be created for LdapName or DistinguishedName");
}
}
public void testGetAsText()
{
NameEditor editor = new NameEditor(DistinguishedName.class);
DistinguishedName name = new DistinguishedName("uid=zzz, ou=people");
editor.setValue(name);
Assert.assertEquals(editor.getAsText(), "uid=zzz, ou=people");
editor.setValue(null);
Assert.assertEquals(editor.getAsText(), "");
}
public void testSetAsText()
{
NameEditor editor = new NameEditor(DistinguishedName.class);
editor.setAsText("uid=zzz, ou=people");
DistinguishedName expected = new DistinguishedName("uid=zzz, ou=people");
Assert.assertEquals(editor.getValue(), expected);
}
}

View File

@@ -1,109 +0,0 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import junit.framework.TestCase;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapperFactory;
public class ReferencedEntryEditorFactoryTest extends TestCase
{
private LdapTemplate ldapTemplate;
private ObjectDirectoryMapperFactory odmFactory;
private ObjectDirectoryMapper odm;
private ReferencedEntryEditorFactory editorFactory;
protected void setUp() throws Exception
{
super.setUp();
ldapTemplate = EasyMock.createStrictMock(LdapTemplate.class);
odmFactory = EasyMock.createStrictMock(ObjectDirectoryMapperFactory.class);
odm = EasyMock.createStrictMock(ObjectDirectoryMapper.class);
editorFactory = new ReferencedEntryEditorFactory(null, ldapTemplate);
editorFactory.setObjectDirectoryMapperFactory(odmFactory);
}
/**
* Asserts that a referenced entry editor is created, after successful mapping of
* the referenced entity.
*
* @throws MappingException
*
*/
public void testReferencedEditorForClass_successfulMapping()
throws MappingException
{
EasyMock.expect(odmFactory.objectDirectoryMapperForClass(TestReferencedEntry.class)).andReturn(odm);
replayMocks();
try
{
editorFactory.referencedEntryEditorForClass(TestReferencedEntry.class);
//second call to return cached editor, verified by the fact that objectDirectoryMapperForClass
//is only called once.
editorFactory.referencedEntryEditorForClass(TestReferencedEntry.class);
}
catch (MappingException e)
{
fail();
}
verifyMocks();
}
/**
* Asserts that an exception is thrown when the referenced entity cannot be mapped
*
* @throws org.springframework.ldap.odm.mapping.MappingException
*
*
*/
public void testReferencedEditorForClass_unsuccessfulMapping()
throws MappingException
{
ReferencedEntryEditorFactory editorFactory =
new ReferencedEntryEditorFactory(null, ldapTemplate);
editorFactory.setObjectDirectoryMapperFactory(odmFactory);
EasyMock.expect(odmFactory.objectDirectoryMapperForClass(TestReferencedEntry.class))
.andThrow(new MappingException("unsuccessful mapping"));
replayMocks();
try
{
editorFactory.referencedEntryEditorForClass(TestReferencedEntry.class);
fail("Should've thrown exception");
}
catch (MappingException e)
{
//expected behaviour
}
verifyMocks();
}
private void verifyMocks()
{
EasyMock.verify(ldapTemplate);
EasyMock.verify(odmFactory);
EasyMock.verify(odm);
}
private void replayMocks()
{
EasyMock.replay(ldapTemplate);
EasyMock.replay(odmFactory);
EasyMock.replay(odm);
}
}

View File

@@ -1,113 +0,0 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import junit.framework.TestCase;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
public class ReferencedEntryEditorTest extends TestCase
{
private LdapTemplate ldapTemplate;
private ObjectDirectoryMapper objectDirectoryMapper;
private ReferencedEntryEditor editor;
protected void setUp() throws Exception
{
super.setUp();
ldapTemplate = EasyMock.createStrictMock(LdapTemplate.class);
objectDirectoryMapper = EasyMock.createStrictMock(ObjectDirectoryMapper.class);
editor = new ReferencedEntryEditor(new DistinguishedName("dc=example, dc=com"),
ldapTemplate, objectDirectoryMapper);
}
public void testGetAsText() throws MappingException
{
TestReferencedEntry referencedEntry = new TestReferencedEntry();
editor.setValue(referencedEntry);
EasyMock.expect(objectDirectoryMapper.buildDn(referencedEntry))
.andReturn(new DistinguishedName("uid=referencedEntry, ou=foobars"));
replayMocks();
editor.getAsText();
verifyMocks();
}
public void testGetAsTextThrowsRuntimeExceptionWhenMappingExceptionOccurs()
throws MappingException
{
TestReferencedEntry referencedEntry = new TestReferencedEntry();
editor.setValue(referencedEntry);
EasyMock.expect(objectDirectoryMapper.buildDn(referencedEntry))
.andThrow(new MappingException("no mapper"));
replayMocks();
try
{
editor.getAsText();
fail("Should've propogated mapping exception.");
}
catch (RuntimeException e)
{
//expected
}
verifyMocks();
}
public void testSetAsText()
{
DistinguishedName referenceName =
new DistinguishedName("uid = referencedEntry, ou=foobars");
TestReferencedEntry referencedEntry = new TestReferencedEntry();
EasyMock.expect(ldapTemplate.lookup(referenceName, objectDirectoryMapper))
.andReturn(referencedEntry);
replayMocks();
editor.setAsText("uid = referencedEntry, ou=foobars");
verifyMocks();
}
public void testSetAsTextRemovingBaseDn()
{
DistinguishedName referenceName =
new DistinguishedName("uid = referencedEntry, ou=foobars");
TestReferencedEntry referencedEntry = new TestReferencedEntry();
EasyMock.expect(ldapTemplate.lookup(referenceName, objectDirectoryMapper))
.andReturn(referencedEntry);
replayMocks();
editor.setAsText("uid = referencedEntry, ou=foobars, dc=example, dc=com");
verifyMocks();
}
private void verifyMocks()
{
EasyMock.verify(ldapTemplate);
EasyMock.verify(objectDirectoryMapper);
}
private void replayMocks()
{
EasyMock.replay(ldapTemplate);
EasyMock.replay(objectDirectoryMapper);
}
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
@NamingAttribute("cn")
@NamingSuffix({"ou = people", "dc = example", "dc=com"})
@ObjectClasses({"top", "person", "organizationalPerson", "inetorgperson"})
public class TestReferencedEntry
{
@DirAttribute("cn")
private String name;
@DirAttribute("addr")
private String address;
@DirAttribute("mail")
private String mail;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getMail()
{
return mail;
}
public void setMail(String mail)
{
this.mail = mail;
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.DistinguishedName;
import org.testng.Assert;
import javax.naming.ldap.LdapName;
import java.util.Date;
import java.util.List;
public class ValidConversionTypeTest extends TestCase
{
private static final Log LOGGER = LogFactory.getLog(ValidConversionTypeTest.class);
public void testListTypes()
{
LOGGER.debug(ValidConversionType.listTypes());
}
public void testIsValidConversionType()
{
Assert.assertFalse(ValidConversionType.isValidConversionType(List.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(byte[].class));
Assert.assertTrue(ValidConversionType.isValidConversionType(Boolean.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(String.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(String[].class));
Assert.assertTrue(ValidConversionType.isValidConversionType(Date.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(Date[].class));
Assert.assertTrue(ValidConversionType.isValidConversionType(LdapName.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(LdapName[].class));
Assert.assertTrue(ValidConversionType.isValidConversionType(DistinguishedName.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(DistinguishedName[].class));
Assert.assertTrue(ValidConversionType.isValidConversionType(Integer.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(Integer[].class));
Assert.assertTrue(ValidConversionType.isValidConversionType(Long.class));
Assert.assertTrue(ValidConversionType.isValidConversionType(Long[].class));
}
}

View File

@@ -20,7 +20,7 @@ public abstract class AbstractObjectDirectoryMapTest extends TestCase
public void testAttributeNameFor()
{
Assert.assertEquals(odm.attributeNameFor("Identifier"), "uid");
Assert.assertEquals(odm.attributeNameFor("identifier"), "uid");
}
public void testThrowsExceptionWhenClassArgumentIsNull() throws MappingException
@@ -45,7 +45,7 @@ public abstract class AbstractObjectDirectoryMapTest extends TestCase
public void testbeanPropertyNameFor()
{
Assert.assertEquals(odm.beanPropertyNameFor("uid"), "Identifier");
Assert.assertEquals(odm.beanPropertyNameFor("uid"), "identifier");
}
public void testBeanPropertyNames()

View File

@@ -8,8 +8,8 @@ package org.springframework.ldap.odm.mapping;
import junit.framework.TestCase;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.typeconversion.LdapTypeConverter;
import org.springframework.ldap.odm.typeconversion.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.entity.UnitTestPerson;
public class ObjectDirectoryMapperFactoryTest extends TestCase

View File

@@ -13,8 +13,8 @@ import org.apache.commons.logging.LogFactory;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.typeconversion.LdapTypeConverter;
import org.springframework.ldap.odm.typeconversion.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.entity.UnitTestPerson;
import javax.naming.Name;