import code
This commit is contained in:
4
src/test/java/ru/dmitriymx/reflection/EmptyClass.java
Normal file
4
src/test/java/ru/dmitriymx/reflection/EmptyClass.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
public class EmptyClass {
|
||||
}
|
||||
124
src/test/java/ru/dmitriymx/reflection/ReflectionClassTest.java
Normal file
124
src/test/java/ru/dmitriymx/reflection/ReflectionClassTest.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ReflectionClassTest {
|
||||
|
||||
@Test
|
||||
public void fieldsList() {
|
||||
final List<String> expectedList = Collections.singletonList("MAGIC_NUMBER");
|
||||
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
|
||||
List<ReflectionField> fieldsList = refStaticObj.fieldsList();
|
||||
assertNotNull(fieldsList);
|
||||
fieldsList = fieldsList.stream().filter(refField -> !refField.name().equals("$jacocoData"))
|
||||
.collect(Collectors.toList());
|
||||
assertFalse(fieldsList.isEmpty());
|
||||
|
||||
for (ReflectionField refField : fieldsList) {
|
||||
assertTrue(
|
||||
"Static field '" + refField.name() + "' not contains in expectedList",
|
||||
expectedList.contains(refField.name()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldsList_noFields() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.EmptyClass"));
|
||||
|
||||
assertEquals(0, refStaticObj.fieldsList().stream()
|
||||
.filter(refField -> !refField.name().equals("$jacocoData"))
|
||||
.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void field() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
ReflectionField refField = refStaticObj.field("MAGIC_NUMBER");
|
||||
|
||||
assertNotNull(refField);
|
||||
}
|
||||
|
||||
@Test(expected = ReflectionException.class)
|
||||
public void field_notExists() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
refStaticObj.field("field_not_exists");
|
||||
}
|
||||
|
||||
@Test(expected = ReflectionException.class)
|
||||
public void field_notStatic() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
refStaticObj.field("simpleField");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodsList() {
|
||||
final List<String> expectedList = Collections.singletonList("getMagicNumber");
|
||||
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
// исключаем проксирующий метод от JaCoCo
|
||||
List<ReflectionMethod> methodList = refStaticObj.methodsList().stream()
|
||||
.filter(refMethod -> !refMethod.name().equals("$jacocoInit")).collect(Collectors.toList());
|
||||
|
||||
for (ReflectionMethod refMethod : methodList) {
|
||||
assertTrue(
|
||||
"Static method '" + refMethod.name() + "' not contains in expectedList",
|
||||
expectedList.contains(refMethod.name()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodsList_noMethods() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.EmptyClass"));
|
||||
// исключаем проксирующий метод от JaCoCo
|
||||
List<ReflectionMethod> methodList = refStaticObj.methodsList().stream()
|
||||
.filter(refMethod -> !refMethod.name().equals("$jacocoInit")).collect(Collectors.toList());
|
||||
|
||||
assertTrue(methodList.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void method() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
|
||||
ReflectionMethod refMethod = refStaticObj.method("getMagicNumber");
|
||||
assertNotNull(refMethod);
|
||||
}
|
||||
|
||||
@Test(expected = ReflectionException.class)
|
||||
public void method_notExists() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
refStaticObj.method("not_exists_method");
|
||||
}
|
||||
|
||||
@Test(expected = ReflectionException.class)
|
||||
public void method_notStatic() {
|
||||
ReflectionClass refStaticObj = new ReflectionClass(
|
||||
safeClassForName("ru.dmitriymx.reflection.SomeObject"));
|
||||
refStaticObj.method("getSimpleField");
|
||||
}
|
||||
|
||||
private Class safeClassForName(String clazz) {
|
||||
try {
|
||||
return Class.forName(clazz);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
24
src/test/java/ru/dmitriymx/reflection/SomeObject.java
Normal file
24
src/test/java/ru/dmitriymx/reflection/SomeObject.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
class SomeObject {
|
||||
|
||||
private static final int MAGIC_NUMBER = 33;
|
||||
private final String finalizedField = "value123";
|
||||
private String simpleField = "defaultValue";
|
||||
|
||||
public String getSimpleField() {
|
||||
return simpleField;
|
||||
}
|
||||
|
||||
public void setSimpleField(String value) {
|
||||
simpleField = value;
|
||||
}
|
||||
|
||||
private void setSimpleField() {
|
||||
simpleField = "123value";
|
||||
}
|
||||
|
||||
public static int getMagicNumber() {
|
||||
return MAGIC_NUMBER;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user