Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Assert;
- import org.junit.Test;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- public class T11TestSetAge {
- private static final String CLASS_NOT_PRESENT_ERROR_MESSAGE = "Class '%s' not present";
- private static final String METHOD_RETURN_TYPE_ERROR = "Method '%s' in class '%s' should have return type '%s'";
- private static final String WRONG_SALARY_IS_SET = "Wrong salary is set";
- private static final String FIRST_NAME = "firstName";
- private static final String SECOND_NAME = "secondName";
- private static final String NAME_OF_FIELD_AGE = "age";
- private static final int YOUNG_PERSON_AGE = 20;
- private static final int OLD_PERSON_AGE = 40;
- private static final double PERSON_SALARY = 500D;
- private static final String PERSON = "Person";
- private static final String[] methodNames = new String[]{
- "setAge"
- };
- private static final HashMap<String, Class> methodReturnTypes = new HashMap<String, Class>() {{
- put("setAge", void.class);
- }};
- private static final HashMap<String, Class[]> methodParameters = new HashMap<String, Class[]>() {{
- put("setAge", new Class[]{int.class});
- }};
- @Test
- public void test() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
- assertExistingMethodsAndWorksCorrect(methodNames);
- }
- private void assertExistingMethodsAndWorksCorrect(String[] methodNames) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
- Class cl = getClass(PERSON);
- for (String methodName : methodNames) {
- Method method =
- methodParameters.get(methodName).length == 0
- ? cl.getDeclaredMethod(methodName)
- : cl.getDeclaredMethod(methodName, methodParameters.get(methodName));
- Class<?> returnType = method.getReturnType();
- Assert.assertTrue(
- String.format(METHOD_RETURN_TYPE_ERROR,
- methodName,
- PERSON,
- methodReturnTypes.get(methodName)),
- returnType.equals(methodReturnTypes.get(methodName)));
- Assert.assertTrue(WRONG_SALARY_IS_SET,
- assertSetAgeCorrect(method, cl));
- }
- }
- private boolean assertSetAgeCorrect(Method method, Class cl) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
- return correctSetAge(method, cl)
- && correctSetAgeWithZERO(method, cl);
- }
- private boolean correctSetAgeWithZERO(Method setAge, Class cl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
- Constructor constructor = cl.getDeclaredConstructor(String.class,
- String.class, int.class, double.class);
- constructor.setAccessible(true);
- Object person = constructor.newInstance(FIRST_NAME, SECOND_NAME,
- OLD_PERSON_AGE, PERSON_SALARY);
- boolean error = false;
- try {
- setAge.invoke(person, 0);
- } catch (InvocationTargetException ite) {
- error = true;
- }
- return error;
- }
- private boolean correctSetAge(Method setAge, Class cl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
- Constructor constructor = cl.getDeclaredConstructor(String.class,
- String.class, int.class, double.class);
- constructor.setAccessible(true);
- Object person = constructor.newInstance(FIRST_NAME, SECOND_NAME,
- YOUNG_PERSON_AGE, PERSON_SALARY);
- setAge.invoke(person, YOUNG_PERSON_AGE);
- Field fieldAge = person.getClass().getDeclaredField(NAME_OF_FIELD_AGE);
- fieldAge.setAccessible(true);
- int age = (int) fieldAge.get(person);
- return YOUNG_PERSON_AGE == age;
- }
- private Class getClass(String className) {
- Assert.assertTrue(String.format(CLASS_NOT_PRESENT_ERROR_MESSAGE, className),
- Classes.allClasses.containsKey(className));
- return Classes.allClasses.get(className);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement