aboutsummaryrefslogtreecommitdiff
path: root/test/core/src/lombok/DirectoryRunner.java
blob: 40b21aa0a46f6327c23f27d45178fe45c70bcc63 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package lombok;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;

import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

public class DirectoryRunner extends Runner {
	
	private final Description description;
	private final Map<String, Description> tests = new TreeMap<String, Description>();
	private final Throwable failure;
	private File beforeDirectory;
	private File afterDirectory;
	
	public DirectoryRunner(Class<?> testClass) {
		description = Description.createSuiteDescription(testClass);
		Throwable error = null;
		try {
			addTests(testClass);
		}
		catch (Throwable t) {
			error = t;
		}
		this.failure = error;
	}

	private void addTests(Class<?> testClass) throws Exception {
		Method beforeMethod = testClass.getDeclaredMethod("getBeforeDirectory");
		beforeDirectory = (File) beforeMethod.invoke(null);
		
		Method afterMethod = testClass.getDeclaredMethod("getAfterDirectory");
		afterDirectory = (File) afterMethod.invoke(null);
		
		for (File file : beforeDirectory.listFiles()) {
			Description testDescription = Description.createTestDescription(testClass, file.getName());
			description.addChild(testDescription);
			tests.put(file.getName(), testDescription);
		}
	}

	@Override
	public Description getDescription() {
		return description;
	}

	@Override
	public void run(RunNotifier notifier) {
		if (failure != null) {
			notifier.fireTestStarted(description);
			notifier.fireTestFailure(new Failure(description, failure));
			notifier.fireTestFinished(description);
			return;
		}
		
		for (Map.Entry<String, Description> entry : tests.entrySet()) {
			Description testDescription = entry.getValue();
			notifier.fireTestStarted(testDescription);
			try {
				if (!runTest(entry.getKey())) {
					notifier.fireTestIgnored(testDescription);
				}
			}
			catch (Throwable t) {
				notifier.fireTestFailure(new Failure(testDescription, t));
			}
			notifier.fireTestFinished(testDescription);
		}
	}

	private boolean runTest(String fileName) throws Throwable {
		File file = new File(beforeDirectory, fileName);
		if (mustIgnore(file)) {
			return false;
		}
		TestViaDelombok.compareFile(afterDirectory, file);
		return true;
	}

	private boolean mustIgnore(File file) throws FileNotFoundException, IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = reader.readLine();
		reader.close();
		return "//ignore".equals(line);
	}
}