diff --git a/Hometask_7/src/Launcher.java b/Hometask_7/src/Launcher.java new file mode 100644 index 0000000..292ff50 --- /dev/null +++ b/Hometask_7/src/Launcher.java @@ -0,0 +1,11 @@ +import tester.Tester; +import tests_providers.TestProvider_2; + +public class Launcher { + public static void main(String[] args) { + Tester.start(TestProvider_2.class); + + System.out.println("-------------------------------"); + Tester.start("TestProvider_3"); + } +} diff --git a/Hometask_7/src/annotations/AfterSuite.java b/Hometask_7/src/annotations/AfterSuite.java new file mode 100644 index 0000000..e59ed42 --- /dev/null +++ b/Hometask_7/src/annotations/AfterSuite.java @@ -0,0 +1,12 @@ +package annotations; + +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/Hometask_7/src/annotations/BeforeSuite.java b/Hometask_7/src/annotations/BeforeSuite.java new file mode 100644 index 0000000..68dc315 --- /dev/null +++ b/Hometask_7/src/annotations/BeforeSuite.java @@ -0,0 +1,12 @@ +package annotations; + +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/Hometask_7/src/annotations/Test.java b/Hometask_7/src/annotations/Test.java new file mode 100644 index 0000000..68338de --- /dev/null +++ b/Hometask_7/src/annotations/Test.java @@ -0,0 +1,12 @@ +package annotations; + +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 priority() default 10; +} diff --git a/Hometask_7/src/tester/Tester.java b/Hometask_7/src/tester/Tester.java new file mode 100644 index 0000000..8002844 --- /dev/null +++ b/Hometask_7/src/tester/Tester.java @@ -0,0 +1,147 @@ +package tester; + +import annotations.Test; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.*; + +public class Tester { + private static List listOfMethods; + private static List listOfAnnotations; + + public static void start(Class testClass) { + + + listOfMethods = findMethodsWithAnnotations(testClass.getDeclaredMethods()); + listOfAnnotations = findAllAnnotations(listOfMethods); + checkQuantityOfBeforeSuiteAnnotations(listOfAnnotations); + checkQuantityOfAfterSuiteAnnotations(listOfAnnotations); + provideSortedListOfAnnotatedMethods(listOfMethods); + doTests(listOfMethods,testClass); + + + } + + public static void start(String name) { + Class testClass = null; + try { + testClass = Class.forName("tests_providers." + name); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + listOfMethods = findMethodsWithAnnotations(testClass.getDeclaredMethods()); + listOfAnnotations = findAllAnnotations(listOfMethods); + checkQuantityOfBeforeSuiteAnnotations(listOfAnnotations); + checkQuantityOfAfterSuiteAnnotations(listOfAnnotations); + provideSortedListOfAnnotatedMethods(listOfMethods); + doTests(listOfMethods,testClass); + + + } + + private static void checkQuantityOfBeforeSuiteAnnotations(List list) { + int countB = 0; + + + for (Annotation annotation : list) { + if (annotation.annotationType().getSimpleName().equals("BeforeSuite")) { + countB++; + } + } + if (countB > 1) { + throw new RuntimeException("There is to many methods with annotation @BeforeSuite"); + } + } + + private static void checkQuantityOfAfterSuiteAnnotations(List list) { + int countA = 0; + for (Annotation annotation : list) { + if (annotation.annotationType().getSimpleName().equals("BeforeSuite")) { + countA++; + } + } + if (countA > 1) { + throw new RuntimeException("There is to many methods with annotation @AfterSuite"); + } + } + + + private static List provideSortedListOfAnnotatedMethods(List list) { + Method start = null; + Method end = null; + Iterator iterator = list.iterator(); + while (iterator.hasNext()){ + Method method = iterator.next(); + for (Annotation annotation : method.getDeclaredAnnotations()) { + if (annotation.annotationType().getSimpleName().equals("BeforeSuite")) { + start = method; + iterator.remove(); + } + if (annotation.annotationType().getSimpleName().equals("AfterSuite")) { + end = method; + iterator.remove(); + } + } + } + list.sort((x,y)->x.getDeclaredAnnotation(Test.class).priority()- y.getDeclaredAnnotation(Test.class).priority()); + if (start != null) { + list.add(0, start); + } + if (end != null) { + list.add(end); + } + return list; + } + + private static ArrayList findMethodsWithAnnotations(Method[] methods) { + ArrayList list = new ArrayList<>(); + for (Method method : methods) { + + if (method.getDeclaredAnnotations().length != 0) { + list.add(method); + } + } + if (list.size() == 0) { + throw new RuntimeException("Provided class does not contain any method to test"); + } + return list; + } + private static void doTests(List listOfMethods, Class testClass){ + try { + Object o = testClass.getConstructor().newInstance(); + for (Method method : listOfMethods) { + if (Modifier.isPrivate(method.getModifiers())) { + invokePrivateMethod(o, method); + } else { + method.invoke(o); + } + } + }catch (Exception e) { + e.printStackTrace(); + } + + + } + private static ArrayList findAllAnnotations(List methods){ + ArrayList list = new ArrayList<>(); + for (Method method : methods) { + list.addAll(Arrays.asList(method.getDeclaredAnnotations())); + } + return list; + } + private static void invokePrivateMethod(Object o, Method method){ + method.setAccessible(true); + try { + method.invoke(o); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + method.setAccessible(false); + } + +} diff --git a/Hometask_7/src/tests_providers/TestProvider_1.java b/Hometask_7/src/tests_providers/TestProvider_1.java new file mode 100644 index 0000000..48c3fc1 --- /dev/null +++ b/Hometask_7/src/tests_providers/TestProvider_1.java @@ -0,0 +1,42 @@ +package tests_providers; + +import annotations.AfterSuite; +import annotations.BeforeSuite; +import annotations.Test; + +public class TestProvider_1 { + + @BeforeSuite + public void testMethodStart() { + System.out.println("Test with annotation @BeforeSuite. Starting tests..."); + } + @BeforeSuite + void testMethodStart1() { + System.out.println("Starting tests..."); + } + + @Test(priority = 1) + public void test1(){ + System.out.println("Test with priority 1"); + } + @Test() + public void testDefault(){ + System.out.println("Test with default priority"); + } + @Test(priority = 10) + public void test10(){ + System.out.println("Test with priority 10"); + } + @Test(priority = 3) + public void test3(){ + System.out.println("Test with priority 3"); + } + @Test(priority = 3) + public void test3_1(){ + System.out.println("Test with priority 3"); + } + @AfterSuite + public void testMethodEnd() { + System.out.println("Test with annotation @AfterSuite. The end of tests...."); + } +} diff --git a/Hometask_7/src/tests_providers/TestProvider_2.java b/Hometask_7/src/tests_providers/TestProvider_2.java new file mode 100644 index 0000000..afe273f --- /dev/null +++ b/Hometask_7/src/tests_providers/TestProvider_2.java @@ -0,0 +1,37 @@ +package tests_providers; + +import annotations.*; + + +public class TestProvider_2 { + @BeforeSuite + public void testMethodStart() { + System.out.println("Test with annotation @BeforeSuite. Starting tests..."); + } + + + @Test(priority = 1) + public void test1(){ + System.out.println("Test with priority 1"); + } + @Test() + public void testDefault(){ + System.out.println("Test with default priority"); + } + @Test(priority = 10) + public void test10(){ + System.out.println("Test with priority 10"); + } + @Test(priority = 3) + public void test3(){ + System.out.println("Test with priority 3"); + } + @Test(priority = 3) + public void test3_1(){ + System.out.println("Test with priority 3"); + } + @AfterSuite + public void testMethodEnd() { + System.out.println("Test with annotation @AfterSuite. The end of tests...."); + } +} diff --git a/Hometask_7/src/tests_providers/TestProvider_3.java b/Hometask_7/src/tests_providers/TestProvider_3.java new file mode 100644 index 0000000..855ba6a --- /dev/null +++ b/Hometask_7/src/tests_providers/TestProvider_3.java @@ -0,0 +1,43 @@ +package tests_providers; + +import annotations.AfterSuite; +import annotations.Test; + +public class TestProvider_3 { + + + + @Test(priority = 1) + public void test1(){ + System.out.println("Test with priority 1"); + } + + public void test_noAnn(){ + System.out.println("Test without annotation"); + } + public void test_noAnn2(){ + System.out.println("Test without annotation"); + } + @Test() + private void testDefault(){ + System.out.println("Private test with default priority"); + } + @Test(priority = 10) + public void test10(){ + System.out.println("Test with priority 10"); + } + @Test(priority = 3) + public void test3(){ + System.out.println("Test with priority 3"); + } + @Test(priority = 3) + public void test3_1(){ + System.out.println("Test with priority 3"); + } + @AfterSuite + public void testMethodEnd() { + System.out.println("Test with annotation @AfterSuite. The end of tests...."); + } + + +}