Skip to content

Commit 081ccc2

Browse files
committed
Update AnnotationUtilsTest.java
1 parent a679bde commit 081ccc2

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/AnnotationUtilsTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.stereotype.Service;
2929

3030
import javax.lang.model.element.AnnotationMirror;
31+
import javax.lang.model.element.AnnotationValue;
3132
import javax.lang.model.element.Element;
3233
import javax.lang.model.element.ExecutableElement;
3334
import javax.lang.model.element.VariableElement;
@@ -46,6 +47,7 @@
4647
import java.lang.annotation.Target;
4748
import java.util.List;
4849
import java.util.Map;
50+
import java.util.Map.Entry;
4951

5052
import static io.microsphere.annotation.processor.util.AnnotationUtils.findAllAnnotations;
5153
import static io.microsphere.annotation.processor.util.AnnotationUtils.findAnnotation;
@@ -55,16 +57,21 @@
5557
import static io.microsphere.annotation.processor.util.AnnotationUtils.getAnnotation;
5658
import static io.microsphere.annotation.processor.util.AnnotationUtils.getAnnotations;
5759
import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttribute;
60+
import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttributeName;
61+
import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttributes;
5862
import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttributesMap;
5963
import static io.microsphere.annotation.processor.util.AnnotationUtils.getValue;
6064
import static io.microsphere.annotation.processor.util.AnnotationUtils.isAnnotationPresent;
6165
import static io.microsphere.annotation.processor.util.AnnotationUtils.matchesAnnotationTypeName;
66+
import static io.microsphere.annotation.processor.util.AnnotationUtils.matchesAttributeMethod;
6267
import static io.microsphere.annotation.processor.util.FieldUtils.findField;
6368
import static io.microsphere.annotation.processor.util.MethodUtils.findMethod;
6469
import static io.microsphere.annotation.processor.util.MethodUtils.getAllDeclaredMethods;
6570
import static io.microsphere.lang.function.Predicates.alwaysFalse;
6671
import static io.microsphere.lang.function.Predicates.alwaysTrue;
72+
import static io.microsphere.util.ArrayUtils.EMPTY_STRING_ARRAY;
6773
import static io.microsphere.util.ArrayUtils.ofArray;
74+
import static io.microsphere.util.StringUtils.EMPTY_STRING;
6875
import static java.lang.annotation.ElementType.METHOD;
6976
import static java.lang.annotation.ElementType.TYPE;
7077
import static java.util.Collections.emptyMap;
@@ -422,6 +429,9 @@ public void testGetAttribute() {
422429
String[] cacheNames = getAttribute(cacheableAnnotation, "cacheNames");
423430
assertArrayEquals(ofArray("cache-1", "cache-2"), cacheNames);
424431

432+
String key = getAttribute(cacheableAnnotation, "key");
433+
assertEquals(EMPTY_STRING, key);
434+
425435
DeclaredType cacheableAnnotationType = cacheableAnnotation.getAnnotationType();
426436
AnnotationMirror targetAnnotation = findAnnotation(cacheableAnnotationType, Target.class);
427437
ElementType[] elementTypes = getAttribute(targetAnnotation, "value");
@@ -529,6 +539,73 @@ public void testGetAttributesMapOnNull() {
529539
assertSame(emptyMap(), attributesMap);
530540
}
531541

542+
@Test
543+
public void testGetAttributesOnAnnotatedClass() {
544+
Map<ExecutableElement, AnnotationValue> attributes = getAttributes(testTypeElement, Service.class);
545+
assertServiceAttributes(attributes);
546+
547+
attributes = getAttributes(testTypeElement, Service.class, false);
548+
assertServiceAttributes(attributes);
549+
}
550+
551+
@Test
552+
public void testGetAttributesOnAnnotatedMethod() {
553+
ExecutableElement method = findMethod(testTypeElement, "echo", String.class);
554+
Map<ExecutableElement, AnnotationValue> attributes = getAttributes(method, Cacheable.class, false);
555+
assertEquals(1, attributes.size());
556+
assertAttributeEntry(attributes, "cacheNames", ofArray("cache-1", "cache-2"));
557+
558+
559+
attributes = getAttributes(method, Cacheable.class, true);
560+
assertEquals(9, attributes.size());
561+
assertAttributeEntry(attributes, "value", EMPTY_STRING_ARRAY);
562+
assertAttributeEntry(attributes, "cacheNames", ofArray("cache-1", "cache-2"));
563+
assertAttributeEntry(attributes, "key", EMPTY_STRING);
564+
assertAttributeEntry(attributes, "keyGenerator", EMPTY_STRING);
565+
assertAttributeEntry(attributes, "cacheManager", EMPTY_STRING);
566+
assertAttributeEntry(attributes, "cacheResolver", EMPTY_STRING);
567+
assertAttributeEntry(attributes, "condition", EMPTY_STRING);
568+
assertAttributeEntry(attributes, "unless", EMPTY_STRING);
569+
assertAttributeEntry(attributes, "sync", false);
570+
}
571+
572+
void assertServiceAttributes(Map<ExecutableElement, AnnotationValue> attributes) {
573+
assertEquals(1, attributes.size());
574+
assertAttributeEntry(attributes, "value", "testService");
575+
}
576+
577+
void assertAttributeEntry(Map<ExecutableElement, AnnotationValue> attributes, String attributeName, Object attributeValue) {
578+
for (Entry<ExecutableElement, AnnotationValue> entry : attributes.entrySet()) {
579+
ExecutableElement attributeMethod = entry.getKey();
580+
if (matchesAttributeMethod(attributeMethod, attributeName)) {
581+
assertAttributeEntry(entry, attributeName, attributeValue);
582+
break;
583+
}
584+
}
585+
}
586+
587+
void assertAttributeEntry(Entry<ExecutableElement, AnnotationValue> attributeEntry, String attributeName, Object attributeValue) {
588+
ExecutableElement attributeMethod = attributeEntry.getKey();
589+
AnnotationValue annotationValue = attributeEntry.getValue();
590+
assertEquals(attributeName, getAttributeName(attributeMethod));
591+
Object value = getAttribute(attributeMethod.getReturnType(), annotationValue);
592+
Class<?> attributeValueClass = value.getClass();
593+
if (attributeValueClass.isArray()) {
594+
Class<?> componentType = attributeValueClass.getComponentType();
595+
if (String.class.equals(componentType)) {
596+
assertArrayEquals((String[]) attributeValue, (String[]) value);
597+
}
598+
} else {
599+
assertEquals(attributeValue, value);
600+
}
601+
}
602+
603+
@Test
604+
public void testGetAttributesOnNull() {
605+
Map<ExecutableElement, AnnotationValue> attributes = getAttributes(null);
606+
assertSame(emptyMap(), attributes);
607+
}
608+
532609
private void assertFindMetaAnnotation(Element element, Class<? extends Annotation> annotationClass) {
533610
assertAnnotation(findMetaAnnotation(element, annotationClass), annotationClass);
534611
}

0 commit comments

Comments
 (0)