1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.external;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.lang.annotation.Annotation;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.lang.reflect.InvocationTargetException;
28
29 import org.apache.commons.lang3.AnnotationUtils;
30 import org.apache.commons.lang3.exception.UncheckedException;
31 import org.junit.jupiter.api.Test;
32 import org.junitpioneer.jupiter.SetSystemProperty;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class AnnotationEqualsTest {
54
55
56
57 @Retention(RetentionPolicy.RUNTIME)
58 public @interface PublicTag {
59
60
61
62
63
64 String value();
65 }
66
67 @Retention(RetentionPolicy.RUNTIME)
68 @interface Tag {
69 String value();
70 }
71
72 static class ThrowingTag implements Tag {
73 @Override
74 public Class<? extends Annotation> annotationType() {
75 return Tag.class;
76 }
77
78 @Override
79 public String value() {
80 throw new IllegalArgumentException("boom");
81 }
82 }
83
84 @PublicTag("value")
85 @Tag("value")
86 private final Object a = new Object();
87
88 @PublicTag("value")
89 @Tag("value")
90 private final Object b = new Object();
91
92 @Test
93 @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
94 void equalsRejectsPackagePrivateAnnotationsWithoutForcedAccess() throws Exception {
95 final Tag tagA = getClass().getDeclaredField("a").getAnnotation(Tag.class);
96 final Tag tagB = getClass().getDeclaredField("b").getAnnotation(Tag.class);
97 final IllegalStateException ex = assertThrows(IllegalStateException.class, () -> AnnotationUtils.equals(tagA, tagB));
98 assertInstanceOf(IllegalAccessException.class, ex.getCause());
99 }
100
101 @Test
102 void equalsWorksOnPackagePrivateAnnotations() throws Exception {
103 final Tag tagA = getClass().getDeclaredField("a").getAnnotation(Tag.class);
104 final Tag tagB = getClass().getDeclaredField("b").getAnnotation(Tag.class);
105 assertTrue(AnnotationUtils.equals(tagA, tagB));
106 }
107
108 @Test
109 @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
110 void equalsWorksOnPublicAnnotationsWithoutForcedAccess() throws Exception {
111 final PublicTag tagA = getClass().getDeclaredField("a").getAnnotation(PublicTag.class);
112 final PublicTag tagB = getClass().getDeclaredField("b").getAnnotation(PublicTag.class);
113 assertTrue(AnnotationUtils.equals(tagA, tagB));
114 }
115
116 @Test
117 void equalsWrapsReflectiveOperationException() throws Exception {
118 final Tag tagA = new ThrowingTag();
119 final Tag tagB = getClass().getDeclaredField("b").getAnnotation(Tag.class);
120
121 final IllegalStateException ex =
122 assertThrows(IllegalStateException.class, () -> AnnotationUtils.equals(tagA, tagB));
123 assertInstanceOf(InvocationTargetException.class, ex.getCause());
124 assertEquals("boom", ((InvocationTargetException) ex.getCause()).getTargetException().getMessage());
125 }
126
127 @Test
128 @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
129 void hashCodeRejectsPackagePrivateAnnotationsWithoutForcedAccess() throws Exception {
130 final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
131 final UncheckedException ex = assertThrows(UncheckedException.class, () -> AnnotationUtils.hashCode(tag));
132 assertInstanceOf(IllegalAccessException.class, ex.getCause());
133 }
134
135 @Test
136 void hashCodeWorksOnPackagePrivateAnnotations() throws Exception {
137 final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
138 assertEquals(tag.hashCode(), AnnotationUtils.hashCode(tag));
139 }
140
141 @Test
142 @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
143 void hashCodeWorksOnPublicAnnotationsWithoutForcedAccess() throws Exception {
144 final PublicTag tag = getClass().getDeclaredField("a").getAnnotation(PublicTag.class);
145 assertEquals(tag.hashCode(), AnnotationUtils.hashCode(tag));
146 }
147
148 @Test
149 @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
150 void toStringRejectsPackagePrivateAnnotationsWithoutForcedAccess() throws Exception {
151 final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
152 final UncheckedException ex = assertThrows(UncheckedException.class, () -> AnnotationUtils.toString(tag));
153 assertInstanceOf(IllegalAccessException.class, ex.getCause());
154 }
155
156 @Test
157 void toStringWorksOnPackagePrivateAnnotations() throws Exception {
158 final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
159 final String text = AnnotationUtils.toString(tag);
160 assertTrue(text.contains("value=value"), text);
161 }
162
163 @Test
164 @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
165 void toStringWorksOnPublicAnnotationsWithoutForcedAccess() throws Exception {
166 final PublicTag tag = getClass().getDeclaredField("a").getAnnotation(PublicTag.class);
167 assertEquals("@" + PublicTag.class.getName() + "(value=value)", AnnotationUtils.toString(tag));
168 }
169
170 }