verify that a user-generated annotation which does not directly implement equals() nevertheless compares equivalently to a 'real' annotation

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@996705 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2010-09-13 22:12:35 +00:00
parent 5615bbb7ad
commit 973b9b2e5b
1 changed files with 41 additions and 0 deletions

View File

@ -26,6 +26,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
@ -427,4 +430,42 @@ public class AnnotationUtilsTest {
.getClass()));
}
}
@Test(timeout = 666)
public void testGeneratedAnnotationEquivalentToRealAnnotation() throws Exception {
final Test real = getClass().getDeclaredMethod(
"testGeneratedAnnotationEquivalentToRealAnnotation").getAnnotation(Test.class);
InvocationHandler generatedTestInvocationHandler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("equals".equals(method.getName()) && method.getParameterTypes().length == 1) {
return proxy == args[0];
}
if ("hashCode".equals(method.getName()) && method.getParameterTypes().length == 0) {
return System.identityHashCode(proxy);
}
if ("toString".equals(method.getName()) && method.getParameterTypes().length == 0) {
return "Test proxy";
}
return method.invoke(real, args);
}
};
Test generated = (Test) Proxy.newProxyInstance(Thread.currentThread()
.getContextClassLoader(), new Class[] { Test.class },
generatedTestInvocationHandler);
assertTrue(real.equals(generated));
assertFalse(generated.equals(real));
assertTrue(AnnotationUtils.equals(generated, real));
assertTrue(AnnotationUtils.equals(real, generated));
Test generated2 = (Test) Proxy.newProxyInstance(Thread.currentThread()
.getContextClassLoader(), new Class[] { Test.class },
generatedTestInvocationHandler);
assertFalse(generated.equals(generated2));
assertFalse(generated2.equals(generated));
assertTrue(AnnotationUtils.equals(generated, generated2));
assertTrue(AnnotationUtils.equals(generated2, generated));
}
}