Nuiton-utils-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
December 2010
- 6 participants
- 45 discussions
Author: tchemit
Date: 2010-12-29 20:29:05 +0100 (Wed, 29 Dec 2010)
New Revision: 1995
Url: http://nuiton.org/repositories/revision/nuiton-utils/1995
Log:
[maven-release-plugin] prepare for next development iteration
Modified:
trunk/pom.xml
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2010-12-29 19:29:04 UTC (rev 1994)
+++ trunk/pom.xml 2010-12-29 19:29:05 UTC (rev 1995)
@@ -37,7 +37,7 @@
</parent>
<artifactId>nuiton-utils</artifactId>
- <version>1.5.3</version>
+ <version>2.0-SNAPSHOT</version>
<dependencies>
@@ -214,9 +214,9 @@
<!-- Source control management. -->
<scm>
- <connection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/tags/nuiton-utils-1.5.3</connection>
- <developerConnection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/tags/nuiton-utils-1.5.3</developerConnection>
- <url>http://www.nuiton.org/repositories/browse/nuiton-utils/tags/nuiton-utils-1.…</url>
+ <connection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/trunk</connection>
+ <developerConnection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/trunk</developerConnection>
+ <url>http://www.nuiton.org/repositories/browse/nuiton-utils/trunk</url>
</scm>
<profiles>
1
0
Author: tchemit
Date: 2010-12-29 20:29:04 +0100 (Wed, 29 Dec 2010)
New Revision: 1994
Url: http://nuiton.org/repositories/revision/nuiton-utils/1994
Log:
[maven-release-plugin] copy for tag nuiton-utils-1.5.3
Added:
tags/nuiton-utils-1.5.3/
Property changes on: tags/nuiton-utils-1.5.3
___________________________________________________________________
Added: svn:ignore
+ target
velocity.log
maven.log
.eclipse
.classpath
*.iml
*.ipr
*.iws
.settings
.project
Added: svn:mergeinfo
+
1
0
Author: tchemit
Date: 2010-12-29 20:29:02 +0100 (Wed, 29 Dec 2010)
New Revision: 1993
Url: http://nuiton.org/repositories/revision/nuiton-utils/1993
Log:
[maven-release-plugin] prepare release nuiton-utils-1.5.3
Modified:
trunk/pom.xml
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2010-12-29 19:21:53 UTC (rev 1992)
+++ trunk/pom.xml 2010-12-29 19:29:02 UTC (rev 1993)
@@ -37,7 +37,7 @@
</parent>
<artifactId>nuiton-utils</artifactId>
- <version>1.5.3-SNAPSHOT</version>
+ <version>1.5.3</version>
<dependencies>
@@ -214,9 +214,9 @@
<!-- Source control management. -->
<scm>
- <connection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/trunk</connection>
- <developerConnection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/trunk</developerConnection>
- <url>http://www.nuiton.org/repositories/browse/nuiton-utils/trunk</url>
+ <connection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/tags/nuiton-utils-1.5.3</connection>
+ <developerConnection>scm:svn:http://svn.nuiton.org/svn/nuiton-utils/tags/nuiton-utils-1.5.3</developerConnection>
+ <url>http://www.nuiton.org/repositories/browse/nuiton-utils/tags/nuiton-utils-1.…</url>
</scm>
<profiles>
1
0
r1992 - in trunk/src: main/java/org/nuiton/util/beans test/java/org/nuiton/util/beans
by tchemit@users.nuiton.org 29 Dec '10
by tchemit@users.nuiton.org 29 Dec '10
29 Dec '10
Author: tchemit
Date: 2010-12-29 20:21:53 +0100 (Wed, 29 Dec 2010)
New Revision: 1992
Url: http://nuiton.org/repositories/revision/nuiton-utils/1992
Log:
add headers + svn keywords on new classes
Modified:
trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java 2010-12-29 17:54:25 UTC (rev 1991)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java 2010-12-29 19:21:53 UTC (rev 1992)
@@ -1,3 +1,27 @@
+/*
+ * #%L
+ * Nuiton Utils
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2010 CodeLutin
+ * %%
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
package org.nuiton.util.beans;
import org.apache.commons.logging.Log;
Property changes on: trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
___________________________________________________________________
Modified: svn:keywords
- Author Date Id Revision
+ Author Date Id Revision HeadURL
Modified: trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java 2010-12-29 17:54:25 UTC (rev 1991)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java 2010-12-29 19:21:53 UTC (rev 1992)
@@ -1,3 +1,27 @@
+/*
+ * #%L
+ * Nuiton Utils
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2010 CodeLutin
+ * %%
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
package org.nuiton.util.beans;
import org.junit.Assert;
Property changes on: trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
___________________________________________________________________
Modified: svn:keywords
- Author Date Id Revision
+ Author Date Id Revision HeadURL
1
0
29 Dec '10
Author: tchemit
Date: 2010-12-29 18:54:25 +0100 (Wed, 29 Dec 2010)
New Revision: 1991
Url: http://nuiton.org/repositories/revision/nuiton-utils/1991
Log:
- Depreciates TestUtil
- Add some usefull methods on BinderFactory to obtain cached binder model
Modified:
trunk/src/main/java/org/nuiton/util/TestUtil.java
trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
Modified: trunk/src/main/java/org/nuiton/util/TestUtil.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/TestUtil.java 2010-12-29 15:30:39 UTC (rev 1990)
+++ trunk/src/main/java/org/nuiton/util/TestUtil.java 2010-12-29 17:54:25 UTC (rev 1991)
@@ -38,8 +38,9 @@
* Created: 22 juin 2010
*
* @author fdesbois <fdesbois(a)codelutin.com>
- * @version $Id$
+ * @deprecated since 1.5.3, will not be replaced here and will be removed in 2.0
*/
+@Deprecated
public class TestUtil {
private static Log log = LogFactory.getLog(TestUtil.class);
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java 2010-12-29 15:30:39 UTC (rev 1990)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java 2010-12-29 17:54:25 UTC (rev 1991)
@@ -25,14 +25,14 @@
* <pre>
* Binder<BeanA, BeanA> binder = BinderFactory.newBinder(BeanA.class, "mycontext");
* </pre>
- *
+ * <p/>
* This is usefull when you register your own binder model in the factory (see
* next section) to bind different things from the same type of objects...
- *
+ * <p/>
* <h1>Register a new binder model</h1>
* To register a new binder's model use one of the method {@code registerBinderModel(XXX)}.
* <p/>
- *
+ * <p/>
* More documentation will come soon, yu can see the package info javadoc or
* unit tests...
*
@@ -190,7 +190,43 @@
}
}
+ /**
+ * Tells if there is a cached binder model for the given parameters.
+ *
+ * @param sourceType the type of source
+ * @param targetType the type of target
+ * @param contextName the context's name of the searched binder
+ * @param <S> the type of source
+ * @param <T> the type of target
+ * @return {@code true} if there is a cached binder model for the given
+ * parameters, {@code false} otherwise.
+ */
+ public static <S, T> boolean isBinderModelExists(Class<S> sourceType,
+ Class<T> targetType,
+ String contextName) {
+ Binder.BinderModel<S, T> model =
+ getBinderModels().get(sourceType, targetType, contextName);
+ return model != null;
+ }
+ /**
+ * Obtain a cached binder model.
+ *
+ * @param sourceType the type of source
+ * @param targetType the type of target
+ * @param contextName the context's name of the searched binder
+ * @param <S> the type of source
+ * @param <T> the type of target
+ * @return the cached binder model or {@code null} if not found.
+ */
+ public static <S, T> Binder.BinderModel<S, T> getCachedBinderModel(Class<S> sourceType,
+ Class<T> targetType,
+ String contextName) {
+ Binder.BinderModel<S, T> model =
+ getBinderModels().get(sourceType, targetType, contextName);
+ return model;
+ }
+
protected static BindelModelEntryMap getBinderModels() {
if (binderModels == null) {
binderModels = new BindelModelEntryMap();
@@ -207,7 +243,10 @@
}
/**
- * Gets the registred binder given his types and his context's name.
+ * Instanciate a new binder given his types and his context's name.
+ * <p/>
+ * If the corresponding binder model does not exist, then it will be created
+ * and cached (using the {@link BinderModelBuilder#newDefaultBuilder(Class, Class)} method).
*
* @param sourceType the type of source
* @param targetType the type of target
@@ -215,7 +254,7 @@
* @param binderType type of binder required
* @param <S> the type of source
* @param <T> the type of target
- * @return the registred binder or {@code null} if not found.
+ * @return the new instanciated binder.
*/
protected static <S, T, B extends Binder<S, T>> Binder<S, T> newBinder0(Class<S> sourceType,
Class<T> targetType,
1
0
r1990 - in trunk/src: main/java/org/nuiton/util/beans test/java/org/nuiton/util/beans
by tchemit@users.nuiton.org 29 Dec '10
by tchemit@users.nuiton.org 29 Dec '10
29 Dec '10
Author: tchemit
Date: 2010-12-29 16:30:39 +0100 (Wed, 29 Dec 2010)
New Revision: 1990
Url: http://nuiton.org/repositories/revision/nuiton-utils/1990
Log:
Evolution #1179: Improve Binder api
Removed:
trunk/src/main/java/org/nuiton/util/beans/BinderModel.java
Modified:
trunk/src/main/java/org/nuiton/util/beans/Binder.java
trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java
trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java
trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java
trunk/src/main/java/org/nuiton/util/beans/package-info.java
trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java
trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java
trunk/src/test/java/org/nuiton/util/beans/BinderTest.java
Modified: trunk/src/main/java/org/nuiton/util/beans/Binder.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/Binder.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/main/java/org/nuiton/util/beans/Binder.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -29,6 +29,7 @@
import org.apache.commons.logging.LogFactory;
import org.nuiton.util.ObjectUtil;
+import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -49,18 +50,24 @@
* It is based on a {@link BinderModel} which contains the mapping of properties
* to transfert from the source object to the destination object.
* <p/>
- * Use the method {@link #copy(Object, Object,String...)} to transfert properties.
+ * Use the method {@link #copy(Object, Object, String...)} to transfert properties.
* <p/>
- * Use the method {@link #obtainProperties(Object,String...)} to obtain
+ * Use the method {@link #obtainProperties(Object, String...)} to obtain some
+ * properties from a given object.
+ * <p/>
+ * For more informations about how to obtain a binder, see the
+ * {@link BinderFactory} or the package info javadoc or unit tests...
*
- * @author tchemit <chemit(a)codelutin.com>
* @param <I> the source bean type
* @param <O> the destination bean type
+ * @author tchemit <chemit(a)codelutin.com>
+ * @see BinderFactory
+ * @see BinderModelBuilder
* @since 1.1.5
*/
public class Binder<I, O> implements Serializable {
- /** Logger */
+ /** Logger. */
private static final Log log = LogFactory.getLog(Binder.class);
private static final long serialVersionUID = 1L;
@@ -83,7 +90,7 @@
/** To duplicate the collection */
duplicate {
-
+ @SuppressWarnings({"unchecked"})
@Override
public Object copy(Object readValue) {
if (readValue instanceof Set<?>) {
@@ -243,10 +250,11 @@
* specifed, will use all the properties defined in
* binder)
* @throws NullPointerException if target parameter is {@code null}
+ * @throws RuntimeException if a property can not be copied to the target object
*/
protected void copy(I source, O target, boolean excludeProperties,
String... propertyNames)
- throws NullPointerException {
+ throws RuntimeException {
if (target == null) {
throw new NullPointerException("parameter 'target' can no be null");
}
@@ -289,12 +297,13 @@
read = getCollectionValue(sourceProperty, read);
}
model.getTargetWriteMethod(targetProperty).invoke(target, read);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
- throw new RuntimeException(e);
- } catch (InstantiationException e) {
- throw new RuntimeException(e);
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Could not bind property [" +
+ source.getClass().getName() + ":" +
+ sourceProperty + "] to [" +
+ target.getClass().getName() + ":" +
+ targetProperty + "]", e);
}
}
}
@@ -371,40 +380,300 @@
}
protected Object bindProperty(String sourceProperty, Object read) throws IllegalAccessException, InstantiationException {
- Binder<?,?> binder = model.getBinder(sourceProperty);
+ Binder<?, ?> binder = model.getBinder(sourceProperty);
Object result = bind(binder, read);
return result;
}
-
+ @SuppressWarnings({"unchecked"})
protected Object bindCollection(String sourceProperty, Object read) throws IllegalAccessException, InstantiationException {
if (read == null) {
return null;
}
- Binder<?,?> binder = model.getBinder(sourceProperty);
+ Binder<?, ?> binder = model.getBinder(sourceProperty);
- Collection result = null;
+ Collection result;
if (read instanceof Set<?>) {
result = new HashSet();
- }
- // in any other cases, let says this is a ArrayList
- if (read instanceof Collection<?>) {
+ } else {
+
+ // in any other cases, let says this is a ArrayList
result = new ArrayList();
}
-
- for (Object o : (Collection<?>) read) {
+ Collection<?> collection = (Collection<?>) read;
+ for (Object o : collection) {
Object r = bind(binder, o);
result.add(r);
}
return result;
}
+ @SuppressWarnings({"unchecked"})
protected Object bind(Binder binder, Object read) throws IllegalAccessException, InstantiationException {
Object result = read.getClass().newInstance();
binder.copy(read, result);
return result;
}
+
+ /**
+ * Model of a {@link Binder}.
+ * <p/>
+ * TODO tchemit 20100225 should have special cases for collections treatment.
+ *
+ * @param <S> the source type
+ * @param <T> the target type
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 1.1.5
+ */
+ public static class BinderModel<S, T> implements Serializable {
+
+ /** source type */
+ protected final Class<S> sourceType;
+
+ /** destination type */
+ protected final Class<T> targetType;
+
+ /** source type descriptors (key are property names) */
+ protected final Map<String, PropertyDescriptor> sourceDescriptors;
+
+ /** destination descriptors (key are property names) */
+ protected final Map<String, PropertyDescriptor> targetDescriptors;
+
+ /**
+ * properties mapping (key are source properties, value are destination
+ * properties)
+ */
+ protected final Map<String, String> propertiesMapping;
+
+ /** mapping of collection properties strategies */
+ protected Map<String, CollectionStrategy> collectionStrategies;
+
+ /** mapping of extra binders to use to copy properties */
+ protected Map<String, Binder<?, ?>> binders;
+
+ private static final long serialVersionUID = 2L;
+
+ public BinderModel(Class<S> sourceType, Class<T> targetType) {
+ this.sourceType = sourceType;
+ this.targetType = targetType;
+ sourceDescriptors = new TreeMap<String, PropertyDescriptor>();
+ targetDescriptors = new TreeMap<String, PropertyDescriptor>();
+ propertiesMapping = new TreeMap<String, String>();
+ collectionStrategies = new TreeMap<String, CollectionStrategy>();
+ binders = new TreeMap<String, Binder<?, ?>>();
+ }
+
+ /**
+ * Gets the type of the binder's source.
+ *
+ * @return the type of the source object in the binder
+ */
+ public Class<S> getSourceType() {
+ return sourceType;
+ }
+
+ /**
+ * Gets the type of the binder's destination
+ *
+ * @return the type of the destination object in the binder
+ */
+ public Class<T> getTargetType() {
+ return targetType;
+ }
+
+ /**
+ * Gets all registred property names of the binder's source type.
+ *
+ * @return the array of all source object properties names to bind
+ */
+ public String[] getSourceDescriptors() {
+ Set<String> universe = sourceDescriptors.keySet();
+ return universe.toArray(new String[sourceDescriptors.size()]);
+ }
+
+ public CollectionStrategy getCollectionStrategy(String property) {
+ return collectionStrategies.get(property);
+ }
+
+ /**
+ * Gets all registred property names of the binder's destination type.
+ *
+ * @return the array of all source object properties names to bind
+ */
+ public String[] getTargetDescriptors() {
+ Set<String> universe = targetDescriptors.keySet();
+ return universe.toArray(new String[targetDescriptors.size()]);
+ }
+
+ /**
+ * Gets the destination property name given the
+ *
+ * @param sourceProperty the name of the source property to bind
+ * @return the name of the destination object property to bind, or
+ * {@code null} if {@code propertySrc} is unknown in the model
+ */
+ public String getTargetProperty(String sourceProperty) {
+ if (!containsSourceProperty(sourceProperty)) {
+ return null;
+ }
+ String dstProperty = propertiesMapping.get(sourceProperty);
+ return dstProperty;
+ }
+
+ /**
+ * Gets the bean descriptor of the source type for the given
+ * destination property.
+ *
+ * @param sourceProperty name of the source type property name
+ * @return the descriptor or {@code null} if not found.
+ */
+ public PropertyDescriptor getSourceDescriptor(String sourceProperty) {
+ // check src property is registred
+ if (!containsSourceProperty(sourceProperty)) {
+ return null;
+ }
+ PropertyDescriptor descriptor = sourceDescriptors.get(sourceProperty);
+ return descriptor;
+ }
+
+ /**
+ * @param srcProperty the name of a property of the source object.
+ * @return the method to read in a source object for the given property.
+ */
+ public Method getSourceReadMethod(String srcProperty) {
+ PropertyDescriptor descriptor = getSourceDescriptor(srcProperty);
+ Method readMethod = null;
+ if (descriptor != null) {
+ readMethod = descriptor.getReadMethod();
+ }
+ return readMethod;
+ }
+
+ /**
+ * @param sourceProperty the name of a property of the source object.
+ * @return the method to write in a source object for the given property.
+ */
+ public Method getSourceWriteMethod(String sourceProperty) {
+ PropertyDescriptor descriptor = getSourceDescriptor(sourceProperty);
+ Method writeMethod = null;
+ if (descriptor != null) {
+ writeMethod = descriptor.getWriteMethod();
+ }
+ return writeMethod;
+ }
+
+ /**
+ * Gets the bean descriptor of the destination type for the given
+ * destination property.
+ *
+ * @param targetProperty name of the destination type property name
+ * @return the descriptor or {@code null} if not found.
+ */
+ public PropertyDescriptor getTargetDescriptor(String targetProperty) {
+ // check dst property is registred
+ if (!containsTargetProperty(targetProperty)) {
+ return null;
+ }
+ PropertyDescriptor descriptor = targetDescriptors.get(targetProperty);
+ return descriptor;
+ }
+
+ /**
+ * @param targetProperty the name of a property of the destination object.
+ * @return the method to read in a destination object for the given
+ * property.
+ */
+ public Method getTargetReadMethod(String targetProperty) {
+ PropertyDescriptor descriptor = getTargetDescriptor(targetProperty);
+ Method readMethod = null;
+ if (descriptor != null) {
+ readMethod = descriptor.getReadMethod();
+ }
+ return readMethod;
+ }
+
+ /**
+ * @param targetProperty the name of a property of the destination object.
+ * @return the method to write in a destination object for the given
+ * property.
+ */
+ public Method getTargetWriteMethod(String targetProperty) {
+ PropertyDescriptor descriptor = getTargetDescriptor(targetProperty);
+ Method writeMethod = null;
+ if (descriptor != null) {
+ writeMethod = descriptor.getWriteMethod();
+ }
+ return writeMethod;
+ }
+
+ public Class<?> getCollectionType(String sourceProperty) {
+ Method method = getSourceReadMethod(sourceProperty);
+ Class<?> type = method.getReturnType();
+ if (Collection.class.isAssignableFrom(type)) {
+ return type;
+ }
+ return null;
+ }
+
+ public void addCollectionStrategy(String propertyName,
+ CollectionStrategy strategy) {
+ collectionStrategies.put(propertyName, strategy);
+ }
+
+ public void addBinder(String propertyName, Binder<?, ?> binder) {
+ binders.put(propertyName, binder);
+ }
+
+ protected boolean containsSourceProperty(String sourceProperty) {
+ return propertiesMapping.containsKey(sourceProperty);
+ }
+
+ protected boolean containsTargetProperty(String targetProperty) {
+ return propertiesMapping.containsValue(targetProperty);
+ }
+
+ protected boolean containsCollectionProperty(String propertyName) {
+ return collectionStrategies.containsKey(propertyName);
+ }
+
+ protected boolean containsBinderProperty(String propertyName) {
+ return binders.containsKey(propertyName);
+ }
+
+ protected void addBinding(PropertyDescriptor sourceDescriptor,
+ PropertyDescriptor targetDescriptor) {
+
+ String sourceProperty = sourceDescriptor.getName();
+ String targetProperty = targetDescriptor.getName();
+ sourceDescriptors.put(sourceProperty, sourceDescriptor);
+ targetDescriptors.put(targetProperty, targetDescriptor);
+ propertiesMapping.put(sourceProperty, targetProperty);
+ }
+
+ protected void removeBinding(String source) {
+ String target = propertiesMapping.get(source);
+
+ sourceDescriptors.remove(source);
+ targetDescriptors.remove(target);
+ propertiesMapping.remove(source);
+
+ if (containsBinderProperty(source)) {
+ binders.remove(source);
+ }
+ if (containsCollectionProperty(source)) {
+ collectionStrategies.remove(source);
+ }
+ }
+
+ protected Map<String, String> getPropertiesMapping() {
+ return propertiesMapping;
+ }
+
+ public Binder<?, ?> getBinder(String sourceProperty) {
+ return binders.get(sourceProperty);
+ }
+ }
}
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -36,19 +36,21 @@
import java.util.TreeMap;
/**
- * A builder of {@link BinderModel} and {@link Binder}.
+ * A builder of {@link Binder.BinderModel} and {@link Binder}.
* <p/>
* A {@code binder} permits to copy some properties from a bean to another one.
*
* @author tchemit <chemit(a)codelutin.com>
- * @see BinderModel
+ * @see Binder.BinderModel
* @see Binder
* @since 1.1.5
+ * @deprecated since 1.5.3, prefer use the {@link BinderModelBuilder} instead, will be removed in version 2.0
*/
+@Deprecated
public class BinderBuilder {
/** current model used to build the binder */
- protected BinderModel<?, ?> model;
+ protected Binder.BinderModel<?, ?> model;
/** source properties descriptors */
protected Map<String, PropertyDescriptor> sourceDescriptors;
@@ -135,7 +137,7 @@
}
// init model
- model = new BinderModel(sourceType, targetType);
+ model = new Binder.BinderModel(sourceType, targetType);
// obtain source descriptors
sourceDescriptors = new TreeMap<String, PropertyDescriptor>();
@@ -425,7 +427,7 @@
}
}
- protected BinderModel<?, ?> getModel() {
+ protected Binder.BinderModel<?, ?> getModel() {
return model;
}
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -11,6 +11,7 @@
/**
* Factory of {@link Binder}.
* <p/>
+ * <h1>Obtain a new binder</h1>
* To obtain a new binder you can use the {@code newBinder(XXX)} methods.
* <p/>
* For example to obtain a mirrored binder (same source and target type) which
@@ -18,7 +19,23 @@
* <pre>
* Binder<BeanA, BeanA> binder = BinderFactory.newBinder(BeanA.class);
* </pre>
+ * <h1>Usage of contextale binder</h1>
+ * It is possible to use different binder for same source and target type, using a
+ * extra context name parameter, like this :
+ * <pre>
+ * Binder<BeanA, BeanA> binder = BinderFactory.newBinder(BeanA.class, "mycontext");
+ * </pre>
*
+ * This is usefull when you register your own binder model in the factory (see
+ * next section) to bind different things from the same type of objects...
+ *
+ * <h1>Register a new binder model</h1>
+ * To register a new binder's model use one of the method {@code registerBinderModel(XXX)}.
+ * <p/>
+ *
+ * More documentation will come soon, yu can see the package info javadoc or
+ * unit tests...
+ *
* @author tchemit <chemit(a)codelutin.com>
* @since 1.5.3
*/
@@ -106,41 +123,41 @@
return binder;
}
- public static <S, T> BinderModel<S, T> registerBinderModel(BinderModelBuilder<S, T> binderModelBuilder) throws IllegalArgumentException {
- BinderModel<S, T> model = registerBinderModel(binderModelBuilder, null);
+ public static <S, T> Binder.BinderModel<S, T> registerBinderModel(BinderModelBuilder<S, T> binderModelBuilder) throws IllegalArgumentException {
+ Binder.BinderModel<S, T> model = registerBinderModel(binderModelBuilder, null);
return model;
}
- public static <S, T> BinderModel<S, T> registerBinderModel(Binder<S, T> binder) throws IllegalArgumentException {
- BinderModel<S, T> model = registerBinderModel(binder, null);
+ public static <S, T> Binder.BinderModel<S, T> registerBinderModel(Binder<S, T> binder) throws IllegalArgumentException {
+ Binder.BinderModel<S, T> model = registerBinderModel(binder, null);
return model;
}
- public static <S, T> BinderModel<S, T> registerBinderModel(BinderModel<S, T> model) throws IllegalArgumentException {
+ public static <S, T> Binder.BinderModel<S, T> registerBinderModel(Binder.BinderModel<S, T> model) throws IllegalArgumentException {
registerBinderModel(model, null);
return model;
}
- public static <S, T> BinderModel<S, T> registerBinderModel(BinderModelBuilder<S, T> binderModelBuilder,
- String contextName) throws IllegalArgumentException {
- BinderModel<S, T> model = binderModelBuilder.getModel();
+ public static <S, T> Binder.BinderModel<S, T> registerBinderModel(BinderModelBuilder<S, T> binderModelBuilder,
+ String contextName) throws IllegalArgumentException {
+ Binder.BinderModel<S, T> model = binderModelBuilder.getModel();
registerBinderModel(model, contextName);
return model;
}
- public static <S, T> BinderModel<S, T> registerBinderModel(Binder<S, T> binder,
- String contextName) throws IllegalArgumentException {
- BinderModel<S, T> model = binder.getModel();
+ public static <S, T> Binder.BinderModel<S, T> registerBinderModel(Binder<S, T> binder,
+ String contextName) throws IllegalArgumentException {
+ Binder.BinderModel<S, T> model = binder.getModel();
registerBinderModel(model, contextName);
return model;
}
- public static <S, T> BinderModel<S, T> registerBinderModel(BinderModel<S, T> model,
- String contextName) throws IllegalArgumentException {
+ public static <S, T> Binder.BinderModel<S, T> registerBinderModel(Binder.BinderModel<S, T> model,
+ String contextName) throws IllegalArgumentException {
// check if the given model is not already registred for the given context
- BinderModel<S, T> registredModel =
+ Binder.BinderModel<S, T> registredModel =
getBinderModels().get(model, contextName);
// let's add this model into cache of models
@@ -181,7 +198,7 @@
return binderModels;
}
- protected static String toString(BinderModel<?, ?> model, String contextName) {
+ protected static String toString(Binder.BinderModel<?, ?> model, String contextName) {
return toString(model.getSourceType(), model.getTargetType(), contextName);
}
@@ -206,7 +223,7 @@
Class<B> binderType) {
// obtain the cached model
- BinderModel<S, T> model =
+ Binder.BinderModel<S, T> model =
getBinderModels().get(sourceType, targetType, contextName);
if (model == null) {
@@ -237,18 +254,18 @@
return binder;
}
- public static class BindelModelEntryMap implements Map<BinderModelEntry, BinderModel<?, ?>> {
+ public static class BindelModelEntryMap implements Map<BinderModelEntry, Binder.BinderModel<?, ?>> {
- protected Map<BinderModelEntry, BinderModel<?, ?>> delegate;
+ protected Map<BinderModelEntry, Binder.BinderModel<?, ?>> delegate;
public BindelModelEntryMap() {
- delegate = new HashMap<BinderModelEntry, BinderModel<?, ?>>();
+ delegate = new HashMap<BinderModelEntry, Binder.BinderModel<?, ?>>();
}
- public <S, T> BinderModel<S, T> get(Class<S> source,
- Class<T> target,
- String contextName) {
- BinderModel<S, T> result = null;
+ public <S, T> Binder.BinderModel<S, T> get(Class<S> source,
+ Class<T> target,
+ String contextName) {
+ Binder.BinderModel<S, T> result = null;
for (BinderModelEntry key : binderModels.keySet()) {
if (!key.getSourceType().equals(source)) {
@@ -268,18 +285,18 @@
}
}
- result = (BinderModel<S, T>) binderModels.get(key);
+ result = (Binder.BinderModel<S, T>) binderModels.get(key);
break;
}
return result;
}
- public <S, T> BinderModel<S, T> get(BinderModel<S, T> model,
- String contextName) {
+ public <S, T> Binder.BinderModel<S, T> get(Binder.BinderModel<S, T> model,
+ String contextName) {
Class<S> source = model.getSourceType();
Class<T> target = model.getTargetType();
- BinderModel<S, T> result = get(source, target, contextName);
+ Binder.BinderModel<S, T> result = get(source, target, contextName);
return result;
}
@@ -304,20 +321,20 @@
}
@Override
- public BinderModel<?, ?> get(Object key) {
+ public Binder.BinderModel<?, ?> get(Object key) {
return delegate.get(key);
}
- public BinderModel<?, ?> put(BinderModelEntry key, BinderModel<?, ?> value) {
+ public Binder.BinderModel<?, ?> put(BinderModelEntry key, Binder.BinderModel<?, ?> value) {
return delegate.put(key, value);
}
@Override
- public BinderModel<?, ?> remove(Object key) {
+ public Binder.BinderModel<?, ?> remove(Object key) {
return delegate.remove(key);
}
- public void putAll(Map<? extends BinderModelEntry, ? extends BinderModel<?, ?>> m) {
+ public void putAll(Map<? extends BinderModelEntry, ? extends Binder.BinderModel<?, ?>> m) {
delegate.putAll(m);
}
@@ -332,12 +349,12 @@
}
@Override
- public Collection<BinderModel<?, ?>> values() {
+ public Collection<Binder.BinderModel<?, ?>> values() {
return delegate.values();
}
@Override
- public Set<Entry<BinderModelEntry, BinderModel<?, ?>>> entrySet() {
+ public Set<Entry<BinderModelEntry, Binder.BinderModel<?, ?>>> entrySet() {
return delegate.entrySet();
}
@@ -365,7 +382,7 @@
this.name = name;
}
- public BinderModelEntry(BinderModel<?, ?> model, String contextName) {
+ public BinderModelEntry(Binder.BinderModel<?, ?> model, String contextName) {
this(model.getSourceType(), model.getTargetType(), contextName);
}
Deleted: trunk/src/main/java/org/nuiton/util/beans/BinderModel.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderModel.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderModel.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -1,294 +0,0 @@
-/*
- * #%L
- * Nuiton Utils
- *
- * $Id$
- * $HeadURL$
- * %%
- * Copyright (C) 2004 - 2010 CodeLutin
- * %%
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
- * License along with this program. If not, see
- * <http://www.gnu.org/licenses/lgpl-3.0.html>.
- * #L%
- */
-
-package org.nuiton.util.beans;
-
-import java.beans.PropertyDescriptor;
-import java.io.Serializable;
-import java.lang.reflect.Method;
-import java.util.Collection;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-
-
-/**
- * Model of a {@link Binder}.
- * <p/>
- * TODO-TC20100225 should have special cases for collections treatment.
- *
- * @param <S> the source type
- * @param <T> the target type
- * @author tchemit <chemit(a)codelutin.com>
- * @since 1.1.5
- */
-public class BinderModel<S, T> implements Serializable {
-
- /** source type */
- protected final Class<S> sourceType;
-
- /** destination type */
- protected final Class<T> targetType;
-
- /** source type descriptors (key are property names) */
- protected final Map<String, PropertyDescriptor> sourceDescriptors;
-
- /** destination descriptors (key are property names) */
- protected final Map<String, PropertyDescriptor> targetDescriptors;
-
- /**
- * properties mapping (key are source properties, value are destination
- * properties)
- */
- protected final Map<String, String> propertiesMapping;
-
- /** mapping of collection properties strategies */
- protected Map<String, Binder.CollectionStrategy> collectionStrategies;
-
- /** mapping of extra binders to use to copy properties */
- protected Map<String, Binder<?, ?>> binders;
-
- private static final long serialVersionUID = 2L;
-
- public BinderModel(Class<S> sourceType, Class<T> targetType) {
- this.sourceType = sourceType;
- this.targetType = targetType;
- sourceDescriptors = new TreeMap<String, PropertyDescriptor>();
- targetDescriptors = new TreeMap<String, PropertyDescriptor>();
- propertiesMapping = new TreeMap<String, String>();
- collectionStrategies = new TreeMap<String, Binder.CollectionStrategy>();
- binders = new TreeMap<String, Binder<?, ?>>();
- }
-
- /**
- * Gets the type of the binder's source.
- *
- * @return the type of the source object in the binder
- */
- public Class<S> getSourceType() {
- return sourceType;
- }
-
- /**
- * Gets the type of the binder's destination
- *
- * @return the type of the destination object in the binder
- */
- public Class<T> getTargetType() {
- return targetType;
- }
-
- /**
- * Gets all registred property names of the binder's source type.
- *
- * @return the array of all source object properties names to bind
- */
- public String[] getSourceDescriptors() {
- Set<String> universe = sourceDescriptors.keySet();
- return universe.toArray(new String[sourceDescriptors.size()]);
- }
-
- public Binder.CollectionStrategy getCollectionStrategy(String property) {
- return collectionStrategies.get(property);
- }
-
- /**
- * Gets all registred property names of the binder's destination type.
- *
- * @return the array of all source object properties names to bind
- */
- public String[] getTargetDescriptors() {
- Set<String> universe = targetDescriptors.keySet();
- return universe.toArray(new String[targetDescriptors.size()]);
- }
-
- /**
- * Gets the destination property name given the
- *
- * @param sourceProperty the name of the source property to bind
- * @return the name of the destination object property to bind, or
- * {@code null} if {@code propertySrc} is unknown in the model
- */
- public String getTargetProperty(String sourceProperty) {
- if (!containsSourceProperty(sourceProperty)) {
- return null;
- }
- String dstProperty = propertiesMapping.get(sourceProperty);
- return dstProperty;
- }
-
- /**
- * Gets the bean descriptor of the source type for the given
- * destination property.
- *
- * @param sourceProperty name of the source type property name
- * @return the descriptor or {@code null} if not found.
- */
- public PropertyDescriptor getSourceDescriptor(String sourceProperty) {
- // check src property is registred
- if (!containsSourceProperty(sourceProperty)) {
- return null;
- }
- PropertyDescriptor descriptor = sourceDescriptors.get(sourceProperty);
- return descriptor;
- }
-
- /**
- * @param srcProperty the name of a property of the source object.
- * @return the method to read in a source object for the given property.
- */
- public Method getSourceReadMethod(String srcProperty) {
- PropertyDescriptor descriptor = getSourceDescriptor(srcProperty);
- Method readMethod = null;
- if (descriptor != null) {
- readMethod = descriptor.getReadMethod();
- }
- return readMethod;
- }
-
- /**
- * @param sourceProperty the name of a property of the source object.
- * @return the method to write in a source object for the given property.
- */
- public Method getSourceWriteMethod(String sourceProperty) {
- PropertyDescriptor descriptor = getSourceDescriptor(sourceProperty);
- Method writeMethod = null;
- if (descriptor != null) {
- writeMethod = descriptor.getWriteMethod();
- }
- return writeMethod;
- }
-
- /**
- * Gets the bean descriptor of the destination type for the given
- * destination property.
- *
- * @param targetProperty name of the destination type property name
- * @return the descriptor or {@code null} if not found.
- */
- public PropertyDescriptor getTargetDescriptor(String targetProperty) {
- // check dst property is registred
- if (!containsTargetProperty(targetProperty)) {
- return null;
- }
- PropertyDescriptor descriptor = targetDescriptors.get(targetProperty);
- return descriptor;
- }
-
- /**
- * @param targetProperty the name of a property of the destination object.
- * @return the method to read in a destination object for the given
- * property.
- */
- public Method getTargetReadMethod(String targetProperty) {
- PropertyDescriptor descriptor = getTargetDescriptor(targetProperty);
- Method readMethod = null;
- if (descriptor != null) {
- readMethod = descriptor.getReadMethod();
- }
- return readMethod;
- }
-
- /**
- * @param targetProperty the name of a property of the destination object.
- * @return the method to write in a destination object for the given
- * property.
- */
- public Method getTargetWriteMethod(String targetProperty) {
- PropertyDescriptor descriptor = getTargetDescriptor(targetProperty);
- Method writeMethod = null;
- if (descriptor != null) {
- writeMethod = descriptor.getWriteMethod();
- }
- return writeMethod;
- }
-
- public Class<?> getCollectionType(String sourceProperty) {
- Method method = getSourceReadMethod(sourceProperty);
- Class<?> type = method.getReturnType();
- if (Collection.class.isAssignableFrom(type)) {
- return type;
- }
- return null;
- }
-
- public void addCollectionStrategy(String propertyName,
- Binder.CollectionStrategy strategy) {
- collectionStrategies.put(propertyName, strategy);
- }
-
- public void addBinder(String propertyName, Binder<?, ?> binder) {
- binders.put(propertyName, binder);
- }
-
- protected boolean containsSourceProperty(String sourceProperty) {
- return propertiesMapping.containsKey(sourceProperty);
- }
-
- protected boolean containsTargetProperty(String targetProperty) {
- return propertiesMapping.containsValue(targetProperty);
- }
-
- protected boolean containsCollectionProperty(String propertyName) {
- return collectionStrategies.containsKey(propertyName);
- }
-
- protected boolean containsBinderProperty(String propertyName) {
- return binders.containsKey(propertyName);
- }
-
- protected void addBinding(PropertyDescriptor sourceDescriptor,
- PropertyDescriptor targetDescriptor) {
-
- String sourceProperty = sourceDescriptor.getName();
- String targetProperty = targetDescriptor.getName();
- sourceDescriptors.put(sourceProperty, sourceDescriptor);
- targetDescriptors.put(targetProperty, targetDescriptor);
- propertiesMapping.put(sourceProperty, targetProperty);
- }
-
- protected void removeBinding(String source) {
- String target = propertiesMapping.get(source);
-
- sourceDescriptors.remove(source);
- targetDescriptors.remove(target);
- propertiesMapping.remove(source);
-
- if (containsBinderProperty(source)) {
- binders.remove(source);
- }
- if (containsCollectionProperty(source)) {
- collectionStrategies.remove(source);
- }
- }
-
- protected Map<String, String> getPropertiesMapping() {
- return propertiesMapping;
- }
-
- public Binder<?, ?> getBinder(String sourceProperty) {
- return binders.get(sourceProperty);
- }
-}
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -38,7 +38,7 @@
import java.util.TreeMap;
/**
- * Class to create a new {@link BinderModel}.
+ * Class to create a new {@link Binder.BinderModel}.
* <p/>
* <p/>
* A such object is designed to build only one model of binder and can not be
@@ -99,14 +99,14 @@
* </pre>
*
* @author tchemit <chemit(a)codelutin.com>
- * @see BinderModel
+ * @see Binder.BinderModel
* @see Binder
* @since 1.5.3
*/
public class BinderModelBuilder<S, T> {
/** current model used to build the binder */
- protected BinderModel<S, T> model;
+ protected Binder.BinderModel<S, T> model;
/** source properties descriptors */
protected Map<String, PropertyDescriptor> sourceDescriptors;
@@ -371,7 +371,7 @@
}
// init model
- model = new BinderModel<S, T>(sourceType, targetType);
+ model = new Binder.BinderModel<S, T>(sourceType, targetType);
// obtain source descriptors
sourceDescriptors = new TreeMap<String, PropertyDescriptor>();
@@ -447,7 +447,7 @@
}
- protected BinderModel<S, T> getModel() {
+ protected Binder.BinderModel<S, T> getModel() {
return model;
}
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -225,7 +225,7 @@
* @param name the context's name
*/
public static void registerBinder(Binder<?, ?> binder, String name) {
- BinderModel<?, ?> model = binder.getModel();
+ Binder.BinderModel<?, ?> model = binder.getModel();
BinderEntry entry = new BinderEntry(
model.getSourceType(),
model.getTargetType(),
Modified: trunk/src/main/java/org/nuiton/util/beans/package-info.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/package-info.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/main/java/org/nuiton/util/beans/package-info.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -25,6 +25,76 @@
/**
* Packages for all stuff of bean transformations (binder, and others...).
*
+ * This package contains two api :
+ * <ul>
+ * <li> the Binder api to copy objects</li>
+ * <li> Some javabeans compiliant api</li>
+ * </ul>
+ *
+ * <h1>The <b>Binder</b> api</h1>
+ * <p>
+ * This api permits to some object properties from an object to another one.
+ * </p>
+ * <h2>Obtain a binder</h2>
+ * A {@link Binder} contains a safe model named {@link Binder.BinderModel} which knows
+ * all properties that can be copied.
+ * <br/>
+ * To use this api, you have only to get a {@link Binder} object from the
+ * {@link BinderFactory} like this :
+ * <pre>
+ * Binder<A, A> binder = BinderFactory.newBinder(A.class);
+ * </pre>
+ *
+ * It is also possible to build a more sofisticated binder which will only copy
+ * some properties, using the {@link BinderModelBuilder}.
+ *
+ * <h2>Use a binder</h2>
+ * Once you have a binder, you use the {@link Binder} api :
+ * <p/>
+ * To copy all properties from an object to another one :
+ * <pre>
+ * binder.copy(source, target);
+ * </pre>
+ *
+ * To copy just some properties from an object to another one :
+ * <pre>
+ * binder.copy(source, target, "propertyOne", "propertyTwo");
+ * </pre>
+ *
+ * To copy all properties except some :
+ * <pre>
+ * binder.copyExcluding(source, target, "propertyToExeclude");
+ * </pre>
+ *
+ * To obtain some properties from an object, use the following code :
+ * <pre>
+ * Map<String, Object> properties = binder.obtainProperties(source, "propertyOne", "propertyTwo");
+ * </pre>
+ *
+ * <h2>Building a new BinderModel</h2>
+ * <p>
+ * In two words, you have to use the {@link BinderModelBuilder} object to do this.
+ * then register your binder model into the {@link BinderFactory} using
+ * one of the method {@code BinderFactory#registerBinderModel(XXX)}.
+ * </p>
+ * More explainations will come soon...
+ * <br/>
+ * You can go and look on the unit tests which describe it pretty well :) :
+ * <pre>
+ * org.nuiton.util.beans.BinderModelBuilderTest
+ * </pre>
+ *
+ * <h1> JavaBeans api</h1>
+ *
+ * <h2>{@link BeanMonitor} class</h2>
+ * This object permits to listen javaBeans and keep modifications made on a bean.
+ *
+ * More exalanations will come soon, meanwhile you can see the test class :
+ * <pre>
+ * org.nuiton.util.beans.BeanMonitorTest
+ * </pre>
+ *
+ *
* @since 1.1.5
*/
package org.nuiton.util.beans;
Modified: trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -182,7 +182,7 @@
public void testAddSimpleProperties() throws Exception {
builder.createBinderModel(BeanA.class);
- BinderModel<?, ?> model = builder.getModel();
+ Binder.BinderModel<?, ?> model = builder.getModel();
try {
builder.addSimpleProperties((String) null);
@@ -226,7 +226,7 @@
@Test
public void testAddProperty() throws Exception {
builder.createBinderModel(BeanA.class, BeanB.class);
- BinderModel<?, ?> model = builder.getModel();
+ Binder.BinderModel<?, ?> model = builder.getModel();
try {
builder.addProperty(null, null);
@@ -301,7 +301,7 @@
builder.addProperties(BeanB.PROPERTY_A, BeanB.PROPERTY_A,
BeanB.PROPERTY_B, BeanB.PROPERTY_B2);
- BinderModel<?, ?> model = builder.getModel();
+ Binder.BinderModel<?, ?> model = builder.getModel();
Assert.assertEquals(2, model.getSourceDescriptors().length);
Assert.assertEquals(2, model.getTargetDescriptors().length);
Map<String, String> map = model.getPropertiesMapping();
Modified: trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -66,7 +66,7 @@
Assert.assertNull(BinderFactory.binderModels);
- BinderModel<BeanA, BeanA> model =
+ Binder.BinderModel<BeanA, BeanA> model =
BinderFactory.registerBinderModel(BinderModelBuilder.newDefaultBuilder(BeanA.class));
Assert.assertNotNull(BinderFactory.binderModels);
@@ -74,7 +74,7 @@
Assert.assertEquals(model,
BinderFactory.newBinder(BeanA.class).getModel());
- BinderModel<BeanA, BeanA> model1 =
+ Binder.BinderModel<BeanA, BeanA> model1 =
BinderFactory.registerBinderModel(BinderModelBuilder.newDefaultBuilder(BeanA.class));
Assert.assertNotNull(BinderFactory.binderModels);
Modified: trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -393,7 +393,7 @@
builderAA = BinderModelBuilder.newEmptyBuilder(BeanA.class);
- BinderModel<BeanA, BeanA> model = builderAA.getModel();
+ Binder.BinderModel<BeanA, BeanA> model = builderAA.getModel();
// limit case
try {
@@ -437,7 +437,7 @@
builderAB =
BinderModelBuilder.newEmptyBuilder(BeanA.class, BeanB.class);
- BinderModel<BeanA, BeanB> model = builderAB.getModel();
+ Binder.BinderModel<BeanA, BeanB> model = builderAB.getModel();
// limit cases
@@ -512,7 +512,7 @@
builderAB.addProperties(BeanB.PROPERTY_A, BeanB.PROPERTY_A,
BeanB.PROPERTY_B, BeanB.PROPERTY_B2);
- BinderModel<?, ?> model = builderAB.getModel();
+ Binder.BinderModel<?, ?> model = builderAB.getModel();
Assert.assertEquals(2, model.getSourceDescriptors().length);
Assert.assertEquals(2, model.getTargetDescriptors().length);
Modified: trunk/src/test/java/org/nuiton/util/beans/BinderTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderTest.java 2010-12-28 16:52:49 UTC (rev 1989)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderTest.java 2010-12-29 15:30:39 UTC (rev 1990)
@@ -25,55 +25,77 @@
package org.nuiton.util.beans;
-import org.junit.*;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
import java.util.Map;
public class BinderTest {
BeanA a;
+
BeanB b;
Binder<BeanA, BeanA> binderA;
+
Binder<BeanA, BeanB> binderB;
+
private static final String VALUE_A = "a";
+
private static final String VALUE_B = "b";
+
private static final String VALUE_C = "c";
+
private static final int VALUE_E = 10;
@BeforeClass
public static void beforeClass() {
- BinderProvider.binders = null;
+ // remove all previous models from the factory
+ BinderFactory.clear();
- BinderBuilder builder = new BinderBuilder();
- BinderProvider.registerBinder(builder.
- createBinderModel(BeanA.class).
- addSimpleProperties(BeanA.PROPERTY_A));
+ // creates a mirrored binder model from A -> A with only one property
+ BinderModelBuilder<BeanA, BeanA> builder =
+ BinderModelBuilder.newEmptyBuilder(BeanA.class).
+ addSimpleProperties(BeanA.PROPERTY_A);
- BinderProvider.registerBinder(builder.
- createBinderModel(BeanA.class, BeanB.class).
- addSimpleProperties(BeanA.PROPERTY_A, BeanA.PROPERTY_B).
- addProperty(BeanA.PROPERTY_C, BeanB.PROPERTY_C2).
- addProperty(BeanA.PROPERTY_E, BeanB.PROPERTY_E2)
- );
+ // register the model into factory
+ BinderFactory.registerBinderModel(builder);
+ // creates a binder model from A -> B with more properties
+
+ BinderModelBuilder<BeanA, BeanB> builder1 =
+ BinderModelBuilder.newEmptyBuilder(BeanA.class, BeanB.class).
+ addSimpleProperties(BeanA.PROPERTY_A, BeanA.PROPERTY_B).
+ addProperty(BeanA.PROPERTY_C, BeanB.PROPERTY_C2).
+ addProperty(BeanA.PROPERTY_E, BeanB.PROPERTY_E2);
+
+ // register the model into factory
+ BinderFactory.registerBinderModel(builder1);
+
}
@AfterClass
public static void tearDown() {
- BinderProvider.binders = null;
+ // remove all models from the factory
+ BinderFactory.clear();
}
@Before
public void setUp() {
- binderA = BinderProvider.getBinder(BeanA.class);
- binderB = BinderProvider.getBinder(BeanA.class, BeanB.class);
+ // get the binder A -> A
+ binderA = BinderFactory.newBinder(BeanA.class);
+ // get the binder A -> B
+ binderB = BinderFactory.newBinder(BeanA.class, BeanB.class);
+
a = new BeanA();
b = new BeanB();
}
@@ -132,7 +154,7 @@
Assert.assertNull(b.getB());
Assert.assertNull(b.getB2());
Assert.assertNull(b.getC());
- Assert.assertEquals(0,b.getE2());
+ Assert.assertEquals(0, b.getE2());
binderB.copy(a, b);
Assert.assertEquals(VALUE_A, b.getA());
@@ -145,11 +167,28 @@
Assert.assertNull(b.getA());
Assert.assertNull(b.getB());
Assert.assertNull(b.getC2());
- Assert.assertEquals(0,b.getE2());
+ Assert.assertEquals(0, b.getE2());
}
-
+
@Test
+ public void testCopyIncluding() throws Exception {
+
+ a.setA(VALUE_A);
+ a.setB(VALUE_B);
+ a.setC(VALUE_C);
+ a.setE(VALUE_E);
+
+ // copy only the property A
+ binderA.copy(a, b, BeanA.PROPERTY_A);
+ Assert.assertEquals(VALUE_A, b.getA());
+ Assert.assertNull(b.getB());
+ Assert.assertNull(b.getB2());
+ Assert.assertNull(b.getC());
+ Assert.assertEquals(0, b.getE2());
+ }
+
+ @Test
public void testCopyExcluding() throws Exception {
a.setA(VALUE_A);
1
0
r1989 - in trunk/src: main/java/org/nuiton/util/beans test/java/org/nuiton/util/beans
by tchemit@users.nuiton.org 28 Dec '10
by tchemit@users.nuiton.org 28 Dec '10
28 Dec '10
Author: tchemit
Date: 2010-12-28 17:52:49 +0100 (Tue, 28 Dec 2010)
New Revision: 1989
Url: http://nuiton.org/repositories/revision/nuiton-utils/1989
Log:
Improve binder api (need more docs)
Added:
trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java
trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java
Modified:
trunk/src/main/java/org/nuiton/util/beans/Binder.java
trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java
trunk/src/main/java/org/nuiton/util/beans/BinderModel.java
trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java
trunk/src/test/java/org/nuiton/util/beans/BeanA.java
trunk/src/test/java/org/nuiton/util/beans/BeanB.java
trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java
trunk/src/test/java/org/nuiton/util/beans/BinderProviderTest.java
Modified: trunk/src/main/java/org/nuiton/util/beans/Binder.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/Binder.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/main/java/org/nuiton/util/beans/Binder.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -313,8 +313,8 @@
*
* @param model the model of the binder
*/
- protected void setModel(BinderModel<?, ?> model) {
- this.model = (BinderModel<I, O>) model;
+ protected void setModel(BinderModel<I, O> model) {
+ this.model = model;
}
/**
@@ -371,7 +371,7 @@
}
protected Object bindProperty(String sourceProperty, Object read) throws IllegalAccessException, InstantiationException {
- Binder binder = model.getBinder(sourceProperty);
+ Binder<?,?> binder = model.getBinder(sourceProperty);
Object result = bind(binder, read);
return result;
}
@@ -383,7 +383,7 @@
return null;
}
- Binder binder = model.getBinder(sourceProperty);
+ Binder<?,?> binder = model.getBinder(sourceProperty);
Collection result = null;
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -187,9 +187,9 @@
throw new NullPointerException("binderType can not be null");
}
try {
- B binder = binderType.newInstance();
+ Binder binder = binderType.newInstance();
binder.setModel(model);
- return binder;
+ return (B)binder;
} catch (Exception e) {
throw new IllegalStateException(
"could not instanciate binder " + binderType, e);
Added: trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java (rev 0)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -0,0 +1,419 @@
+package org.nuiton.util.beans;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Factory of {@link Binder}.
+ * <p/>
+ * To obtain a new binder you can use the {@code newBinder(XXX)} methods.
+ * <p/>
+ * For example to obtain a mirrored binder (same source and target type) which
+ * will be able to copy all accepting properties, use this code :
+ * <pre>
+ * Binder<BeanA, BeanA> binder = BinderFactory.newBinder(BeanA.class);
+ * </pre>
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 1.5.3
+ */
+public class BinderFactory {
+
+ /** Logger. */
+ private static final Log log = LogFactory.getLog(BinderFactory.class);
+
+ /** Cache of registred binders indexed by their unique entry */
+ protected static BindelModelEntryMap binderModels;
+
+ /**
+ * Gets the registred mirror binder (source type = target type) with no
+ * context name specified.
+ *
+ * @param sourceType the type of source and target
+ * @param <S> the type of source and target
+ * @return the registred binder or {@code null} if not found.
+ */
+ public static <S> Binder<S, S> newBinder(Class<S> sourceType) {
+ return newBinder0(sourceType, sourceType, null, Binder.class);
+ }
+
+ /**
+ * Gets the registred mirror binder (source type = target type) with the
+ * given context name.
+ *
+ * @param sourceType the type of source and target
+ * @param contextName the context's name of the searched binder
+ * @param <S> the type of source and target
+ * @return the registred binder or {@code null} if not found.
+ */
+ public static <S> Binder<S, S> newBinder(Class<S> sourceType,
+ String contextName) {
+ return newBinder0(sourceType, sourceType, contextName, Binder.class);
+ }
+
+ /**
+ * Gets the registred binder given his types with no context name.
+ *
+ * @param sourceType the type of source
+ * @param targetType the type of target
+ * @param <S> the type of source
+ * @param <T> the type of target
+ * @return the registred binder or {@code null} if not found.
+ */
+ public static <S, T> Binder<S, T> newBinder(Class<S> sourceType,
+ Class<T> targetType) {
+ return newBinder0(sourceType, targetType, null, Binder.class);
+ }
+
+
+ /**
+ * Gets the registred binder given his types with no context name.
+ *
+ * @param sourceType the type of source
+ * @param targetType the type of target
+ * @param contextName the context's name of the searched binder
+ * @param <S> the type of source
+ * @param <T> the type of target
+ * @return the registred binder or {@code null} if not found.
+ */
+ public static <S, T> Binder<S, T> newBinder(Class<S> sourceType,
+ Class<T> targetType,
+ String contextName) {
+ return newBinder0(sourceType, targetType, contextName, Binder.class);
+ }
+
+ /**
+ * Gets the registred binder given his types and his context's name.
+ *
+ * @param sourceType the type of source
+ * @param targetType the type of target
+ * @param contextName the context's name of the searched binder
+ * @param binderType type of binder required
+ * @param <S> the type of source
+ * @param <T> the type of target
+ * @return the new instanciated binder.
+ */
+ public static <S, T, B extends Binder<S, T>> B newBinder(Class<S> sourceType,
+ Class<T> targetType,
+ String contextName,
+ Class<B> binderType) {
+ B binder = (B) newBinder0(sourceType, targetType, contextName, binderType);
+ return binder;
+ }
+
+ public static <S, T> BinderModel<S, T> registerBinderModel(BinderModelBuilder<S, T> binderModelBuilder) throws IllegalArgumentException {
+ BinderModel<S, T> model = registerBinderModel(binderModelBuilder, null);
+ return model;
+ }
+
+ public static <S, T> BinderModel<S, T> registerBinderModel(Binder<S, T> binder) throws IllegalArgumentException {
+ BinderModel<S, T> model = registerBinderModel(binder, null);
+ return model;
+ }
+
+ public static <S, T> BinderModel<S, T> registerBinderModel(BinderModel<S, T> model) throws IllegalArgumentException {
+
+ registerBinderModel(model, null);
+ return model;
+ }
+
+ public static <S, T> BinderModel<S, T> registerBinderModel(BinderModelBuilder<S, T> binderModelBuilder,
+ String contextName) throws IllegalArgumentException {
+ BinderModel<S, T> model = binderModelBuilder.getModel();
+ registerBinderModel(model, contextName);
+ return model;
+ }
+
+ public static <S, T> BinderModel<S, T> registerBinderModel(Binder<S, T> binder,
+ String contextName) throws IllegalArgumentException {
+ BinderModel<S, T> model = binder.getModel();
+ registerBinderModel(model, contextName);
+ return model;
+ }
+
+ public static <S, T> BinderModel<S, T> registerBinderModel(BinderModel<S, T> model,
+ String contextName) throws IllegalArgumentException {
+
+ // check if the given model is not already registred for the given context
+ BinderModel<S, T> registredModel =
+ getBinderModels().get(model, contextName);
+
+ // let's add this model into cache of models
+ BinderModelEntry key = new BinderModelEntry(model, contextName);
+
+ if (registredModel != null) {
+
+ // this model is already registred, remove it from cache
+ if (log.isWarnEnabled()) {
+ log.warn("Remove existing binder model from cache : " +
+ toString(registredModel, contextName));
+ }
+ }
+
+ // add new model into cache
+ getBinderModels().put(key, model);
+ return model;
+ }
+
+ /**
+ * Clear the cache of registred binder models.
+ * <p/>
+ * <b>Note :<b> This is a convienient method for test purposes and should
+ * be used in a normal usage of this provider.
+ */
+ public static void clear() {
+ if (binderModels != null) {
+ binderModels.clear();
+ binderModels = null;
+ }
+ }
+
+
+ protected static BindelModelEntryMap getBinderModels() {
+ if (binderModels == null) {
+ binderModels = new BindelModelEntryMap();
+ }
+ return binderModels;
+ }
+
+ protected static String toString(BinderModel<?, ?> model, String contextName) {
+ return toString(model.getSourceType(), model.getTargetType(), contextName);
+ }
+
+ protected static String toString(Class<?> sourceType, Class<?> targetType, String contextName) {
+ return "<" + sourceType.getName() + " - " + targetType.getName() + " > [" + contextName + "] ";
+ }
+
+ /**
+ * Gets the registred binder given his types and his context's name.
+ *
+ * @param sourceType the type of source
+ * @param targetType the type of target
+ * @param contextName the context's name of the searched binder
+ * @param binderType type of binder required
+ * @param <S> the type of source
+ * @param <T> the type of target
+ * @return the registred binder or {@code null} if not found.
+ */
+ protected static <S, T, B extends Binder<S, T>> Binder<S, T> newBinder0(Class<S> sourceType,
+ Class<T> targetType,
+ String contextName,
+ Class<B> binderType) {
+
+ // obtain the cached model
+ BinderModel<S, T> model =
+ getBinderModels().get(sourceType, targetType, contextName);
+
+ if (model == null) {
+
+ // model not yet registred, let's create it
+
+ if (log.isInfoEnabled()) {
+ log.info("No binder model found for " +
+ toString(sourceType, targetType, contextName) +
+ ", will create a new default one.");
+ }
+
+ BinderModelBuilder<S, T> builder =
+ BinderModelBuilder.newDefaultBuilder(sourceType, targetType);
+
+ // register the new binder model
+ model = registerBinderModel(builder, contextName);
+ }
+
+ B binder;
+ try {
+ binder = binderType.newInstance();
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not instanciate binder of type " + binderType, e);
+ }
+
+ binder.setModel(model);
+ return binder;
+ }
+
+ public static class BindelModelEntryMap implements Map<BinderModelEntry, BinderModel<?, ?>> {
+
+ protected Map<BinderModelEntry, BinderModel<?, ?>> delegate;
+
+ public BindelModelEntryMap() {
+ delegate = new HashMap<BinderModelEntry, BinderModel<?, ?>>();
+ }
+
+ public <S, T> BinderModel<S, T> get(Class<S> source,
+ Class<T> target,
+ String contextName) {
+ BinderModel<S, T> result = null;
+
+ for (BinderModelEntry key : binderModels.keySet()) {
+ if (!key.getSourceType().equals(source)) {
+ continue;
+ }
+ if (!key.getTargetType().equals(target)) {
+ continue;
+ }
+
+ if (key.getName() == null) {
+ if (contextName != null) {
+ continue;
+ }
+ } else {
+ if (!key.getName().equals(contextName)) {
+ continue;
+ }
+ }
+
+ result = (BinderModel<S, T>) binderModels.get(key);
+ break;
+ }
+ return result;
+ }
+
+ public <S, T> BinderModel<S, T> get(BinderModel<S, T> model,
+ String contextName) {
+
+ Class<S> source = model.getSourceType();
+ Class<T> target = model.getTargetType();
+ BinderModel<S, T> result = get(source, target, contextName);
+ return result;
+ }
+
+ @Override
+ public int size() {
+ return delegate.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return delegate.isEmpty();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return delegate.containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ return delegate.containsValue(value);
+ }
+
+ @Override
+ public BinderModel<?, ?> get(Object key) {
+ return delegate.get(key);
+ }
+
+ public BinderModel<?, ?> put(BinderModelEntry key, BinderModel<?, ?> value) {
+ return delegate.put(key, value);
+ }
+
+ @Override
+ public BinderModel<?, ?> remove(Object key) {
+ return delegate.remove(key);
+ }
+
+ public void putAll(Map<? extends BinderModelEntry, ? extends BinderModel<?, ?>> m) {
+ delegate.putAll(m);
+ }
+
+ @Override
+ public void clear() {
+ delegate.clear();
+ }
+
+ @Override
+ public Set<BinderModelEntry> keySet() {
+ return delegate.keySet();
+ }
+
+ @Override
+ public Collection<BinderModel<?, ?>> values() {
+ return delegate.values();
+ }
+
+ @Override
+ public Set<Entry<BinderModelEntry, BinderModel<?, ?>>> entrySet() {
+ return delegate.entrySet();
+ }
+
+ }
+
+ /**
+ * Definition of an binder model entry (source and target types + context name).
+ * <p/>
+ * <b>Note :</b>When no context is specified, we always use a
+ * {@code null} context name.
+ */
+ public static class BinderModelEntry {
+
+ protected final Class<?> sourceType;
+
+ protected final Class<?> targetType;
+
+ protected final String name;
+
+ public BinderModelEntry(Class<?> sourceType,
+ Class<?> targetType,
+ String name) {
+ this.sourceType = sourceType;
+ this.targetType = targetType;
+ this.name = name;
+ }
+
+ public BinderModelEntry(BinderModel<?, ?> model, String contextName) {
+ this(model.getSourceType(), model.getTargetType(), contextName);
+ }
+
+ public Class<?> getSourceType() {
+ return sourceType;
+ }
+
+ public Class<?> getTargetType() {
+ return targetType;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ BinderModelEntry that = (BinderModelEntry) o;
+
+ return (name == null ? that.name == null : name.equals(that.name)) &&
+ sourceType.equals(that.sourceType) &&
+ targetType.equals(that.targetType);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = sourceType.hashCode();
+ result = 31 * result + targetType.hashCode();
+ result = 31 * result + (name != null ? name.hashCode() : 0);
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder buffer = new StringBuilder("<");
+ buffer.append(super.toString());
+ buffer.append(", sourceType: ").append(getSourceType()).append(',');
+ buffer.append(" targetType: ").append(getTargetType()).append(',');
+ buffer.append(" name: ").append(getName()).append('>');
+
+ return buffer.toString();
+ }
+ }
+}
Property changes on: trunk/src/main/java/org/nuiton/util/beans/BinderFactory.java
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ native
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderModel.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderModel.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderModel.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -39,9 +39,9 @@
* <p/>
* TODO-TC20100225 should have special cases for collections treatment.
*
- * @author tchemit <chemit(a)codelutin.com>
* @param <S> the source type
* @param <T> the target type
+ * @author tchemit <chemit(a)codelutin.com>
* @since 1.1.5
*/
public class BinderModel<S, T> implements Serializable {
@@ -269,6 +269,21 @@
propertiesMapping.put(sourceProperty, targetProperty);
}
+ protected void removeBinding(String source) {
+ String target = propertiesMapping.get(source);
+
+ sourceDescriptors.remove(source);
+ targetDescriptors.remove(target);
+ propertiesMapping.remove(source);
+
+ if (containsBinderProperty(source)) {
+ binders.remove(source);
+ }
+ if (containsCollectionProperty(source)) {
+ collectionStrategies.remove(source);
+ }
+ }
+
protected Map<String, String> getPropertiesMapping() {
return propertiesMapping;
}
Copied: trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java (from rev 1988, trunk/src/main/java/org/nuiton/util/beans/BinderBuilder.java)
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java (rev 0)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -0,0 +1,485 @@
+/*
+ * #%L
+ * Nuiton Utils
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2010 CodeLutin
+ * %%
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
+
+package org.nuiton.util.beans;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * Class to create a new {@link BinderModel}.
+ * <p/>
+ * <p/>
+ * A such object is designed to build only one model of binder and can not be
+ * used directly to create a new binder, it prepares only the model of a new
+ * binder, which after must be registred in the {@link BinderFactory} to obtain
+ * a real {@link Binder}.
+ * <p/>
+ * If you want to create more than one binder model, use each time a new
+ * binder builder.
+ * <p/>
+ * To obtain a new instance of a build please use one of the factories method :
+ * <ul>
+ * <li>{@link #newEmptyBuilder(Class)}} to create a binder model with same
+ * source and target type</li>
+ * <li>{@link #newEmptyBuilder(Class, Class)} to create a binder model with a
+ * possible different source and target type</li>
+ * <li>{@link #newDefaultBuilder(Class)} to create a binder model with same
+ * source and target type and then fill the model with all matching properties.</li>
+ * <li>{@link #newDefaultBuilder(Class, Class)} to create a binder model
+ * with a possible different source and target type and then fill the model
+ * with all matching properties.</li>
+ * </ul>
+ * Then you can use folowing methods to specify what to put in the copy model :
+ * <ul>
+ * <li>{@link #addSimpleProperties(String...)} to add in the binder model simple
+ * properties (a simple property is a property present in both source and target type)</li>
+ * <li>{@link #addProperty(String, String)} to add in the binder model a single
+ * property (from source type) to be copied to another property (in target type)</li>
+ * <li>{@link #addProperties(String...)} to add in the binder model properties
+ * (says here you specify some couple of properties (sourcePropertyName,
+ * targetPropertyName) to be added in the binder model)</li>
+ * <li>{@link #addBinder(String, Binder)} to add in the binder model
+ * another binder to be used to copy the given simple property (same name in
+ * source and target type)</li>
+ * <li>{@link #addCollectionStrategy(Binder.CollectionStrategy, String...)} to
+ * specify the strategy to be used to bind some collection some source type to
+ * target type</li>
+ * </ul>
+ * <b>Note :</b> You can chain thoses methods since all of them always return
+ * the current instance of the builder :
+ * <pre>
+ * builder.addSimpleProperties(...).addProperty(...).addBinder(...)
+ * </pre>
+ * <p/>
+ * Here is an example of how to use the {@link BinderModelBuilder} :
+ * <pre>
+ * BinderModelBuilder<Bean, Bean> builder = new BinderModelBuilder(Bean.class);
+ * builder.addSimpleProperties("name", "surname");
+ * BinderFactory.registerBinderModel(builder);
+ * Binder<Bean, Bean> binder = BinderFactory.getBinder(BeanA.class);
+ * <p/>
+ * </pre>
+ * <p/>
+ * Once the binder is registred into the {@link BinderFactory}, you can get it
+ * each time you need it :
+ * <pre>
+ * Binder<Bean, Bean> binder = BinderFactory.getBinder(Bean.class);
+ * </pre>
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @see BinderModel
+ * @see Binder
+ * @since 1.5.3
+ */
+public class BinderModelBuilder<S, T> {
+
+ /** current model used to build the binder */
+ protected BinderModel<S, T> model;
+
+ /** source properties descriptors */
+ protected Map<String, PropertyDescriptor> sourceDescriptors;
+
+ /** target properties descriptors */
+ protected Map<String, PropertyDescriptor> targetDescriptors;
+
+ /**
+ * Creates a new mirrored and empty model binder for the given {@code type}.
+ *
+ * @param type the type of mirrored binder
+ * @return the new instanciated builder
+ */
+ public static <S> BinderModelBuilder<S, S> newEmptyBuilder(Class<S> type) {
+ return new BinderModelBuilder<S, S>(type, type);
+ }
+
+ /**
+ * Creates a new empty model binder for the given types.
+ *
+ * @param sourceType type of the source of the binder
+ * @param targetType type of the target of the binder
+ * @return the new instanciated builder
+ */
+ public static <S, T> BinderModelBuilder<S, T> newEmptyBuilder(Class<S> sourceType,
+ Class<T> targetType) {
+ return new BinderModelBuilder<S, T>(sourceType, targetType);
+ }
+
+ /**
+ * Creates a new mirrored model builder and fill the model with all matching
+ * and available property from the given type.
+ *
+ * @param sourceType the mirrored type of the binder model to create
+ * @param <S> the mirrored type of the binder model to create
+ * @return the new instanciated model builder fully filled
+ */
+ public static <S> BinderModelBuilder<S, S> newDefaultBuilder(Class<S> sourceType) {
+ return newDefaultBuilder(sourceType, sourceType);
+
+ }
+
+ /**
+ * Creates a new model builder and fill the model with all matching
+ * and available properties from the source type to the target type.
+ *
+ * @param sourceType the source type of the model to create
+ * @param targetType the target type of the model to create
+ * @param <S> the source type of the binder model to create
+ * @param <T> the target type of the binder model to create
+ * @return the new instanciated model builder fully filled
+ */
+ public static <S, T> BinderModelBuilder<S, T> newDefaultBuilder(Class<S> sourceType,
+ Class<T> targetType) {
+ BinderModelBuilder<S, T> builder =
+ newEmptyBuilder(sourceType, targetType);
+ Map<String, PropertyDescriptor> source = builder.sourceDescriptors;
+ Map<String, PropertyDescriptor> target = builder.targetDescriptors;
+ List<String> properties = new ArrayList<String>();
+ for (String propertyName : source.keySet()) {
+ if (!target.containsKey(propertyName)) {
+
+ // not exactly match for this property, do not use this property
+ continue;
+ }
+ PropertyDescriptor sourceDescriptor = source.get(propertyName);
+ if (sourceDescriptor.getReadMethod() == null) {
+
+ // no getter on source, do not use this property
+ continue;
+ }
+ PropertyDescriptor targetDescriptor = target.get(propertyName);
+ if (targetDescriptor.getWriteMethod() == null) {
+
+ // no setter on target, do not use this property
+ continue;
+ }
+
+ // can safely use this property
+ properties.add(propertyName);
+ }
+
+ // add all detected properties
+ builder.addSimpleProperties(
+ properties.toArray(new String[properties.size()]));
+ return builder;
+ }
+
+
+ /**
+ * Add to the binder model some simple properties (says source property name
+ * = target property name).
+ * <p/>
+ * <b>Note:</b> If no model is present, the method will fail.
+ *
+ * @param properties the name of mirrored property
+ * @return the instance of the builder
+ * @throws IllegalStateException if no model was previously created
+ * @throws NullPointerException if a property is {@code null}
+ */
+ public BinderModelBuilder<S, T> addSimpleProperties(String... properties)
+ throws IllegalStateException, NullPointerException {
+ for (String property : properties) {
+ if (property == null) {
+ throw new NullPointerException(
+ "parameter 'properties' can not contains a null value");
+ }
+ addProperty0(property, property);
+ }
+ return this;
+ }
+
+ /**
+ * Add to the binder model some simple properties (says source property name
+ * = target property name).
+ * <p/>
+ * <b>Note:</b> If no model is present, the method will fail.
+ *
+ * @param sourceProperty the name of the source property to bind
+ * @param targetProperty the name of the target property to bind
+ * @return the instance of the builder
+ * @throws IllegalStateException if no model was previously created
+ * @throws NullPointerException if a parameter is {@code null}
+ */
+
+ public BinderModelBuilder<S, T> addProperty(String sourceProperty,
+ String targetProperty)
+ throws IllegalStateException, NullPointerException {
+ if (sourceProperty == null) {
+ throw new NullPointerException(
+ "parameter 'sourceProperty' can not be null");
+ }
+ if (targetProperty == null) {
+ throw new NullPointerException(
+ "parameter 'targetProperty' can not be null");
+ }
+ addProperty0(sourceProperty, targetProperty);
+ return this;
+ }
+
+ /**
+ * Add to the binder model some properties.
+ * <p/>
+ * Parameter {@code sourceAndTargetProperties} must be a array of couple
+ * of {@code sourceProperty}, {@code targetProperty}.
+ * <p/>
+ * Example :
+ * <pre>
+ * builder.addProperties("name","name2","text","text");
+ * </pre>
+ * <p/>
+ * <b>Note:</b> If no model is present, the method will fail.
+ *
+ * @param sourceAndTargetProperties the couple of (sourceProperty -
+ * targetProperty) to bind
+ * @return the instance of the builder
+ * @throws IllegalStateException if no model was previously created
+ * @throws IllegalArgumentException if there is not the same number of
+ * source and target properties
+ * @throws NullPointerException if a parameter is {@code null}
+ */
+ public BinderModelBuilder<S, T> addProperties(String... sourceAndTargetProperties)
+ throws IllegalStateException, IllegalArgumentException,
+ NullPointerException {
+ if (sourceAndTargetProperties.length % 2 != 0) {
+ throw new IllegalArgumentException(
+ "must have couple(s) of sourceProperty,targetProperty) " +
+ "but had " + Arrays.toString(sourceAndTargetProperties));
+ }
+ for (int i = 0, max = sourceAndTargetProperties.length / 2;
+ i < max; i++) {
+ String sourceProperty = sourceAndTargetProperties[2 * i];
+ String targetProperty = sourceAndTargetProperties[2 * i + 1];
+ if (sourceProperty == null) {
+ throw new NullPointerException(
+ "parameter 'sourceAndTargetProperties' can not " +
+ "contains a null value");
+ }
+ if (targetProperty == null) {
+ throw new NullPointerException(
+ "parameter 'sourceAndTargetProperties' can not " +
+ "contains a null value");
+ }
+ addProperty0(sourceProperty, targetProperty);
+ }
+ return this;
+ }
+
+ public BinderModelBuilder<S, T> addBinder(String propertyName, Binder<?, ?> binder) {
+
+ // check property is registred
+ if (!model.containsSourceProperty(propertyName)) {
+ throw new IllegalArgumentException(
+ "source property '" + propertyName + "' " +
+ " is NOT registred.");
+ }
+
+ // check property is the same type of given binder
+ PropertyDescriptor descriptor = sourceDescriptors.get(propertyName);
+ Class<?> type = descriptor.getPropertyType();
+
+ if (!Collection.class.isAssignableFrom(type) &&
+ !binder.model.getSourceType().isAssignableFrom(type)) {
+ throw new IllegalStateException(
+ "source property '" + propertyName +
+ "' has not the same type [" + type +
+ "] of the binder [" + binder.model.getSourceType() + "].");
+ }
+
+ // can safely add the strategy
+ model.addBinder(propertyName, binder);
+
+ return this;
+ }
+
+ public BinderModelBuilder<S, T> addCollectionStrategy(Binder.CollectionStrategy strategy,
+ String... propertyNames) {
+
+ for (String propertyName : propertyNames) {
+
+ // check property is registred
+ if (!model.containsSourceProperty(propertyName)) {
+ throw new IllegalArgumentException(
+ "source property '" + propertyName + "' " +
+ " is NOT registred.");
+ }
+
+ // check property is collection type
+ PropertyDescriptor descriptor = sourceDescriptors.get(propertyName);
+ Class<?> type = descriptor.getPropertyType();
+ if (!Collection.class.isAssignableFrom(type)) {
+ throw new IllegalStateException(
+ "source property '" + propertyName +
+ "' is not a collection type [" + type + "]");
+ }
+
+ // can safely add the strategy
+ model.addCollectionStrategy(propertyName, strategy);
+ }
+ return this;
+ }
+
+ /**
+ * Creates a binder for the given types.
+ *
+ * @param sourceType type of the source of the binder
+ * @param targetType type of the target of the binder
+ */
+ protected BinderModelBuilder(Class<S> sourceType, Class<T> targetType) {
+ if (sourceType == null) {
+ throw new NullPointerException("sourceType can not be null");
+ }
+ if (targetType == null) {
+ throw new NullPointerException("targetType can not be null");
+ }
+
+ if (model != null) {
+ throw new IllegalStateException(
+ "there is already a binderModel in construction, release " +
+ "it with the method createBinder before using this method."
+ );
+ }
+
+ // init model
+ model = new BinderModel<S, T>(sourceType, targetType);
+
+ // obtain source descriptors
+ sourceDescriptors = new TreeMap<String, PropertyDescriptor>();
+ loadDescriptors(model.getSourceType(), sourceDescriptors);
+
+ // obtain target descriptors
+ targetDescriptors = new TreeMap<String, PropertyDescriptor>();
+ loadDescriptors(model.getTargetType(), targetDescriptors);
+
+ }
+
+ protected void addProperty0(String sourceProperty,
+ String targetProperty) {
+
+ // obtain source descriptor
+ PropertyDescriptor sourceDescriptor =
+ sourceDescriptors.get(sourceProperty);
+ if (sourceDescriptor == null) {
+ throw new IllegalArgumentException("no property '" +
+ sourceProperty + "' " + "found on type " +
+ model.getSourceType());
+ }
+ // check srcProperty is readable
+ Method readMethod = sourceDescriptor.getReadMethod();
+ if (readMethod == null) {
+ throw new IllegalArgumentException("property '" + sourceProperty +
+ "' " + "is not readable on type " + model.getSourceType());
+ }
+
+ // obtain dst descriptor
+ PropertyDescriptor targetDescriptor =
+ targetDescriptors.get(targetProperty);
+ if (targetDescriptor == null) {
+ throw new IllegalArgumentException("no property '" +
+ targetProperty + "' " + "found on type " +
+ model.getTargetType());
+ }
+ // check dstProperty is writable
+ Method writeMethod = sourceDescriptor.getWriteMethod();
+ if (writeMethod == null) {
+ throw new IllegalArgumentException("property '" + targetProperty +
+ "' " + "is not writable on type " + model.getTargetType());
+ }
+
+ // check types are ok
+ Class<?> sourceType = sourceDescriptor.getPropertyType();
+ Class<?> targetType = targetDescriptor.getPropertyType();
+ //TODO-TC20100221 : should check if primitive and boxed it in such case
+ if (!sourceType.equals(targetType)) {
+ throw new IllegalArgumentException("source property '" +
+ sourceProperty + "' and target property '" +
+ targetProperty + "' are not compatible ( sourceType : " +
+ sourceType + " vs targetType :" + targetType + ')');
+ }
+
+ // check srcProperty does not exist
+ if (model.containsSourceProperty(sourceProperty)) {
+
+ // just remove the old property mapping
+ model.removeBinding(sourceProperty);
+
+ }
+
+ // check dstProperty does not exist
+ // here we can not deal with it since we should remove the source
+ // property for the entry and this is a bit unatural
+ if (model.containsTargetProperty(targetProperty)) {
+ throw new IllegalArgumentException("destination property '" +
+ targetProperty + "' " + " was already registred.");
+ }
+ // safe to add the binding
+ model.addBinding(sourceDescriptor, targetDescriptor);
+ }
+
+
+ protected BinderModel<S, T> getModel() {
+ return model;
+ }
+
+ protected void clear() {
+ sourceDescriptors = null;
+ targetDescriptors = null;
+ model = null;
+ }
+
+ protected static void loadDescriptors(
+ Class<?> type,
+ Map<String, PropertyDescriptor> descriptors) {
+ try {
+
+ BeanInfo beanInfo = Introspector.getBeanInfo(type);
+ for (PropertyDescriptor descriptor :
+ beanInfo.getPropertyDescriptors()) {
+ if (!descriptors.containsKey(descriptor.getName())) {
+ descriptors.put(descriptor.getName(), descriptor);
+ }
+ }
+ } catch (IntrospectionException e) {
+ throw new RuntimeException("Could not obtain bean properties " +
+ "descriptors for source type " + type, e);
+ }
+ Class<?>[] interfaces = type.getInterfaces();
+ for (Class<?> i : interfaces) {
+ loadDescriptors(i, descriptors);
+ }
+ Class<?> superClass = type.getSuperclass();
+ if (superClass != null && !Object.class.equals(superClass)) {
+ loadDescriptors(superClass, descriptors);
+ }
+ }
+}
Modified: trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/main/java/org/nuiton/util/beans/BinderProvider.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -60,7 +60,9 @@
* @see Binder
* @see BinderBuilder
* @since 1.1.5
+ * @deprecated since 1.5.3, use now the {@link BinderFactory} instead, will be remove in version {@code 1.6}.
*/
+@Deprecated
public class BinderProvider {
/** Logger */
Modified: trunk/src/test/java/org/nuiton/util/beans/BeanA.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BeanA.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/test/java/org/nuiton/util/beans/BeanA.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -30,6 +30,7 @@
public class BeanA {
+ public static final String PROPERTY_AA = "aa";
public static final String PROPERTY_A = "a";
public static final String PROPERTY_B = "b";
@@ -42,12 +43,16 @@
public static final String PROPERTY_F = "f";
- protected String a, b, c, d;
+ protected String aa, a, b, c, d;
protected int e, f;
protected PropertyChangeSupport pcs = new PropertyChangeSupport(this);
+ public void setAa(String aa) {
+ this.aa = aa;
+ }
+
public String getA() {
return a;
}
Modified: trunk/src/test/java/org/nuiton/util/beans/BeanB.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BeanB.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/test/java/org/nuiton/util/beans/BeanB.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -27,6 +27,8 @@
public class BeanB extends BeanA {
+ public static final String PROPERTY_BB = "bb";
+
public static final String PROPERTY_A2 = "a2";
public static final String PROPERTY_B2 = "b2";
@@ -39,10 +41,16 @@
public static final String PROPERTY_F2 = "f2";
+ String bb;
+
String a2, b2, c2, d2;
int e2, f2;
+ public String getBb() {
+ return bb;
+ }
+
public String getA2() {
return a2;
}
Modified: trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -29,12 +29,19 @@
import org.junit.Before;
import org.junit.Test;
+import javax.xml.ws.BindingProvider;
import java.beans.PropertyDescriptor;
import java.util.Map;
+/**
+ * @author tchemit <chemit(a)codelutin.com>
+ * @deprecated since 1.5.3, will be removed when {@link BindingProvider} will be removed.
+ */
+@Deprecated
public class BinderBuilderTest {
private BinderBuilder builder;
+
protected static final String PROPERTY_CLASS = "class";
@Before
@@ -57,8 +64,9 @@
sourceDescriptors = builder.sourceDescriptors;
Assert.assertNotNull(sourceDescriptors);
- Assert.assertEquals(7, sourceDescriptors.size());
+ Assert.assertEquals(8, sourceDescriptors.size());
Assert.assertTrue(sourceDescriptors.containsKey(PROPERTY_CLASS));
+ Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_AA));
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_A));
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_B));
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_C));
@@ -69,8 +77,9 @@
targetDescriptors = builder.targetDescriptors;
Assert.assertNotNull(targetDescriptors);
- Assert.assertEquals(7, targetDescriptors.size());
+ Assert.assertEquals(8, targetDescriptors.size());
Assert.assertTrue(targetDescriptors.containsKey(PROPERTY_CLASS));
+ Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_AA));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_A));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_B));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_C));
@@ -115,8 +124,9 @@
sourceDescriptors = builder.sourceDescriptors;
Assert.assertNotNull(sourceDescriptors);
- Assert.assertEquals(7, sourceDescriptors.size());
+ Assert.assertEquals(8, sourceDescriptors.size());
Assert.assertTrue(sourceDescriptors.containsKey(PROPERTY_CLASS));
+ Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_AA));
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_A));
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_B));
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_C));
@@ -124,16 +134,19 @@
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_E));
Assert.assertTrue(sourceDescriptors.containsKey(BeanA.PROPERTY_F));
+
targetDescriptors = builder.targetDescriptors;
Assert.assertNotNull(targetDescriptors);
- Assert.assertEquals(13, targetDescriptors.size());
+ Assert.assertEquals(15, targetDescriptors.size());
Assert.assertTrue(targetDescriptors.containsKey(PROPERTY_CLASS));
+ Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_AA));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_A));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_B));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_C));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_D));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_E));
Assert.assertTrue(targetDescriptors.containsKey(BeanA.PROPERTY_F));
+ Assert.assertTrue(targetDescriptors.containsKey(BeanB.PROPERTY_BB));
Assert.assertTrue(targetDescriptors.containsKey(BeanB.PROPERTY_A2));
Assert.assertTrue(targetDescriptors.containsKey(BeanB.PROPERTY_B2));
Assert.assertTrue(targetDescriptors.containsKey(BeanB.PROPERTY_C2));
@@ -143,7 +156,6 @@
Assert.assertEquals(BeanA.class, builder.getModel().getSourceType());
Assert.assertEquals(BeanB.class, builder.getModel().getTargetType());
-
}
@Test(expected = IllegalStateException.class)
@@ -272,7 +284,7 @@
try {
builder.addProperties(BeanB.PROPERTY_A, BeanB.PROPERTY_A2,
- BeanB.PROPERTY_B);
+ BeanB.PROPERTY_B);
Assert.fail();
} catch (IllegalArgumentException e) {
Assert.assertTrue(true);
@@ -280,14 +292,14 @@
try {
builder.addProperties(BeanB.PROPERTY_A, null,
- BeanB.PROPERTY_B, BeanB.PROPERTY_B);
+ BeanB.PROPERTY_B, BeanB.PROPERTY_B);
Assert.fail();
} catch (NullPointerException e) {
Assert.assertTrue(true);
}
builder.addProperties(BeanB.PROPERTY_A, BeanB.PROPERTY_A,
- BeanB.PROPERTY_B, BeanB.PROPERTY_B2);
+ BeanB.PROPERTY_B, BeanB.PROPERTY_B2);
BinderModel<?, ?> model = builder.getModel();
Assert.assertEquals(2, model.getSourceDescriptors().length);
@@ -307,7 +319,7 @@
public void testCreateBinder() throws Exception {
builder.createBinderModel(BeanA.class, BeanB.class);
builder.addProperties(BeanB.PROPERTY_A, BeanB.PROPERTY_A,
- BeanB.PROPERTY_B, BeanB.PROPERTY_B2);
+ BeanB.PROPERTY_B, BeanB.PROPERTY_B2);
Binder<BeanA, BeanB> binder =
(Binder<BeanA, BeanB>) builder.createBinder();
Added: trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java (rev 0)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -0,0 +1,119 @@
+package org.nuiton.util.beans;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the {@link BinderFactory}.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 1.5.3
+ */
+
+public class BinderFactoryTest {
+
+ @Before
+ public void setUp() throws Exception {
+ BinderFactory.clear();
+ }
+
+ @Test
+ public void testNewBinder() throws Exception {
+
+ // Limit cases
+ try {
+ BinderFactory.newBinder(null);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ try {
+ BinderFactory.newBinder(null, (Class<?>) null);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ try {
+ BinderFactory.newBinder(BeanA.class, (Class<?>) null);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ try {
+ BinderFactory.newBinder(null, BeanA.class);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ Assert.assertTrue(BinderFactory.binderModels.isEmpty());
+
+ Binder<BeanA, BeanA> binderAA = BinderFactory.newBinder(BeanA.class);
+
+ Assert.assertNotNull(binderAA);
+ Assert.assertNotNull(BinderFactory.binderModels);
+ Assert.assertEquals(1, BinderFactory.binderModels.size());
+
+ }
+
+
+ @Test
+ public void testRegisterBinderModel() throws Exception {
+
+ Assert.assertNull(BinderFactory.binderModels);
+
+ BinderModel<BeanA, BeanA> model =
+ BinderFactory.registerBinderModel(BinderModelBuilder.newDefaultBuilder(BeanA.class));
+
+ Assert.assertNotNull(BinderFactory.binderModels);
+ Assert.assertEquals(1, BinderFactory.binderModels.size());
+ Assert.assertEquals(model,
+ BinderFactory.newBinder(BeanA.class).getModel());
+
+ BinderModel<BeanA, BeanA> model1 =
+ BinderFactory.registerBinderModel(BinderModelBuilder.newDefaultBuilder(BeanA.class));
+
+ Assert.assertNotNull(BinderFactory.binderModels);
+ Assert.assertEquals(1, BinderFactory.binderModels.size());
+ Assert.assertNotSame(model, model1);
+ Assert.assertEquals(model1,
+ BinderFactory.newBinder(BeanA.class).getModel());
+
+ BinderFactory.registerBinderModel(model1, "context");
+
+ Assert.assertNotNull(BinderFactory.binderModels);
+ Assert.assertEquals(2, BinderFactory.binderModels.size());
+ Assert.assertEquals(
+ model1,
+ BinderFactory.newBinder(BeanA.class, "context").getModel());
+
+
+ BinderFactory.registerBinderModel(model, "context");
+
+
+ Assert.assertNotNull(BinderFactory.binderModels);
+ Assert.assertEquals(2, BinderFactory.binderModels.size());
+ Assert.assertEquals(
+ model,
+ BinderFactory.newBinder(BeanA.class, "context").getModel());
+
+ }
+
+
+ @Test
+ public void testClear() throws Exception {
+ Assert.assertNull(BinderFactory.binderModels);
+ BinderFactory.newBinder(BeanA.class);
+ Assert.assertNotNull(BinderFactory.binderModels);
+ Assert.assertEquals(1, BinderFactory.binderModels.size());
+
+ BinderFactory.clear();
+
+ Assert.assertNull(BinderFactory.binderModels);
+
+ }
+}
Property changes on: trunk/src/test/java/org/nuiton/util/beans/BinderFactoryTest.java
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ native
Copied: trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java (from rev 1988, trunk/src/test/java/org/nuiton/util/beans/BinderBuilderTest.java)
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java (rev 0)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderModelBuilderTest.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -0,0 +1,543 @@
+/*
+ * #%L
+ * Nuiton Utils
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2010 CodeLutin
+ * %%
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
+
+package org.nuiton.util.beans;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Map;
+
+/**
+ * Tests the {@link BinderModelBuilder}.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 1.5.3
+ */
+public class BinderModelBuilderTest {
+
+ protected static final String PROPERTY_CLASS = "class";
+
+ BinderModelBuilder<BeanA, BeanA> builderAA;
+
+ BinderModelBuilder<BeanA, BeanB> builderAB;
+
+ BinderModelBuilder<BeanB, BeanB> builderBB;
+
+ BinderModelBuilder<BeanB, BeanA> builderBA;
+
+ @Test
+ public void newEmptyBuilder() throws Exception {
+
+ // Limit cases
+
+ try {
+ BinderModelBuilder.newEmptyBuilder(null);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ BinderModelBuilder.newEmptyBuilder(null, null);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ BinderModelBuilder.newEmptyBuilder(BeanA.class, null);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ BinderModelBuilder.newEmptyBuilder(null, BeanA.class);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ // A --> A
+
+ builderAA = BinderModelBuilder.newEmptyBuilder(BeanA.class);
+ assertBuilder(builderAA, BeanA.class, BeanA.class);
+
+ assertDescriptor(builderAA.sourceDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ assertDescriptor(builderAA.targetDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ builderAA = BinderModelBuilder.newEmptyBuilder(BeanA.class, BeanA.class);
+ assertBuilder(builderAA, BeanA.class, BeanA.class);
+
+ assertDescriptor(builderAA.sourceDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ assertDescriptor(builderAA.targetDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ // A --> B
+
+ builderAB = BinderModelBuilder.newEmptyBuilder(BeanA.class, BeanB.class);
+ assertBuilder(builderAB, BeanA.class, BeanB.class);
+
+ assertDescriptor(builderAB.sourceDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ assertDescriptor(builderAB.targetDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+
+ // B --> B
+
+ builderBB = BinderModelBuilder.newEmptyBuilder(BeanB.class);
+ assertBuilder(builderBB, BeanB.class, BeanB.class);
+
+ assertDescriptor(builderBB.sourceDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+ assertDescriptor(builderBB.targetDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+
+ builderBB = BinderModelBuilder.newEmptyBuilder(BeanB.class, BeanB.class);
+ assertBuilder(builderBB, BeanB.class, BeanB.class);
+
+ assertDescriptor(builderBB.sourceDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+ assertDescriptor(builderBB.targetDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+
+ // B --> A
+
+ builderBA = BinderModelBuilder.newEmptyBuilder(BeanB.class, BeanA.class);
+ assertBuilder(builderBA, BeanB.class, BeanA.class);
+
+ assertDescriptor(builderBA.sourceDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+ assertDescriptor(builderBA.targetDescriptors,
+ PROPERTY_CLASS,
+ BeanA.PROPERTY_AA,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F
+ );
+ }
+
+
+ @Test
+ public void newDefaultBuilder() throws Exception {
+
+ // Limit cases
+
+ try {
+ BinderModelBuilder.newDefaultBuilder(null);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ BinderModelBuilder.newDefaultBuilder(null, null);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ BinderModelBuilder.newDefaultBuilder(BeanA.class, null);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ BinderModelBuilder.newDefaultBuilder(null, BeanA.class);
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ // A --> A
+
+ builderAA = BinderModelBuilder.newDefaultBuilder(BeanA.class);
+ assertBuilder(builderAA, BeanA.class, BeanA.class);
+ assertDescriptor(builderAA.getModel().propertiesMapping,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ builderAA = BinderModelBuilder.newDefaultBuilder(BeanA.class, BeanA.class);
+ assertBuilder(builderAA, BeanA.class, BeanA.class);
+ assertDescriptor(builderAA.getModel().propertiesMapping,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ // A --> B
+
+ builderAB = BinderModelBuilder.newDefaultBuilder(BeanA.class, BeanB.class);
+ assertBuilder(builderAB, BeanA.class, BeanB.class);
+ assertDescriptor(builderAB.getModel().propertiesMapping,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F
+ );
+
+ // B --> B
+
+ builderBB = BinderModelBuilder.newDefaultBuilder(BeanB.class);
+ assertBuilder(builderBB, BeanB.class, BeanB.class);
+
+ assertDescriptor(builderBB.getModel().propertiesMapping,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+
+ builderBB = BinderModelBuilder.newDefaultBuilder(BeanB.class, BeanB.class);
+ assertBuilder(builderBB, BeanB.class, BeanB.class);
+ assertDescriptor(builderBB.getModel().propertiesMapping,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+
+ // B --> A
+
+ builderBA = BinderModelBuilder.newDefaultBuilder(BeanB.class, BeanA.class);
+ assertBuilder(builderBA, BeanB.class, BeanA.class);
+ assertDescriptor(builderBA.getModel().propertiesMapping,
+ BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B,
+ BeanB.PROPERTY_C,
+ BeanB.PROPERTY_D,
+ BeanB.PROPERTY_E,
+ BeanB.PROPERTY_F
+ );
+
+ }
+
+ @Test
+ public void testAddSimpleProperties() throws Exception {
+
+ builderAA = BinderModelBuilder.newEmptyBuilder(BeanA.class);
+
+ BinderModel<BeanA, BeanA> model = builderAA.getModel();
+
+ // limit case
+ try {
+ builderAA.addSimpleProperties((String) null);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ // not existing source property
+ try {
+ builderAA.addSimpleProperties(BeanB.PROPERTY_A2);
+ Assert.fail();
+ } catch (IllegalArgumentException e) {
+ Assert.assertTrue(true);
+ }
+
+ builderAA.addSimpleProperties(BeanA.PROPERTY_A);
+ Assert.assertEquals(1, model.getSourceDescriptors().length);
+ Assert.assertEquals(1, model.getTargetDescriptors().length);
+ Map<String, String> map = model.getPropertiesMapping();
+ assertDescriptor(map, BeanB.PROPERTY_A);
+ Assert.assertEquals(BeanA.PROPERTY_A, map.get(BeanA.PROPERTY_A));
+
+ builderAA.addSimpleProperties(BeanA.PROPERTY_A);
+ Assert.assertEquals(1, model.getSourceDescriptors().length);
+ Assert.assertEquals(1, model.getTargetDescriptors().length);
+ assertDescriptor(map, BeanB.PROPERTY_A);
+ Assert.assertEquals(BeanA.PROPERTY_A, map.get(BeanA.PROPERTY_A));
+
+ builderAA.addSimpleProperties(BeanA.PROPERTY_B);
+ Assert.assertEquals(2, model.getSourceDescriptors().length);
+ Assert.assertEquals(2, model.getTargetDescriptors().length);
+ assertDescriptor(map, BeanB.PROPERTY_A, BeanB.PROPERTY_B);
+ Assert.assertEquals(BeanA.PROPERTY_B, map.get(BeanA.PROPERTY_B));
+
+ }
+
+ @Test
+ public void testAddProperty() throws Exception {
+ builderAB =
+ BinderModelBuilder.newEmptyBuilder(BeanA.class, BeanB.class);
+
+ BinderModel<BeanA, BeanB> model = builderAB.getModel();
+
+ // limit cases
+
+ try {
+ builderAB.addProperty(null, null);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ builderAB.addProperty(BeanA.PROPERTY_A, null);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+ try {
+ builderAB.addProperty(null, BeanA.PROPERTY_A);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ // not existing source property
+ try {
+ builderAB.addProperty(BeanB.PROPERTY_A2, BeanB.PROPERTY_A);
+ Assert.fail();
+ } catch (IllegalArgumentException e) {
+ Assert.assertTrue(true);
+ }
+
+ builderAB.addProperty(BeanA.PROPERTY_A, BeanA.PROPERTY_A);
+ Assert.assertEquals(1, model.getSourceDescriptors().length);
+ Assert.assertEquals(1, model.getTargetDescriptors().length);
+ Map<String, String> map = model.getPropertiesMapping();
+ assertDescriptor(map, BeanB.PROPERTY_A);
+ Assert.assertEquals(BeanA.PROPERTY_A, map.get(BeanA.PROPERTY_A));
+
+ builderAB.addProperty(BeanB.PROPERTY_A, BeanB.PROPERTY_A2);
+ Assert.assertEquals(1, model.getSourceDescriptors().length);
+ Assert.assertEquals(1, model.getTargetDescriptors().length);
+ assertDescriptor(map, BeanB.PROPERTY_A);
+ Assert.assertEquals(BeanB.PROPERTY_A2, map.get(BeanA.PROPERTY_A));
+
+ builderAB.addProperty(BeanA.PROPERTY_B, BeanB.PROPERTY_B2);
+ Assert.assertEquals(2, model.getSourceDescriptors().length);
+ Assert.assertEquals(2, model.getTargetDescriptors().length);
+ assertDescriptor(map, BeanB.PROPERTY_A, BeanB.PROPERTY_B);
+ Assert.assertEquals(BeanB.PROPERTY_B2, map.get(BeanB.PROPERTY_B));
+ }
+
+ @Test
+ public void testAddProperties() throws Exception {
+ builderAB =
+ BinderModelBuilder.newEmptyBuilder(BeanA.class, BeanB.class);
+
+ try {
+ builderAB.addProperties(BeanB.PROPERTY_A, BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B);
+ Assert.fail();
+ } catch (IllegalArgumentException e) {
+ Assert.assertTrue(true);
+ }
+
+ try {
+ builderAB.addProperties(BeanB.PROPERTY_A, null,
+ BeanB.PROPERTY_B, BeanB.PROPERTY_B);
+ Assert.fail();
+ } catch (NullPointerException e) {
+ Assert.assertTrue(true);
+ }
+
+ builderAB.addProperties(BeanB.PROPERTY_A, BeanB.PROPERTY_A,
+ BeanB.PROPERTY_B, BeanB.PROPERTY_B2);
+
+ BinderModel<?, ?> model = builderAB.getModel();
+
+ Assert.assertEquals(2, model.getSourceDescriptors().length);
+ Assert.assertEquals(2, model.getTargetDescriptors().length);
+ Map<String, String> map = model.getPropertiesMapping();
+ assertDescriptor(map, BeanB.PROPERTY_A, BeanB.PROPERTY_B);
+ Assert.assertEquals(BeanA.PROPERTY_A, map.get(BeanA.PROPERTY_A));
+ Assert.assertEquals(BeanB.PROPERTY_B2, map.get(BeanA.PROPERTY_B));
+ }
+
+ protected void assertDescriptor(Map<String, ?> descriptors,
+ String... propertyNames) {
+ Assert.assertNotNull(descriptors);
+ Assert.assertEquals(propertyNames.length, descriptors.size());
+ for (String propertyName : propertyNames) {
+ Assert.assertTrue(descriptors.containsKey(propertyName));
+ }
+ }
+
+ protected <S, T> void assertBuilder(BinderModelBuilder<S, T> builder,
+ Class<S> sourceType,
+ Class<T> targetType) {
+ Assert.assertNotNull(builder);
+ Assert.assertNotNull(builder.getModel());
+ Assert.assertEquals(sourceType, builder.getModel().getSourceType());
+ Assert.assertEquals(targetType, builder.getModel().getTargetType());
+ }
+
+}
Modified: trunk/src/test/java/org/nuiton/util/beans/BinderProviderTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/util/beans/BinderProviderTest.java 2010-12-27 15:58:28 UTC (rev 1988)
+++ trunk/src/test/java/org/nuiton/util/beans/BinderProviderTest.java 2010-12-28 16:52:49 UTC (rev 1989)
@@ -29,6 +29,13 @@
import org.junit.Before;
import org.junit.Test;
+import javax.xml.ws.BindingProvider;
+
+/**
+ * @author tchemit <chemit(a)codelutin.com>
+ * @deprecated since 1.5.3, will be removed when {@link BindingProvider} will be removed.
+ */
+@Deprecated
public class BinderProviderTest {
Binder<BeanA, BeanA> binderA;
@@ -86,6 +93,7 @@
public void testRegisterBinderWithName() throws Exception {
Assert.assertNull(BinderProvider.binders);
+ String yo = "yo";
try {
BinderProvider.registerBinder((Binder<?,?>) null, null);
Assert.fail();
1
0
Author: bleny
Date: 2010-12-27 16:58:28 +0100 (Mon, 27 Dec 2010)
New Revision: 1988
Url: http://nuiton.org/repositories/revision/nuiton-utils/1988
Log:
add tchemit as developer in pom
Modified:
trunk/pom.xml
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2010-12-24 15:12:19 UTC (rev 1987)
+++ trunk/pom.xml 2010-12-27 15:58:28 UTC (rev 1988)
@@ -154,6 +154,17 @@
</roles>
</developer>
+ <developer>
+ <name>Tony Chemit</name>
+ <id>tchemit</id>
+ <email>chemit(a)codelutin.com</email>
+ <organization>CodeLutin</organization>
+ <timezone>+2</timezone>
+ <roles>
+ <role>developer</role>
+ </roles>
+ </developer>
+
</developers>
<!-- ************************************************************* -->
1
0
Author: bpoussin
Date: 2010-12-24 16:12:19 +0100 (Fri, 24 Dec 2010)
New Revision: 1987
Url: http://nuiton.org/repositories/revision/nuiton-utils/1987
Log:
add javadoc
Modified:
trunk/src/main/java/org/nuiton/util/ApplicationConfig.java
Modified: trunk/src/main/java/org/nuiton/util/ApplicationConfig.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-12-23 16:27:13 UTC (rev 1986)
+++ trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-12-24 15:12:19 UTC (rev 1987)
@@ -1929,6 +1929,14 @@
output.println("options " + options);
}
+ /**
+ * Return all configuration used with value, that respect includePattern
+ *
+ * @param includePattern null for all value, or config key pattern (ex: "wikitty.*")
+ * @param padding for better presentation, you can use padding to align '=' sign
+ * @return string that represent config
+ * @since 1.5.2
+ */
public String getPrintableConfig(String includePattern, int padding) {
String msg = "Configuration:\n";
for (String key : getFlatOptions().stringPropertyNames()) {
1
0
Author: fdesbois
Date: 2010-12-23 17:27:13 +0100 (Thu, 23 Dec 2010)
New Revision: 1986
Url: http://nuiton.org/repositories/revision/nuiton-utils/1986
Log:
Make PeriodDates Serializable
Modified:
trunk/src/main/java/org/nuiton/util/PeriodDates.java
Modified: trunk/src/main/java/org/nuiton/util/PeriodDates.java
===================================================================
--- trunk/src/main/java/org/nuiton/util/PeriodDates.java 2010-12-23 11:45:57 UTC (rev 1985)
+++ trunk/src/main/java/org/nuiton/util/PeriodDates.java 2010-12-23 16:27:13 UTC (rev 1986)
@@ -25,6 +25,7 @@
package org.nuiton.util;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@@ -39,8 +40,10 @@
* @author fdesbois
* @version $Id$
*/
-public class PeriodDates {
+public class PeriodDates implements Serializable {
+ private static final long serialVersionUID = 1L;
+
private String pattern;
private Calendar fromCalendar;
1
0