Permalink
Browse files

Added support for calling builder to create target property value if …

…provided value is a builder (for single-valued properties)
  • Loading branch information...
David Marquis David Marquis
David Marquis authored and David Marquis committed Oct 16, 2011
1 parent 3a3d173 commit 4c16e56a3081762f45796313e388b6fbf56560d1
@@ -83,6 +83,8 @@ private void setTargetProperty(Object target, String property, Object value) thr
if (valueAsCollection != null) {
valueAsCollection = buildBuildersInCollection(valueAsCollection);
value = transformCollectionToTargetTypeIfPossible(value, valueAsCollection, targetPropertyType);
+ } else {
+ value = buildIfBuilderInstance(value);
}
}
@@ -118,22 +120,36 @@ private Object collectionToArray(Collection<Object> valueAsCollection, Class tar
}
private Collection<Object> buildBuildersInCollection(Collection<Object> collectionWithBuilders) {
- if (builderDelegate == null) {
+ if (!hasBuilderDelegate()) {
return collectionWithBuilders;
}
Collection<Object> transformed = new ArrayList<Object>(collectionWithBuilders.size());
for (Object element : collectionWithBuilders) {
- if (builderDelegate.isBuilderInstance(element)) {
- element = builderDelegate.build(element);
- }
+ element = buildIfBuilderInstance(element);
transformed.add(element);
}
return transformed;
}
+ private boolean hasBuilderDelegate() {
+ return (builderDelegate != null);
+ }
+
+ private Object buildIfBuilderInstance(Object value) {
+ if (!hasBuilderDelegate()) {
+ return value;
+ }
+
+ if (builderDelegate.isBuilderInstance(value)) {
+ return builderDelegate.build(value);
+ }
+
+ return value;
+ }
+
private Collection<Object> convertToCollectionIfMultiValued(Object value) {
Class valueClass = value.getClass();
Collection<Object> valueAsCollection = null;
@@ -12,6 +12,7 @@
import static com.fluentinterface.ReflectionBuilder.implementationFor;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.collection.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
@@ -158,6 +159,18 @@ public void shouldSetPropertyValueArrayOfPrimitivesToArrayOfPrimitives() {
assertThat(built.getAgesOfMarriages(), is(new int[] {23, 45}));
}
+ @Test
+ public void shouldSetPropertyValueBuilderToObject() {
+
+ Person built = personBuilder
+ .withPartner(
+ aPerson().withName("Diane")
+ ).build();
+
+ assertThat(built.getPartner(), notNullValue());
+ assertThat(built.getPartner().getName(), is("Diane"));
+ }
+
@Test(expected = IllegalStateException.class)
public void shouldFailWhenBuilderUsesAnUnknownProperty() {
@@ -7,6 +7,7 @@
public class Person {
private String name;
private int age;
+ private Person partner;
private List<Person> friends;
private Set<String> surnames;
private Person[] parents;
@@ -29,6 +30,14 @@ public void setAge(int age) {
this.age = age;
}
+ public Person getPartner() {
+ return partner;
+ }
+
+ public void setPartner(Person partner) {
+ this.partner = partner;
+ }
+
public List<Person> getFriends() {
return friends;
}
@@ -15,6 +15,8 @@
PersonBuilder withAge(String age);
+ PersonBuilder withPartner(PersonBuilder diane);
+
PersonBuilder withSurnames(String... surnames);
PersonBuilder withFriends(Person... friends);

0 comments on commit 4c16e56

Please sign in to comment.