Skip to content

Commit

Permalink
Fix as the code review from
Browse files Browse the repository at this point in the history
  • Loading branch information
charleech authored and charleech committed Aug 21, 2014
1 parent 077c4ad commit dc58989
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 109 deletions.
@@ -1,9 +1,9 @@
package com.fluentinterface.proxy.impl;

import com.fluentinterface.proxy.AttributeAccessStrategy;

import java.lang.reflect.Field;

import com.fluentinterface.proxy.AttributeAccessStrategy;

/**
* Strategy that sets the target bean's attributes directly using the Reflection API (without going through the setters).
*/
Expand Down Expand Up @@ -42,26 +42,16 @@ private Field getFieldFromClass(Class<?> clazz, String fieldName) {
try {
return clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
return getFieldsFromHierarchy(clazz, fieldName);
return findFieldFromAncestors(clazz, fieldName);
}
}

private static Field getFieldsFromHierarchy(Class<?> clazz, String fieldName) {
Field result;

private Field findFieldFromAncestors(Class<?> clazz, String fieldName) {
Class<?> parent = clazz.getSuperclass();

if (parent == null) {
if (parent == Object.class) {
return null;
}


try {
result = parent.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
result = getFieldsFromHierarchy(parent, fieldName);
}


return result;
return getFieldFromClass(parent, fieldName);
}
}
182 changes: 90 additions & 92 deletions src/test/java/com/fluentinterface/BuilderProxyDaoTest.java
Expand Up @@ -2,8 +2,8 @@

import static com.fluentinterface.ReflectionBuilder.implementationFor;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Calendar;

Expand All @@ -12,7 +12,6 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import com.fluentinterface.domain.PersonBuilder;
import com.fluentinterface.domain.dao.Customer;
import com.fluentinterface.domain.dao.CustomerBuilder;
import com.fluentinterface.domain.dao.CustomerType;
Expand All @@ -24,116 +23,115 @@

@RunWith(Parameterized.class)
public class BuilderProxyDaoTest {

@Parameterized.Parameters
public static Iterable<Object[]> strategies() {
return asList(
new Object[] {new FieldAttributeAccessStrategy()},
new Object[] {new SetterAttributeAccessStrategy()}
);
);
}
private CustomerBuilder customerBuilder;
private EmployeeBuilder employeeBuilder;

private CustomerBuilder customerBuilder;

private EmployeeBuilder employeeBuilder;

private AttributeAccessStrategy attributeAccessStrategy;

public BuilderProxyDaoTest(AttributeAccessStrategy attributeAccessStrategy) {
this.attributeAccessStrategy = attributeAccessStrategy;
}

@Before
public void setup() throws InstantiationException, IllegalAccessException {
customerBuilder = aCustomer();
employeeBuilder = anEmployee();
}
private CustomerBuilder aCustomer(){

private CustomerBuilder aCustomer(){
return implementationFor(CustomerBuilder.class)
.builds(Customer.class)
.builds(Customer.class)
.usingAttributeAccessStrategy(attributeAccessStrategy)
.create();
}
private EmployeeBuilder anEmployee(){
}

private EmployeeBuilder anEmployee(){
return implementationFor(EmployeeBuilder.class)
.builds(Employee.class)
.builds(Employee.class)
.usingAttributeAccessStrategy(attributeAccessStrategy)
.create();
}

@Test
public void whenBuildCustomer() {
Calendar cal = Calendar.getInstance();

Customer customer = customerBuilder
.withId("c-001")
.withVersion(1)
.withDescription("register via internet")
.withCreatedBy("admin-agent")
.withCreated(cal)
.withUpdatedBy("admin-agent")
.withUpdated(cal)

.withFirstName("Charlee")
.withLastName("Ch.")
.withAddress("my-address")

.withType(CustomerType.BASIC)

.build();

assertThat(customer.getId(), is("c-001"));
assertThat(customer.getVersion(), is(1));
assertThat(customer.getDescription(), is("register via internet"));
assertThat(customer.getCreatedBy(), is("admin-agent"));
assertThat(customer.getCreated().getTimeInMillis(), is(cal.getTimeInMillis()));
assertThat(customer.getUpdatedBy(), is("admin-agent"));
assertThat(customer.getUpdated().getTimeInMillis(), is(cal.getTimeInMillis()));

assertThat(customer.getFirstName(), is("Charlee"));
assertThat(customer.getLastName(), is("Ch."));
assertThat(customer.getAddress(), is("my-address"));

assertThat(customer.getType(), is(CustomerType.BASIC));
}

@Test
public void whenBuildEmployee() {
Calendar cal = Calendar.getInstance();

Employee employee = employeeBuilder
.withId("c-001")
.withVersion(1)
.withDescription("register via internet")
.withCreatedBy("admin-agent")
.withCreated(cal)
.withUpdatedBy("admin-agent")
.withUpdated(cal)

.withFirstName("Charlee")
.withLastName("Ch.")
.withAddress("my-address")

.withDepartment("my-dept")
.withSalary(100.50D)

.build();

assertThat(employee.getId(), is("c-001"));
assertThat(employee.getVersion(), is(1));
assertThat(employee.getDescription(), is("register via internet"));
assertThat(employee.getCreatedBy(), is("admin-agent"));
assertThat(employee.getCreated().getTimeInMillis(), is(cal.getTimeInMillis()));
assertThat(employee.getUpdatedBy(), is("admin-agent"));
assertThat(employee.getUpdated().getTimeInMillis(), is(cal.getTimeInMillis()));

assertThat(employee.getFirstName(), is("Charlee"));
assertThat(employee.getLastName(), is("Ch."));
assertThat(employee.getAddress(), is("my-address"));

assertThat(employee.getDepartment(), is("my-dept"));
assertThat(employee.getSalary(), is(100.50D));
}
}

@Test
public void whenBuildCustomer() {
Customer customer = customerBuilder
.withId("c-001").withVersion(1)

.withFirstName("Charlee")
.withLastName("Ch.")

.build();

assertThat(customer.getId(), is("c-001"));
assertThat(customer.getVersion(), is(1));

assertThat(customer.getFirstName(), is("Charlee"));
assertThat(customer.getLastName(), is("Ch."));

}

@Test
public void whenBuildEmployee() {

Employee employee = employeeBuilder
.withId("c-001")
.withVersion(1)

.withFirstName("Charlee")
.withLastName("Ch.")

.withDepartment("my-dept")

.build();

assertThat(employee.getId(), is("c-001"));
assertThat(employee.getVersion(), is(1));


assertThat(employee.getFirstName(), is("Charlee"));
assertThat(employee.getLastName(), is("Ch."));

assertThat(employee.getDepartment(), is("my-dept"));
}

@Test
public void shouldSetPropertyToEnum() {
Customer customer = customerBuilder
.withType(CustomerType.BASIC)
.build();

assertThat(customer.getType(), is(CustomerType.BASIC));
}

@Test
public void shouldSetPropertyToCalendar() {
Calendar calendar = Calendar.getInstance();
Customer customer = customerBuilder.withCreated(calendar).build();

assertThat(customer.getCreated().getTimeInMillis(),
is(calendar.getTimeInMillis()));
}

@Test
public void shouleSetPropertyToDouble() {
Employee employee = employeeBuilder.withSalary(100.50D).build();

assertThat(employee.getSalary(), is(100.50D));
}

@Test(expected = IllegalStateException.class)
public void shouldFailWhenBuilderUsesAnUnknownProperty() {

customerBuilder.withAnUnknownProperty("fails").build();
}
}
Expand Up @@ -6,4 +6,7 @@ public interface CustomerBuilder
extends HumanCommonBuilder<CustomerBuilder>,
Builder<Customer> {
CustomerBuilder withType(final CustomerType type);

/** Setting unknown properties will fail and not ascend to the Object */
CustomerBuilder withAnUnknownProperty(final String value);
}

0 comments on commit dc58989

Please sign in to comment.