|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.core.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.ElementType; |
| 20 | +import java.lang.annotation.Inherited; |
| 21 | +import java.lang.annotation.Retention; |
| 22 | +import java.lang.annotation.RetentionPolicy; |
| 23 | +import java.lang.annotation.Target; |
| 24 | +import java.lang.reflect.AnnotatedElement; |
| 25 | + |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import static org.junit.Assert.*; |
| 29 | +import static org.springframework.core.annotation.AnnotatedElementUtils.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * Unit tests that verify support for finding multiple composed annotations on |
| 33 | + * a single annotated element. |
| 34 | + * |
| 35 | + * <p>See <a href="https://jira.spring.io/browse/SPR-13486">SPR-13486</a>. |
| 36 | + * |
| 37 | + * @author Sam Brannen |
| 38 | + * @since 4.3 |
| 39 | + * @see AnnotatedElementUtils |
| 40 | + * @see AnnotatedElementUtilsTests |
| 41 | + */ |
| 42 | +public class MultipleComposedAnnotationsOnSingleAnnotatedElementTests { |
| 43 | + |
| 44 | + @Test |
| 45 | + public void multipleComposedAnnotationsOnClass() { |
| 46 | + assertMultipleComposedAnnotations(MultipleCachesClass.class); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void multipleComposedAnnotationsOnMethod() throws Exception { |
| 51 | + AnnotatedElement element = getClass().getDeclaredMethod("multipleCachesMethod"); |
| 52 | + assertMultipleComposedAnnotations(element); |
| 53 | + } |
| 54 | + |
| 55 | + private void assertMultipleComposedAnnotations(AnnotatedElement element) { |
| 56 | + assertNotNull(element); |
| 57 | + |
| 58 | + // Prerequisites |
| 59 | + FooCache fooCache = element.getAnnotation(FooCache.class); |
| 60 | + BarCache barCache = element.getAnnotation(BarCache.class); |
| 61 | + assertNotNull(fooCache); |
| 62 | + assertNotNull(barCache); |
| 63 | + assertEquals("fooKey", fooCache.key()); |
| 64 | + assertEquals("barKey", barCache.key()); |
| 65 | + |
| 66 | + // Assert the status quo for finding the 1st merged annotation. |
| 67 | + Cacheable cacheable = findMergedAnnotation(element, Cacheable.class); |
| 68 | + assertNotNull(cacheable); |
| 69 | + assertEquals("fooCache", cacheable.value()); |
| 70 | + assertEquals("fooKey", cacheable.key()); |
| 71 | + |
| 72 | + // TODO Introduce findMergedAnnotations(...) in AnnotatedElementUtils. |
| 73 | + |
| 74 | + // assertEquals("barCache", cacheable.value()); |
| 75 | + // assertEquals("barKey", cacheable.key()); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + // ------------------------------------------------------------------------- |
| 80 | + |
| 81 | + /** |
| 82 | + * Mock of {@code org.springframework.cache.annotation.Cacheable}. |
| 83 | + */ |
| 84 | + @Target({ ElementType.METHOD, ElementType.TYPE }) |
| 85 | + @Retention(RetentionPolicy.RUNTIME) |
| 86 | + @Inherited |
| 87 | + @interface Cacheable { |
| 88 | + |
| 89 | + String value(); |
| 90 | + |
| 91 | + String key() default ""; |
| 92 | + } |
| 93 | + |
| 94 | + @Cacheable("fooCache") |
| 95 | + @Target({ ElementType.METHOD, ElementType.TYPE }) |
| 96 | + @Retention(RetentionPolicy.RUNTIME) |
| 97 | + @Inherited |
| 98 | + @interface FooCache { |
| 99 | + |
| 100 | + @AliasFor(annotation = Cacheable.class) |
| 101 | + String key() default ""; |
| 102 | + } |
| 103 | + |
| 104 | + @Cacheable("barCache") |
| 105 | + @Target({ ElementType.METHOD, ElementType.TYPE }) |
| 106 | + @Retention(RetentionPolicy.RUNTIME) |
| 107 | + @Inherited |
| 108 | + @interface BarCache { |
| 109 | + |
| 110 | + @AliasFor(annotation = Cacheable.class) |
| 111 | + String key(); |
| 112 | + } |
| 113 | + |
| 114 | + @FooCache(key = "fooKey") |
| 115 | + @BarCache(key = "barKey") |
| 116 | + private static class MultipleCachesClass { |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + @FooCache(key = "fooKey") |
| 121 | + @BarCache(key = "barKey") |
| 122 | + private void multipleCachesMethod() { |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments