Manual tweaks of patch from Jasper on July 26.
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +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;
|
||||
|
||||
|
||||
public class ReferencedEditorFactoryTest extends TestCase
|
||||
{
|
||||
|
||||
public void testReferencedEditorForClass()
|
||||
{
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.naming.InvalidNameException;
|
||||
import javax.naming.ldap.LdapName;
|
||||
|
||||
import junit.framework.Assert;
|
||||
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;
|
||||
|
||||
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("byte[] attributes should pass through unchanged.",
|
||||
Arrays.equals(objectToTranslate, translated));
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception: byte[] attributes should pass through unchanged.");
|
||||
}
|
||||
}
|
||||
|
||||
public void testConvertToBoolean()
|
||||
{
|
||||
String objectToTranslate = "true";
|
||||
try
|
||||
{
|
||||
Object translated = typeConverter.convertIfNecessary(
|
||||
objectToTranslate, Boolean.class);
|
||||
Assert.assertEquals(true, translated);
|
||||
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
public void testConvertFromBoolean()
|
||||
{
|
||||
boolean objectToTranslate = false;
|
||||
try
|
||||
{
|
||||
Object translated = typeConverter.getAsText(objectToTranslate);
|
||||
Assert.assertEquals(translated, "false");
|
||||
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
public void testConvertToString()
|
||||
{
|
||||
String objectToTranslate = "onetwothree";
|
||||
try
|
||||
{
|
||||
Object translated = typeConverter.convertIfNecessary(
|
||||
objectToTranslate, String.class);
|
||||
Assert.assertEquals("onetwothree", translated);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
public void testConvertToDate() throws ParseException
|
||||
{
|
||||
String object1 = "19700101100000.000+1000";
|
||||
|
||||
try
|
||||
{
|
||||
Object translated = typeConverter.convertIfNecessary(object1,
|
||||
Date.class);
|
||||
Assert.assertEquals(translated, new Date(0L));
|
||||
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
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));
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
catch (InvalidNameException e)
|
||||
{
|
||||
Assert.fail("Problem with test: Ldap name can't be parsed.");
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
catch (InvalidNameException e)
|
||||
{
|
||||
Assert.fail("Problem with test: Ldap name can't be parsed.");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
public void testConvertToLong()
|
||||
{
|
||||
String object1 = "9887342";
|
||||
try
|
||||
{
|
||||
Object translated = typeConverter.convertIfNecessary(object1,
|
||||
Long.class);
|
||||
Assert.assertEquals(9887342L, translated);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
public void testConvertToInteger()
|
||||
{
|
||||
String object1 = "9887342";
|
||||
|
||||
try
|
||||
{
|
||||
Object translated = typeConverter.convertIfNecessary(object1,
|
||||
Integer.class);
|
||||
Assert.assertEquals(9887342, translated);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
{
|
||||
Assert.fail("Unexpected exception during translation");
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.typeconversion;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user