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
23 changes: 23 additions & 0 deletions resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<properties>
<Property name="basePath">logs</Property>
</properties>

<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>

<File name="LogFile" fileName="${basePath}/app.log">
<PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</appenders>

<loggers>
<root level="debug">
<appender-ref ref="Console"/>
<appender-ref ref="LogFile"/>
</root>
</loggers>
</configuration>
34 changes: 34 additions & 0 deletions src/lesson7/TestEx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lesson7;

import java.util.Arrays;

public class TestEx {

public int[] task1(int[] data) {
for (int i = data.length - 1; i >= 0; i--) {
if (data[i] == 4) {
return Arrays.copyOfRange(data, i + 1, data.length);
}
}
throw new RuntimeException("Invalid array");
}

public boolean task2(int[] data) {
boolean contains1 = false;
boolean contains2 = false;

for (int i = 0; i < data.length; i++) {
switch (data[i]) {
case 1:
contains1 = true;
break;
case 4:
contains2 = true;
break;
default:
return false;
}
}
return contains1 && contains2;
}
}
41 changes: 41 additions & 0 deletions src/lesson7/classes_for_tests/TestExJU4TestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lesson7.classes_for_tests;

import lesson7.test_framework.my_annotations.AfterSuite;
import lesson7.test_framework.my_annotations.BeforeSuite;
import lesson7.test_framework.my_annotations.Test;
import test_ex.TestEx;

public class TestExJU4TestCase {
private TestEx dz;

@BeforeSuite
public void prepare() {
dz = new TestEx();
System.out.println("Выполняется метод @BeforeSuite prepare()");
}

@AfterSuite
public void finish() {
dz = null;
System.out.println("Выполняется метод @AfterSuite finish()");
}

/*@BeforeSuite // Получим RuntimeException
public void prepare2() {
dz = new TestEx();
}*/

@Test
public void test_task1_where_4_is_not_last() {
// int[] data = {1, 2, 3, 4, 5, 6, 7};
// Assert.assertArrayEquals(new int[]{5, 6, 7,}, dz.task1(data));
System.out.println("Что-то выполняется в тестовом методе test_task1_where_4_is_not_last()");
}

@Test
public void test_task1_with_some_4() {
// int[] data = {1, 4, 3, 4, 5, 6, 4};
// Assert.assertArrayEquals(new int[]{}, dz.task1(data));
System.out.println("Что-то выполняется в тестовом методе test_task1_with_some_4()");
}
}
138 changes: 138 additions & 0 deletions src/lesson7/test_framework/TestManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package lesson7.test_framework;

import lesson7.TestEx;
import lesson7.classes_for_tests.TestExJU4TestCase;
import lesson7.test_framework.my_annotations.AfterSuite;
import lesson7.test_framework.my_annotations.BeforeSuite;
import lesson7.test_framework.my_annotations.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestManager {
public static void main(String[] args) {
Class clazz = TestExJU4TestCase.class;
start(clazz);
}

public static void start(Class clazz) {
doIt(clazz);
}

public static void start(String className) {
Class clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
doIt(clazz);
}

private static void doIt(Class clazz) {
Method[] methods = clazz.getDeclaredMethods();
checkAnnotations(methods);
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}

startMethodWithBeforeSuiteAnnotation(clazz, methods);
startMethodWithTestAnnotation(clazz, methods);
startMethodWithAfterSuiteAnnotation(clazz, methods);
}

private static void checkAnnotations(Method[] methods) {
int beforeCount = 0;
int afterCount = 0;
for (Method method : methods) {
// BeforeSuite annotation = method.getAnnotation(BeforeSuite.class);
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
// System.out.println("method " + method + " annotation: " + annotation);
if (annotation instanceof BeforeSuite) {
beforeCount++;
}
if (annotation instanceof AfterSuite) {
afterCount++;
}
}
if (beforeCount > 1 || afterCount > 1) {
throw new RuntimeException();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

при бросании своих исключений всегда описывайте причину ошибки, чтобы потом легче было понимать её

}
}
}

private static void startMethodWithAfterSuiteAnnotation(Class clazz, Method[] methods) {


for (Method method : methods) {
AfterSuite asAnnotaion = method.getAnnotation(AfterSuite.class);
if (asAnnotaion != null) {
try {
// Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
Constructor<TestExJU4TestCase> defaultConstructor = clazz.getDeclaredConstructor();
TestExJU4TestCase testClass = defaultConstructor.newInstance();
method.invoke(testClass);
// method.invoke(clazz.newInstance());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}

private static void startMethodWithTestAnnotation(Class clazz, Method[] methods) {
for (Method method : methods) {
Test testAnnotaion = method.getAnnotation(Test.class);
if (testAnnotaion != null) {
try {
// Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
Constructor<TestExJU4TestCase> defaultConstructor = clazz.getDeclaredConstructor();
TestExJU4TestCase testClass = defaultConstructor.newInstance();
method.invoke(testClass);
// method.invoke(clazz.newInstance());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}

private static void startMethodWithBeforeSuiteAnnotation(Class clazz, Method[] methods) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 одинаковых метода, отличаются лишь типом аннотации. Лучше использовать один обобщенный метод, где обобщение - тип аннотации

for (Method method : methods) {
BeforeSuite bsAnnotaion = method.getAnnotation(BeforeSuite.class);
if (bsAnnotaion != null) {
try {
// Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
Constructor<TestExJU4TestCase> defaultConstructor = clazz.getDeclaredConstructor();
TestExJU4TestCase testClass = defaultConstructor.newInstance();
method.invoke(testClass);
// method.invoke(clazz.newInstance());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/lesson7/test_framework/my_annotations/AfterSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package lesson7.test_framework.my_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 {
}
11 changes: 11 additions & 0 deletions src/lesson7/test_framework/my_annotations/BeforeSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package lesson7.test_framework.my_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 src/lesson7/test_framework/my_annotations/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lesson7.test_framework.my_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 {

}
Loading