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 Hometask_7/src/Launcher.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
12 changes: 12 additions & 0 deletions Hometask_7/src/annotations/AfterSuite.java
Original file line number Diff line number Diff line change
@@ -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 {

}
12 changes: 12 additions & 0 deletions Hometask_7/src/annotations/BeforeSuite.java
Original file line number Diff line number Diff line change
@@ -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 {

}
12 changes: 12 additions & 0 deletions Hometask_7/src/annotations/Test.java
Original file line number Diff line number Diff line change
@@ -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;
}
147 changes: 147 additions & 0 deletions Hometask_7/src/tester/Tester.java
Original file line number Diff line number Diff line change
@@ -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<Method> listOfMethods;
private static List<Annotation> 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<Annotation> 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<Annotation> 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<Method> provideSortedListOfAnnotatedMethods(List<Method> list) {
Method start = null;
Method end = null;
Iterator<Method> 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<Method> findMethodsWithAnnotations(Method[] methods) {
ArrayList<Method> 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<Method> 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<Annotation> findAllAnnotations(List<Method> methods){
ArrayList<Annotation> 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);
}

}
42 changes: 42 additions & 0 deletions Hometask_7/src/tests_providers/TestProvider_1.java
Original file line number Diff line number Diff line change
@@ -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....");
}
}
37 changes: 37 additions & 0 deletions Hometask_7/src/tests_providers/TestProvider_2.java
Original file line number Diff line number Diff line change
@@ -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....");
}
}
43 changes: 43 additions & 0 deletions Hometask_7/src/tests_providers/TestProvider_3.java
Original file line number Diff line number Diff line change
@@ -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....");
}


}