import code
This commit is contained in:
108
src/test/java/ru/dmitriymx/reflection/ReflectionObjectTest.java
Normal file
108
src/test/java/ru/dmitriymx/reflection/ReflectionObjectTest.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ReflectionObjectTest {
|
||||
|
||||
@Test
|
||||
public void getOriginalObject() {
|
||||
final String strObj = "StringObject";
|
||||
|
||||
assertEquals(strObj, new ReflectionObject(strObj).getOriginalObject());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getOriginalObject_classCast() {
|
||||
final String strObj = "StringObject";
|
||||
|
||||
assertEquals(strObj, new ReflectionObject(strObj).getOriginalObject(String.class));
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void getOriginalObject_classCast_Exception() {
|
||||
final String strObj = "StringObject";
|
||||
|
||||
new ReflectionObject(strObj).getOriginalObject(Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldsList() {
|
||||
final List<String> expectedList = Arrays.asList("finalizedField", "simpleField");
|
||||
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
for (ReflectionField refField : refObj.fieldList()) {
|
||||
if (!refField.isStatic()) {
|
||||
assertTrue(
|
||||
"Field '" + refField.name() + "' not contains in expectedList",
|
||||
expectedList.contains(refField.name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldsList_noFields() {
|
||||
ReflectionObject refObj = new ReflectionObject(new Object());
|
||||
assertTrue(refObj.fieldList().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void field() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
ReflectionField refField = refObj.field("finalizedField");
|
||||
|
||||
assertNotNull(refField);
|
||||
}
|
||||
|
||||
@Test(expected = ReflectionException.class)
|
||||
public void field_notExists() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
refObj.field("field_not_exists");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodsList() {
|
||||
final List<String> expectedList = Arrays.asList("getSimpleField", "setSimpleField");
|
||||
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
for (ReflectionMethod refMethod : refObj.methodsList()) {
|
||||
if (!refMethod.isStatic()) {
|
||||
assertTrue(
|
||||
"Method '" + refMethod.name() + "' not contains in expectedList",
|
||||
expectedList.contains(refMethod.name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodsList_noMethods() {
|
||||
ReflectionObject refObj = new ReflectionObject(new EmptyClass());
|
||||
// исключаем проксирующий метод от JaCoCo
|
||||
long count = refObj.methodsList().stream().filter(refMethod -> !refMethod.name().equals("$jacocoInit")).count();
|
||||
assertEquals(0, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void method() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
|
||||
ReflectionMethod refMethod = refObj.method("getSimpleField");
|
||||
assertNotNull(refMethod);
|
||||
|
||||
refMethod = refObj.method("setSimpleField");
|
||||
assertNotNull(refMethod);
|
||||
|
||||
refMethod = refObj.method("setSimpleField", String.class);
|
||||
assertNotNull(refMethod);
|
||||
}
|
||||
|
||||
@Test(expected = ReflectionException.class)
|
||||
public void method_notExists() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
refObj.method("not_exists_method");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user