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
7 changes: 7 additions & 0 deletions .travis.yml
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
31 changes: 31 additions & 0 deletions 05.XUnit/pom.xml
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>
54 changes: 54 additions & 0 deletions 05.XUnit/src/main/java/ru/spbau/mit/alyokhina/Main.java
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 05.XUnit/src/main/java/ru/spbau/mit/alyokhina/TestExecutor.java
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));
Copy link

Choose a reason for hiding this comment

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

хорошо было бы проверить, что на метод не навесили более одной аннотации

Copy link
Owner Author

Choose a reason for hiding this comment

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

image

Это проверяется в функции getMethods

}

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());
}
}
}


}
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 05.XUnit/src/main/java/ru/spbau/mit/alyokhina/TestResult.java
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();
}
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 {
}
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 {
}
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 {
}
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 {
}
Loading