-
Notifications
You must be signed in to change notification settings - Fork 0
XUnit #9
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
ilkivoo
wants to merge
4
commits into
master
Choose a base branch
from
hw5.XUnit
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
XUnit #9
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,7 @@ | ||
| language: java | ||
| jdk: | ||
| - oraclejdk8 | ||
| os: | ||
| - linux | ||
| script: | ||
| - chmod +x buildscript.sh && ./buildscript.sh |
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,31 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>XUnit</groupId> | ||
| <artifactId>XUnit</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
|
||
| </project> |
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,54 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| /** | ||
| * Class for print tests results | ||
| */ | ||
| public class Main { | ||
| /** | ||
| * print result | ||
| * @param args class name | ||
| */ | ||
| public static void main(String[] args) { | ||
| if (args.length != 1) { | ||
| System.out.println("Expected class name"); | ||
| return; | ||
| } | ||
| try { | ||
| TestExecutor testExecutor = new TestExecutor(Class.forName(args[0])); | ||
|
|
||
| List<TestResult> results = testExecutor.run(); | ||
| int all = 0, success = 0, fail = 0, ignored = 0; | ||
| for (TestResult testResult : results) { | ||
| System.out.print("Class: " + testResult.getClassName() + ", Test: " + testResult.getTestName() + ", Time: " + ((Long) testResult.getTime()).toString()); | ||
| all++; | ||
| if (testResult.isFail()) { | ||
| fail++; | ||
| System.out.print(" -- Failed: "); | ||
| if (testResult.getException() != null) { | ||
| System.out.println(testResult.getException().getMessage()); | ||
| } else { | ||
| System.out.println("Expected exception not received"); | ||
| } | ||
| } else { | ||
| if (testResult.causeOfIgnoring().equals("")) { | ||
| success++; | ||
| System.out.println(" -- Success"); | ||
| } else { | ||
| ignored++; | ||
| System.out.println(" -- Ignored: " + testResult.causeOfIgnoring()); | ||
| } | ||
| } | ||
| System.out.println("All: " + ((Integer) all).toString() + ", success: " + ((Integer) success).toString() + | ||
| ", fail: " + ((Integer) fail).toString() + ", ignored: " + ((Integer) ignored).toString()); | ||
| } | ||
| } catch (InstantiationException | IllegalAccessException | TestExecutorException | | ||
| InvocationTargetException | ClassNotFoundException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| } |
195 changes: 195 additions & 0 deletions
195
05.XUnit/src/main/java/ru/spbau/mit/alyokhina/TestExecutor.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,195 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import ru.spbau.mit.alyokhina.annotation.*; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** Class for tests run */ | ||
| public class TestExecutor { | ||
| /** Instance of class for calling tests */ | ||
| private Object instance; | ||
|
|
||
| /** Class name for test runs */ | ||
| private String testClassName; | ||
|
|
||
| /** Methods with annotation Before */ | ||
| private List<Method> before = new ArrayList<>(); | ||
|
|
||
| /** Methods with annotation BeforeClass */ | ||
| private List<Method> beforeClass = new ArrayList<>(); | ||
|
|
||
| /** Methods with annotation After */ | ||
| private List<Method> after = new ArrayList<>(); | ||
|
|
||
| /** Methods with annotation AfterClass */ | ||
| private List<Method> afterClass = new ArrayList<>(); | ||
|
|
||
| /** Methods with annotation Test */ | ||
| private List<Method> tests = new ArrayList<>(); | ||
|
|
||
|
|
||
| /** | ||
| * Constructor | ||
| * @param clazz class from which tests will be run | ||
| * @throws IllegalAccessException if newInstance threw IllegalAccessException | ||
| * @throws InstantiationException if newInstance threw InstantiationException | ||
| * @throws TestExecutorException if if wrong number methods with annotation | ||
| */ | ||
| public TestExecutor(Class<?> clazz) throws IllegalAccessException, InstantiationException, TestExecutorException { | ||
| getMethods(clazz); | ||
| instance = clazz.newInstance(); | ||
| testClassName = clazz.getName(); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Run tests | ||
| * @return list of results of each test | ||
| * @throws InvocationTargetException if we catch exception from the BeforeClass or the AfterClass | ||
| * @throws IllegalAccessException if invoke threw InvocationTargetException | ||
| */ | ||
| public List<TestResult> run() throws InvocationTargetException, IllegalAccessException { | ||
| List<TestResult> results = new ArrayList<>(); | ||
| if (beforeClass.size() != 0) { | ||
| beforeClass.get(0).setAccessible(true); | ||
| beforeClass.get(0).invoke(instance); | ||
| } | ||
|
|
||
| for (Method method : tests) { | ||
| method.setAccessible(true); | ||
| results.add(invoke(method)); | ||
| } | ||
|
|
||
| if (afterClass.size() != 0) { | ||
| afterClass.get(0).setAccessible(true); | ||
| afterClass.get(0).invoke(instance); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * | ||
| * @param time test run time | ||
| * @param className class name for test | ||
| * @param testName test name | ||
| * @param isFail test failure | ||
| * @param causeOfIgnoring reason for which the test was ignored | ||
| * @param e Exception that was thrown by the test | ||
| * @return information about test in interface TestResult | ||
| */ | ||
| private TestResult getResult(final long time, final String className, final String testName, | ||
| final boolean isFail, final String causeOfIgnoring, final Exception e) { | ||
| return new TestResult() { | ||
| @Override | ||
| public long getTime() { | ||
| return time; | ||
| } | ||
|
|
||
| @Override | ||
| public String getClassName() { | ||
| return className; | ||
| } | ||
|
|
||
| @Override | ||
| public String getTestName() { | ||
| return testName; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isFail() { | ||
| return isFail; | ||
| } | ||
|
|
||
| @Override | ||
| public String causeOfIgnoring() { | ||
| return causeOfIgnoring; | ||
| } | ||
|
|
||
| @Override | ||
| public Exception getException() { | ||
| return e; | ||
| } | ||
|
|
||
| }; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * invoke method | ||
| * @param method method that will be called | ||
| * @return information about passing the test | ||
| * @throws IllegalAccessException if invoke threw IllegalAccessException | ||
| */ | ||
| private TestResult invoke(final Method method) throws IllegalAccessException { | ||
| final Test testAnnotation = method.getAnnotation(Test.class); | ||
|
|
||
| if (!testAnnotation.ignore().equals("")) { | ||
| return getResult(0, testClassName, method.getName(), false, testAnnotation.ignore(), null); | ||
| } | ||
|
|
||
| long startTimer = System.currentTimeMillis(); | ||
| Exception exception = null; | ||
| try { | ||
| if (before.size() != 0) { | ||
| before.get(0).setAccessible(true); | ||
| before.get(0).invoke(instance); | ||
| } | ||
| method.invoke(instance); | ||
|
|
||
| if (after.size() != 0) { | ||
| after.get(0).setAccessible(true); | ||
| after.get(0).invoke(instance); | ||
| } | ||
| } catch (InvocationTargetException e) { | ||
| exception = (Exception) e.getCause(); | ||
| } | ||
| long endTimer = System.currentTimeMillis(); | ||
|
|
||
| if ((exception != null && !testAnnotation.expected().isInstance(exception)) || | ||
| (exception == null && !testAnnotation.expected().equals(Test.IgnoredThrowable.class))) { | ||
| return getResult(endTimer - startTimer, testClassName, method.getName(), true, "", exception); | ||
| } | ||
|
|
||
| return getResult(endTimer - startTimer, testClassName, method.getName(), false, "", exception); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Group methods with annotation in class | ||
| * @param testClazz class for test | ||
| * @throws TestExecutorException if wrong number methods with annotation | ||
| */ | ||
| private void getMethods(Class<?> testClazz) throws TestExecutorException { | ||
| Class[] classes = {After.class, AfterClass.class, Before.class, BeforeClass.class, Test.class}; | ||
| List<List<Method>> lists = new ArrayList<>(); | ||
| lists.add(after); | ||
| lists.add(afterClass); | ||
| lists.add(before); | ||
| lists.add(beforeClass); | ||
| lists.add(tests); | ||
| for (Method method : testClazz.getDeclaredMethods()) { | ||
| boolean flag = false; | ||
| for (int i = 0; i < classes.length; i++) { | ||
| if (method.getAnnotation(classes[i]) != null) { | ||
| if (flag) { | ||
| throw new TestExecutorException("too much annotations for method " + method.getName()); | ||
| } | ||
| lists.get(i).add(method); | ||
| flag = true; | ||
| } | ||
| } | ||
| } | ||
| for (int i = 0; i < lists.size() - 1; i++) { | ||
| if (lists.get(i).size() > 1) { | ||
| throw new TestExecutorException("too much methods with annotation " + classes[i].getName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
11 changes: 11 additions & 0 deletions
11
05.XUnit/src/main/java/ru/spbau/mit/alyokhina/TestExecutorException.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 ru.spbau.mit.alyokhina; | ||
|
|
||
| /** | ||
| * Exception for Test Executor | ||
| * Throws if wrong number methods with annotation | ||
| */ | ||
| public class TestExecutorException extends Exception { | ||
| public TestExecutorException(String msg) { | ||
| super(msg); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
05.XUnit/src/main/java/ru/spbau/mit/alyokhina/TestResult.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,22 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| /** Interface for information about passing the test */ | ||
| public interface TestResult { | ||
| /** Time of the test */ | ||
| long getTime(); | ||
|
|
||
| /** The name of the class in which the test was called */ | ||
| String getClassName(); | ||
|
|
||
| /** The test name */ | ||
| String getTestName(); | ||
|
|
||
| /** Test failure */ | ||
| boolean isFail(); | ||
|
|
||
| /** The reason for which the test was ignored */ | ||
| String causeOfIgnoring(); | ||
|
|
||
| /** The exception that was thrown by the test */ | ||
| Exception getException(); | ||
| } |
12 changes: 12 additions & 0 deletions
12
05.XUnit/src/main/java/ru/spbau/mit/alyokhina/annotation/After.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,12 @@ | ||
| package ru.spbau.mit.alyokhina.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** Annotation of the method that will be run after each test */ | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface After { | ||
| } |
12 changes: 12 additions & 0 deletions
12
05.XUnit/src/main/java/ru/spbau/mit/alyokhina/annotation/AfterClass.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,12 @@ | ||
| package ru.spbau.mit.alyokhina.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** Annotation of the method that will be run after the test runs */ | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface AfterClass { | ||
| } |
12 changes: 12 additions & 0 deletions
12
05.XUnit/src/main/java/ru/spbau/mit/alyokhina/annotation/Before.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,12 @@ | ||
| package ru.spbau.mit.alyokhina.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** Annotation of the method that will be run before each test */ | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Before { | ||
| } |
12 changes: 12 additions & 0 deletions
12
05.XUnit/src/main/java/ru/spbau/mit/alyokhina/annotation/BeforeClass.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,12 @@ | ||
| package ru.spbau.mit.alyokhina.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** Annotation of the method that will be run before the test runs */ | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface BeforeClass { | ||
| } |
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.
хорошо было бы проверить, что на метод не навесили более одной аннотации
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.
Это проверяется в функции getMethods