Advertisement
damesova

Test

Jul 2nd, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. import org.junit.Assert;
  2. import org.junit.Test;
  3.  
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.lang.reflect.Method;
  8. import java.util.HashMap;
  9.  
  10. public class T11TestSetAge {
  11.     private static final String CLASS_NOT_PRESENT_ERROR_MESSAGE = "Class '%s' not present";
  12.     private static final String METHOD_RETURN_TYPE_ERROR = "Method '%s' in class '%s' should have return type '%s'";
  13.     private static final String WRONG_SALARY_IS_SET = "Wrong salary is set";
  14.     private static final String FIRST_NAME = "firstName";
  15.     private static final String SECOND_NAME = "secondName";
  16.     private static final String NAME_OF_FIELD_AGE = "age";
  17.     private static final int YOUNG_PERSON_AGE = 20;
  18.     private static final int OLD_PERSON_AGE = 40;
  19.     private static final double PERSON_SALARY = 500D;
  20.  
  21.     private static final String PERSON = "Person";
  22.  
  23.  
  24.     private static final String[] methodNames = new String[]{
  25.             "setAge"
  26.     };
  27.  
  28.     private static final HashMap<String, Class> methodReturnTypes = new HashMap<String, Class>() {{
  29.         put("setAge", void.class);
  30.     }};
  31.  
  32.     private static final HashMap<String, Class[]> methodParameters = new HashMap<String, Class[]>() {{
  33.         put("setAge", new Class[]{int.class});
  34.     }};
  35.  
  36.  
  37.     @Test
  38.     public void test() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
  39.         assertExistingMethodsAndWorksCorrect(methodNames);
  40.     }
  41.  
  42.     private void assertExistingMethodsAndWorksCorrect(String[] methodNames) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
  43.         Class cl = getClass(PERSON);
  44.         for (String methodName : methodNames) {
  45.             Method method =
  46.                     methodParameters.get(methodName).length == 0
  47.                             ? cl.getDeclaredMethod(methodName)
  48.                             : cl.getDeclaredMethod(methodName, methodParameters.get(methodName));
  49.             Class<?> returnType = method.getReturnType();
  50.             Assert.assertTrue(
  51.                     String.format(METHOD_RETURN_TYPE_ERROR,
  52.                             methodName,
  53.                             PERSON,
  54.                             methodReturnTypes.get(methodName)),
  55.                     returnType.equals(methodReturnTypes.get(methodName)));
  56.  
  57.             Assert.assertTrue(WRONG_SALARY_IS_SET,
  58.                     assertSetAgeCorrect(method, cl));
  59.         }
  60.     }
  61.  
  62.     private boolean assertSetAgeCorrect(Method method, Class cl) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
  63.         return correctSetAge(method, cl)
  64.                 && correctSetAgeWithZERO(method, cl);
  65.     }
  66.  
  67.     private boolean correctSetAgeWithZERO(Method setAge, Class cl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
  68.         Constructor constructor = cl.getDeclaredConstructor(String.class,
  69.                 String.class, int.class, double.class);
  70.         constructor.setAccessible(true);
  71.         Object person = constructor.newInstance(FIRST_NAME, SECOND_NAME,
  72.                 OLD_PERSON_AGE, PERSON_SALARY);
  73.  
  74.         boolean error = false;
  75.         try {
  76.             setAge.invoke(person, 0);
  77.         } catch (InvocationTargetException ite) {
  78.             error = true;
  79.         }
  80.  
  81.         return error;
  82.     }
  83.  
  84.     private boolean correctSetAge(Method setAge, Class cl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
  85.  
  86.         Constructor constructor = cl.getDeclaredConstructor(String.class,
  87.                 String.class, int.class, double.class);
  88.         constructor.setAccessible(true);
  89.         Object person = constructor.newInstance(FIRST_NAME, SECOND_NAME,
  90.                 YOUNG_PERSON_AGE, PERSON_SALARY);
  91.  
  92.         setAge.invoke(person, YOUNG_PERSON_AGE);
  93.  
  94.         Field fieldAge = person.getClass().getDeclaredField(NAME_OF_FIELD_AGE);
  95.         fieldAge.setAccessible(true);
  96.         int age = (int) fieldAge.get(person);
  97.  
  98.         return YOUNG_PERSON_AGE == age;
  99.     }
  100.  
  101.     private Class getClass(String className) {
  102.         Assert.assertTrue(String.format(CLASS_NOT_PRESENT_ERROR_MESSAGE, className),
  103.                 Classes.allClasses.containsKey(className));
  104.         return Classes.allClasses.get(className);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement