aboutsummaryrefslogtreecommitdiff
path: root/test/core/src/lombok/ReflectionFileTester.java
blob: 32de9c54479121160df5d8e4d59906bd973c90eb (plain)
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
55
56
57
58
59
60
package lombok;

import java.io.File;
import java.lang.reflect.Method;

import org.junit.Test;

public class ReflectionFileTester {
	
	private final File before;
	private final File after;
	
	public ReflectionFileTester(String beforePath, String afterPath) {
		before = new File(beforePath);
		after = new File(afterPath);
		if (!before.isDirectory()) {
			throw new IllegalArgumentException(beforePath + " is not a directory");
		}
		if (!after.isDirectory()) {
			throw new IllegalArgumentException(afterPath + " is not a directory");
		}
	}
	
	public boolean verify(Class<?> clazz) {
		boolean result = true;
		for (File f : before.listFiles()) {
			String fileName = f.getName();
			if (!fileName.endsWith(".java")) {
				continue;
			}
			String methodName = "test" + fileName.substring(0, fileName.length() - 5);
			try {
				Method method = clazz.getDeclaredMethod(methodName);
				if (method.getAnnotation(Test.class) == null) {
					result = false;
					System.err.printf("Class %s method %s is not a @Test method\n", clazz.getName(), methodName);
				}
			} 
			catch (NoSuchMethodException e) {
				result = false;
				System.err.printf("Class %s has no method %s\n", clazz.getName(), methodName);
			}
		}
		return result;
	}
	
	public void test() throws Exception {
		StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		if (stackTrace.length < 3) {
			throw new Error("No stacktrace available");
		}
		String methodName = stackTrace[2].getMethodName();
		if (!methodName.startsWith("test")) {
			throw new IllegalStateException("test() should be called from a methos that starts with 'test'");
		}
		String fileName = methodName.substring(4).concat(".java");
		File testFile = new File(before, fileName);
		TestViaDelombok.compareFile(after, testFile);
	}
}