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); } //region Field tests @Test public void fieldsList() { final List 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 field_getter() { ReflectionObject refObj = new ReflectionObject(new SomeObject()); ReflectionField refField = refObj.field("someIntField"); assertNotNull(refField); ReflectionMethod refGetter = refField.getter(); assertNotNull(refGetter); } @Test public void field_setter() { ReflectionObject refObj = new ReflectionObject(new SomeObject()); ReflectionField refField = refObj.field("someIntField"); assertNotNull(refField); ReflectionMethod refSetter = refField.setter(); assertNotNull(refSetter); } @Test public void fieldsWithAnnotation() { ReflectionObject refObj = new ReflectionObject(new SomeObject()); List list = refObj.fieldsWithAnnotation(SomeAnnotation.class); assertEquals(1, list.size()); ReflectionField refField = list.get(0); assertEquals("someIntField", refField.name()); SomeAnnotation annotation = refField.annotation(SomeAnnotation.class); assertNotNull(annotation); } //endregion //region Method tests @Test public void methodsList() { final List 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 public void method_generic() { SomeGenericObject someGenericObject = new SomeGenericObject<>(); String key = "KEY!"; int value = 3306; ReflectionObject refObj = new ReflectionObject(someGenericObject); ReflectionMethod refMethod = refObj.method("genericMethod", Object.class, Object.class); assertNotNull(refMethod); refMethod.invoke(key, value); assertFalse(someGenericObject.map.isEmpty()); assertTrue(someGenericObject.map.containsKey(key)); assertEquals(value, (int) someGenericObject.map.get(key)); } @Test(expected = ReflectionException.class) public void method_notExists() { ReflectionObject refObj = new ReflectionObject(new SomeObject()); refObj.method("not_exists_method"); } //endregion }