Skip to content

Commit

Permalink
add ReflectUtilTest test case (opengoofy#641) (opengoofy#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gdk666 authored Sep 7, 2022
1 parent 5f003fb commit df68a19
Showing 1 changed file with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit df68a19

Please sign in to comment.