Last active
August 29, 2015 14:16
-
-
Save dmcg/89aa33d535794464b5df to your computer and use it in GitHub Desktop.
ValueExpectations2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private User activeUser(final Customer customer) { | |
return mock(User.class, | |
new Object() { | |
boolean isAccountNonLocked = true; | |
boolean isEnabled = true; | |
boolean isAccountNonExpired = true; | |
boolean isPasswordProvided = true; | |
boolean isCredentialsNonExpired = true; | |
Customer getCustomer = customer; | |
ImmutableList<?> getAuthorities = ImmutableList.of(); | |
}); | |
} | |
public <T> T mock(Class<T> clas, Object value) { | |
T result = mockery.mock(clas); | |
mockery.checking(new ValueExpectations(result, fieldsOf(value))); | |
return result; | |
} | |
public static Iterable<Map.Entry<String, Object>> fieldsOf(final Object o) { | |
Function<Field, Map.Entry<String, Object>> valueForFieldOf = new Function<Field, Map.Entry<String, Object>>() { | |
@Override | |
public Map.Entry<String, Object> apply(Field field) { | |
try { | |
return Pair.of(field.getName(), field.get(o)); | |
} catch (IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
}; | |
return Iterables.transform(ImmutableList.copyOf(o.getClass().getDeclaredFields()), valueForFieldOf); | |
} | |
public static class ValueExpectations implements ExpectationBuilder { | |
private final Iterable<Map.Entry<String, Object>> values; | |
private final Object subject; | |
public ValueExpectations(Object subject, Iterable<Map.Entry<String, Object>> values) { | |
this.subject = subject; | |
this.values = values; | |
} | |
public <T> ValueExpectations(T subject, Object... pairs) { | |
this(subject, mapEntriesFor(pairs)); | |
} | |
@Override | |
public void buildExpectations(Action action, ExpectationCollector expectationCollector) { | |
for (Map.Entry<String, Object> value : values) { | |
expectationCollector.add(expecationBuilderFor(value.getKey(), value.getValue()).toExpectation(action)); | |
} | |
} | |
private InvocationExpectationBuilder expecationBuilderFor(String name, Object value) { | |
InvocationExpectationBuilder builder = new InvocationExpectationBuilder(); | |
builder.of(subject); | |
builder.method(name); | |
builder.setAction(new ReturnValueAction(value)); | |
return builder; | |
} | |
private static Iterable<Map.Entry<String, Object>> mapEntriesFor(Object[] pairs) { | |
if (pairs.length % 2 != 0) | |
throw new IllegalArgumentException("argument must be pairs"); | |
List<Map.Entry<String, Object>> result = Lists.newArrayListWithCapacity(pairs.length / 2); | |
for (int i = 0; i < pairs.length;) { | |
result.add(Pair.of((String)pairs[i++], pairs[i++])); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment