diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverter.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverter.java index e69de29b..70377023 100644 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverter.java +++ b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverter.java @@ -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; + +/** + *

+ * 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: + *

  • + * + * + *
  • + *

    + *

    + * 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) + *

    + *

    + */ +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; + } + } + + +} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/NameEditor.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/NameEditor.java index e69de29b..70420c3b 100644 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/NameEditor.java +++ b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/NameEditor.java @@ -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 LdapName.class for String <--> LdapName and + * DistinguishedName.class 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); + } + } +} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditor.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditor.java index e69de29b..f55dafb0 100644 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditor.java +++ b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditor.java @@ -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)); + } +} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactory.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactory.java index e69de29b..2865bcc1 100644 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactory.java +++ b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactory.java @@ -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 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 + * MappingException 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 ObjectDirectoryMapperFactory to use when attempting + * to build a ReferencedEntryEditor. + */ + public void setObjectDirectoryMapperFactory(ObjectDirectoryMapperFactory + mapperFactory) + { + this.odmFactory = mapperFactory; + } +} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ValidConversionType.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ValidConversionType.java index e69de29b..e1f0fa1e 100644 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ValidConversionType.java +++ b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/typeconversion/ValidConversionType.java @@ -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; + } + + + +} + diff --git a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/attributetypes/ReferencedEditorFactoryTest.java b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/attributetypes/ReferencedEditorFactoryTest.java deleted file mode 100644 index 486200c5..00000000 --- a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/attributetypes/ReferencedEditorFactoryTest.java +++ /dev/null @@ -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); - } - -} diff --git a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverterTest.java b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverterTest.java index e69de29b..4428e556 100644 --- a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverterTest.java +++ b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/LdapTypeConverterTest.java @@ -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; + } + } + +} diff --git a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/NameEditorTest.java b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/NameEditorTest.java index e69de29b..19bca922 100644 --- a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/NameEditorTest.java +++ b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/NameEditorTest.java @@ -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); + + + } + + +} diff --git a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactoryTest.java b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactoryTest.java index e69de29b..7c66e680 100644 --- a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactoryTest.java +++ b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorFactoryTest.java @@ -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); + } + + +} diff --git a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorTest.java b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorTest.java index e69de29b..88f8252e 100644 --- a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorTest.java +++ b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ReferencedEntryEditorTest.java @@ -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); + } + + +} diff --git a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/TestReferencedEntry.java b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/TestReferencedEntry.java index e69de29b..b4dcbf61 100644 --- a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/TestReferencedEntry.java +++ b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/TestReferencedEntry.java @@ -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; + } +} \ No newline at end of file diff --git a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ValidConversionTypeTest.java b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ValidConversionTypeTest.java index e69de29b..da69ecc3 100644 --- a/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ValidConversionTypeTest.java +++ b/spring-ldap-odm/src/test/java/org/springframework/ldap/odm/typeconversion/ValidConversionTypeTest.java @@ -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)); + } + + +}