/
ConfigurationsTest.java
104 lines (86 loc) · 3.39 KB
/
ConfigurationsTest.java
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
96
97
98
99
100
101
102
103
104
package net.coobird.thumbnailator.util;
import org.junit.*;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import java.net.URL;
import java.net.URLClassLoader;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(Enclosed.class)
public class ConfigurationsTest {
@Ignore
private static class Base {
@Before
public void resetConfiguration() {
Configurations.init();
}
@AfterClass
public static void clearConfiguration() {
Configurations.clear();
}
}
@RunWith(AllTruePropertiesTest.CustomRunner.class)
public static class AllTruePropertiesTest extends Base {
@Test
public void test() {
for (Configurations config : Configurations.values()) {
assertTrue(config.getBoolean());
}
}
public static class CustomRunner extends ThreadContextClassLoaderReplacingClassRunner {
public CustomRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
protected ClassLoader getCustomClassLoader() {
return new PropertiesFileRedirectingClassLoader("Configurations/all_true.txt");
}
}
}
@RunWith(AllFalsePropertiesTest.CustomRunner.class)
public static class AllFalsePropertiesTest extends Base {
@Test
public void test() {
for (Configurations config : Configurations.values()) {
assertFalse(config.getBoolean());
}
}
public static class CustomRunner extends ThreadContextClassLoaderReplacingClassRunner {
public CustomRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
protected ClassLoader getCustomClassLoader() {
return new PropertiesFileRedirectingClassLoader("Configurations/all_false.txt");
}
}
}
public abstract static class ThreadContextClassLoaderReplacingClassRunner extends BlockJUnit4ClassRunner {
public ThreadContextClassLoaderReplacingClassRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
protected abstract ClassLoader getCustomClassLoader();
@Override
public void run(RunNotifier notifier) {
Thread.currentThread().setContextClassLoader(getCustomClassLoader());
super.run(notifier);
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
}
}
@Ignore
public static class PropertiesFileRedirectingClassLoader extends URLClassLoader {
private final String resourceForPropertiesFile;
public PropertiesFileRedirectingClassLoader(String resourceForPropertiesFile) {
super(new URL[] {});
this.resourceForPropertiesFile = resourceForPropertiesFile;
}
@Override
public URL getResource(String name) {
if (name.startsWith("thumbnailator.properties")) {
return ClassLoader.getSystemClassLoader().getResource(resourceForPropertiesFile);
}
return super.getResource(name);
}
}
}