summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/testing/ClearAfterClass.java
blob: 7f87f9a4d77f069d62a91d5fa289675b97252513 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package scala.tools.testing;

import org.junit.ClassRule;
import org.junit.rules.TestRule;
import org.junit.runners.model.Statement;

import java.io.Closeable;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Extend this class to use JUnit's @ClassRule. This annotation only works on static methods,
 * which cannot be written in Scala.
 *
 * Example: {@link scala.tools.nsc.backend.jvm.opt.InlinerTest}
 */
public class ClearAfterClass {
    private static Map<Class<?>, Map<String, Object>> cache = new ConcurrentHashMap<>();

    @ClassRule
    public static TestRule clearClassCache() {
        return (statement, desc) -> new Statement() {
            @Override
            public void evaluate() throws Throwable {
                ConcurrentHashMap<String, Object> perClassCache = new ConcurrentHashMap<>();
                cache.put(desc.getTestClass(), perClassCache);
                try {
                    statement.evaluate();
                } finally {
                    perClassCache.values().forEach(ClearAfterClass::closeIfClosable);
                    cache.remove(desc.getTestClass());
                }
            }
        };
    }

    private static void closeIfClosable(Object o) {
        if (o instanceof Closeable) {
            try {
                ((Closeable) o).close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T cached(String key, scala.Function0<T> t) {
        Map<String, Object> perClassCache = cache.get(getClass());
        return (T) perClassCache.computeIfAbsent(key, s -> t.apply());
    }

}