|
| 1 | +package pl.wavesoftware.testing; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.rules.TestRule; |
| 5 | +import org.junit.runner.Description; |
| 6 | +import org.junit.runners.model.Statement; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.lang.reflect.Modifier; |
| 11 | +import java.net.URISyntaxException; |
| 12 | +import java.nio.file.FileVisitResult; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.nio.file.SimpleFileVisitor; |
| 17 | +import java.nio.file.attribute.BasicFileAttributes; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a> |
| 21 | + * @since 25.03.16 |
| 22 | + */ |
| 23 | +public class JmhCleaner implements TestRule { |
| 24 | + private static final String GENERATED_TEST_SOURCES = "generated-test-sources"; |
| 25 | + private static final String TEST_ANNOTATIONS = "test-annotations"; |
| 26 | + private final Class<?> testClass; |
| 27 | + |
| 28 | + public JmhCleaner(Class<?> testClass) { |
| 29 | + this.testClass = validateTestClass(testClass); |
| 30 | + } |
| 31 | + |
| 32 | + private Class<?> validateTestClass(Class<?> testClass) { |
| 33 | + boolean hasTests = false; |
| 34 | + for (Method method : testClass.getDeclaredMethods()) { |
| 35 | + Test annot = method.getAnnotation(Test.class); |
| 36 | + if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && annot != null) { |
| 37 | + hasTests = true; |
| 38 | + break; |
| 39 | + } |
| 40 | + } |
| 41 | + if (!hasTests) { |
| 42 | + throw new IllegalArgumentException("You need to pass a test class to constructor of JmhCleaner!!"); |
| 43 | + } |
| 44 | + return testClass; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + @Override |
| 49 | + public Statement apply(final Statement base, Description description) { |
| 50 | + |
| 51 | + return new Statement() { |
| 52 | + @Override |
| 53 | + public void evaluate() throws Throwable { |
| 54 | + try { |
| 55 | + base.evaluate(); |
| 56 | + } finally { |
| 57 | + cleanup(); |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + private void cleanup() throws IOException, URISyntaxException { |
| 64 | + String location = testClass.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); |
| 65 | + Path testAnnotationsPath = Paths.get(location).getParent().resolve(GENERATED_TEST_SOURCES).resolve(TEST_ANNOTATIONS); |
| 66 | + if (!testAnnotationsPath.toFile().isDirectory()) { |
| 67 | + return; |
| 68 | + } |
| 69 | + Files.walkFileTree(testAnnotationsPath, new SimpleFileVisitor<Path>() { |
| 70 | + @Override |
| 71 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 72 | + Files.delete(file); |
| 73 | + return FileVisitResult.CONTINUE; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 78 | + Files.delete(dir); |
| 79 | + return FileVisitResult.CONTINUE; |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | +} |
0 commit comments