branch develop updated (aa8b7e0 -> 0600c4c)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git from aa8b7e0 refs #3546 (back to version 2.9.1) new 0582981 Add Binder.injectProperties method new 0600c4c Add a injectProperties method to Binder Closes #3753 Merge branch 'feature/3753' into develop The 2 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 0600c4c12416f77a8f4719315e8242992e1df139 Merge: aa8b7e0 0582981 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Aug 17 08:54:40 2015 +0200 Add a injectProperties method to Binder Closes #3753 Merge branch 'feature/3753' into develop commit 05829817ca5294570341450dbf6d3b9db44626e2 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Aug 17 08:53:54 2015 +0200 Add Binder.injectProperties method See #3753 Summary of changes: src/main/java/org/nuiton/util/beans/Binder.java | 52 ++++++++++++++++++++++ .../java/org/nuiton/util/beans/BinderTest.java | 46 +++++++++++++++++++ src/test/resources/log4j.properties | 1 + 3 files changed, 99 insertions(+) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git commit 05829817ca5294570341450dbf6d3b9db44626e2 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Aug 17 08:53:54 2015 +0200 Add Binder.injectProperties method See #3753 --- src/main/java/org/nuiton/util/beans/Binder.java | 52 ++++++++++++++++++++++ .../java/org/nuiton/util/beans/BinderTest.java | 46 +++++++++++++++++++ src/test/resources/log4j.properties | 1 + 3 files changed, 99 insertions(+) diff --git a/src/main/java/org/nuiton/util/beans/Binder.java b/src/main/java/org/nuiton/util/beans/Binder.java index 529da44..f4a630a 100644 --- a/src/main/java/org/nuiton/util/beans/Binder.java +++ b/src/main/java/org/nuiton/util/beans/Binder.java @@ -246,6 +246,58 @@ public class Binder<I, O> implements Serializable { } /** + * Inject all not null properties to the target bean. + * + * @param properties properties to set into bean + * @param target the bean to set + * @since 3.0 + */ + public void injectProperties(Map<String, Object> properties, O target) { + injectProperties(properties, target, false); + } + + /** + * Inject all properties to the target bean. + * + * @param properties properties to set into bean + * @param target the bean to set + * @param includeNullValues {@code true} to set also null properties values + * @since 3.0 + */ + public void injectProperties(Map<String, Object> properties, O target, boolean includeNullValues) { + + for (Map.Entry<String, Object> entry : properties.entrySet()) { + String propertyName = entry.getKey(); + if (!getModel().containsTargetProperty(propertyName)) { + + throw new IllegalStateException("Could not find property '" + propertyName + "' in binder " + this + "."); + } + + Object propertyValue = entry.getValue(); + if (propertyValue == null && !includeNullValues) { + + // Skip null value + continue; + } + + if (log.isDebugEnabled()) { + log.debug("Inject property: " + propertyName + " to " + target); + } + Method writeMethod = getModel().getTargetWriteMethod(propertyName); + try { + writeMethod.invoke(target, propertyValue); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException( + "Could not set property [" + + target.getClass().getName() + ":" + + propertyName + "]", e); + } + + } + + } + + /** * Copy properties from a source bean to a destination one according to * the model of the binder. If {@code propertyNames} is defined, only * those properties will be copied. diff --git a/src/test/java/org/nuiton/util/beans/BinderTest.java b/src/test/java/org/nuiton/util/beans/BinderTest.java index 2e513e5..2a37cbe 100644 --- a/src/test/java/org/nuiton/util/beans/BinderTest.java +++ b/src/test/java/org/nuiton/util/beans/BinderTest.java @@ -22,6 +22,7 @@ package org.nuiton.util.beans; +import com.google.common.collect.ImmutableMap; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; @@ -161,6 +162,51 @@ public class BinderTest { } @Test + public void testInjectProperties() { + + ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); + builder.put(BeanA.PROPERTY_A, "a"); + + Assert.assertNull(a.getA()); + + { + ImmutableMap<String, Object> properties = builder.build(); + binderA.injectProperties(properties, a); + + Assert.assertEquals(a.getA(), properties.get(BeanA.PROPERTY_A)); + + } + + builder.put(BeanA.PROPERTY_B, "b"); + + { + + ImmutableMap<String, Object> properties = builder.build(); + try { + binderA.injectProperties(properties, a); + Assert.fail(); + } catch (IllegalStateException e) { + // Ok normal case binder does not have properties b + } + + } + + { + + Assert.assertNull(b.getA()); + Assert.assertNull(b.getB()); + + ImmutableMap<String, Object> properties = builder.build(); + binderB.injectProperties(properties, b); + + Assert.assertEquals(b.getA(), properties.get(BeanB.PROPERTY_A)); + Assert.assertEquals(b.getB(), properties.get(BeanB.PROPERTY_B)); + Assert.assertEquals(b.getC(), properties.get(BeanB.PROPERTY_B)); + } + + } + + @Test public void testCopy() { a.setA(VALUE_A); diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties index 67c3151..1d27876 100644 --- a/src/test/resources/log4j.properties +++ b/src/test/resources/log4j.properties @@ -29,3 +29,4 @@ log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) %M - %m%n # package level log4j.logger.org.nuiton.util=INFO +log4j.logger.org.nuiton.util.beans.Binder=DEBUG -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git commit 0600c4c12416f77a8f4719315e8242992e1df139 Merge: aa8b7e0 0582981 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Aug 17 08:54:40 2015 +0200 Add a injectProperties method to Binder Closes #3753 Merge branch 'feature/3753' into develop src/main/java/org/nuiton/util/beans/Binder.java | 52 ++++++++++++++++++++++ .../java/org/nuiton/util/beans/BinderTest.java | 46 +++++++++++++++++++ src/test/resources/log4j.properties | 1 + 3 files changed, 99 insertions(+) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm