/
PluginFinderTest.java
138 lines (106 loc) · 3.73 KB
/
PluginFinderTest.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2017 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.plugins;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import java.io.File;
import java.net.URL;
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.internal.util.io.IOUtil;
import org.mockito.plugins.PluginSwitch;
import org.mockitoutil.TestBase;
public class PluginFinderTest extends TestBase {
@Mock PluginSwitch switcher;
@InjectMocks PluginFinder finder;
public @Rule TemporaryFolder tmp = new TemporaryFolder();
@Test
public void empty_resources() {
assertNull(finder.findPluginClass(Collections.<URL>emptyList()));
}
@Test
public void no_valid_impl() throws Exception {
File f = tmp.newFile();
// when
IOUtil.writeText(" \n ", f);
// then
assertNull(finder.findPluginClass(asList(f.toURI().toURL())));
}
@Test
public void single_implementation() throws Exception {
File f = tmp.newFile();
when(switcher.isEnabled("foo.Foo")).thenReturn(true);
// when
IOUtil.writeText(" foo.Foo ", f);
// then
assertEquals("foo.Foo", finder.findPluginClass(asList(f.toURI().toURL())));
}
@Test
public void single_implementation_disabled() throws Exception {
File f = tmp.newFile();
when(switcher.isEnabled("foo.Foo")).thenReturn(false);
// when
IOUtil.writeText(" foo.Foo ", f);
// then
assertEquals(null, finder.findPluginClass(asList(f.toURI().toURL())));
}
@Test
public void multiple_implementations_only_one_enabled() throws Exception {
File f1 = tmp.newFile();
File f2 = tmp.newFile();
when(switcher.isEnabled("Bar")).thenReturn(true);
// when
IOUtil.writeText("Foo", f1);
IOUtil.writeText("Bar", f2);
// then
assertEquals("Bar", finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL())));
}
@Test
public void multiple_implementations_only_one_useful() throws Exception {
File f1 = tmp.newFile();
File f2 = tmp.newFile();
when(switcher.isEnabled(anyString())).thenReturn(true);
// when
IOUtil.writeText(" ", f1);
IOUtil.writeText("X", f2);
// then
assertEquals("X", finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL())));
}
@Test
public void multiple_empty_implementations() throws Exception {
File f1 = tmp.newFile();
File f2 = tmp.newFile();
when(switcher.isEnabled(anyString())).thenReturn(true);
// when
IOUtil.writeText(" ", f1);
IOUtil.writeText("\n", f2);
// then
assertEquals(null, finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL())));
}
@Test
public void problems_loading_impl() throws Exception {
String fileName = "xxx";
File f = tmp.newFile(fileName);
// when
IOUtil.writeText("Bar", f);
when(switcher.isEnabled(anyString())).thenThrow(new RuntimeException("Boo!"));
try {
// when
finder.findPluginClass(asList(f.toURI().toURL()));
// then
fail();
} catch (Exception e) {
assertThat(e).hasMessageContaining(fileName);
assertThat(e.getCause()).hasMessage("Boo!");
}
}
}