-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson7 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mskmrv
wants to merge
4
commits into
master
Choose a base branch
from
lesson7
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lesson7 #7
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f962c66
Добавил логгирование с помощью log4j2.
mskmrv b68cfc9
Сделаны тесты к задаче 1 (по материалаам разбора ДЗ)
mskmrv 515c135
Сделаны тесты к задаче 2 (по материалаам разбора ДЗ)
mskmrv 35f055d
Сначала выполняется метод с аннотацией @BeforeSuite, затем выполняютс…
mskmrv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
src/lesson7/test_framework/my_annotations/BeforeSuite.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
при бросании своих исключений всегда описывайте причину ошибки, чтобы потом легче было понимать её