From df68a19b16a38ca16e9bf39820397ad015ec2da3 Mon Sep 17 00:00:00 2001 From: Gdk666 <763366136@qq.com> Date: Wed, 7 Sep 2022 21:10:47 +0800 Subject: [PATCH] add ReflectUtilTest test case (#641) (#679) --- .../common/toolkit/ReflectUtilTest.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ReflectUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ReflectUtilTest.java index ccc9e94f..db68c774 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ReflectUtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ReflectUtilTest.java @@ -17,6 +17,97 @@ package cn.hippo4j.common.toolkit; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.Objects; + public class ReflectUtilTest { + @Test + public void getFieldValueTest() { + TestSubClass testSubClass = new TestSubClass(); + Object privateField = ReflectUtil.getFieldValue(testSubClass, "privateField"); + Assert.isTrue(Objects.equals("privateField", privateField)); + + Object field = ReflectUtil.getFieldValue(testSubClass, "field"); + Assert.isTrue(Objects.equals("field", field)); + + } + + @Test + public void getFieldValueByFiledTest(){ + TestSubClass testSubClass = new TestSubClass(); + Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField"); + + Object privateFieldVal = ReflectUtil.getFieldValue(testSubClass, privateField); + Assert.isTrue(Objects.equals("privateField", privateFieldVal)); + } + + @Test + public void getFieldTest(){ + Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField"); + Assert.notNull(privateField); + + Field field = ReflectUtil.getField(TestSubClass.class, "field"); + Assert.notNull(field); + } + + @Test + public void getFieldsTest(){ + Field[] fields = ReflectUtil.getFields(TestSubClass.class); + Assert.isTrue(Objects.equals(4, fields.length)); + } + + @Test + public void getFieldsDirectlyTest(){ + Field[] fields = ReflectUtil.getFieldsDirectly(TestSubClass.class, false); + Assert.isTrue(Objects.equals(2, fields.length)); + + fields = ReflectUtil.getFieldsDirectly(TestSubClass.class, true); + Assert.isTrue(Objects.equals(4, fields.length)); + } + + @Test + public void getFieldNameTest(){ + Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField"); + String fieldName = ReflectUtil.getFieldName(privateField); + Assert.notNull(fieldName); + + Field subField = ReflectUtil.getField(TestSubClass.class, "subField"); + String subfieldName = ReflectUtil.getFieldName(subField); + Assert.notNull(subfieldName); + } + + @Test + public void setFieldValueTest(){ + TestClass testClass = new TestClass(); + ReflectUtil.setFieldValue(testClass, "field", "fieldVal"); + Assert.isTrue(Objects.equals("fieldVal", testClass.getField())); + + Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField"); + ReflectUtil.setFieldValue(testClass, privateField, "privateFieldVal"); + Assert.isTrue(Objects.equals("privateFieldVal", testClass.getPrivateField())); + + } + + + @Getter + static class TestClass { + private String privateField; + protected String field; + + public TestClass() { + this.privateField = "privateField"; + this.field = "field"; + } + } + + class TestSubClass extends TestClass { + private String subField; + } + }