diff --git a/src/lesson7/AfterSuite.java b/src/lesson7/AfterSuite.java new file mode 100644 index 0000000..7dc768e --- /dev/null +++ b/src/lesson7/AfterSuite.java @@ -0,0 +1,11 @@ +package lesson7; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface AfterSuite { +} diff --git a/src/lesson7/BeforeSuite.java b/src/lesson7/BeforeSuite.java new file mode 100644 index 0000000..c13c2ed --- /dev/null +++ b/src/lesson7/BeforeSuite.java @@ -0,0 +1,11 @@ +package lesson7; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface BeforeSuite { +} diff --git a/src/lesson7/Cat.java b/src/lesson7/Cat.java new file mode 100644 index 0000000..16e9bbc --- /dev/null +++ b/src/lesson7/Cat.java @@ -0,0 +1,54 @@ +package lesson7; + +public class Cat { + public String name; + public int age; + public String color; + + public Cat(String name, int age, String color) { + this.name = name; + this.age = age; + this.color = color; + } + + public Cat() { + this.name = "Paul"; + this.age = 2; + this.color = "white"; + } + + @Test(prioritet = 4) + public void meow() { + System.out.println("meow..."); + } + + @AfterSuite + public void jump() { + System.out.println("jump..."); + } + + @BeforeSuite + public void getName() { + System.out.println("name: " + name); + } + + @Test(prioritet = 5) + public void getAge() { + System.out.println("age: " + age); + } + + @Test(prioritet = 2) + public void getColor() { + System.out.println("color: " + color); + } + + @Test + public void purr() { + System.out.println("purr..."); + } + + public void speakTheHumanLanguage(String words) { + System.out.println(words); + } +} + diff --git a/src/lesson7/Main.java b/src/lesson7/Main.java new file mode 100644 index 0000000..1d67a4a --- /dev/null +++ b/src/lesson7/Main.java @@ -0,0 +1,7 @@ +package lesson7; + +public class Main { + public static void main(String[] args) { + TestClass.start(Cat.class); + } +} diff --git a/src/lesson7/Test.java b/src/lesson7/Test.java new file mode 100644 index 0000000..0b403b3 --- /dev/null +++ b/src/lesson7/Test.java @@ -0,0 +1,12 @@ +package lesson7; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Test { + int prioritet() default 10; +} diff --git a/src/lesson7/TestClass.java b/src/lesson7/TestClass.java new file mode 100644 index 0000000..306fa3a --- /dev/null +++ b/src/lesson7/TestClass.java @@ -0,0 +1,107 @@ +package lesson7; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Comparator; + +public class TestClass { + + public static void start(Class c) { + getClassInfo(c); + + Method[] methods = c.getDeclaredMethods(); + ArrayList al = new ArrayList<>(); + + //Найдем все методы с аннотацией @Test + for(Method o : methods) { + if (o.isAnnotationPresent(Test.class)) { + al.add(o); + } + } + + //Отсортируем ArrayList в порядке уменьшения приоритета (1 - наивысший приоритет, 10 - самый низкий) + al.sort(new Comparator() { + @Override + public int compare(Method o1, Method o2) { + return o1.getAnnotation(Test.class).prioritet() - o2.getAnnotation(Test.class).prioritet(); + } + }); + + //Найдем и добавим методы с аннотациями @BeforeSuite и @AfterSuite на нулевое и последнее места в ArrayList + //Если найдем больше одного таких методов, выбросим исключение + for(Method o : methods) { + if (o.isAnnotationPresent(BeforeSuite.class)) { + if (!al.get(0).isAnnotationPresent(BeforeSuite.class)) { + al.add(0, o); + } + else { + throw new RuntimeException("Аннотация @BeforeSuite должна быть единственной"); + } + } + } + + for(Method o : methods) { + if (o.isAnnotationPresent(AfterSuite.class)) { + if (!al.get(al.size() - 1).isAnnotationPresent(BeforeSuite.class)) { + al.add(al.size() - 1, o); + } + else { + throw new RuntimeException("Аннотация @AfterSuite должна быть единственной"); + } + } + } + + //Создаем объект тестируемого класса и выполняем методы из подготовленного ArrayList + try { + Object object = c.newInstance(); + for(Method m : al) { + m.invoke(object, null); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void getClassInfo(Class c) { + System.out.println("Сведения о классе:"); + System.out.println("Имя класса: " + c.getSimpleName()); + Constructor[] cons = c.getConstructors(); + System.out.println("Количество конструкторов: " + cons.length); + int i = 1; + for (Constructor v : cons) { + System.out.println("Конструктор " + i + ": "); + System.out.println(" Количество параметров: " + v.getParameterCount()); + System.out.print(" Тип параметров: "); + Class[] ts = v.getParameterTypes(); + for (Class e : ts) { + System.out.print(e.getSimpleName() + " "); + } + System.out.println(); + i++; + } + + Method[] met = c.getDeclaredMethods(); + System.out.println("Количество методов: " + met.length); + i = 1; + for (Method v : met) { + System.out.print(" " + v.getName() + "("); + Class[] ts = v.getParameterTypes(); + for (Class e : ts) { + System.out.print(e.getSimpleName()); + } + System.out.println(")"); + i++; + } + Field[] f = c.getFields(); + System.out.println("Полей: " + f.length); + for (Field v : f) { + System.out.println(" " + v.getName() + " " + v.getAnnotatedType().getType().getTypeName()); + } + } +} + + + +