mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!782 完善annotation包相关测试用例
Merge pull request !782 from Createsequence/v6-dev
This commit is contained in:
commit
dd64ab878e
@ -1,29 +0,0 @@
|
|||||||
package cn.hutool.core.annotation;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用于单元测试的注解类<br>
|
|
||||||
* 注解类相关说明见:<a href="https://www.cnblogs.com/xdp-gacl/p/3622275.html">https://www.cnblogs.com/xdp-gacl/p/3622275.html</a>
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
// Retention注解决定MyAnnotation注解的生命周期
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
// Target注解决定MyAnnotation注解可以加在哪些成分上,如加在类身上,或者属性身上,或者方法身上等成分
|
|
||||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
|
||||||
public @interface AnnotationForTest {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 注解的默认属性值
|
|
||||||
*
|
|
||||||
* @return 属性值
|
|
||||||
*/
|
|
||||||
String value() default "";
|
|
||||||
|
|
||||||
@Alias("value")
|
|
||||||
String retry() default "";
|
|
||||||
}
|
|
@ -1,49 +1,136 @@
|
|||||||
package cn.hutool.core.annotation;
|
package cn.hutool.core.annotation;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.*;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link AnnotationUtil}
|
||||||
|
*/
|
||||||
public class AnnotationUtilTest {
|
public class AnnotationUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getCombinationAnnotationsTest(){
|
public void testToCombination() {
|
||||||
final Annotation[] annotations = AnnotationUtil.getAnnotations(ClassWithAnnotation.class, true);
|
CombinationAnnotationElement element = AnnotationUtil.toCombination(ClassForTest.class);
|
||||||
Assert.assertNotNull(annotations);
|
Assert.assertEquals(2, element.getAnnotations().length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAnnotations() {
|
||||||
|
Annotation[] annotations = AnnotationUtil.getAnnotations(ClassForTest.class, true);
|
||||||
Assert.assertEquals(2, annotations.length);
|
Assert.assertEquals(2, annotations.length);
|
||||||
}
|
annotations = AnnotationUtil.getAnnotations(ClassForTest.class, false);
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getCombinationAnnotationsWithClassTest(){
|
|
||||||
final AnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassWithAnnotation.class, AnnotationForTest.class);
|
|
||||||
Assert.assertNotNull(annotations);
|
|
||||||
Assert.assertEquals(1, annotations.length);
|
Assert.assertEquals(1, annotations.length);
|
||||||
Assert.assertEquals("测试", annotations[0].value());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAnnotationValueTest() {
|
public void testGetCombinationAnnotations() {
|
||||||
final Object value = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest.class);
|
MetaAnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassForTest.class, MetaAnnotationForTest.class);
|
||||||
Assert.assertEquals("测试", value);
|
Assert.assertEquals(1, annotations.length);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAnnotationSyncAlias() {
|
public void testAnnotations() {
|
||||||
// 直接获取
|
MetaAnnotationForTest[] annotations1 = AnnotationUtil.getAnnotations(ClassForTest.class, false, MetaAnnotationForTest.class);
|
||||||
Assert.assertEquals("", ClassWithAnnotation.class.getAnnotation(AnnotationForTest.class).retry());
|
Assert.assertEquals(0, annotations1.length);
|
||||||
|
annotations1 = AnnotationUtil.getAnnotations(ClassForTest.class, true, MetaAnnotationForTest.class);
|
||||||
|
Assert.assertEquals(1, annotations1.length);
|
||||||
|
|
||||||
// 加别名适配
|
Annotation[] annotations2 = AnnotationUtil.getAnnotations(
|
||||||
final AnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(ClassWithAnnotation.class, AnnotationForTest.class);
|
ClassForTest.class, false, t -> ObjUtil.equals(t.annotationType(), MetaAnnotationForTest.class)
|
||||||
Assert.assertEquals("测试", annotation.retry());
|
);
|
||||||
|
Assert.assertEquals(0, annotations2.length);
|
||||||
|
annotations2 = AnnotationUtil.getAnnotations(
|
||||||
|
ClassForTest.class, true, t -> ObjUtil.equals(t.annotationType(), MetaAnnotationForTest.class)
|
||||||
|
);
|
||||||
|
Assert.assertEquals(1, annotations2.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AnnotationForTest("测试")
|
@Test
|
||||||
@RepeatAnnotationForTest
|
public void testGetAnnotation() {
|
||||||
static class ClassWithAnnotation{
|
MetaAnnotationForTest annotation = AnnotationUtil.getAnnotation(ClassForTest.class, MetaAnnotationForTest.class);
|
||||||
public void test(){
|
Assert.assertNotNull(annotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHasAnnotation() {
|
||||||
|
Assert.assertTrue(AnnotationUtil.hasAnnotation(ClassForTest.class, MetaAnnotationForTest.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAnnotationValue() {
|
||||||
|
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||||
|
Assert.assertEquals(annotation.value(), AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class));
|
||||||
|
Assert.assertEquals(annotation.value(), AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class, "value"));
|
||||||
|
Assert.assertNull(AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class, "property"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAnnotationValueMap() {
|
||||||
|
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||||
|
Map<String, Object> valueMap = AnnotationUtil.getAnnotationValueMap(ClassForTest.class, AnnotationForTest.class);
|
||||||
|
Assert.assertNotNull(valueMap);
|
||||||
|
Assert.assertEquals(1, valueMap.size());
|
||||||
|
Assert.assertEquals(annotation.value(), valueMap.get("value"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRetentionPolicy() {
|
||||||
|
RetentionPolicy policy = AnnotationForTest.class.getAnnotation(Retention.class).value();
|
||||||
|
Assert.assertEquals(policy, AnnotationUtil.getRetentionPolicy(AnnotationForTest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetTargetType() {
|
||||||
|
ElementType[] types = AnnotationForTest.class.getAnnotation(Target.class).value();
|
||||||
|
Assert.assertArrayEquals(types, AnnotationUtil.getTargetType(AnnotationForTest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsDocumented() {
|
||||||
|
Assert.assertFalse(AnnotationUtil.isDocumented(AnnotationForTest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsInherited() {
|
||||||
|
Assert.assertFalse(AnnotationUtil.isInherited(AnnotationForTest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetValue() {
|
||||||
|
AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||||
|
String newValue = "is a new value";
|
||||||
|
Assert.assertNotEquals(newValue, annotation.value());
|
||||||
|
AnnotationUtil.setValue(annotation, "value", newValue);
|
||||||
|
Assert.assertEquals(newValue, annotation.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAnnotationAlias() {
|
||||||
|
MetaAnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(AnnotationForTest.class, MetaAnnotationForTest.class);
|
||||||
|
Assert.assertEquals(annotation.value(), annotation.alias());
|
||||||
|
Assert.assertEquals(MetaAnnotationForTest.class, annotation.annotationType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Target(ElementType.TYPE_USE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
private @interface MetaAnnotationForTest{
|
||||||
|
@Alias(value = "alias")
|
||||||
|
String value() default "";
|
||||||
|
String alias() default "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MetaAnnotationForTest("foo")
|
||||||
|
@Target(ElementType.TYPE_USE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
private @interface AnnotationForTest{
|
||||||
|
String value() default "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@AnnotationForTest("foo")
|
||||||
|
private static class ClassForTest{}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
package cn.hutool.core.annotation;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link CombinationAnnotationElement}
|
||||||
|
*/
|
||||||
|
public class CombinationAnnotationElementTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOf() {
|
||||||
|
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||||
|
Assert.assertNotNull(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsAnnotationPresent() {
|
||||||
|
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||||
|
Assert.assertTrue(element.isAnnotationPresent(MetaAnnotationForTest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAnnotation() {
|
||||||
|
AnnotationForTest annotation1 = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||||
|
MetaAnnotationForTest annotation2 = AnnotationForTest.class.getAnnotation(MetaAnnotationForTest.class);
|
||||||
|
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||||
|
Assert.assertEquals(annotation1, element.getAnnotation(AnnotationForTest.class));
|
||||||
|
Assert.assertEquals(annotation2, element.getAnnotation(MetaAnnotationForTest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAnnotations() {
|
||||||
|
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||||
|
Annotation[] annotations = element.getAnnotations();
|
||||||
|
Assert.assertEquals(2, annotations.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetDeclaredAnnotations() {
|
||||||
|
CombinationAnnotationElement element = CombinationAnnotationElement.of(ClassForTest.class, a -> true);
|
||||||
|
Annotation[] annotations = element.getDeclaredAnnotations();
|
||||||
|
Assert.assertEquals(2, annotations.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Target(ElementType.TYPE_USE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
private @interface MetaAnnotationForTest{ }
|
||||||
|
|
||||||
|
@MetaAnnotationForTest
|
||||||
|
@Target(ElementType.TYPE_USE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
private @interface AnnotationForTest{ }
|
||||||
|
|
||||||
|
@AnnotationForTest
|
||||||
|
private static class ClassForTest{}
|
||||||
|
|
||||||
|
}
|
@ -1,16 +0,0 @@
|
|||||||
package cn.hutool.core.annotation;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author hongda.li 2022-04-26 17:09
|
|
||||||
*/
|
|
||||||
@AnnotationForTest("repeat-annotation")
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
// Target注解决定MyAnnotation注解可以加在哪些成分上,如加在类身上,或者属性身上,或者方法身上等成分
|
|
||||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
|
||||||
public @interface RepeatAnnotationForTest {
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user