Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lesson7/AfterSuite.java
Original file line number Diff line number Diff line change
@@ -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 {
}
11 changes: 11 additions & 0 deletions src/lesson7/BeforeSuite.java
Original file line number Diff line number Diff line change
@@ -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 {
}
54 changes: 54 additions & 0 deletions src/lesson7/Cat.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

7 changes: 7 additions & 0 deletions src/lesson7/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lesson7;

public class Main {
public static void main(String[] args) {
TestClass.start(Cat.class);
}
}
12 changes: 12 additions & 0 deletions src/lesson7/Test.java
Original file line number Diff line number Diff line change
@@ -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;
}
107 changes: 107 additions & 0 deletions src/lesson7/TestClass.java
Original file line number Diff line number Diff line change
@@ -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<Method> al = new ArrayList<>();

//Найдем все методы с аннотацией @Test
for(Method o : methods) {
if (o.isAnnotationPresent(Test.class)) {
al.add(o);
}
}

//Отсортируем ArrayList в порядке уменьшения приоритета (1 - наивысший приоритет, 10 - самый низкий)
al.sort(new Comparator<Method>() {
@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());
}
}
}