Open
Description
Hi,
project lombok comes with a @Wither annotation who permit to handle better
immuability of object, with fluent style :
http://projectlombok.org/features/experimental/Wither.html
I try to create a "wither" tester, if it can help...
(don't you want migrate to github btw ?)
/**
* Test the wither and ensure it sets the field being tested if and only if a Wither method was defined.
*
*/
public class WitherTester implements Tester {
public void run(final PojoClass pojoClass)
{
final Object classInstance = ValidationHelper.getBasicInstance(pojoClass);
for (final PojoField fieldEntry : pojoClass.getPojoFields()) {
PojoMethod witherMethod = getFieldWither(pojoClass, fieldEntry);
if (witherMethod != null) {
final Object value = RandomFactory.getRandomValue(fieldEntry);
final Object oldValue = fieldEntry.get(classInstance);
IdentityHandlerStub.registerIdentityHandlerStubForValue(value);
LoggerFactory.getLogger(this.getClass()).debug("Testing Field [{0}] with random value [{1}]", fieldEntry, value);
this.nullabilityCheck(classInstance, fieldEntry, witherMethod);
final Object newPojoInstance = invokeWither(witherMethod, classInstance, value);
Affirm.affirmEquals("Wither test failed, the original object must not be modify for field=[" + fieldEntry + "]", oldValue,
fieldEntry.get(classInstance));
Affirm.affirmEquals("Wither test failed, non equal value for field=[" + fieldEntry + "]", value,
fieldEntry.get(newPojoInstance));
IdentityHandlerStub.unregisterIdentityHandlerStubForValue(value);
} else {
LoggerFactory.getLogger(this.getClass()).debug("Field [{0}] has no wither method skipping", fieldEntry);
}
}
}
protected void nullabilityCheck(Object classInstance, PojoField fieldEntry, PojoMethod witherMethod)
{
boolean mustHandleNull = expectNonnull(witherMethod);
try {
invokeWither(witherMethod, classInstance, null);
if (mustHandleNull) {
Affirm.fail("Wither test failed, wither must handle NullPointerException when field=[" + fieldEntry
+ "] is null");
}
} catch (ReflectionException e) {
if (!(e.getCause().getCause() instanceof NullPointerException)) {
throw e;
}
if (!mustHandleNull) {
Affirm.fail("Wither test failed, wither must not generate NullPointerException when field=[" + fieldEntry
+ "] is null");
}
}
}
private boolean expectNonnull(PojoMethod witherMethod)
{
return witherMethod.getPojoParameters().get(0).getAnnotation(Nonnull.class) != null;
}
private Object invokeWither(PojoMethod witherMethod, Object classInstance, Object value)
{
return witherMethod.invoke(classInstance, value);
}
private PojoMethod getFieldWither(PojoClass pojoClass, PojoField fieldEntry)
{
String normalizedFieldName = formattedFieldName(fieldEntry.getName());
for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) {
if (pojoMethod.getName().startsWith("with" + normalizedFieldName)
&& pojoMethod.getReturnType().equals(pojoClass.getClazz())) {
return pojoMethod;
}
}
return null;
}
/**
* Properly formatted field name, this will change the first letter to upper case only if the second letter isn't upper.
*
* @param fieldName The field to proper case.
* @return Formatted field name.
*/
private static String formattedFieldName(final String fieldName)
{
if (isSecondLetterUpperCase(fieldName)) {
return fieldName;
}
return camelCase(fieldName);
}
private static boolean isSecondLetterUpperCase(String fieldName)
{
return fieldName.length() > 1 && Character.isUpperCase(Character.codePointAt(fieldName, 1));
}
private static String camelCase(String fieldName)
{
return fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
}
}
Original issue reported on code.google.com by [email protected]
on 18 Feb 2015 at 12:32