Convert to Linux Line Endings

This commit is contained in:
Josh Cummings
2022-07-21 15:09:00 -06:00
parent 15f0a8a79d
commit 9ded1758d6
270 changed files with 27844 additions and 27844 deletions

View File

@@ -1,132 +1,132 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.core;
import org.springframework.ldap.query.LdapQuery;
import javax.naming.Name;
import javax.naming.directory.SearchControls;
import java.util.List;
/**
* The OdmManager interface provides generic CRUD (create/read/update/delete)
* and searching operations against an LDAP directory.
* <p>
* Each managed Java class must be appropriately annotated using
* {@link org.springframework.ldap.odm.annotations}.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
* @author Mattias Hellborg Arthursson
*
* @see org.springframework.ldap.odm.annotations.Entry
* @see org.springframework.ldap.odm.annotations.Attribute
* @see org.springframework.ldap.odm.annotations.Id
* @see org.springframework.ldap.odm.annotations.Transient
* @deprecated This functionality is automatically available in LdapTemplate as of version 2.0
*/
public interface OdmManager {
/**
* Read a named entry from the LDAP directory.
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param dn The distinguished name of the entry to read from the LDAP directory.
* @return The entry as read from the directory
*
* @throws org.springframework.ldap.NamingException on error.
*/
<T> T read(Class<T> clazz, Name dn);
/**
* Create the given entry in the LDAP directory.
*
* @param entry The entry to be create, it must <em>not</em> already exist in the directory.
*
* @throws org.springframework.ldap.NamingException on error.
*/
void create(Object entry);
/**
* Update the given entry in the LDAP directory.
*
* @param entry The entry to update, it must already exist in the directory.
*
* @throws org.springframework.ldap.NamingException on error.
*/
void update(Object entry);
/**
* Delete an entry from the LDAP directory.
*
* @param entry The entry to delete, it must already exist in the directory.
*
* @throws org.springframework.ldap.NamingException on error.
*/
void delete(Object entry);
/**
* Find all entries in the LDAP directory of a given type.
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param base The root of the sub-tree at which to begin the search.
* @param searchControls The scope of the search.
* @return All entries that are of the type represented by the given
* Java class
*
* @throws org.springframework.ldap.NamingException on error.
*/
<T> List<T> findAll(Class<T> clazz, Name base, SearchControls searchControls);
/**
* Search for entries in the LDAP directory.
* <p>
* Only those entries that both match the given search filter and
* are represented by the given Java class are returned
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param base The root of the sub-tree at which to begin the search.
* @param filter An LDAP search filter.
* @param searchControls The scope of the search.
* @return All matching entries.
*
* @throws org.springframework.ldap.NamingException on error.
*
* @see <a href="https://java.sun.com/products/jndi/tutorial/basics/directory/filter.html">Sun's JNDI tutorial description of search filters.</a>
* @see <a href="https://www.rfc-editor.org/rfc/rfc4515.txt">LDAP: String Representation of Search Filters RFC.</a>
*/
<T> List<T> search(Class<T> clazz, Name base, String filter, SearchControls searchControls);
/**
* Search for entries in the LDAP directory.
* <p>
* Only those entries that both match the query search filter and
* are represented by the given Java class are returned.
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param query the LDAP query specification
* @return All matching entries.
*
* @throws org.springframework.ldap.NamingException on error.
* @see org.springframework.ldap.query.LdapQueryBuilder
*/
<T> List<T> search(Class<T> clazz, LdapQuery query);
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.core;
import org.springframework.ldap.query.LdapQuery;
import javax.naming.Name;
import javax.naming.directory.SearchControls;
import java.util.List;
/**
* The OdmManager interface provides generic CRUD (create/read/update/delete)
* and searching operations against an LDAP directory.
* <p>
* Each managed Java class must be appropriately annotated using
* {@link org.springframework.ldap.odm.annotations}.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
* @author Mattias Hellborg Arthursson
*
* @see org.springframework.ldap.odm.annotations.Entry
* @see org.springframework.ldap.odm.annotations.Attribute
* @see org.springframework.ldap.odm.annotations.Id
* @see org.springframework.ldap.odm.annotations.Transient
* @deprecated This functionality is automatically available in LdapTemplate as of version 2.0
*/
public interface OdmManager {
/**
* Read a named entry from the LDAP directory.
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param dn The distinguished name of the entry to read from the LDAP directory.
* @return The entry as read from the directory
*
* @throws org.springframework.ldap.NamingException on error.
*/
<T> T read(Class<T> clazz, Name dn);
/**
* Create the given entry in the LDAP directory.
*
* @param entry The entry to be create, it must <em>not</em> already exist in the directory.
*
* @throws org.springframework.ldap.NamingException on error.
*/
void create(Object entry);
/**
* Update the given entry in the LDAP directory.
*
* @param entry The entry to update, it must already exist in the directory.
*
* @throws org.springframework.ldap.NamingException on error.
*/
void update(Object entry);
/**
* Delete an entry from the LDAP directory.
*
* @param entry The entry to delete, it must already exist in the directory.
*
* @throws org.springframework.ldap.NamingException on error.
*/
void delete(Object entry);
/**
* Find all entries in the LDAP directory of a given type.
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param base The root of the sub-tree at which to begin the search.
* @param searchControls The scope of the search.
* @return All entries that are of the type represented by the given
* Java class
*
* @throws org.springframework.ldap.NamingException on error.
*/
<T> List<T> findAll(Class<T> clazz, Name base, SearchControls searchControls);
/**
* Search for entries in the LDAP directory.
* <p>
* Only those entries that both match the given search filter and
* are represented by the given Java class are returned
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param base The root of the sub-tree at which to begin the search.
* @param filter An LDAP search filter.
* @param searchControls The scope of the search.
* @return All matching entries.
*
* @throws org.springframework.ldap.NamingException on error.
*
* @see <a href="https://java.sun.com/products/jndi/tutorial/basics/directory/filter.html">Sun's JNDI tutorial description of search filters.</a>
* @see <a href="https://www.rfc-editor.org/rfc/rfc4515.txt">LDAP: String Representation of Search Filters RFC.</a>
*/
<T> List<T> search(Class<T> clazz, Name base, String filter, SearchControls searchControls);
/**
* Search for entries in the LDAP directory.
* <p>
* Only those entries that both match the query search filter and
* are represented by the given Java class are returned.
*
* @param <T> The Java type to return
* @param clazz The Java type to return
* @param query the LDAP query specification
* @return All matching entries.
*
* @throws org.springframework.ldap.NamingException on error.
* @see org.springframework.ldap.query.LdapQueryBuilder
*/
<T> List<T> search(Class<T> clazz, LdapQuery query);
}

View File

@@ -1,145 +1,145 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.core.impl;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.Filter;
import org.springframework.ldap.filter.HardcodedFilter;
import org.springframework.ldap.odm.core.OdmManager;
import org.springframework.ldap.odm.typeconversion.ConverterManager;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.StringUtils;
import javax.naming.Name;
import javax.naming.directory.SearchControls;
import java.util.List;
import java.util.Set;
/**
* An implementation of {@link org.springframework.ldap.odm.core.OdmManager} which
* uses {@link org.springframework.ldap.odm.typeconversion.ConverterManager} to
* convert between Java and LDAP representations of attribute values.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
* @author Mattias Hellborg Arthursson
* @deprecated This functionality is automatically available in LdapTemplate as of version 2.0
*/
public final class OdmManagerImpl implements OdmManager {
// The link to the LDAP directory
private final LdapTemplate ldapTemplate;
private DefaultObjectDirectoryMapper objectDirectoryMapper;
public OdmManagerImpl(ConverterManager converterManager,
LdapOperations ldapOperations,
Set<Class<?>> managedClasses) {
this.ldapTemplate = (LdapTemplate)ldapOperations;
objectDirectoryMapper = new DefaultObjectDirectoryMapper();
if(converterManager != null) {
objectDirectoryMapper.setConverterManager(converterManager);
}
if (managedClasses!=null) {
for (Class<?> managedClass: managedClasses) {
addManagedClass(managedClass);
}
}
this.ldapTemplate.setObjectDirectoryMapper(objectDirectoryMapper);
}
public OdmManagerImpl(ConverterManager converterManager,
ContextSource contextSource,
Set<Class<?>> managedClasses) {
this(converterManager, new LdapTemplate(contextSource), managedClasses);
}
public OdmManagerImpl(ConverterManager converterManager,
ContextSource contextSource) {
this(converterManager, contextSource, null);
}
/**
* Adds an {@link org.springframework.ldap.odm.annotations} annotated class to the set
* managed by this OdmManager.
*
* @param managedClass The class to add to the managed set.
*/
public void addManagedClass(Class<?> managedClass) {
objectDirectoryMapper.manageClass(managedClass);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#create(java.lang.Object)
*/
public <T> T read(Class<T> clazz, Name dn) {
return ldapTemplate.findByDn(dn, clazz);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#create(java.lang.Object)
*/
public void create(Object entry) {
ldapTemplate.create(entry);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#update(java.lang.Object, boolean)
*/
public void update(Object entry) {
ldapTemplate.update(entry);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#delete(javax.naming.Name)
*/
public void delete(Object entry) {
ldapTemplate.delete(entry);
}
/* (non-Javadoc)
* @see org.springframework.ldap.odm.core.OdmManager#search(java.lang.Class, javax.naming.Name, java.lang.String, javax.naming.directory.SearchControls)
*/
public <T> List<T> search(Class<T> managedClass, Name base, String filter, SearchControls scope) {
Filter searchFilter = null;
if(StringUtils.hasText(filter)) {
searchFilter = new HardcodedFilter(filter);
}
return ldapTemplate.find(base, searchFilter, scope, managedClass);
}
@Override
public <T> List<T> search(Class<T> clazz, LdapQuery query) {
return ldapTemplate.find(query, clazz);
}
public <T> List<T> findAll(Class<T> managedClass, Name base, SearchControls scope) {
return ldapTemplate.findAll(base, scope, managedClass);
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.core.impl;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.Filter;
import org.springframework.ldap.filter.HardcodedFilter;
import org.springframework.ldap.odm.core.OdmManager;
import org.springframework.ldap.odm.typeconversion.ConverterManager;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.StringUtils;
import javax.naming.Name;
import javax.naming.directory.SearchControls;
import java.util.List;
import java.util.Set;
/**
* An implementation of {@link org.springframework.ldap.odm.core.OdmManager} which
* uses {@link org.springframework.ldap.odm.typeconversion.ConverterManager} to
* convert between Java and LDAP representations of attribute values.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
* @author Mattias Hellborg Arthursson
* @deprecated This functionality is automatically available in LdapTemplate as of version 2.0
*/
public final class OdmManagerImpl implements OdmManager {
// The link to the LDAP directory
private final LdapTemplate ldapTemplate;
private DefaultObjectDirectoryMapper objectDirectoryMapper;
public OdmManagerImpl(ConverterManager converterManager,
LdapOperations ldapOperations,
Set<Class<?>> managedClasses) {
this.ldapTemplate = (LdapTemplate)ldapOperations;
objectDirectoryMapper = new DefaultObjectDirectoryMapper();
if(converterManager != null) {
objectDirectoryMapper.setConverterManager(converterManager);
}
if (managedClasses!=null) {
for (Class<?> managedClass: managedClasses) {
addManagedClass(managedClass);
}
}
this.ldapTemplate.setObjectDirectoryMapper(objectDirectoryMapper);
}
public OdmManagerImpl(ConverterManager converterManager,
ContextSource contextSource,
Set<Class<?>> managedClasses) {
this(converterManager, new LdapTemplate(contextSource), managedClasses);
}
public OdmManagerImpl(ConverterManager converterManager,
ContextSource contextSource) {
this(converterManager, contextSource, null);
}
/**
* Adds an {@link org.springframework.ldap.odm.annotations} annotated class to the set
* managed by this OdmManager.
*
* @param managedClass The class to add to the managed set.
*/
public void addManagedClass(Class<?> managedClass) {
objectDirectoryMapper.manageClass(managedClass);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#create(java.lang.Object)
*/
public <T> T read(Class<T> clazz, Name dn) {
return ldapTemplate.findByDn(dn, clazz);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#create(java.lang.Object)
*/
public void create(Object entry) {
ldapTemplate.create(entry);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#update(java.lang.Object, boolean)
*/
public void update(Object entry) {
ldapTemplate.update(entry);
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.odm.core.OdmManager#delete(javax.naming.Name)
*/
public void delete(Object entry) {
ldapTemplate.delete(entry);
}
/* (non-Javadoc)
* @see org.springframework.ldap.odm.core.OdmManager#search(java.lang.Class, javax.naming.Name, java.lang.String, javax.naming.directory.SearchControls)
*/
public <T> List<T> search(Class<T> managedClass, Name base, String filter, SearchControls scope) {
Filter searchFilter = null;
if(StringUtils.hasText(filter)) {
searchFilter = new HardcodedFilter(filter);
}
return ldapTemplate.find(base, searchFilter, scope, managedClass);
}
@Override
public <T> List<T> search(Class<T> clazz, LdapQuery query) {
return ldapTemplate.find(query, clazz);
}
public <T> List<T> findAll(Class<T> managedClass, Name base, SearchControls scope) {
return ldapTemplate.findAll(base, scope, managedClass);
}
}

View File

@@ -1,118 +1,118 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.core.impl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.typeconversion.ConverterManager;
import java.util.Set;
/**
* A Spring Factory bean which creates {@link OdmManagerImpl} instances.
* <p>
* Typical configuration would appear as follows:
* <pre>
* &lt;bean id="odmManager" class="org.springframework.ldap.odm.core.impl.OdmManagerImplFactoryBean"&gt;
* &lt;property name="converterManager" ref="converterManager" /&gt;
* &lt;property name="contextSource" ref="contextSource" /&gt;
* &lt;property name="managedClasses"&gt;
* &lt;set&gt;
* &lt;value&gt;org.myorg.myldapentries.Person&lt;/value&gt;
* &lt;value&gt;org.myorg.myldapentries.OrganizationalUnit&lt;/value&gt;
* &lt;/set&gt;
* &lt;/property&gt;
* &lt;/bean&gt;
* </pre>
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
* @deprecated This functionality is automatically available in LdapTemplate as of version 2.0
*/
public final class OdmManagerImplFactoryBean implements FactoryBean {
private LdapOperations ldapOperations = null;
private Set<Class<?>> managedClasses=null;
private ConverterManager converterManager=null;
/**
* Set the LdapOperations instance to use to interact with the LDAP directory.
*
* @param ldapOperations the LdapOperations instance to use.
*/
public void setLdapOperations(LdapOperations ldapOperations) {
this.ldapOperations = ldapOperations;
}
/**
* Set the ContextSource to use to interact with the LDAP directory.
* @param contextSource The ContextSource to use.
*/
public void setContextSource(ContextSource contextSource) {
this.ldapOperations = new LdapTemplate(contextSource);
}
/**
* Set the list of {@link org.springframework.ldap.odm.annotations}
* annotated classes the OdmManager will process.
* @param managedClasses The list of classes to manage.
*/
public void setManagedClasses(Set<Class<?>> managedClasses) {
this.managedClasses=managedClasses;
}
/**
* Set the ConverterManager to use to convert between LDAP
* and Java representations of attributes.
* @param converterManager The ConverterManager to use.
*/
public void setConverterManager(ConverterManager converterManager) {
this.converterManager=converterManager;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
public Object getObject() throws Exception {
if (ldapOperations==null) {
throw new FactoryBeanNotInitializedException("contextSource ldapOperations property has not been set");
}
if (managedClasses==null) {
throw new FactoryBeanNotInitializedException("managedClasses property has not been set");
}
if (converterManager==null) {
throw new FactoryBeanNotInitializedException("converterManager property has not been set");
}
return new OdmManagerImpl(converterManager, ldapOperations, managedClasses);
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
public Class<?> getObjectType() {
return OdmManagerImpl.class;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
public boolean isSingleton() {
return true;
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.core.impl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.typeconversion.ConverterManager;
import java.util.Set;
/**
* A Spring Factory bean which creates {@link OdmManagerImpl} instances.
* <p>
* Typical configuration would appear as follows:
* <pre>
* &lt;bean id="odmManager" class="org.springframework.ldap.odm.core.impl.OdmManagerImplFactoryBean"&gt;
* &lt;property name="converterManager" ref="converterManager" /&gt;
* &lt;property name="contextSource" ref="contextSource" /&gt;
* &lt;property name="managedClasses"&gt;
* &lt;set&gt;
* &lt;value&gt;org.myorg.myldapentries.Person&lt;/value&gt;
* &lt;value&gt;org.myorg.myldapentries.OrganizationalUnit&lt;/value&gt;
* &lt;/set&gt;
* &lt;/property&gt;
* &lt;/bean&gt;
* </pre>
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
* @deprecated This functionality is automatically available in LdapTemplate as of version 2.0
*/
public final class OdmManagerImplFactoryBean implements FactoryBean {
private LdapOperations ldapOperations = null;
private Set<Class<?>> managedClasses=null;
private ConverterManager converterManager=null;
/**
* Set the LdapOperations instance to use to interact with the LDAP directory.
*
* @param ldapOperations the LdapOperations instance to use.
*/
public void setLdapOperations(LdapOperations ldapOperations) {
this.ldapOperations = ldapOperations;
}
/**
* Set the ContextSource to use to interact with the LDAP directory.
* @param contextSource The ContextSource to use.
*/
public void setContextSource(ContextSource contextSource) {
this.ldapOperations = new LdapTemplate(contextSource);
}
/**
* Set the list of {@link org.springframework.ldap.odm.annotations}
* annotated classes the OdmManager will process.
* @param managedClasses The list of classes to manage.
*/
public void setManagedClasses(Set<Class<?>> managedClasses) {
this.managedClasses=managedClasses;
}
/**
* Set the ConverterManager to use to convert between LDAP
* and Java representations of attributes.
* @param converterManager The ConverterManager to use.
*/
public void setConverterManager(ConverterManager converterManager) {
this.converterManager=converterManager;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
public Object getObject() throws Exception {
if (ldapOperations==null) {
throw new FactoryBeanNotInitializedException("contextSource ldapOperations property has not been set");
}
if (managedClasses==null) {
throw new FactoryBeanNotInitializedException("managedClasses property has not been set");
}
if (converterManager==null) {
throw new FactoryBeanNotInitializedException("converterManager property has not been set");
}
return new OdmManagerImpl(converterManager, ldapOperations, managedClasses);
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
public Class<?> getObjectType() {
return OdmManagerImpl.class;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
public boolean isSingleton() {
return true;
}
}

View File

@@ -1,149 +1,149 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.tools;
import org.springframework.util.StringUtils;
/**
* Simple value class to hold the schema of an attribute.
* <p>
* It is only public to allow Freemarker access.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*/
public final class AttributeSchema {
private final String name;
private final String syntax;
private final boolean isMultiValued;
private final boolean isPrimitive;
private final String scalarType;
private final boolean isBinary;
private final boolean isArray;
public AttributeSchema(final String name, final String syntax, final boolean isMultiValued,
final boolean isPrimitive, final boolean isBinary, final boolean isArray, final String scalarType) {
this.name = name;
this.syntax = syntax;
this.isMultiValued = isMultiValued;
this.isPrimitive = isPrimitive;
this.scalarType = scalarType;
this.isBinary = isBinary;
this.isArray = isArray;
}
public boolean getIsArray() {
return isArray;
}
public boolean getIsBinary() {
return isBinary;
}
public boolean getIsPrimitive() {
return isPrimitive;
}
public String getScalarType() {
return scalarType;
}
public String getName() {
return name;
}
public String getJavaName() {
return StringUtils.replace(name, "-", "");
}
public String getSyntax() {
return syntax;
}
public boolean getIsMultiValued() {
return isMultiValued;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format(
"{ name=%1$s, syntax=%2$s, isMultiValued=%3$s, isPrimitive=%4$s, isBinary=%5$s, isArray=%6$s, scalarType=%7$s }",
name, syntax, isMultiValued, isPrimitive, isBinary, isArray, scalarType);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (isArray ? 1231 : 1237);
result = prime * result + (isBinary ? 1231 : 1237);
result = prime * result + (isMultiValued ? 1231 : 1237);
result = prime * result + (isPrimitive ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((scalarType == null) ? 0 : scalarType.hashCode());
result = prime * result + ((syntax == null) ? 0 : syntax.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AttributeSchema other = (AttributeSchema) obj;
if (isArray != other.isArray)
return false;
if (isBinary != other.isBinary)
return false;
if (isMultiValued != other.isMultiValued)
return false;
if (isPrimitive != other.isPrimitive)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (scalarType == null) {
if (other.scalarType != null)
return false;
} else if (!scalarType.equals(other.scalarType))
return false;
if (syntax == null) {
if (other.syntax != null)
return false;
} else if (!syntax.equals(other.syntax))
return false;
return true;
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.tools;
import org.springframework.util.StringUtils;
/**
* Simple value class to hold the schema of an attribute.
* <p>
* It is only public to allow Freemarker access.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*/
public final class AttributeSchema {
private final String name;
private final String syntax;
private final boolean isMultiValued;
private final boolean isPrimitive;
private final String scalarType;
private final boolean isBinary;
private final boolean isArray;
public AttributeSchema(final String name, final String syntax, final boolean isMultiValued,
final boolean isPrimitive, final boolean isBinary, final boolean isArray, final String scalarType) {
this.name = name;
this.syntax = syntax;
this.isMultiValued = isMultiValued;
this.isPrimitive = isPrimitive;
this.scalarType = scalarType;
this.isBinary = isBinary;
this.isArray = isArray;
}
public boolean getIsArray() {
return isArray;
}
public boolean getIsBinary() {
return isBinary;
}
public boolean getIsPrimitive() {
return isPrimitive;
}
public String getScalarType() {
return scalarType;
}
public String getName() {
return name;
}
public String getJavaName() {
return StringUtils.replace(name, "-", "");
}
public String getSyntax() {
return syntax;
}
public boolean getIsMultiValued() {
return isMultiValued;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format(
"{ name=%1$s, syntax=%2$s, isMultiValued=%3$s, isPrimitive=%4$s, isBinary=%5$s, isArray=%6$s, scalarType=%7$s }",
name, syntax, isMultiValued, isPrimitive, isBinary, isArray, scalarType);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (isArray ? 1231 : 1237);
result = prime * result + (isBinary ? 1231 : 1237);
result = prime * result + (isMultiValued ? 1231 : 1237);
result = prime * result + (isPrimitive ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((scalarType == null) ? 0 : scalarType.hashCode());
result = prime * result + ((syntax == null) ? 0 : syntax.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AttributeSchema other = (AttributeSchema) obj;
if (isArray != other.isArray)
return false;
if (isBinary != other.isBinary)
return false;
if (isMultiValued != other.isMultiValued)
return false;
if (isPrimitive != other.isPrimitive)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (scalarType == null) {
if (other.scalarType != null)
return false;
} else if (!scalarType.equals(other.scalarType))
return false;
if (syntax == null) {
if (other.syntax != null)
return false;
} else if (!syntax.equals(other.syntax))
return false;
return true;
}
}

View File

@@ -1,114 +1,114 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.tools;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Simple value class to hold the schema of an object class
* <p>
* It is public only to allow Freemarker access.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*/
public final class ObjectSchema {
private final Set<AttributeSchema> must = new HashSet<AttributeSchema>();
private final Set<AttributeSchema> may = new HashSet<AttributeSchema>();
private final Set<String> objectClass = new HashSet<String>();
public void addMust(AttributeSchema must) {
// if may attributes contain must attribute, remove from may and add to must
if (this.may.contains(must)) {
this.may.remove(must);
}
this.must.add(must);
}
public void addMay(AttributeSchema may) {
// only add may if not in must
if (!this.must.contains(may)) {
this.may.add(may);
}
}
public void addObjectClass(String objectClass) {
this.objectClass.add(objectClass);
}
public Set<AttributeSchema> getMust() {
return Collections.unmodifiableSet(must);
}
public Set<AttributeSchema> getMay() {
return Collections.unmodifiableSet(may);
}
public Set<String> getObjectClass() {
return Collections.unmodifiableSet(objectClass);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("objectClass=%1$s | must=%2$s | may=%3$s", objectClass, must, may);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((may == null) ? 0 : may.hashCode());
result = prime * result + ((must == null) ? 0 : must.hashCode());
result = prime * result + ((objectClass == null) ? 0 : objectClass.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ObjectSchema other = (ObjectSchema) obj;
if (may == null) {
if (other.may != null)
return false;
} else if (!may.equals(other.may))
return false;
if (must == null) {
if (other.must != null)
return false;
} else if (!must.equals(other.must))
return false;
if (objectClass == null) {
if (other.objectClass != null)
return false;
} else if (!objectClass.equals(other.objectClass))
return false;
return true;
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.tools;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Simple value class to hold the schema of an object class
* <p>
* It is public only to allow Freemarker access.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*/
public final class ObjectSchema {
private final Set<AttributeSchema> must = new HashSet<AttributeSchema>();
private final Set<AttributeSchema> may = new HashSet<AttributeSchema>();
private final Set<String> objectClass = new HashSet<String>();
public void addMust(AttributeSchema must) {
// if may attributes contain must attribute, remove from may and add to must
if (this.may.contains(must)) {
this.may.remove(must);
}
this.must.add(must);
}
public void addMay(AttributeSchema may) {
// only add may if not in must
if (!this.must.contains(may)) {
this.may.add(may);
}
}
public void addObjectClass(String objectClass) {
this.objectClass.add(objectClass);
}
public Set<AttributeSchema> getMust() {
return Collections.unmodifiableSet(must);
}
public Set<AttributeSchema> getMay() {
return Collections.unmodifiableSet(may);
}
public Set<String> getObjectClass() {
return Collections.unmodifiableSet(objectClass);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("objectClass=%1$s | must=%2$s | may=%3$s", objectClass, must, may);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((may == null) ? 0 : may.hashCode());
result = prime * result + ((must == null) ? 0 : must.hashCode());
result = prime * result + ((objectClass == null) ? 0 : objectClass.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ObjectSchema other = (ObjectSchema) obj;
if (may == null) {
if (other.may != null)
return false;
} else if (!may.equals(other.may))
return false;
if (must == null) {
if (other.must != null)
return false;
} else if (!must.equals(other.must))
return false;
if (objectClass == null) {
if (other.objectClass != null)
return false;
} else if (!objectClass.equals(other.objectClass))
return false;
return true;
}
}

View File

@@ -1,186 +1,186 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.tools;
import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import java.util.HashSet;
import java.util.Set;
// Processes LDAP Schema
/* package */ final class SchemaReader {
private final DirContext schemaContext;
private final SyntaxToJavaClass syntaxToJavaClass;
private final Set<String> binarySet;
public SchemaReader(DirContext schemaContext, SyntaxToJavaClass syntaxToJavaClass, Set<String> binarySet) {
this.schemaContext = schemaContext;
this.syntaxToJavaClass = syntaxToJavaClass;
this.binarySet = binarySet;
}
// Get the object schema for the given object classes
public ObjectSchema getObjectSchema(Set<String> objectClasses)
throws NamingException, ClassNotFoundException {
ObjectSchema result = new ObjectSchema();
createObjectClass(objectClasses, schemaContext, result);
return result;
}
private enum SchemaAttributeType {
SUP, MUST, MAY, UNKNOWN
}
private SchemaAttributeType getSchemaAttributeType(String type) {
SchemaAttributeType result = SchemaAttributeType.UNKNOWN;
if (type.equals("SUP")) {
result = SchemaAttributeType.SUP;
} else {
if (type.equals("MUST")) {
result = SchemaAttributeType.MUST;
} else {
if (type.equals("MAY")) {
result = SchemaAttributeType.MAY;
}
}
}
return result;
}
private AttributeSchema createAttributeSchema(String name, DirContext schemaContext)
throws NamingException, ClassNotFoundException {
// Get the schema definition
Attributes attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + name);
String syntax = null;
while(syntax == null) {
Attribute syntaxAttribute = attributeSchema.get("SYNTAX");
if(syntaxAttribute != null) {
syntax = ((String)syntaxAttribute.get()).split("\\{")[0];
} else {
// Try to recursively retrieve syntax for super definition.
Attribute supAttribute = attributeSchema.get("SUP");
if(supAttribute == null) {
// Well, at least we tried
throw new IllegalArgumentException("Unable to get syntax definition for attribute " + name);
} else {
attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + supAttribute.get());
}
}
}
// Is it binary?
boolean isBinary=binarySet.contains(syntax);
// Use it to look up the required Java class
ClassInfo classInfo = syntaxToJavaClass.getClassInfo(syntax);
// Now we can set the java class
String javaClassName = null;
boolean isPrimitive = false;
boolean isArray = false;
if (classInfo!=null) {
javaClassName=classInfo.getClassName();
Class<?> javaClass=Class.forName(classInfo.getFullClassName());
javaClassName=javaClass.getSimpleName();
isPrimitive=javaClass.isPrimitive();
isArray=javaClass.isArray();
} else {
if (isBinary) {
javaClassName="byte[]";
isPrimitive=false;
isArray=true;
} else {
javaClassName="String";
isPrimitive=false;
isArray=false;
}
}
return new AttributeSchema(name, syntax,
attributeSchema.get("SINGLE-VALUE") == null,
isPrimitive, isBinary, isArray, javaClassName);
}
// Recursively extract schema from the directory and process it
private void createObjectClass(Set<String> objectClasses, DirContext schemaContext, ObjectSchema schema)
throws NamingException, ClassNotFoundException {
// Super classes
Set<String> supList = new HashSet<String>();
// For each of the given object classes
for (String objectClass : objectClasses) {
// Add to set of included object classes
schema.addObjectClass(objectClass);
// Grab the LDAP schema of the object class
Attributes attributes = schemaContext.getAttributes("ClassDefinition/" + objectClass);
NamingEnumeration<? extends Attribute> valuesEnumeration = attributes.getAll();
// Loop through each of the attributes
while (valuesEnumeration.hasMoreElements()) {
Attribute currentAttribute = valuesEnumeration.nextElement();
// Get the attribute name and lower case it (as this is all case indep)
String currentId = currentAttribute.getID().toUpperCase();
// Is this a MUST, MAY or SUP attribute
SchemaAttributeType type = getSchemaAttributeType(currentId);
// Loop through all the values
NamingEnumeration<?> currentValues = currentAttribute.getAll();
while (currentValues.hasMoreElements()) {
String currentValue = (String)currentValues.nextElement();
switch (type) {
case SUP:
// Its a super class
String lowerCased=currentValue.toLowerCase();
if (!schema.getObjectClass().contains(lowerCased)) {
supList.add(lowerCased);
}
break;
case MUST:
// Add must attribute
schema.addMust(createAttributeSchema(currentValue, schemaContext));
break;
case MAY:
// Add may attribute
schema.addMay(createAttributeSchema(currentValue, schemaContext));
break;
default:
// Nothing to do
}
}
}
// Recurse for super classes
createObjectClass(supList, schemaContext, schema);
}
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.tools;
import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import java.util.HashSet;
import java.util.Set;
// Processes LDAP Schema
/* package */ final class SchemaReader {
private final DirContext schemaContext;
private final SyntaxToJavaClass syntaxToJavaClass;
private final Set<String> binarySet;
public SchemaReader(DirContext schemaContext, SyntaxToJavaClass syntaxToJavaClass, Set<String> binarySet) {
this.schemaContext = schemaContext;
this.syntaxToJavaClass = syntaxToJavaClass;
this.binarySet = binarySet;
}
// Get the object schema for the given object classes
public ObjectSchema getObjectSchema(Set<String> objectClasses)
throws NamingException, ClassNotFoundException {
ObjectSchema result = new ObjectSchema();
createObjectClass(objectClasses, schemaContext, result);
return result;
}
private enum SchemaAttributeType {
SUP, MUST, MAY, UNKNOWN
}
private SchemaAttributeType getSchemaAttributeType(String type) {
SchemaAttributeType result = SchemaAttributeType.UNKNOWN;
if (type.equals("SUP")) {
result = SchemaAttributeType.SUP;
} else {
if (type.equals("MUST")) {
result = SchemaAttributeType.MUST;
} else {
if (type.equals("MAY")) {
result = SchemaAttributeType.MAY;
}
}
}
return result;
}
private AttributeSchema createAttributeSchema(String name, DirContext schemaContext)
throws NamingException, ClassNotFoundException {
// Get the schema definition
Attributes attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + name);
String syntax = null;
while(syntax == null) {
Attribute syntaxAttribute = attributeSchema.get("SYNTAX");
if(syntaxAttribute != null) {
syntax = ((String)syntaxAttribute.get()).split("\\{")[0];
} else {
// Try to recursively retrieve syntax for super definition.
Attribute supAttribute = attributeSchema.get("SUP");
if(supAttribute == null) {
// Well, at least we tried
throw new IllegalArgumentException("Unable to get syntax definition for attribute " + name);
} else {
attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + supAttribute.get());
}
}
}
// Is it binary?
boolean isBinary=binarySet.contains(syntax);
// Use it to look up the required Java class
ClassInfo classInfo = syntaxToJavaClass.getClassInfo(syntax);
// Now we can set the java class
String javaClassName = null;
boolean isPrimitive = false;
boolean isArray = false;
if (classInfo!=null) {
javaClassName=classInfo.getClassName();
Class<?> javaClass=Class.forName(classInfo.getFullClassName());
javaClassName=javaClass.getSimpleName();
isPrimitive=javaClass.isPrimitive();
isArray=javaClass.isArray();
} else {
if (isBinary) {
javaClassName="byte[]";
isPrimitive=false;
isArray=true;
} else {
javaClassName="String";
isPrimitive=false;
isArray=false;
}
}
return new AttributeSchema(name, syntax,
attributeSchema.get("SINGLE-VALUE") == null,
isPrimitive, isBinary, isArray, javaClassName);
}
// Recursively extract schema from the directory and process it
private void createObjectClass(Set<String> objectClasses, DirContext schemaContext, ObjectSchema schema)
throws NamingException, ClassNotFoundException {
// Super classes
Set<String> supList = new HashSet<String>();
// For each of the given object classes
for (String objectClass : objectClasses) {
// Add to set of included object classes
schema.addObjectClass(objectClass);
// Grab the LDAP schema of the object class
Attributes attributes = schemaContext.getAttributes("ClassDefinition/" + objectClass);
NamingEnumeration<? extends Attribute> valuesEnumeration = attributes.getAll();
// Loop through each of the attributes
while (valuesEnumeration.hasMoreElements()) {
Attribute currentAttribute = valuesEnumeration.nextElement();
// Get the attribute name and lower case it (as this is all case indep)
String currentId = currentAttribute.getID().toUpperCase();
// Is this a MUST, MAY or SUP attribute
SchemaAttributeType type = getSchemaAttributeType(currentId);
// Loop through all the values
NamingEnumeration<?> currentValues = currentAttribute.getAll();
while (currentValues.hasMoreElements()) {
String currentValue = (String)currentValues.nextElement();
switch (type) {
case SUP:
// Its a super class
String lowerCased=currentValue.toLowerCase();
if (!schema.getObjectClass().contains(lowerCased)) {
supList.add(lowerCased);
}
break;
case MUST:
// Add must attribute
schema.addMust(createAttributeSchema(currentValue, schemaContext));
break;
case MAY:
// Add may attribute
schema.addMay(createAttributeSchema(currentValue, schemaContext));
break;
default:
// Nothing to do
}
}
}
// Recurse for super classes
createObjectClass(supList, schemaContext, schema);
}
}
}

View File

@@ -1,239 +1,239 @@
package org.springframework.ldap.odm.tools;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import javax.naming.AuthenticationException;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.io.PrintStream;
import java.util.Hashtable;
/**
* A simple utility to list LDAP directory schema.
* <p>
* <code>SchemaViewer</code> takes the following flags:
* <ul>
* <li><code>-h,--help&lt;</code> Print this help message</li>
* <li><code>-l,--url &lt;arg&gt;</code> Ldap url of directory to bind to (defaults to ldap://127.0.0.1:389)</li>
* <li><code>-u,--username &lt;arg&gt;</code> DN to bind with (defaults to "")</li>
* <li><code>-p,--password &lt;arg&gt;</code> Password to bind with (defaults to "")</li>
* <li><code>-o,--objectclass &lt;arg&gt;</code> Object class name or ? for all. Print object class schema</li>
* <li><code>-a,--attribute &lt;arg&gt;</code> Attribute name or ? for all. Print attribute schema</li>
* <li><code>-s,--syntax &lt;arg&gt;</code> Syntax or ? for all. Print syntax</li>
* </ul>
*
* Only one of <code>-a</code>, <code>-o</code> and <code>-s</code> should be specified.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*
*/
public final class SchemaViewer {
private static final String DEFAULT_URL="ldap://127.0.0.1:389";
private enum Flag {
URL("l", "url"),
USERNAME("u", "username"),
PASSWORD("p", "password"),
OBJECTCLASS("o", "objectclass"),
ATTRIBUTE("a", "attribute"),
SYNTAX("s", "syntax"),
HELP("h", "help"),
ERROR("e", "error");
private String shortName;
private String longName;
private Flag(String shortName, String longName) {
this.shortName = shortName;
this.longName = longName;
}
public String getShort() {
return shortName;
}
public String getLong() {
return longName;
}
@Override
public String toString() {
return String.format("short=%1$s, long=%2$s", shortName, longName);
}
}
private enum SchemaContext {
OBJECTCLASS("ClassDefinition"), ATTRIBUTE("AttributeDefinition"), SYNTAX("SyntaxDefinition");
private String value;
private SchemaContext(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return String.format("value=%1$s", value);
}
}
private static final Options DEFAULT_OPTIONS = new Options();
static {
DEFAULT_OPTIONS.addOption(Flag.URL.getShort(), Flag.URL.getLong(), true, "Ldap url (defaults to " + DEFAULT_URL + ")");
DEFAULT_OPTIONS.addOption(Flag.USERNAME.getShort(), Flag.USERNAME.getLong(), true, "DN to bind with (defaults to \"\")");
DEFAULT_OPTIONS.addOption(Flag.PASSWORD.getShort(), Flag.PASSWORD.getLong(), true, "Password to bind with defaults to \"\")");
DEFAULT_OPTIONS.addOption(Flag.OBJECTCLASS.getShort(), Flag.OBJECTCLASS.getLong(), true,
"Object class name or ? for all. Print object class schema");
DEFAULT_OPTIONS.addOption(Flag.ATTRIBUTE.getShort(), Flag.ATTRIBUTE.getLong(), true,
"Attribute name or ? for all. Print attribute schema");
DEFAULT_OPTIONS.addOption(Flag.SYNTAX.getShort(), Flag.SYNTAX.getLong(), true,
"Syntax OID or ? for all. Print attribute syntax");
DEFAULT_OPTIONS.addOption(Flag.HELP.getShort(), Flag.HELP.getLong(), false, "Print this help message");
DEFAULT_OPTIONS.addOption(Flag.ERROR.getShort(), Flag.ERROR.getLong(), false, "Send output to standard error");
}
/**
* Not to be instantiated.
*/
private SchemaViewer() {
}
private static void printAttrs(Attributes attrs) throws NamingException {
NamingEnumeration<? extends Attribute> attrsEnum = attrs.getAll();
while (attrsEnum.hasMore()) {
Attribute currentAttr = attrsEnum.next();
outstream.print(String.format("%1$s:", currentAttr.getID()));
NamingEnumeration<?> valuesEnum = currentAttr.getAll();
while (valuesEnum.hasMoreElements()) {
outstream.print(String.format("%1$s ", valuesEnum.nextElement().toString()));
}
outstream.println();
}
}
private static void printObject(String contextName, String schemaName, DirContext schemaContext)
throws NameNotFoundException, NamingException {
DirContext oContext = (DirContext)schemaContext.lookup(contextName + "/" + schemaName);
outstream.println("NAME:" + schemaName);
printAttrs(oContext.getAttributes(""));
}
private static void printSchema(String contextName, DirContext schemaContext) throws NameNotFoundException,
NamingException {
outstream.println();
NamingEnumeration<NameClassPair> schemaList = schemaContext.list(contextName);
while (schemaList.hasMore()) {
NameClassPair ncp = schemaList.nextElement();
printObject(contextName, ncp.getName(), schemaContext);
outstream.println();
}
outstream.println();
}
private static void print(String optionValue, String contextName, DirContext schemaContext)
throws NameNotFoundException, NamingException {
if (optionValue.equals(WILDCARD)) {
printSchema(contextName, schemaContext);
} else {
printObject(contextName, optionValue, schemaContext);
}
}
private static PrintStream outstream=System.out;
private final static String WILDCARD = "?";
public static void main(String[] argv) {
CommandLineParser parser = new PosixParser();
CommandLine cmd = null;
try {
cmd = parser.parse(DEFAULT_OPTIONS, argv);
} catch (ParseException e) {
System.out.println(e.getMessage());
System.exit(1);
}
if (cmd.hasOption(Flag.HELP.getShort())) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(120, SchemaViewer.class.getSimpleName(), null, DEFAULT_OPTIONS, null, true);
System.exit(0);
}
if (cmd.hasOption(Flag.ERROR.getShort())) {
outstream=System.err;
}
String url = cmd.getOptionValue(Flag.URL.getShort(), DEFAULT_URL);
String user = cmd.getOptionValue(Flag.USERNAME.getShort(), "");
String pass = cmd.getOptionValue(Flag.PASSWORD.getShort(), "");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.PROVIDER_URL, url);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
if (user != null) {
env.put(Context.SECURITY_PRINCIPAL, user);
}
if (pass != null) {
env.put(Context.SECURITY_CREDENTIALS, pass);
if (user == null) {
System.err.println("You must specify a user if you specify a password");
System.exit(1);
}
}
try {
DirContext context = new InitialDirContext(env);
DirContext schemaContext = context.getSchema("");
if (cmd.hasOption(Flag.OBJECTCLASS.getShort())) {
print(cmd.getOptionValue(Flag.OBJECTCLASS.getShort()), SchemaContext.OBJECTCLASS.getValue(),
schemaContext);
}
if (cmd.hasOption(Flag.ATTRIBUTE.getShort())) {
print(cmd.getOptionValue(Flag.ATTRIBUTE.getShort()), SchemaContext.ATTRIBUTE.getValue(), schemaContext);
}
if (cmd.hasOption(Flag.SYNTAX.getShort())) {
print(cmd.getOptionValue(Flag.SYNTAX.getShort()), SchemaContext.SYNTAX.getValue(), schemaContext);
}
} catch (AuthenticationException e) {
System.err.println(String.format("Failed to bind to ldap server at %1$s", url));
} catch (CommunicationException e) {
System.err.println(String.format("Failed to contact ldap server at %1$s", url));
} catch (NameNotFoundException e) {
System.err.println(String.format("Can't find object %1$s", e.getMessage()));
} catch (NamingException e) {
System.err.println(e.toString());
}
}
}
package org.springframework.ldap.odm.tools;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import javax.naming.AuthenticationException;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.io.PrintStream;
import java.util.Hashtable;
/**
* A simple utility to list LDAP directory schema.
* <p>
* <code>SchemaViewer</code> takes the following flags:
* <ul>
* <li><code>-h,--help&lt;</code> Print this help message</li>
* <li><code>-l,--url &lt;arg&gt;</code> Ldap url of directory to bind to (defaults to ldap://127.0.0.1:389)</li>
* <li><code>-u,--username &lt;arg&gt;</code> DN to bind with (defaults to "")</li>
* <li><code>-p,--password &lt;arg&gt;</code> Password to bind with (defaults to "")</li>
* <li><code>-o,--objectclass &lt;arg&gt;</code> Object class name or ? for all. Print object class schema</li>
* <li><code>-a,--attribute &lt;arg&gt;</code> Attribute name or ? for all. Print attribute schema</li>
* <li><code>-s,--syntax &lt;arg&gt;</code> Syntax or ? for all. Print syntax</li>
* </ul>
*
* Only one of <code>-a</code>, <code>-o</code> and <code>-s</code> should be specified.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*
*/
public final class SchemaViewer {
private static final String DEFAULT_URL="ldap://127.0.0.1:389";
private enum Flag {
URL("l", "url"),
USERNAME("u", "username"),
PASSWORD("p", "password"),
OBJECTCLASS("o", "objectclass"),
ATTRIBUTE("a", "attribute"),
SYNTAX("s", "syntax"),
HELP("h", "help"),
ERROR("e", "error");
private String shortName;
private String longName;
private Flag(String shortName, String longName) {
this.shortName = shortName;
this.longName = longName;
}
public String getShort() {
return shortName;
}
public String getLong() {
return longName;
}
@Override
public String toString() {
return String.format("short=%1$s, long=%2$s", shortName, longName);
}
}
private enum SchemaContext {
OBJECTCLASS("ClassDefinition"), ATTRIBUTE("AttributeDefinition"), SYNTAX("SyntaxDefinition");
private String value;
private SchemaContext(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return String.format("value=%1$s", value);
}
}
private static final Options DEFAULT_OPTIONS = new Options();
static {
DEFAULT_OPTIONS.addOption(Flag.URL.getShort(), Flag.URL.getLong(), true, "Ldap url (defaults to " + DEFAULT_URL + ")");
DEFAULT_OPTIONS.addOption(Flag.USERNAME.getShort(), Flag.USERNAME.getLong(), true, "DN to bind with (defaults to \"\")");
DEFAULT_OPTIONS.addOption(Flag.PASSWORD.getShort(), Flag.PASSWORD.getLong(), true, "Password to bind with defaults to \"\")");
DEFAULT_OPTIONS.addOption(Flag.OBJECTCLASS.getShort(), Flag.OBJECTCLASS.getLong(), true,
"Object class name or ? for all. Print object class schema");
DEFAULT_OPTIONS.addOption(Flag.ATTRIBUTE.getShort(), Flag.ATTRIBUTE.getLong(), true,
"Attribute name or ? for all. Print attribute schema");
DEFAULT_OPTIONS.addOption(Flag.SYNTAX.getShort(), Flag.SYNTAX.getLong(), true,
"Syntax OID or ? for all. Print attribute syntax");
DEFAULT_OPTIONS.addOption(Flag.HELP.getShort(), Flag.HELP.getLong(), false, "Print this help message");
DEFAULT_OPTIONS.addOption(Flag.ERROR.getShort(), Flag.ERROR.getLong(), false, "Send output to standard error");
}
/**
* Not to be instantiated.
*/
private SchemaViewer() {
}
private static void printAttrs(Attributes attrs) throws NamingException {
NamingEnumeration<? extends Attribute> attrsEnum = attrs.getAll();
while (attrsEnum.hasMore()) {
Attribute currentAttr = attrsEnum.next();
outstream.print(String.format("%1$s:", currentAttr.getID()));
NamingEnumeration<?> valuesEnum = currentAttr.getAll();
while (valuesEnum.hasMoreElements()) {
outstream.print(String.format("%1$s ", valuesEnum.nextElement().toString()));
}
outstream.println();
}
}
private static void printObject(String contextName, String schemaName, DirContext schemaContext)
throws NameNotFoundException, NamingException {
DirContext oContext = (DirContext)schemaContext.lookup(contextName + "/" + schemaName);
outstream.println("NAME:" + schemaName);
printAttrs(oContext.getAttributes(""));
}
private static void printSchema(String contextName, DirContext schemaContext) throws NameNotFoundException,
NamingException {
outstream.println();
NamingEnumeration<NameClassPair> schemaList = schemaContext.list(contextName);
while (schemaList.hasMore()) {
NameClassPair ncp = schemaList.nextElement();
printObject(contextName, ncp.getName(), schemaContext);
outstream.println();
}
outstream.println();
}
private static void print(String optionValue, String contextName, DirContext schemaContext)
throws NameNotFoundException, NamingException {
if (optionValue.equals(WILDCARD)) {
printSchema(contextName, schemaContext);
} else {
printObject(contextName, optionValue, schemaContext);
}
}
private static PrintStream outstream=System.out;
private final static String WILDCARD = "?";
public static void main(String[] argv) {
CommandLineParser parser = new PosixParser();
CommandLine cmd = null;
try {
cmd = parser.parse(DEFAULT_OPTIONS, argv);
} catch (ParseException e) {
System.out.println(e.getMessage());
System.exit(1);
}
if (cmd.hasOption(Flag.HELP.getShort())) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(120, SchemaViewer.class.getSimpleName(), null, DEFAULT_OPTIONS, null, true);
System.exit(0);
}
if (cmd.hasOption(Flag.ERROR.getShort())) {
outstream=System.err;
}
String url = cmd.getOptionValue(Flag.URL.getShort(), DEFAULT_URL);
String user = cmd.getOptionValue(Flag.USERNAME.getShort(), "");
String pass = cmd.getOptionValue(Flag.PASSWORD.getShort(), "");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.PROVIDER_URL, url);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
if (user != null) {
env.put(Context.SECURITY_PRINCIPAL, user);
}
if (pass != null) {
env.put(Context.SECURITY_CREDENTIALS, pass);
if (user == null) {
System.err.println("You must specify a user if you specify a password");
System.exit(1);
}
}
try {
DirContext context = new InitialDirContext(env);
DirContext schemaContext = context.getSchema("");
if (cmd.hasOption(Flag.OBJECTCLASS.getShort())) {
print(cmd.getOptionValue(Flag.OBJECTCLASS.getShort()), SchemaContext.OBJECTCLASS.getValue(),
schemaContext);
}
if (cmd.hasOption(Flag.ATTRIBUTE.getShort())) {
print(cmd.getOptionValue(Flag.ATTRIBUTE.getShort()), SchemaContext.ATTRIBUTE.getValue(), schemaContext);
}
if (cmd.hasOption(Flag.SYNTAX.getShort())) {
print(cmd.getOptionValue(Flag.SYNTAX.getShort()), SchemaContext.SYNTAX.getValue(), schemaContext);
}
} catch (AuthenticationException e) {
System.err.println(String.format("Failed to bind to ldap server at %1$s", url));
} catch (CommunicationException e) {
System.err.println(String.format("Failed to contact ldap server at %1$s", url));
} catch (NameNotFoundException e) {
System.err.println(String.format("Can't find object %1$s", e.getMessage()));
} catch (NamingException e) {
System.err.println(e.toString());
}
}
}

View File

@@ -1,63 +1,63 @@
package org.springframework.ldap.odm.tools;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* A map from an LDAP syntax to the Java class used to represent it.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk>
*/
/* package */ final class SyntaxToJavaClass {
public static final class ClassInfo {
private final String className;
private final String packageName;
private ClassInfo(String className, String packageName) {
this.className = className;
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public String getPackageName() {
return packageName;
}
public String getFullClassName() {
StringBuilder result=new StringBuilder();
if (packageName!=null) {
result.append(packageName).append(".").append(className);
} else {
result.append(className);
}
return result.toString();
}
}
private final Map<String, ClassInfo> mapSyntaxToClassInfo = new HashMap<String, ClassInfo>();
public SyntaxToJavaClass(Map<String, String> mapSyntaxToClass) {
for (Entry<String, String> syntaxAndClass : mapSyntaxToClass.entrySet()) {
String fullClassName = syntaxAndClass.getValue().trim();
String packageName = null;
String className = null;
int lastDotIndex = fullClassName.lastIndexOf('.');
if (lastDotIndex != -1) {
className = fullClassName.substring(lastDotIndex + 1);
packageName = fullClassName.substring(0, lastDotIndex);
} else {
className = fullClassName;
}
mapSyntaxToClassInfo.put(syntaxAndClass.getKey(), new ClassInfo(className, packageName));
}
}
public ClassInfo getClassInfo(String syntax) {
return mapSyntaxToClassInfo.get(syntax);
}
}
package org.springframework.ldap.odm.tools;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* A map from an LDAP syntax to the Java class used to represent it.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk>
*/
/* package */ final class SyntaxToJavaClass {
public static final class ClassInfo {
private final String className;
private final String packageName;
private ClassInfo(String className, String packageName) {
this.className = className;
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public String getPackageName() {
return packageName;
}
public String getFullClassName() {
StringBuilder result=new StringBuilder();
if (packageName!=null) {
result.append(packageName).append(".").append(className);
} else {
result.append(className);
}
return result.toString();
}
}
private final Map<String, ClassInfo> mapSyntaxToClassInfo = new HashMap<String, ClassInfo>();
public SyntaxToJavaClass(Map<String, String> mapSyntaxToClass) {
for (Entry<String, String> syntaxAndClass : mapSyntaxToClass.entrySet()) {
String fullClassName = syntaxAndClass.getValue().trim();
String packageName = null;
String className = null;
int lastDotIndex = fullClassName.lastIndexOf('.');
if (lastDotIndex != -1) {
className = fullClassName.substring(lastDotIndex + 1);
packageName = fullClassName.substring(0, lastDotIndex);
} else {
className = fullClassName;
}
mapSyntaxToClassInfo.put(syntaxAndClass.getKey(), new ClassInfo(className, packageName));
}
}
public ClassInfo getClassInfo(String syntax) {
return mapSyntaxToClassInfo.get(syntax);
}
}

View File

@@ -1,8 +1,8 @@
/**
* Provides a tool to create a Java class representation of a set of LDAP object classes
* and a simple tool to view LDAP schema.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*/
/**
* Provides a tool to create a Java class representation of a set of LDAP object classes
* and a simple tool to view LDAP schema.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk&gt;
*/
package org.springframework.ldap.odm.tools;

View File

@@ -1,156 +1,156 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
/**
* Automatically generated to represent the LDAP object classes
* "organizationalunit", "top".
*/
@Entry(objectClasses = { "organizationalUnit", "top" })
public final class OrganizationalUnit {
@Id
private Name dn;
@Attribute(name = "objectClass", syntax = "1.3.6.1.4.1.1466.115.121.1.38")
private List<String> objectClass = new ArrayList<String>();
@Attribute(name = "ou", syntax = "1.3.6.1.4.1.1466.115.121.1.15")
private String ou;
@Attribute(name = "street", syntax = "1.3.6.1.4.1.1466.115.121.1.15")
private String street;
@Attribute(name = "description", syntax = "1.3.6.1.4.1.1466.115.121.1.15")
private String description;
public OrganizationalUnit() {
}
public OrganizationalUnit(Name dn, String street, String description) {
this.dn = dn;
this.street = street;
this.description = description;
objectClass.add("top");
objectClass.add("organizationalUnit");
int size = dn.size();
if (size > 1) {
ou = dn.get(size - 1).split("=")[1];
} else {
ou = "";
}
}
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
public List<String> getObjectClasses() {
return Collections.unmodifiableList(objectClass);
}
public String getOu() {
return ou;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return String.format("objectClasses=%1$s | dn=%2$s | ou=%3$s | street=%4$s | description=%5$s", objectClass,
dn, ou, street, description);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((dn == null) ? 0 : dn.hashCode());
result = prime * result + ((objectClass == null) ? 0 : new HashSet<String>(objectClass).hashCode());
result = prime * result + ((ou == null) ? 0 : ou.hashCode());
result = prime * result + ((street == null) ? 0 : street.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OrganizationalUnit other = (OrganizationalUnit) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (dn == null) {
if (other.dn != null)
return false;
} else if (!dn.equals(other.dn))
return false;
if (objectClass == null) {
if (other.objectClass != null)
return false;
} else
if (objectClass.size()!=other.objectClass.size() ||
!(new HashSet<String>(objectClass)).equals(new HashSet<String>(other.objectClass)))
return false;
if (ou == null) {
if (other.ou != null)
return false;
} else if (!ou.equals(other.ou))
return false;
if (street == null) {
if (other.street != null)
return false;
} else if (!street.equals(other.street))
return false;
return true;
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
/**
* Automatically generated to represent the LDAP object classes
* "organizationalunit", "top".
*/
@Entry(objectClasses = { "organizationalUnit", "top" })
public final class OrganizationalUnit {
@Id
private Name dn;
@Attribute(name = "objectClass", syntax = "1.3.6.1.4.1.1466.115.121.1.38")
private List<String> objectClass = new ArrayList<String>();
@Attribute(name = "ou", syntax = "1.3.6.1.4.1.1466.115.121.1.15")
private String ou;
@Attribute(name = "street", syntax = "1.3.6.1.4.1.1466.115.121.1.15")
private String street;
@Attribute(name = "description", syntax = "1.3.6.1.4.1.1466.115.121.1.15")
private String description;
public OrganizationalUnit() {
}
public OrganizationalUnit(Name dn, String street, String description) {
this.dn = dn;
this.street = street;
this.description = description;
objectClass.add("top");
objectClass.add("organizationalUnit");
int size = dn.size();
if (size > 1) {
ou = dn.get(size - 1).split("=")[1];
} else {
ou = "";
}
}
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
public List<String> getObjectClasses() {
return Collections.unmodifiableList(objectClass);
}
public String getOu() {
return ou;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return String.format("objectClasses=%1$s | dn=%2$s | ou=%3$s | street=%4$s | description=%5$s", objectClass,
dn, ou, street, description);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((dn == null) ? 0 : dn.hashCode());
result = prime * result + ((objectClass == null) ? 0 : new HashSet<String>(objectClass).hashCode());
result = prime * result + ((ou == null) ? 0 : ou.hashCode());
result = prime * result + ((street == null) ? 0 : street.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OrganizationalUnit other = (OrganizationalUnit) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (dn == null) {
if (other.dn != null)
return false;
} else if (!dn.equals(other.dn))
return false;
if (objectClass == null) {
if (other.objectClass != null)
return false;
} else
if (objectClass.size()!=other.objectClass.size() ||
!(new HashSet<String>(objectClass)).equals(new HashSet<String>(other.objectClass)))
return false;
if (ou == null) {
if (other.ou != null)
return false;
} else if (!ou.equals(other.ou))
return false;
if (street == null) {
if (other.street != null)
return false;
} else if (!street.equals(other.street))
return false;
return true;
}
}

View File

@@ -1,216 +1,216 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Attribute.Type;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import org.springframework.ldap.odm.annotations.Transient;
import javax.naming.Name;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
// Simple LDAP entry for testing
@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" })
public final class Person {
public Person() {
}
public Person(Name dn, String surname, List<String> desc, int telephoneNumber, byte[] jpegPhoto) {
this.dn = dn;
this.surname = surname;
this.desc = desc;
this.telephoneNumber = telephoneNumber;
this.jpegPhoto = jpegPhoto;
objectClasses = new ArrayList<String>();
objectClasses.add("inetOrgPerson");
objectClasses.add("organizationalPerson");
objectClasses.add("person");
objectClasses.add("top");
int size = dn.size();
if (size > 1) {
cn = dn.get(size - 1).split("=")[1];
} else {
cn = "";
}
}
@Transient
private String someRandomField = null;
@Transient
private List<String> someRandomList = new ArrayList<String>();
@Attribute(name = "objectClass")
private List<String> objectClasses;
@Id
private Name dn;
// No annotation on purpose!
private String cn;
@Attribute(name = "sn")
private String surname;
// Everything should be sets and in search operations also as results can be in any order
@Attribute(name = "description")
private List<String> desc;
@Attribute
private int telephoneNumber;
@Attribute(type = Type.BINARY)
byte[] jpegPhoto;
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public List<String> getDesc() {
return desc;
}
public void setDesc(List<String> desc) {
this.desc = desc;
}
public int getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(int telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public byte[] getJpegPhoto() {
return jpegPhoto;
}
public void setJpegPhoto(byte[] jpegPhoto) {
this.jpegPhoto = jpegPhoto;
}
public List<String> getObjectClasses() {
return objectClasses;
}
@Override
public String toString() {
StringBuilder jpegString=new StringBuilder();
if (jpegPhoto!=null) {
for (byte b:jpegPhoto) {
jpegString.append(Byte.toString(b));
}
}
return String.format(
"objectClasses=%1$s | dn=%2$s | cn=%3$s | sn=%4$s | desc=%5$s | telephoneNumber=%6$s | jpegPhoto=%7$s",
objectClasses, dn, cn, surname, desc, telephoneNumber, jpegString);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cn == null) ? 0 : cn.hashCode());
result = prime * result + ((desc == null) ? 0 : new HashSet<String>(desc).hashCode());
result = prime * result + ((dn == null) ? 0 : dn.hashCode());
result = prime * result + Arrays.hashCode(jpegPhoto);
result = prime * result + ((objectClasses == null) ? 0 : new HashSet<String>(objectClasses).hashCode());
result = prime * result + ((someRandomField == null) ? 0 : someRandomField.hashCode());
result = prime * result + ((someRandomList == null) ? 0 : someRandomList.hashCode());
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
result = prime * result + telephoneNumber;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (cn == null) {
if (other.cn != null)
return false;
} else if (!cn.equals(other.cn))
return false;
if (desc == null) {
if (other.desc != null)
return false;
} else if (desc.size()!=other.desc.size() || !(new HashSet<String>(desc)).equals(new HashSet<String>(other.desc)))
return false;
if (dn == null) {
if (other.dn != null)
return false;
} else if (!dn.equals(other.dn))
return false;
if (!Arrays.equals(jpegPhoto, other.jpegPhoto))
return false;
if (objectClasses == null) {
if (other.objectClasses != null)
return false;
} else if (objectClasses.size()!=other.objectClasses.size() || !(new HashSet<String>(objectClasses)).equals(new HashSet<String>(other.objectClasses)))
return false;
if (someRandomField == null) {
if (other.someRandomField != null)
return false;
} else if (!someRandomField.equals(other.someRandomField))
return false;
if (someRandomList == null) {
if (other.someRandomList != null)
return false;
} else if (!someRandomList.equals(other.someRandomList))
return false;
if (surname == null) {
if (other.surname != null)
return false;
} else if (!surname.equals(other.surname))
return false;
if (telephoneNumber != other.telephoneNumber)
return false;
return true;
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Attribute.Type;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import org.springframework.ldap.odm.annotations.Transient;
import javax.naming.Name;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
// Simple LDAP entry for testing
@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" })
public final class Person {
public Person() {
}
public Person(Name dn, String surname, List<String> desc, int telephoneNumber, byte[] jpegPhoto) {
this.dn = dn;
this.surname = surname;
this.desc = desc;
this.telephoneNumber = telephoneNumber;
this.jpegPhoto = jpegPhoto;
objectClasses = new ArrayList<String>();
objectClasses.add("inetOrgPerson");
objectClasses.add("organizationalPerson");
objectClasses.add("person");
objectClasses.add("top");
int size = dn.size();
if (size > 1) {
cn = dn.get(size - 1).split("=")[1];
} else {
cn = "";
}
}
@Transient
private String someRandomField = null;
@Transient
private List<String> someRandomList = new ArrayList<String>();
@Attribute(name = "objectClass")
private List<String> objectClasses;
@Id
private Name dn;
// No annotation on purpose!
private String cn;
@Attribute(name = "sn")
private String surname;
// Everything should be sets and in search operations also as results can be in any order
@Attribute(name = "description")
private List<String> desc;
@Attribute
private int telephoneNumber;
@Attribute(type = Type.BINARY)
byte[] jpegPhoto;
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public List<String> getDesc() {
return desc;
}
public void setDesc(List<String> desc) {
this.desc = desc;
}
public int getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(int telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public byte[] getJpegPhoto() {
return jpegPhoto;
}
public void setJpegPhoto(byte[] jpegPhoto) {
this.jpegPhoto = jpegPhoto;
}
public List<String> getObjectClasses() {
return objectClasses;
}
@Override
public String toString() {
StringBuilder jpegString=new StringBuilder();
if (jpegPhoto!=null) {
for (byte b:jpegPhoto) {
jpegString.append(Byte.toString(b));
}
}
return String.format(
"objectClasses=%1$s | dn=%2$s | cn=%3$s | sn=%4$s | desc=%5$s | telephoneNumber=%6$s | jpegPhoto=%7$s",
objectClasses, dn, cn, surname, desc, telephoneNumber, jpegString);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cn == null) ? 0 : cn.hashCode());
result = prime * result + ((desc == null) ? 0 : new HashSet<String>(desc).hashCode());
result = prime * result + ((dn == null) ? 0 : dn.hashCode());
result = prime * result + Arrays.hashCode(jpegPhoto);
result = prime * result + ((objectClasses == null) ? 0 : new HashSet<String>(objectClasses).hashCode());
result = prime * result + ((someRandomField == null) ? 0 : someRandomField.hashCode());
result = prime * result + ((someRandomList == null) ? 0 : someRandomList.hashCode());
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
result = prime * result + telephoneNumber;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (cn == null) {
if (other.cn != null)
return false;
} else if (!cn.equals(other.cn))
return false;
if (desc == null) {
if (other.desc != null)
return false;
} else if (desc.size()!=other.desc.size() || !(new HashSet<String>(desc)).equals(new HashSet<String>(other.desc)))
return false;
if (dn == null) {
if (other.dn != null)
return false;
} else if (!dn.equals(other.dn))
return false;
if (!Arrays.equals(jpegPhoto, other.jpegPhoto))
return false;
if (objectClasses == null) {
if (other.objectClasses != null)
return false;
} else if (objectClasses.size()!=other.objectClasses.size() || !(new HashSet<String>(objectClasses)).equals(new HashSet<String>(other.objectClasses)))
return false;
if (someRandomField == null) {
if (other.someRandomField != null)
return false;
} else if (!someRandomField.equals(other.someRandomField))
return false;
if (someRandomList == null) {
if (other.someRandomList != null)
return false;
} else if (!someRandomList.equals(other.someRandomList))
return false;
if (surname == null) {
if (other.surname != null)
return false;
} else if (!surname.equals(other.surname))
return false;
if (telephoneNumber != other.telephoneNumber)
return false;
return true;
}
}

View File

@@ -1,102 +1,102 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
import java.util.ArrayList;
import java.util.List;
// Simple LDAP entry for testing
@Entry(objectClasses = { "person", "top" })
public final class PlainPerson {
public PlainPerson() {
}
public PlainPerson(Name dn, String commonName, String surname) {
this.dn = dn;
this.surname = surname;
objectClasses = new ArrayList<String>();
objectClasses.add("top");
objectClasses.add("person");
cn = commonName;
}
@Attribute(name = "objectClass")
private List<String> objectClasses;
@Id
private Name dn;
@Attribute(name = "cn")
private String cn;
@Attribute(name = "sn")
private String surname;
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlainPerson that = (PlainPerson) o;
if (cn != null ? !cn.equals(that.cn) : that.cn != null) return false;
if (dn != null ? !dn.equals(that.dn) : that.dn != null) return false;
if (objectClasses != null ? !objectClasses.equals(that.objectClasses) : that.objectClasses != null)
return false;
if (surname != null ? !surname.equals(that.surname) : that.surname != null) return false;
return true;
}
@Override
public int hashCode() {
int result = objectClasses != null ? objectClasses.hashCode() : 0;
result = 31 * result + (dn != null ? dn.hashCode() : 0);
result = 31 * result + (cn != null ? cn.hashCode() : 0);
result = 31 * result + (surname != null ? surname.hashCode() : 0);
return result;
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
import java.util.ArrayList;
import java.util.List;
// Simple LDAP entry for testing
@Entry(objectClasses = { "person", "top" })
public final class PlainPerson {
public PlainPerson() {
}
public PlainPerson(Name dn, String commonName, String surname) {
this.dn = dn;
this.surname = surname;
objectClasses = new ArrayList<String>();
objectClasses.add("top");
objectClasses.add("person");
cn = commonName;
}
@Attribute(name = "objectClass")
private List<String> objectClasses;
@Id
private Name dn;
@Attribute(name = "cn")
private String cn;
@Attribute(name = "sn")
private String surname;
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlainPerson that = (PlainPerson) o;
if (cn != null ? !cn.equals(that.cn) : that.cn != null) return false;
if (dn != null ? !dn.equals(that.dn) : that.dn != null) return false;
if (objectClasses != null ? !objectClasses.equals(that.objectClasses) : that.objectClasses != null)
return false;
if (surname != null ? !surname.equals(that.surname) : that.surname != null) return false;
return true;
}
@Override
public int hashCode() {
int result = objectClasses != null ? objectClasses.hashCode() : 0;
result = 31 * result + (dn != null ? dn.hashCode() : 0);
result = 31 * result + (cn != null ? cn.hashCode() : 0);
result = 31 * result + (surname != null ? surname.hashCode() : 0);
return result;
}
}

View File

@@ -1,207 +1,207 @@
package org.springframework.ldap.odm.test;
import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.util.BitSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.odm.test.utils.ExecuteRunnable;
import org.springframework.ldap.odm.test.utils.RunnableTest;
import org.springframework.ldap.odm.typeconversion.ConverterException;
import org.springframework.ldap.odm.typeconversion.impl.Converter;
import org.springframework.ldap.odm.typeconversion.impl.ConverterManagerImpl;
import org.springframework.ldap.odm.typeconversion.impl.converters.FromStringConverter;
import org.springframework.ldap.odm.typeconversion.impl.converters.ToStringConverter;
public final class TestConverterManager {
private ConverterManagerImpl converterManager;
@Before
public void setUp() {
converterManager = new ConverterManagerImpl();
Converter ptc = new FromStringConverter();
converterManager.addConverter(String.class, "", Byte.class, ptc);
converterManager.addConverter(String.class, "", Short.class, ptc);
converterManager.addConverter(String.class, "", Integer.class, ptc);
converterManager.addConverter(String.class, "", Long.class, ptc);
converterManager.addConverter(String.class, "", Double.class, ptc);
converterManager.addConverter(String.class, "", Float.class, ptc);
converterManager.addConverter(String.class, "", Boolean.class, ptc);
Converter tsc = new ToStringConverter();
converterManager.addConverter(Byte.class, "", String.class, tsc);
converterManager.addConverter(Short.class, "", String.class, tsc);
converterManager.addConverter(Integer.class, "", String.class, tsc);
converterManager.addConverter(Long.class, "", String.class, tsc);
converterManager.addConverter(Double.class, "", String.class, tsc);
converterManager.addConverter(Float.class, "", String.class, tsc);
converterManager.addConverter(Boolean.class, "", String.class, tsc);
Converter uric = new UriConverter();
converterManager.addConverter(URI.class, "", String.class, uric);
converterManager.addConverter(String.class, "", URI.class, uric);
}
@After
public void tearDown() {
converterManager = null;
}
private static class ConverterTestData<T> {
public final Class<T> destClass;
public final Object sourceData;
public final T expectedValue;
public final String syntax;
public ConverterTestData(Object sourceData, Class<T> destClass, T expectedValue) {
this(sourceData, "", destClass, expectedValue);
}
public ConverterTestData(Object sourceData, String syntax, Class<T> destClass, T expectedValue) {
this.destClass = destClass;
this.sourceData = sourceData;
this.expectedValue = expectedValue;
this.syntax = syntax;
}
@Override
public String toString() {
return String.format("sourceData=%1$s | syntax=%2$s | destClass=%3$s | expectedValue=%4$s", sourceData,
syntax, destClass, expectedValue);
}
}
// Class to Class conversion without any syntaxes
@Test
public void basicTypeConverion() throws Exception {
final ConverterTestData<?>[] primitiveTypeTests = new ConverterTestData<?>[] {
new ConverterTestData<Byte>("33", Byte.class, Byte.valueOf((byte)33)),
new ConverterTestData<Byte>("-88", Byte.class, Byte.valueOf((byte)-88)),
new ConverterTestData<Short>("666", Short.class, Short.valueOf((short)666)),
new ConverterTestData<Short>("-123", Short.class, Short.valueOf((short)-123)),
new ConverterTestData<Integer>("123", Integer.class, Integer.valueOf(123)),
new ConverterTestData<Integer>("-500", Integer.class, Integer.valueOf(-500)),
new ConverterTestData<Long>("123456", Long.class, Long.valueOf(123456)),
new ConverterTestData<Long>("-654321", Long.class, Long.valueOf(-654321)),
new ConverterTestData<Double>("2", Double.class, Double.valueOf(2)),
new ConverterTestData<Double>("-0.4", Double.class, Double.valueOf(-0.4)),
new ConverterTestData<Float>("666", Float.class, Float.valueOf(666)),
new ConverterTestData<Float>("-0.75", Float.class, Float.valueOf(-0.75F)),
new ConverterTestData<Boolean>("false", Boolean.class, Boolean.FALSE),
new ConverterTestData<Boolean>("TRUE", Boolean.class, Boolean.TRUE),
new ConverterTestData<String>("This is a string", String.class, "This is a string"),
new ConverterTestData<String>("This is another String", String.class, "This is another String"),
new ConverterTestData<String>((byte)66, String.class, "66"),
new ConverterTestData<String>((int)1234, String.class, "1234"),
new ConverterTestData<String>((int)-9876, String.class, "-9876"),
new ConverterTestData<URI>("https://google.com/", URI.class, new URI("https://google.com/")),
new ConverterTestData<URI>("https://apache.org/index.html", URI.class, new URI(
"https://apache.org/index.html")),
new ConverterTestData<String>(new URI("https://google.com/"), String.class, "https://google.com/"),
new ConverterTestData<String>(new URI("https://apache.org/index.html"), String.class,
"https://apache.org/index.html") };
new ExecuteRunnable<ConverterTestData<?>>().runTests(new RunnableTest<ConverterTestData<?>>() {
public void runTest(ConverterTestData<?> testData) {
assertEquals(testData.expectedValue, converterManager.convert(testData.sourceData, "",
testData.destClass));
}
}, primitiveTypeTests);
}
private static class SquaredConverter implements Converter {
public <T> T convert(Object source, Class<T> toClass) throws Exception {
Integer intSource = null;
if (source.getClass() == String.class) {
intSource = new Integer((String)source);
}
else {
if (source.getClass() == Integer.class) {
intSource = (Integer)source;
}
}
Integer result = null;
if (intSource != null) {
result = intSource * intSource;
}
return toClass.cast(result);
}
}
private static class CubedConverter implements Converter {
public <T> T convert(Object source, Class<T> toClass) throws Exception {
Integer intSource = null;
if (source.getClass() == String.class) {
intSource = new Integer((String)source);
}
else {
if (source.getClass() == Integer.class) {
intSource = (Integer)source;
}
}
Integer result = null;
if (intSource != null) {
result = intSource * intSource * intSource;
}
return toClass.cast(result);
}
}
// Tests using syntaxes for "finer grained" mapping
@Test
public void syntaxBasedConversion() throws Exception {
Converter squaredConverter = new SquaredConverter();
converterManager.addConverter(String.class, "1", Integer.class, squaredConverter);
converterManager.addConverter(Integer.class, "1", Integer.class, squaredConverter);
Converter cubedConverter = new CubedConverter();
converterManager.addConverter(String.class, "2", Integer.class, cubedConverter);
converterManager.addConverter(Integer.class, "3", Integer.class, cubedConverter);
final ConverterTestData<?>[] syntaxTests = new ConverterTestData<?>[] {
new ConverterTestData<Integer>("3", "", Integer.class, Integer.valueOf(3)),
new ConverterTestData<Integer>("4", "", Integer.class, Integer.valueOf(4)),
new ConverterTestData<Integer>(5, "", Integer.class, Integer.valueOf(5)),
new ConverterTestData<Integer>(6, "", Integer.class, Integer.valueOf(6)),
new ConverterTestData<Integer>("3", "1", Integer.class, Integer.valueOf(9)),
new ConverterTestData<Integer>("4", "1", Integer.class, Integer.valueOf(16)),
new ConverterTestData<Integer>(5, "1", Integer.class, Integer.valueOf(25)),
new ConverterTestData<Integer>(6, "1", Integer.class, Integer.valueOf(36)),
new ConverterTestData<Integer>("3", "2", Integer.class, Integer.valueOf(27)),
new ConverterTestData<Integer>("4", "2", Integer.class, Integer.valueOf(64)),
new ConverterTestData<Integer>(5, "3", Integer.class, Integer.valueOf(125)),
new ConverterTestData<Integer>(6, "3", Integer.class, Integer.valueOf(216)), };
new ExecuteRunnable<ConverterTestData<?>>().runTests(new RunnableTest<ConverterTestData<?>>() {
public void runTest(ConverterTestData<?> testData) {
assertEquals(testData.expectedValue, converterManager.convert(testData.sourceData, testData.syntax,
testData.destClass));
}
}, syntaxTests);
}
// No converter for classes
@Test(expected = ConverterException.class)
public void noClassConverter() throws Exception {
converterManager.convert(BitSet.class, "", Integer.class);
}
// Invalid syntax so converter fails
@Test(expected = ConverterException.class)
public void invalidSyntax() throws Exception {
converterManager.convert(String.class, "not a uri", URI.class);
}
}
package org.springframework.ldap.odm.test;
import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.util.BitSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.odm.test.utils.ExecuteRunnable;
import org.springframework.ldap.odm.test.utils.RunnableTest;
import org.springframework.ldap.odm.typeconversion.ConverterException;
import org.springframework.ldap.odm.typeconversion.impl.Converter;
import org.springframework.ldap.odm.typeconversion.impl.ConverterManagerImpl;
import org.springframework.ldap.odm.typeconversion.impl.converters.FromStringConverter;
import org.springframework.ldap.odm.typeconversion.impl.converters.ToStringConverter;
public final class TestConverterManager {
private ConverterManagerImpl converterManager;
@Before
public void setUp() {
converterManager = new ConverterManagerImpl();
Converter ptc = new FromStringConverter();
converterManager.addConverter(String.class, "", Byte.class, ptc);
converterManager.addConverter(String.class, "", Short.class, ptc);
converterManager.addConverter(String.class, "", Integer.class, ptc);
converterManager.addConverter(String.class, "", Long.class, ptc);
converterManager.addConverter(String.class, "", Double.class, ptc);
converterManager.addConverter(String.class, "", Float.class, ptc);
converterManager.addConverter(String.class, "", Boolean.class, ptc);
Converter tsc = new ToStringConverter();
converterManager.addConverter(Byte.class, "", String.class, tsc);
converterManager.addConverter(Short.class, "", String.class, tsc);
converterManager.addConverter(Integer.class, "", String.class, tsc);
converterManager.addConverter(Long.class, "", String.class, tsc);
converterManager.addConverter(Double.class, "", String.class, tsc);
converterManager.addConverter(Float.class, "", String.class, tsc);
converterManager.addConverter(Boolean.class, "", String.class, tsc);
Converter uric = new UriConverter();
converterManager.addConverter(URI.class, "", String.class, uric);
converterManager.addConverter(String.class, "", URI.class, uric);
}
@After
public void tearDown() {
converterManager = null;
}
private static class ConverterTestData<T> {
public final Class<T> destClass;
public final Object sourceData;
public final T expectedValue;
public final String syntax;
public ConverterTestData(Object sourceData, Class<T> destClass, T expectedValue) {
this(sourceData, "", destClass, expectedValue);
}
public ConverterTestData(Object sourceData, String syntax, Class<T> destClass, T expectedValue) {
this.destClass = destClass;
this.sourceData = sourceData;
this.expectedValue = expectedValue;
this.syntax = syntax;
}
@Override
public String toString() {
return String.format("sourceData=%1$s | syntax=%2$s | destClass=%3$s | expectedValue=%4$s", sourceData,
syntax, destClass, expectedValue);
}
}
// Class to Class conversion without any syntaxes
@Test
public void basicTypeConverion() throws Exception {
final ConverterTestData<?>[] primitiveTypeTests = new ConverterTestData<?>[] {
new ConverterTestData<Byte>("33", Byte.class, Byte.valueOf((byte)33)),
new ConverterTestData<Byte>("-88", Byte.class, Byte.valueOf((byte)-88)),
new ConverterTestData<Short>("666", Short.class, Short.valueOf((short)666)),
new ConverterTestData<Short>("-123", Short.class, Short.valueOf((short)-123)),
new ConverterTestData<Integer>("123", Integer.class, Integer.valueOf(123)),
new ConverterTestData<Integer>("-500", Integer.class, Integer.valueOf(-500)),
new ConverterTestData<Long>("123456", Long.class, Long.valueOf(123456)),
new ConverterTestData<Long>("-654321", Long.class, Long.valueOf(-654321)),
new ConverterTestData<Double>("2", Double.class, Double.valueOf(2)),
new ConverterTestData<Double>("-0.4", Double.class, Double.valueOf(-0.4)),
new ConverterTestData<Float>("666", Float.class, Float.valueOf(666)),
new ConverterTestData<Float>("-0.75", Float.class, Float.valueOf(-0.75F)),
new ConverterTestData<Boolean>("false", Boolean.class, Boolean.FALSE),
new ConverterTestData<Boolean>("TRUE", Boolean.class, Boolean.TRUE),
new ConverterTestData<String>("This is a string", String.class, "This is a string"),
new ConverterTestData<String>("This is another String", String.class, "This is another String"),
new ConverterTestData<String>((byte)66, String.class, "66"),
new ConverterTestData<String>((int)1234, String.class, "1234"),
new ConverterTestData<String>((int)-9876, String.class, "-9876"),
new ConverterTestData<URI>("https://google.com/", URI.class, new URI("https://google.com/")),
new ConverterTestData<URI>("https://apache.org/index.html", URI.class, new URI(
"https://apache.org/index.html")),
new ConverterTestData<String>(new URI("https://google.com/"), String.class, "https://google.com/"),
new ConverterTestData<String>(new URI("https://apache.org/index.html"), String.class,
"https://apache.org/index.html") };
new ExecuteRunnable<ConverterTestData<?>>().runTests(new RunnableTest<ConverterTestData<?>>() {
public void runTest(ConverterTestData<?> testData) {
assertEquals(testData.expectedValue, converterManager.convert(testData.sourceData, "",
testData.destClass));
}
}, primitiveTypeTests);
}
private static class SquaredConverter implements Converter {
public <T> T convert(Object source, Class<T> toClass) throws Exception {
Integer intSource = null;
if (source.getClass() == String.class) {
intSource = new Integer((String)source);
}
else {
if (source.getClass() == Integer.class) {
intSource = (Integer)source;
}
}
Integer result = null;
if (intSource != null) {
result = intSource * intSource;
}
return toClass.cast(result);
}
}
private static class CubedConverter implements Converter {
public <T> T convert(Object source, Class<T> toClass) throws Exception {
Integer intSource = null;
if (source.getClass() == String.class) {
intSource = new Integer((String)source);
}
else {
if (source.getClass() == Integer.class) {
intSource = (Integer)source;
}
}
Integer result = null;
if (intSource != null) {
result = intSource * intSource * intSource;
}
return toClass.cast(result);
}
}
// Tests using syntaxes for "finer grained" mapping
@Test
public void syntaxBasedConversion() throws Exception {
Converter squaredConverter = new SquaredConverter();
converterManager.addConverter(String.class, "1", Integer.class, squaredConverter);
converterManager.addConverter(Integer.class, "1", Integer.class, squaredConverter);
Converter cubedConverter = new CubedConverter();
converterManager.addConverter(String.class, "2", Integer.class, cubedConverter);
converterManager.addConverter(Integer.class, "3", Integer.class, cubedConverter);
final ConverterTestData<?>[] syntaxTests = new ConverterTestData<?>[] {
new ConverterTestData<Integer>("3", "", Integer.class, Integer.valueOf(3)),
new ConverterTestData<Integer>("4", "", Integer.class, Integer.valueOf(4)),
new ConverterTestData<Integer>(5, "", Integer.class, Integer.valueOf(5)),
new ConverterTestData<Integer>(6, "", Integer.class, Integer.valueOf(6)),
new ConverterTestData<Integer>("3", "1", Integer.class, Integer.valueOf(9)),
new ConverterTestData<Integer>("4", "1", Integer.class, Integer.valueOf(16)),
new ConverterTestData<Integer>(5, "1", Integer.class, Integer.valueOf(25)),
new ConverterTestData<Integer>(6, "1", Integer.class, Integer.valueOf(36)),
new ConverterTestData<Integer>("3", "2", Integer.class, Integer.valueOf(27)),
new ConverterTestData<Integer>("4", "2", Integer.class, Integer.valueOf(64)),
new ConverterTestData<Integer>(5, "3", Integer.class, Integer.valueOf(125)),
new ConverterTestData<Integer>(6, "3", Integer.class, Integer.valueOf(216)), };
new ExecuteRunnable<ConverterTestData<?>>().runTests(new RunnableTest<ConverterTestData<?>>() {
public void runTest(ConverterTestData<?> testData) {
assertEquals(testData.expectedValue, converterManager.convert(testData.sourceData, testData.syntax,
testData.destClass));
}
}, syntaxTests);
}
// No converter for classes
@Test(expected = ConverterException.class)
public void noClassConverter() throws Exception {
converterManager.convert(BitSet.class, "", Integer.class);
}
// Invalid syntax so converter fails
@Test(expected = ConverterException.class)
public void invalidSyntax() throws Exception {
converterManager.convert(String.class, "not a uri", URI.class);
}
}

View File

@@ -1,87 +1,87 @@
package org.springframework.ldap.odm.test;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.springframework.ldap.odm.test.utils.ExecuteRunnable;
import org.springframework.ldap.odm.test.utils.RunnableTest;
import org.springframework.ldap.odm.typeconversion.ConverterManager;
import org.springframework.ldap.odm.typeconversion.impl.Converter;
import org.springframework.ldap.odm.typeconversion.impl.ConverterManagerFactoryBean;
public class TestManagerConverterFactory {
private static class NullConverter implements Converter {
public <T> T convert(Object source, Class<T> toClass) throws Exception {
return null;
}
}
private static final Converter nullConverter=new NullConverter();
private static class ConverterConfigTestData {
private Class<?>[] fromClasses;
private String syntax;
private Class<?>[] toClasses;
private ConverterConfigTestData(Class<?>[] fromClasses, String syntax, Class<?>[] toClasses) {
this.fromClasses=fromClasses;
this.syntax=syntax;
this.toClasses=toClasses;
}
}
private static ConverterConfigTestData[] converterConfigTestData=new ConverterConfigTestData[] {
new ConverterConfigTestData(new Class<?>[] { String.class }, "", new Class<?>[] { Integer.class }),
new ConverterConfigTestData(new Class<?>[] { Byte.class, java.lang.Integer.class }, "", new Class<?>[] { String.class, Long.class }),
new ConverterConfigTestData(new Class<?>[] { String.class }, "123", new Class<?>[] { java.net.URI.class }),
};
private static class ConverterTestData {
private final Class<?> fromClass;
private final String syntax;
private final Class<?> toClass;
private final boolean canConvert;
private ConverterTestData(Class<?> fromClass, String syntax, Class<?> toClass, boolean canConvert) {
this.fromClass=fromClass;
this.syntax=syntax;
this.toClass=toClass;
this.canConvert=canConvert;
}
}
private ConverterTestData[] converterTestData=new ConverterTestData[] {
new ConverterTestData(java.lang.String.class, "", java.lang.Integer.class, true),
new ConverterTestData(java.lang.Byte.class, "", java.lang.Long.class, true),
new ConverterTestData(java.lang.Integer.class, "444", java.lang.String.class, true),
new ConverterTestData(java.lang.String.class, "123", java.net.URI.class, true),
new ConverterTestData(java.lang.String.class, "123", java.lang.Byte.class, false),
new ConverterTestData(java.lang.Byte.class, "", java.lang.Integer.class, false)
};
@Test
public void testConverterFactory() throws Exception {
ConverterManagerFactoryBean converterManagerFactory=new ConverterManagerFactoryBean();
Set<ConverterManagerFactoryBean.ConverterConfig> configList=new HashSet<ConverterManagerFactoryBean.ConverterConfig>();
for (ConverterConfigTestData config:converterConfigTestData) {
ConverterManagerFactoryBean.ConverterConfig converterConfig=new ConverterManagerFactoryBean.ConverterConfig();
converterConfig.setFromClasses(new HashSet<Class<?>>(Arrays.asList(config.fromClasses)));
converterConfig.setSyntax(config.syntax);
converterConfig.setToClasses(new HashSet<Class<?>>(Arrays.asList(config.toClasses)));
converterConfig.setConverter(nullConverter);
configList.add(converterConfig);
}
converterManagerFactory.setConverterConfig(configList);
final ConverterManager converterManager=(ConverterManager)converterManagerFactory.getObject();
new ExecuteRunnable<ConverterTestData>().runTests(new RunnableTest<ConverterTestData>() {
public void runTest(ConverterTestData testData) {
assertEquals(testData.canConvert,
converterManager.canConvert(testData.fromClass, testData.syntax, testData.toClass));
}
}, converterTestData);
}
}
package org.springframework.ldap.odm.test;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.springframework.ldap.odm.test.utils.ExecuteRunnable;
import org.springframework.ldap.odm.test.utils.RunnableTest;
import org.springframework.ldap.odm.typeconversion.ConverterManager;
import org.springframework.ldap.odm.typeconversion.impl.Converter;
import org.springframework.ldap.odm.typeconversion.impl.ConverterManagerFactoryBean;
public class TestManagerConverterFactory {
private static class NullConverter implements Converter {
public <T> T convert(Object source, Class<T> toClass) throws Exception {
return null;
}
}
private static final Converter nullConverter=new NullConverter();
private static class ConverterConfigTestData {
private Class<?>[] fromClasses;
private String syntax;
private Class<?>[] toClasses;
private ConverterConfigTestData(Class<?>[] fromClasses, String syntax, Class<?>[] toClasses) {
this.fromClasses=fromClasses;
this.syntax=syntax;
this.toClasses=toClasses;
}
}
private static ConverterConfigTestData[] converterConfigTestData=new ConverterConfigTestData[] {
new ConverterConfigTestData(new Class<?>[] { String.class }, "", new Class<?>[] { Integer.class }),
new ConverterConfigTestData(new Class<?>[] { Byte.class, java.lang.Integer.class }, "", new Class<?>[] { String.class, Long.class }),
new ConverterConfigTestData(new Class<?>[] { String.class }, "123", new Class<?>[] { java.net.URI.class }),
};
private static class ConverterTestData {
private final Class<?> fromClass;
private final String syntax;
private final Class<?> toClass;
private final boolean canConvert;
private ConverterTestData(Class<?> fromClass, String syntax, Class<?> toClass, boolean canConvert) {
this.fromClass=fromClass;
this.syntax=syntax;
this.toClass=toClass;
this.canConvert=canConvert;
}
}
private ConverterTestData[] converterTestData=new ConverterTestData[] {
new ConverterTestData(java.lang.String.class, "", java.lang.Integer.class, true),
new ConverterTestData(java.lang.Byte.class, "", java.lang.Long.class, true),
new ConverterTestData(java.lang.Integer.class, "444", java.lang.String.class, true),
new ConverterTestData(java.lang.String.class, "123", java.net.URI.class, true),
new ConverterTestData(java.lang.String.class, "123", java.lang.Byte.class, false),
new ConverterTestData(java.lang.Byte.class, "", java.lang.Integer.class, false)
};
@Test
public void testConverterFactory() throws Exception {
ConverterManagerFactoryBean converterManagerFactory=new ConverterManagerFactoryBean();
Set<ConverterManagerFactoryBean.ConverterConfig> configList=new HashSet<ConverterManagerFactoryBean.ConverterConfig>();
for (ConverterConfigTestData config:converterConfigTestData) {
ConverterManagerFactoryBean.ConverterConfig converterConfig=new ConverterManagerFactoryBean.ConverterConfig();
converterConfig.setFromClasses(new HashSet<Class<?>>(Arrays.asList(config.fromClasses)));
converterConfig.setSyntax(config.syntax);
converterConfig.setToClasses(new HashSet<Class<?>>(Arrays.asList(config.toClasses)));
converterConfig.setConverter(nullConverter);
configList.add(converterConfig);
}
converterManagerFactory.setConverterConfig(configList);
final ConverterManager converterManager=(ConverterManager)converterManagerFactory.getObject();
new ExecuteRunnable<ConverterTestData>().runTests(new RunnableTest<ConverterTestData>() {
public void runTest(ConverterTestData testData) {
assertEquals(testData.canConvert,
converterManager.canConvert(testData.fromClass, testData.syntax, testData.toClass));
}
}, converterTestData);
}
}

View File

@@ -1,136 +1,136 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.ldap.odm.test.utils.ExecuteRunnable;
import org.springframework.ldap.odm.test.utils.GetFreePort;
import org.springframework.ldap.odm.test.utils.RunnableTest;
import org.springframework.ldap.odm.tools.SchemaViewer;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.ldap.test.LdapTestUtils;
import javax.naming.ldap.LdapName;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
public final class TestSchemaViewer {
// Base DN for test data
private static final LdapName baseName = LdapUtils.newLdapName("o=Whoniverse");
private static final String lineSeparator = System.getProperty ("line.separator");
private static int port;
private static String[] commonFlags;
@BeforeClass
public static void setUpClass() throws Exception {
// Added because the close down of Apache DS on Linux does
// not seem to free up its port.
port=GetFreePort.getFreePort();
commonFlags=new String[] {
"--url", "ldap://127.0.0.1:"+port,
"--username", "",
"--password", "",
"--error"};
// Start an in process LDAP server
LdapTestUtils.startEmbeddedServer(port, baseName.toString(), "odm-test");
}
@AfterClass
public static void tearDownClass() throws Exception {
LdapTestUtils.shutdownEmbeddedServer();
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
private static String runSchemaViewer(String... flags) {
String result=null;
PrintStream originalOut=System.out;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
System.setErr(new PrintStream(output));
List<String> commandFlags=
new ArrayList<String>(Arrays.asList(commonFlags));
commandFlags.addAll(Arrays.asList(flags));
SchemaViewer.main(commandFlags.toArray(new String[0]));
// Turn end of lines into | for portability
result=output.toString().trim().replace(lineSeparator, "|");
} finally {
System.setErr(originalOut);
}
return result;
}
private static class TestData {
private final String flag;
private final String value;
private final String result;
public TestData(String flag, String value, String result) {
this.flag=flag;
this.value=value;
this.result=result;
}
}
// This makes the test dependent on the order in which the data is returned - it is invalid to assume that this will not change
private static TestData[] viewerTestData=new TestData[] {
new TestData("-o", "top",
"NAME:top|MUST:objectClass |X-SCHEMA:system |NAME:top |NUMERICOID:2.5.6.0 |DESC:top of the superclass chain |ABSTRACT:true"),
new TestData("-o", "country",
"NAME:country|MUST:c |X-SCHEMA:core |SUP:top |NAME:country |STRUCTURAL:true |NUMERICOID:2.5.6.2 |DESC:RFC2256: a country |MAY:searchGuide description"),
new TestData("-a", "sn",
"NAME:sn|NAME:sn surname |SUBSTR:caseIgnoreSubstringsMatch |X-SCHEMA:core |SYNTAX:1.3.6.1.4.1.1466.115.121.1.15 |NUMERICOID:2.5.4.4 |SUP:name |DESC:RFC2256: last (family) name(s) for which the entity is known by |USAGE:userApplications |EQUALITY:caseIgnoreMatch"),
new TestData("-a", "jpegPhoto",
"NAME:jpegPhoto|X-SCHEMA:inetorgperson |SYNTAX:1.3.6.1.4.1.1466.115.121.1.28 |NAME:jpegPhoto |USAGE:userApplications |NUMERICOID:0.9.2342.19200300.100.1.60 |DESC:RFC2798: a JPEG image"),
};
// Very simple test - mainly just to exercise the code and to
// ensure we get representative test coverage
@Test
public void testSchemaViewer() throws Exception {
new ExecuteRunnable<TestData>().runTests(new RunnableTest<TestData>() {
public void runTest(TestData testData) {
String result=runSchemaViewer(testData.flag, testData.value);
assertEquals(testData.result, result);
}
}, viewerTestData);
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.ldap.odm.test.utils.ExecuteRunnable;
import org.springframework.ldap.odm.test.utils.GetFreePort;
import org.springframework.ldap.odm.test.utils.RunnableTest;
import org.springframework.ldap.odm.tools.SchemaViewer;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.ldap.test.LdapTestUtils;
import javax.naming.ldap.LdapName;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
public final class TestSchemaViewer {
// Base DN for test data
private static final LdapName baseName = LdapUtils.newLdapName("o=Whoniverse");
private static final String lineSeparator = System.getProperty ("line.separator");
private static int port;
private static String[] commonFlags;
@BeforeClass
public static void setUpClass() throws Exception {
// Added because the close down of Apache DS on Linux does
// not seem to free up its port.
port=GetFreePort.getFreePort();
commonFlags=new String[] {
"--url", "ldap://127.0.0.1:"+port,
"--username", "",
"--password", "",
"--error"};
// Start an in process LDAP server
LdapTestUtils.startEmbeddedServer(port, baseName.toString(), "odm-test");
}
@AfterClass
public static void tearDownClass() throws Exception {
LdapTestUtils.shutdownEmbeddedServer();
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
private static String runSchemaViewer(String... flags) {
String result=null;
PrintStream originalOut=System.out;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
System.setErr(new PrintStream(output));
List<String> commandFlags=
new ArrayList<String>(Arrays.asList(commonFlags));
commandFlags.addAll(Arrays.asList(flags));
SchemaViewer.main(commandFlags.toArray(new String[0]));
// Turn end of lines into | for portability
result=output.toString().trim().replace(lineSeparator, "|");
} finally {
System.setErr(originalOut);
}
return result;
}
private static class TestData {
private final String flag;
private final String value;
private final String result;
public TestData(String flag, String value, String result) {
this.flag=flag;
this.value=value;
this.result=result;
}
}
// This makes the test dependent on the order in which the data is returned - it is invalid to assume that this will not change
private static TestData[] viewerTestData=new TestData[] {
new TestData("-o", "top",
"NAME:top|MUST:objectClass |X-SCHEMA:system |NAME:top |NUMERICOID:2.5.6.0 |DESC:top of the superclass chain |ABSTRACT:true"),
new TestData("-o", "country",
"NAME:country|MUST:c |X-SCHEMA:core |SUP:top |NAME:country |STRUCTURAL:true |NUMERICOID:2.5.6.2 |DESC:RFC2256: a country |MAY:searchGuide description"),
new TestData("-a", "sn",
"NAME:sn|NAME:sn surname |SUBSTR:caseIgnoreSubstringsMatch |X-SCHEMA:core |SYNTAX:1.3.6.1.4.1.1466.115.121.1.15 |NUMERICOID:2.5.4.4 |SUP:name |DESC:RFC2256: last (family) name(s) for which the entity is known by |USAGE:userApplications |EQUALITY:caseIgnoreMatch"),
new TestData("-a", "jpegPhoto",
"NAME:jpegPhoto|X-SCHEMA:inetorgperson |SYNTAX:1.3.6.1.4.1.1466.115.121.1.28 |NAME:jpegPhoto |USAGE:userApplications |NUMERICOID:0.9.2342.19200300.100.1.60 |DESC:RFC2798: a JPEG image"),
};
// Very simple test - mainly just to exercise the code and to
// ensure we get representative test coverage
@Test
public void testSchemaViewer() throws Exception {
new ExecuteRunnable<TestData>().runTests(new RunnableTest<TestData>() {
public void runTest(TestData testData) {
String result=runSchemaViewer(testData.flag, testData.value);
assertEquals(testData.result, result);
}
}, viewerTestData);
}
}

View File

@@ -1,42 +1,42 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import jdepend.framework.JDepend;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class TestsWithJdepend {
private JDepend jdepend;
@Before
public void setUp() throws IOException {
jdepend = new JDepend();
jdepend.addDirectory("build/classes/java/main");
}
@Test
public void testAllPackages() {
jdepend.analyze();
assertEquals(false, jdepend.containsCycles());
}
}
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.odm.test;
import jdepend.framework.JDepend;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class TestsWithJdepend {
private JDepend jdepend;
@Before
public void setUp() throws IOException {
jdepend = new JDepend();
jdepend.addDirectory("build/classes/java/main");
}
@Test
public void testAllPackages() {
jdepend.analyze();
assertEquals(false, jdepend.containsCycles());
}
}

View File

@@ -1,29 +1,29 @@
package org.springframework.ldap.odm.test;
import java.net.URI;
import org.springframework.ldap.odm.typeconversion.impl.Converter;
/**
* A bi-directional converter between {@link java.net.URI} and {@link java.lang.String}.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk>
*/
public class UriConverter implements Converter {
/* (non-Javadoc)
* @see org.springframework.ldap.odm.typeconversion.impl.Converter#convert(java.lang.Object, java.lang.Class)
*/
public <T> T convert(Object source, Class<T> toClass) throws Exception {
T result = null;
if (String.class.isAssignableFrom(source.getClass()) && toClass == URI.class) {
result = toClass.cast(new URI((String)source));
} else {
if (URI.class.isAssignableFrom(source.getClass()) && toClass == String.class) {
result = toClass.cast(source.toString());
}
}
return result;
}
}
package org.springframework.ldap.odm.test;
import java.net.URI;
import org.springframework.ldap.odm.typeconversion.impl.Converter;
/**
* A bi-directional converter between {@link java.net.URI} and {@link java.lang.String}.
*
* @author Paul Harvey &lt;paul.at.pauls-place.me.uk>
*/
public class UriConverter implements Converter {
/* (non-Javadoc)
* @see org.springframework.ldap.odm.typeconversion.impl.Converter#convert(java.lang.Object, java.lang.Class)
*/
public <T> T convert(Object source, Class<T> toClass) throws Exception {
T result = null;
if (String.class.isAssignableFrom(source.getClass()) && toClass == URI.class) {
result = toClass.cast(new URI((String)source));
} else {
if (URI.class.isAssignableFrom(source.getClass()) && toClass == String.class) {
result = toClass.cast(source.toString());
}
}
return result;
}
}

View File

@@ -1,24 +1,24 @@
package org.springframework.ldap.odm.test.utils;
import java.io.File;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class CompilerInterface {
// Compile the given file - when we can drop Java 5 we'll use the Java 6 compiler API
public static void compile(String directory, String file) throws Exception {
File toCompile = new File(directory, file);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> javaFileObjects =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(toCompile));
compiler.getTask(null, fileManager, null, null, null, javaFileObjects).call();
fileManager.close();
}
}
package org.springframework.ldap.odm.test.utils;
import java.io.File;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class CompilerInterface {
// Compile the given file - when we can drop Java 5 we'll use the Java 6 compiler API
public static void compile(String directory, String file) throws Exception {
File toCompile = new File(directory, file);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> javaFileObjects =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(toCompile));
compiler.getTask(null, fileManager, null, null, null, javaFileObjects).call();
fileManager.close();
}
}

View File

@@ -1,6 +1,6 @@
package org.springframework.ldap.odm.test.utils;
// Interface to implement for tests to be run by ExecuteRunnable
public interface RunnableTest<T> {
void runTest(T testData) throws Exception;
}
package org.springframework.ldap.odm.test.utils;
// Interface to implement for tests to be run by ExecuteRunnable
public interface RunnableTest<T> {
void runTest(T testData) throws Exception;
}