View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotSame;
23  import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.lang.annotation.ElementType;
27  import java.lang.annotation.Retention;
28  import java.lang.annotation.RetentionPolicy;
29  import java.lang.annotation.Target;
30  import java.lang.reflect.Array;
31  import java.lang.reflect.Field;
32  import java.lang.reflect.InvocationHandler;
33  import java.lang.reflect.Proxy;
34  import java.time.Duration;
35  import java.util.Arrays;
36  import java.util.Collection;
37  import java.util.Map;
38  import java.util.function.Function;
39  import java.util.function.Supplier;
40  
41  import org.junit.jupiter.api.BeforeEach;
42  import org.junit.jupiter.api.Test;
43  
44  /**
45   */
46  class AnnotationUtilsTest extends AbstractLangTest {
47      @FunctionAnnotation
48      @RunnableAnnotation
49      @SupplierAnnotation
50      @ThrowingSupplierAnnotation
51      private static final class AnnotatedClass {
52      }
53  
54      @Retention(RetentionPolicy.RUNTIME)
55      public @interface AnnotationArray {
56          FunctionAnnotation[] value();
57      }
58  
59      @Retention(RetentionPolicy.RUNTIME)
60      public @interface AnnotationArrayContainer {
61          AnnotationArray[] value();
62      }
63  
64      @AnnotationArray({ @FunctionAnnotation("first"), @FunctionAnnotation("second") })
65      @AnnotationArrayContainer(@AnnotationArray({ @FunctionAnnotation("first"), @FunctionAnnotation("second") }))
66      private static final class ArrayAnnotatedClass {
67      }
68  
69      @Retention(RetentionPolicy.RUNTIME)
70      public @interface FunctionAnnotation {
71          Function<String, String> FUNCTION = value -> value;
72  
73          String value() default "member";
74      }
75  
76      @Retention(RetentionPolicy.RUNTIME)
77      public @interface NestAnnotation {
78          boolean booleanValue();
79          boolean[] booleanValues();
80          byte byteValue();
81          byte[] byteValues();
82          char charValue();
83          char[] charValues();
84          double doubleValue();
85          double[] doubleValues();
86          float floatValue();
87          float[] floatValues();
88          int intValue();
89          int[] intValues();
90          long longValue();
91          long[] longValues();
92          short shortValue();
93          short[] shortValues();
94          Stooge stooge();
95          Stooge[] stooges();
96          String string();
97          String[] strings();
98          Class<?> type();
99          Class<?>[] types();
100     }
101 
102     @Retention(RetentionPolicy.RUNTIME)
103     public @interface RunnableAnnotation {
104         Runnable RUNNABLE = () -> {
105             // Creates a void synthetic method for the regression test.
106         };
107 
108         String value() default "member";
109     }
110 
111     public enum Stooge {
112         MOE, LARRY, CURLY, JOE, SHEMP
113     }
114 
115     @Retention(RetentionPolicy.RUNTIME)
116     public @interface SupplierAnnotation {
117         Supplier<String> SUPPLIER = () -> "constant";
118 
119         String value() default "member";
120     }
121 
122     @Target(ElementType.FIELD)
123     @Retention(RetentionPolicy.RUNTIME)
124     public @interface TestAnnotation {
125         boolean booleanValue();
126         boolean[] booleanValues();
127         byte byteValue();
128         byte[] byteValues();
129         char charValue();
130         char[] charValues();
131         double doubleValue();
132         double[] doubleValues();
133         float floatValue();
134         float[] floatValues();
135         int intValue();
136         int[] intValues();
137         long longValue();
138         long[] longValues();
139         NestAnnotation nest();
140         NestAnnotation[] nests();
141         short shortValue();
142         short[] shortValues();
143         Stooge stooge();
144         Stooge[] stooges();
145         String string();
146         String[] strings();
147         Class<?> type();
148         Class<?>[] types();
149     }
150 
151     @Retention(RetentionPolicy.RUNTIME)
152     @Target({ElementType.METHOD})
153     public @interface TestMethodAnnotation {
154         final class None extends Throwable {
155 
156             private static final long serialVersionUID = 1L;
157         }
158 
159         Class<? extends Throwable> expected() default None.class;
160 
161         long timeout() default 0L;
162     }
163 
164     @Retention(RetentionPolicy.RUNTIME)
165     public @interface ThrowingSupplierAnnotation {
166         Supplier<String> SUPPLIER = () -> {
167             throw new AssertionError("Annotation member access must not invoke a lambda");
168         };
169 
170         String value() default "member";
171     }
172 
173     @TestAnnotation(
174             booleanValue = false,
175             booleanValues = { false },
176             byteValue = 0,
177             byteValues = { 0 },
178             charValue = 0,
179             charValues = { 0 },
180             doubleValue = 0,
181             doubleValues = { 0 },
182             floatValue = 0,
183             floatValues = { 0 },
184             intValue = 0,
185             intValues = { 0 },
186             longValue = 0,
187             longValues = { 0 },
188             nest = @NestAnnotation(
189                     booleanValue = false,
190                     booleanValues = { false },
191                     byteValue = 0,
192                     byteValues = { 0 },
193                     charValue = 0,
194                     charValues = { 0 },
195                     doubleValue = 0,
196                     doubleValues = { 0 },
197                     floatValue = 0,
198                     floatValues = { 0 },
199                     intValue = 0,
200                     intValues = { 0 },
201                     longValue = 0,
202                     longValues = { 0 },
203                     shortValue = 0,
204                     shortValues = { 0 },
205                     stooge = Stooge.CURLY,
206                     stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
207                     string = "",
208                     strings = { "" },
209                     type = Object.class,
210                     types = { Object.class }
211             ),
212             nests = {
213                 @NestAnnotation(
214                         booleanValue = false,
215                         booleanValues = { false },
216                         byteValue = 0,
217                         byteValues = { 0 },
218                         charValue = 0,
219                         charValues = { 0 },
220                         doubleValue = 0,
221                         doubleValues = { 0 },
222                         floatValue = 0,
223                         floatValues = { 0 },
224                         intValue = 0,
225                         intValues = { 0 },
226                         longValue = 0,
227                         longValues = { 0 },
228                         shortValue = 0,
229                         shortValues = { 0 },
230                         stooge = Stooge.CURLY,
231                         stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
232                         string = "",
233                         strings = { "" },
234                         type = Object[].class,
235                         types = { Object[].class }
236                 )
237             },
238             shortValue = 0,
239             shortValues = { 0 },
240             stooge = Stooge.SHEMP,
241             stooges = { Stooge.MOE, Stooge.LARRY, Stooge.CURLY },
242             string = "",
243             strings = { "" },
244             type = Object.class,
245             types = { Object.class }
246     )
247     public Object dummy1;
248 
249     @TestAnnotation(
250             booleanValue = false,
251             booleanValues = { false },
252             byteValue = 0,
253             byteValues = { 0 },
254             charValue = 0,
255             charValues = { 0 },
256             doubleValue = 0,
257             doubleValues = { 0 },
258             floatValue = 0,
259             floatValues = { 0 },
260             intValue = 0,
261             intValues = { 0 },
262             longValue = 0,
263             longValues = { 0 },
264             nest = @NestAnnotation(
265                     booleanValue = false,
266                     booleanValues = { false },
267                     byteValue = 0,
268                     byteValues = { 0 },
269                     charValue = 0,
270                     charValues = { 0 },
271                     doubleValue = 0,
272                     doubleValues = { 0 },
273                     floatValue = 0,
274                     floatValues = { 0 },
275                     intValue = 0,
276                     intValues = { 0 },
277                     longValue = 0,
278                     longValues = { 0 },
279                     shortValue = 0,
280                     shortValues = { 0 },
281                     stooge = Stooge.CURLY,
282                     stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
283                     string = "",
284                     strings = { "" },
285                     type = Object.class,
286                     types = { Object.class }
287             ),
288             nests = {
289                 @NestAnnotation(
290                         booleanValue = false,
291                         booleanValues = { false },
292                         byteValue = 0,
293                         byteValues = { 0 },
294                         charValue = 0,
295                         charValues = { 0 },
296                         doubleValue = 0,
297                         doubleValues = { 0 },
298                         floatValue = 0,
299                         floatValues = { 0 },
300                         intValue = 0,
301                         intValues = { 0 },
302                         longValue = 0,
303                         longValues = { 0 },
304                         shortValue = 0,
305                         shortValues = { 0 },
306                         stooge = Stooge.CURLY,
307                         stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
308                         string = "",
309                         strings = { "" },
310                         type = Object[].class,
311                         types = { Object[].class }
312                 )
313             },
314             shortValue = 0,
315             shortValues = { 0 },
316             stooge = Stooge.SHEMP,
317             stooges = { Stooge.MOE, Stooge.LARRY, Stooge.CURLY },
318             string = "",
319             strings = { "" },
320             type = Object.class,
321             types = { Object.class }
322     )
323     public Object dummy2;
324 
325     @TestAnnotation(
326             booleanValue = false,
327             booleanValues = { false },
328             byteValue = 0,
329             byteValues = { 0 },
330             charValue = 0,
331             charValues = { 0 },
332             doubleValue = 0,
333             doubleValues = { 0 },
334             floatValue = 0,
335             floatValues = { 0 },
336             intValue = 0,
337             intValues = { 0 },
338             longValue = 0,
339             longValues = { 0 },
340             nest = @NestAnnotation(
341                     booleanValue = false,
342                     booleanValues = { false },
343                     byteValue = 0,
344                     byteValues = { 0 },
345                     charValue = 0,
346                     charValues = { 0 },
347                     doubleValue = 0,
348                     doubleValues = { 0 },
349                     floatValue = 0,
350                     floatValues = { 0 },
351                     intValue = 0,
352                     intValues = { 0 },
353                     longValue = 0,
354                     longValues = { 0 },
355                     shortValue = 0,
356                     shortValues = { 0 },
357                     stooge = Stooge.CURLY,
358                     stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
359                     string = "",
360                     strings = { "" },
361                     type = Object.class,
362                     types = { Object.class }
363             ),
364             nests = {
365                 @NestAnnotation(
366                         booleanValue = false,
367                         booleanValues = { false },
368                         byteValue = 0,
369                         byteValues = { 0 },
370                         charValue = 0,
371                         charValues = { 0 },
372                         doubleValue = 0,
373                         doubleValues = { 0 },
374                         floatValue = 0,
375                         floatValues = { 0 },
376                         intValue = 0,
377                         intValues = { 0 },
378                         longValue = 0,
379                         longValues = { 0 },
380                         shortValue = 0,
381                         shortValues = { 0 },
382                         stooge = Stooge.CURLY,
383                         stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
384                         string = "",
385                         strings = { "" },
386                         type = Object[].class,
387                         types = { Object[].class }
388                 ),
389                 //add a second NestAnnotation to break equality:
390                 @NestAnnotation(
391                         booleanValue = false,
392                         booleanValues = { false },
393                         byteValue = 0,
394                         byteValues = { 0 },
395                         charValue = 0,
396                         charValues = { 0 },
397                         doubleValue = 0,
398                         doubleValues = { 0 },
399                         floatValue = 0,
400                         floatValues = { 0 },
401                         intValue = 0,
402                         intValues = { 0 },
403                         longValue = 0,
404                         longValues = { 0 },
405                         shortValue = 0,
406                         shortValues = { 0 },
407                         stooge = Stooge.CURLY,
408                         stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
409                         string = "",
410                         strings = { "" },
411                         type = Object[].class,
412                         types = { Object[].class }
413                 )
414             },
415             shortValue = 0,
416             shortValues = { 0 },
417             stooge = Stooge.SHEMP,
418             stooges = { Stooge.MOE, Stooge.LARRY, Stooge.CURLY },
419             string = "",
420             strings = { "" },
421             type = Object.class,
422             types = { Object.class }
423     )
424     public Object dummy3;
425 
426     @NestAnnotation(
427             booleanValue = false,
428             booleanValues = { false },
429             byteValue = 0,
430             byteValues = { 0 },
431             charValue = 0,
432             charValues = { 0 },
433             doubleValue = 0,
434             doubleValues = { 0 },
435             floatValue = 0,
436             floatValues = { 0 },
437             intValue = 0,
438             intValues = { 0 },
439             longValue = 0,
440             longValues = { 0 },
441             shortValue = 0,
442             shortValues = { 0 },
443             stooge = Stooge.CURLY,
444             stooges = { Stooge.MOE, Stooge.LARRY, Stooge.SHEMP },
445             string = "",
446             strings = { "" },
447             type = Object[].class,
448             types = { Object[].class }
449     )
450     public Object dummy4;
451 
452     private Field field1;
453     private Field field2;
454     private Field field3;
455     private Field field4;
456 
457     @BeforeEach
458     void setup() throws Exception {
459         field1 = getClass().getDeclaredField("dummy1");
460         field2 = getClass().getDeclaredField("dummy2");
461         field3 = getClass().getDeclaredField("dummy3");
462         field4 = getClass().getDeclaredField("dummy4");
463     }
464 
465     @Test
466     void testAnnotationsOfDifferingTypes() {
467         assertFalse(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field4.getAnnotation(NestAnnotation.class)));
468         assertFalse(AnnotationUtils.equals(field4.getAnnotation(NestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
469     }
470 
471     @Test
472     void testBothArgsNull() {
473         assertTrue(AnnotationUtils.equals(null, null));
474     }
475 
476     @Test
477     void testEquivalence() {
478         assertTrue(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field2.getAnnotation(TestAnnotation.class)));
479         assertTrue(AnnotationUtils.equals(field2.getAnnotation(TestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
480     }
481 
482     @Test
483     void testEquivalenceWithoutInvokingSupplier() {
484         final ThrowingSupplierAnnotation first =
485                 AnnotatedClass.class.getAnnotation(ThrowingSupplierAnnotation.class);
486         final ThrowingSupplierAnnotation second =
487                 (ThrowingSupplierAnnotation) Proxy.newProxyInstance(
488                         ThrowingSupplierAnnotation.class.getClassLoader(),
489                         new Class<?>[] { ThrowingSupplierAnnotation.class },
490                         (proxy, method, args) -> method.invoke(first, args));
491 
492         assertNotSame(first, second);
493         assertTrue(AnnotationUtils.equals(first, second));
494     }
495 
496     @Test
497     void testGeneratedAnnotationEquivalentToRealAnnotation() {
498         assertTimeoutPreemptively(Duration.ofSeconds(666L), () -> {
499             final Test real = getClass().getDeclaredMethod(
500                     "testGeneratedAnnotationEquivalentToRealAnnotation").getAnnotation(Test.class);
501 
502             final InvocationHandler generatedTestInvocationHandler = (proxy, method, args) -> {
503                 if ("equals".equals(method.getName()) && method.getParameterTypes().length == 1) {
504                     return Boolean.valueOf(proxy == args[0]);
505                 }
506                 if ("hashCode".equals(method.getName()) && method.getParameterTypes().length == 0) {
507                     return Integer.valueOf(System.identityHashCode(proxy));
508                 }
509                 if ("toString".equals(method.getName()) && method.getParameterTypes().length == 0) {
510                     return "Test proxy";
511                 }
512                 return method.invoke(real, args);
513             };
514 
515             final Test generated = (Test) Proxy.newProxyInstance(Thread.currentThread()
516                             .getContextClassLoader(), new Class[]{Test.class},
517                     generatedTestInvocationHandler);
518             assertEquals(real, generated);
519             assertNotEquals(generated, real);
520             assertTrue(AnnotationUtils.equals(generated, real));
521             assertTrue(AnnotationUtils.equals(real, generated));
522 
523             final Test generated2 = (Test) Proxy.newProxyInstance(Thread.currentThread()
524                             .getContextClassLoader(), new Class[]{Test.class},
525                     generatedTestInvocationHandler);
526             assertNotEquals(generated, generated2);
527             assertNotEquals(generated2, generated);
528             assertTrue(AnnotationUtils.equals(generated, generated2));
529             assertTrue(AnnotationUtils.equals(generated2, generated));
530         });
531     }
532 
533     @Test
534     void testHashCode() {
535         assertTimeoutPreemptively(Duration.ofSeconds(666L), () -> {
536             final Test test = getClass().getDeclaredMethod("testHashCode").getAnnotation(Test.class);
537             assertEquals(test.hashCode(), AnnotationUtils.hashCode(test));
538             final TestAnnotation testAnnotation1 = field1.getAnnotation(TestAnnotation.class);
539             assertEquals(testAnnotation1.hashCode(), AnnotationUtils.hashCode(testAnnotation1));
540             final TestAnnotation testAnnotation3 = field3.getAnnotation(TestAnnotation.class);
541             assertEquals(testAnnotation3.hashCode(), AnnotationUtils.hashCode(testAnnotation3));
542         });
543     }
544 
545     @Test
546     void testHashCodeOfAnnotationArrayEdgeCases() {
547         final AnnotationArray real = ArrayAnnotatedClass.class.getAnnotation(AnnotationArray.class);
548         final FunctionAnnotation first = real.value()[0];
549         final FunctionAnnotation second = real.value()[1];
550         final FunctionAnnotation[][] values = {
551             {}, { first }, { first, second }, { second, first }, { null }, { first, null, second }
552         };
553         for (final FunctionAnnotation[] value : values) {
554             final AnnotationArray generated = (AnnotationArray) Proxy.newProxyInstance(
555                     AnnotationArray.class.getClassLoader(), new Class<?>[] { AnnotationArray.class },
556                     (proxy, method, args) -> "value".equals(method.getName()) ? value : method.invoke(real, args));
557             final int expected = 127 * "value".hashCode() ^ Arrays.hashCode(value);
558             assertEquals(expected, AnnotationUtils.hashCode(generated));
559         }
560     }
561 
562     @Test
563     void testHashCodeOfDeeplyNestedAnnotationArray() {
564         final AnnotationArrayContainer real = ArrayAnnotatedClass.class.getAnnotation(AnnotationArrayContainer.class);
565         final AnnotationArray inner = real.value()[0];
566         final FunctionAnnotation[] values = inner.value();
567         final FunctionAnnotation[] generatedValues = new FunctionAnnotation[values.length];
568         for (int i = 0; i < values.length; i++) {
569             final FunctionAnnotation value = values[i];
570             generatedValues[i] = (FunctionAnnotation) Proxy.newProxyInstance(
571                     FunctionAnnotation.class.getClassLoader(), new Class<?>[] { FunctionAnnotation.class },
572                     (proxy, method, args) -> "hashCode".equals(method.getName())
573                             ? Integer.valueOf(value.hashCode() + 1) : method.invoke(value, args));
574         }
575         final AnnotationArray generatedInner = (AnnotationArray) Proxy.newProxyInstance(
576                 AnnotationArray.class.getClassLoader(), new Class<?>[] { AnnotationArray.class },
577                 (proxy, method, args) -> "value".equals(method.getName()) ? generatedValues : method.invoke(inner, args));
578         final AnnotationArrayContainer generated = (AnnotationArrayContainer) Proxy.newProxyInstance(
579                 AnnotationArrayContainer.class.getClassLoader(), new Class<?>[] { AnnotationArrayContainer.class },
580                 (proxy, method, args) -> "value".equals(method.getName())
581                         ? new AnnotationArray[] { generatedInner } : method.invoke(real, args));
582         assertTrue(AnnotationUtils.equals(real, generated));
583         assertTrue(AnnotationUtils.equals(generated, real));
584         assertEquals(real.hashCode(), AnnotationUtils.hashCode(generated));
585     }
586 
587     @Test
588     void testHashCodeOfNestedAnnotationArray() {
589         assertTimeoutPreemptively(Duration.ofSeconds(666L), () -> {
590             final TestAnnotation real = field1.getAnnotation(TestAnnotation.class);
591             // A nested annotation whose own hashCode() does not follow Annotation#hashCode().
592             final InvocationHandler nestHandler = (proxy, method, args) -> {
593                 if ("hashCode".equals(method.getName()) && method.getParameterTypes().length == 0) {
594                     return Integer.valueOf(real.nests()[0].hashCode() + 1);
595                 }
596                 return method.invoke(real.nests()[0], args);
597             };
598             final NestAnnotation[] nests = { (NestAnnotation) Proxy.newProxyInstance(
599                     NestAnnotation.class.getClassLoader(), new Class[] { NestAnnotation.class }, nestHandler) };
600             final InvocationHandler testHandler = (proxy, method, args) -> {
601                 if ("nests".equals(method.getName()) && method.getParameterTypes().length == 0) {
602                     return nests;
603                 }
604                 return method.invoke(real, args);
605             };
606             final TestAnnotation generated = (TestAnnotation) Proxy.newProxyInstance(
607                     TestAnnotation.class.getClassLoader(), new Class[] { TestAnnotation.class }, testHandler);
608             assertTrue(AnnotationUtils.equals(real, generated));
609             assertTrue(AnnotationUtils.equals(generated, real));
610             assertEquals(AnnotationUtils.hashCode(real), AnnotationUtils.hashCode(generated));
611         });
612     }
613 
614     @Test
615     void testHashCodeWithFunction() {
616         final FunctionAnnotation annotation = AnnotatedClass.class.getAnnotation(FunctionAnnotation.class);
617         assertEquals(annotation.hashCode(), AnnotationUtils.hashCode(annotation));
618     }
619 
620     @Test
621     void testHashCodeWithoutInvokingSupplier() {
622         final ThrowingSupplierAnnotation annotation =
623                 AnnotatedClass.class.getAnnotation(ThrowingSupplierAnnotation.class);
624         final int expected = 127 * "value".hashCode() ^ "member".hashCode();
625 
626         assertEquals(expected, AnnotationUtils.hashCode(annotation));
627     }
628 
629     @Test
630     void testHashCodeWithRunnable() {
631         final RunnableAnnotation annotation = AnnotatedClass.class.getAnnotation(RunnableAnnotation.class);
632         assertEquals(annotation.hashCode(), AnnotationUtils.hashCode(annotation));
633     }
634 
635     @Test
636     void testHashCodeWithSupplier() {
637         final SupplierAnnotation annotation = AnnotatedClass.class.getAnnotation(SupplierAnnotation.class);
638         assertEquals(annotation.hashCode(), AnnotationUtils.hashCode(annotation));
639     }
640 
641     @Test
642     void testIsValidAnnotationMemberType() {
643         for (final Class<?> type : new Class[] { byte.class, short.class, int.class, char.class,
644                 long.class, float.class, double.class, boolean.class, String.class, Class.class,
645                 NestAnnotation.class, TestAnnotation.class, Stooge.class, ElementType.class }) {
646             assertTrue(AnnotationUtils.isValidAnnotationMemberType(type));
647             assertTrue(AnnotationUtils.isValidAnnotationMemberType(Array.newInstance(type, 0)
648                     .getClass()));
649         }
650         for (final Class<?> type : new Class[] { Object.class, Map.class, Collection.class }) {
651             assertFalse(AnnotationUtils.isValidAnnotationMemberType(type));
652             assertFalse(AnnotationUtils.isValidAnnotationMemberType(Array.newInstance(type, 0)
653                     .getClass()));
654         }
655     }
656 
657     @Test
658     void testNonEquivalentAnnotationsOfSameType() {
659         assertFalse(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field3.getAnnotation(TestAnnotation.class)));
660         assertFalse(AnnotationUtils.equals(field3.getAnnotation(TestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
661     }
662 
663     @Test
664     void testOneArgNull() {
665         assertFalse(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), null));
666         assertFalse(AnnotationUtils.equals(null, field1.getAnnotation(TestAnnotation.class)));
667     }
668 
669     @Test
670     void testSameInstance() {
671         assertTrue(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
672     }
673 
674     @Test
675     @TestMethodAnnotation(timeout = 666000)
676     void testToString() {
677         assertTimeoutPreemptively(Duration.ofSeconds(666L), () -> {
678             final TestMethodAnnotation testAnnotation =
679                     getClass().getDeclaredMethod("testToString").getAnnotation(TestMethodAnnotation.class);
680 
681             final String annotationString = AnnotationUtils.toString(testAnnotation);
682             assertTrue(annotationString.startsWith("@org.apache.commons.lang3.AnnotationUtilsTest$TestMethodAnnotation("));
683             assertTrue(annotationString.endsWith(")"));
684             assertTrue(annotationString.contains("expected=class org.apache.commons.lang3.AnnotationUtilsTest$TestMethodAnnotation$None"));
685             assertTrue(annotationString.contains("timeout=666000"));
686             assertTrue(annotationString.contains(", "));
687         });
688     }
689 
690     @Test
691     void testToStringWithRunnable() {
692         final RunnableAnnotation annotation = AnnotatedClass.class.getAnnotation(RunnableAnnotation.class);
693         assertEquals("@" + RunnableAnnotation.class.getName() + "(value=member)", AnnotationUtils.toString(annotation));
694     }
695 
696     @Test
697     void testToStringWithSupplier() {
698         final SupplierAnnotation annotation = AnnotatedClass.class.getAnnotation(SupplierAnnotation.class);
699         assertEquals("@" + SupplierAnnotation.class.getName() + "(value=member)", AnnotationUtils.toString(annotation));
700     }
701 
702     @Test
703     void testToStringWithThrowingSupplier() {
704         final ThrowingSupplierAnnotation annotation = AnnotatedClass.class.getAnnotation(ThrowingSupplierAnnotation.class);
705         assertEquals("@" + ThrowingSupplierAnnotation.class.getName() + "(value=member)", AnnotationUtils.toString(annotation));
706     }
707 
708 }