test: 注解扫描器的完善测试用例;

This commit is contained in:
huangchengxing 2022-06-30 15:30:41 +08:00
parent 062ec707f2
commit 0b60b24950
5 changed files with 278 additions and 36 deletions

View File

@ -11,5 +11,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@interface AnnotationForScannerTest {
String value() default "";
}

View File

@ -7,16 +7,25 @@ import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author huangchengxing
*/
public class FieldAnnotationScannerTest {
@Test
public void testFieldAnnotationScanner() {
FieldAnnotationScanner scanner = new FieldAnnotationScanner();
public void supportTest() {
AnnotationScanner scanner = new FieldAnnotationScanner();
Assert.assertTrue(scanner.support(ReflectUtil.getField(Example.class, "id")));
Assert.assertFalse(scanner.support(ReflectUtil.getMethod(Example.class, "getId")));
Assert.assertFalse(scanner.support(null));
Assert.assertFalse(scanner.support(Example.class));
}
@Test
public void getAnnotationsTest() {
AnnotationScanner scanner = new FieldAnnotationScanner();
Field field = ReflectUtil.getField(Example.class, "id");
Assert.assertNotNull(field);
Assert.assertTrue(scanner.support(field));
@ -25,9 +34,27 @@ public class FieldAnnotationScannerTest {
Assert.assertEquals(AnnotationForScannerTest.class, CollUtil.getFirst(annotations).annotationType());
}
@Test
public void scanTest() {
AnnotationScanner scanner = new FieldAnnotationScanner();
Field field = ReflectUtil.getField(Example.class, "id");
Map<Integer, List<Annotation>> map = new HashMap<>();
scanner.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
field, null
);
Assert.assertEquals(1, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals(AnnotationForScannerTest.class, map.get(0).get(0).annotationType());
}
public static class Example {
@AnnotationForScannerTest
private Integer id;
public Integer getId() {
return id;
}
}
}

View File

@ -1,21 +1,30 @@
package cn.hutool.core.annotation.scanner;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author huangchengxing
* @date 2022/06/10 16:51
*/
public class MateAnnotationScannerTest {
@Test
public void testMateAnnotationScanner() {
public void supportTest() {
AnnotationScanner scanner = new MetaAnnotationScanner();
Assert.assertTrue(scanner.support(AnnotationForScannerTest.class));
Assert.assertFalse(scanner.support(ReflectUtil.getField(Example.class, "id")));
Assert.assertFalse(scanner.support(ReflectUtil.getMethod(Example.class, "getId")));
Assert.assertFalse(scanner.support(null));
Assert.assertFalse(scanner.support(Example.class));
}
@Test
public void getAnnotationsTest() {
AnnotationScanner scanner = new MetaAnnotationScanner();
Assert.assertTrue(scanner.support(AnnotationForScannerTest3.class));
Map<Class<? extends Annotation>, Annotation> annotations = CollUtil.toMap(scanner.getAnnotations(AnnotationForScannerTest3.class), new HashMap<>(), Annotation::annotationType);
@ -35,8 +44,32 @@ public class MateAnnotationScannerTest {
Assert.assertFalse(annotations.containsKey(AnnotationForScannerTest3.class));
}
@AnnotationForScannerTest3
static class Example {}
@Test
public void scanTest() {
AnnotationScanner scanner = new MetaAnnotationScanner();
Map<Integer, List<Annotation>> map = new HashMap<>();
scanner.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
AnnotationForScannerTest3.class, null
);
Assert.assertEquals(3, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals(AnnotationForScannerTest2.class, map.get(0).get(0).annotationType());
Assert.assertEquals(1, map.get(1).size());
Assert.assertEquals(AnnotationForScannerTest1.class, map.get(1).get(0).annotationType());
Assert.assertEquals(1, map.get(2).size());
Assert.assertEquals(AnnotationForScannerTest.class, map.get(2).get(0).annotationType());
}
static class Example {
private Integer id;
public Integer getId() {
return id;
}
}
@AnnotationForScannerTest
@Retention(RetentionPolicy.RUNTIME)

View File

@ -1,35 +1,138 @@
package cn.hutool.core.annotation.scanner;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.*;
/**
* @author huangchengxing
*/
public class MethodAnnotationScannerTest {
@Test
public void testMethodAnnotationScanner() {
public void supportTest() {
AnnotationScanner scanner = new MethodAnnotationScanner();
Assert.assertTrue(scanner.support(ReflectUtil.getMethod(Example.class, "test")));
Assert.assertFalse(scanner.support(null));
Assert.assertFalse(scanner.support(Example.class));
Assert.assertFalse(scanner.support(ReflectUtil.getField(Example.class, "id")));
}
@Test
public void getAnnotationsTest() {
AnnotationScanner scanner = new MethodAnnotationScanner();
Method method = ReflectUtil.getMethod(Example.class, "test");
Assert.assertNotNull(method);
Assert.assertTrue(scanner.support(method));
// 不查找父类中具有相同方法签名的方法
List<Annotation> annotations = scanner.getAnnotations(method);
Assert.assertEquals(1, annotations.size());
Assert.assertEquals(CollUtil.getFirst(annotations).annotationType(), AnnotationForScannerTest.class);
// 查找父类中具有相同方法签名的方法
scanner = new MethodAnnotationScanner(true);
annotations = scanner.getAnnotations(method);
Assert.assertEquals(3, annotations.size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) annotations.get(0)).value());
Assert.assertEquals("SuperClass", ((AnnotationForScannerTest) annotations.get(1)).value());
Assert.assertEquals("SuperInterface", ((AnnotationForScannerTest) annotations.get(2)).value());
// 查找父类中具有相同方法签名的方法但是不查找SuperInterface
scanner = new MethodAnnotationScanner(true).addExcludeTypes(SuperInterface.class);
annotations = scanner.getAnnotations(method);
Assert.assertEquals(2, annotations.size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) annotations.get(0)).value());
Assert.assertEquals("SuperClass", ((AnnotationForScannerTest) annotations.get(1)).value());
// 查找父类中具有相同方法签名的方法但是只查找SuperClass
scanner = new MethodAnnotationScanner(true)
.setFilter(t -> ClassUtil.isAssignable(SuperClass.class, t));
annotations = scanner.getAnnotations(method);
Assert.assertEquals(2, annotations.size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) annotations.get(0)).value());
Assert.assertEquals("SuperClass", ((AnnotationForScannerTest) annotations.get(1)).value());
}
static class Example {
@AnnotationForScannerTest
public void test() {
@Test
public void scanTest() {
Method method = ReflectUtil.getMethod(Example.class, "test");
// 不查找父类中具有相同方法签名的方法
Map<Integer, List<Annotation>> map = new HashMap<>();
new MethodAnnotationScanner(false).scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
method, null
);
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
// 查找父类中具有相同方法签名的方法
map.clear();
new MethodAnnotationScanner(true).scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
method, null
);
Assert.assertEquals(3, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
Assert.assertEquals(1, map.get(1).size());
Assert.assertEquals("SuperClass", ((AnnotationForScannerTest) map.get(1).get(0)).value());
Assert.assertEquals(1, map.get(2).size());
Assert.assertEquals("SuperInterface", ((AnnotationForScannerTest) map.get(2).get(0)).value());
// 查找父类中具有相同方法签名的方法但是不查找SuperInterface
map.clear();
new MethodAnnotationScanner(true)
.addExcludeTypes(SuperInterface.class)
.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
method, null
);
Assert.assertEquals(2, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
Assert.assertEquals(1, map.get(1).size());
Assert.assertEquals("SuperClass", ((AnnotationForScannerTest) map.get(1).get(0)).value());
// 查找父类中具有相同方法签名的方法但是只查找SuperClass
map.clear();
new MethodAnnotationScanner(true)
.setFilter(t -> ClassUtil.isAssignable(SuperClass.class, t))
.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
method, null
);
Assert.assertEquals(2, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
Assert.assertEquals(1, map.get(1).size());
Assert.assertEquals("SuperClass", ((AnnotationForScannerTest) map.get(1).get(0)).value());
}
static class Example extends SuperClass {
private Integer id;
@Override
@AnnotationForScannerTest("Example")
public List<?> test() { return Collections.emptyList(); }
}
static class SuperClass implements SuperInterface {
@Override
@AnnotationForScannerTest("SuperClass")
public Collection<?> test() { return Collections.emptyList(); }
}
interface SuperInterface {
@AnnotationForScannerTest("SuperInterface")
Object test();
}
}
}

View File

@ -1,62 +1,141 @@
package cn.hutool.core.annotation.scanner;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author huangchengxing
* @date 2022/06/10 16:51
*/
public class TypeAnnotationScannerTest {
@Test
public void testTypeAnnotationScanner() {
public void supportTest() {
AnnotationScanner scanner = new TypeAnnotationScanner();
Assert.assertTrue(scanner.support(Example.class));
Assert.assertFalse(scanner.support(ReflectUtil.getField(Example.class, "id")));
Assert.assertFalse(scanner.support(ReflectUtil.getMethod(Example.class, "getId")));
Assert.assertFalse(scanner.support(null));
}
@Test
public void getAnnotationsTest() {
AnnotationScanner scanner = new TypeAnnotationScanner();
List<Annotation> annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(3, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 不查找父接口
scanner = new TypeAnnotationScanner().setIncludeInterfaces(false);
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(2, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 不查找父类
scanner = new TypeAnnotationScanner().setIncludeSupperClass(false);
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(1, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 不查找ExampleSupplerClass.class
scanner = new TypeAnnotationScanner().addExcludeTypes(ExampleSupplerClass.class);
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(1, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
// 只查找ExampleSupplerClass.class
scanner = new TypeAnnotationScanner().setFilter(t -> ClassUtil.isAssignable(ExampleSupplerClass.class, t));
Assert.assertTrue(scanner.support(Example.class));
annotations = scanner.getAnnotations(Example.class);
Assert.assertEquals(2, annotations.size());
annotations.forEach(a -> Assert.assertEquals(a.annotationType(), AnnotationForScannerTest.class));
}
@AnnotationForScannerTest
@Test
public void scanTest() {
Map<Integer, List<Annotation>> map = new HashMap<>();
// 查找父类与父接口
new TypeAnnotationScanner().scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
Example.class, null
);
Assert.assertEquals(3, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
Assert.assertEquals(1, map.get(1).size());
Assert.assertEquals("ExampleSupplerClass", ((AnnotationForScannerTest) map.get(1).get(0)).value());
Assert.assertEquals(1, map.get(2).size());
Assert.assertEquals("ExampleInterface", ((AnnotationForScannerTest) map.get(2).get(0)).value());
// 不查找父接口
map.clear();
new TypeAnnotationScanner()
.setIncludeInterfaces(false)
.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
Example.class, null
);
Assert.assertEquals(2, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
Assert.assertEquals(1, map.get(1).size());
Assert.assertEquals("ExampleSupplerClass", ((AnnotationForScannerTest) map.get(1).get(0)).value());
// 不查找父类
map.clear();
new TypeAnnotationScanner()
.setIncludeSupperClass(false)
.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
Example.class, null
);
Assert.assertEquals(1, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
// 不查找ExampleSupplerClass.class
map.clear();
new TypeAnnotationScanner()
.addExcludeTypes(ExampleSupplerClass.class)
.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
Example.class, null
);
Assert.assertEquals(1, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
// 只查找ExampleSupplerClass.class
map.clear();
new TypeAnnotationScanner()
.setFilter(t -> ClassUtil.isAssignable(ExampleSupplerClass.class, t))
.scan(
(index, annotation) -> map.computeIfAbsent(index, i -> new ArrayList<>()).add(annotation),
Example.class, null
);
Assert.assertEquals(2, map.size());
Assert.assertEquals(1, map.get(0).size());
Assert.assertEquals("Example", ((AnnotationForScannerTest) map.get(0).get(0)).value());
Assert.assertEquals(1, map.get(1).size());
Assert.assertEquals("ExampleSupplerClass", ((AnnotationForScannerTest) map.get(1).get(0)).value());
}
@AnnotationForScannerTest("ExampleSupplerClass")
static class ExampleSupplerClass implements ExampleInterface {}
@AnnotationForScannerTest
@AnnotationForScannerTest("ExampleInterface")
interface ExampleInterface {}
@AnnotationForScannerTest
static class Example extends ExampleSupplerClass {}
@AnnotationForScannerTest("Example")
static class Example extends ExampleSupplerClass {
private Integer id;
public Integer getId() {
return id;
}
}
}