-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
BundleResourceUtilsTest.java
87 lines (73 loc) · 3.2 KB
/
BundleResourceUtilsTest.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
package run.halo.app.plugin.resources;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.pf4j.PluginClassLoader;
import org.pf4j.PluginWrapper;
import org.springframework.core.io.Resource;
import run.halo.app.infra.exception.AccessDeniedException;
import run.halo.app.plugin.HaloPluginManager;
/**
* Tests for {@link BundleResourceUtils}.
*
* @author guqing
* @since 2.0.0
*/
@ExtendWith(MockitoExtension.class)
class BundleResourceUtilsTest {
@Mock
private HaloPluginManager pluginManager;
@BeforeEach
void setUp() throws MalformedURLException {
PluginWrapper pluginWrapper = Mockito.mock(PluginWrapper.class);
PluginClassLoader pluginClassLoader = Mockito.mock(PluginClassLoader.class);
lenient().when(pluginWrapper.getPluginClassLoader()).thenReturn(pluginClassLoader);
lenient().when(pluginManager.getPlugin(eq("fake-plugin"))).thenReturn(pluginWrapper);
lenient().when(pluginClassLoader.getResource(eq("console/main.js"))).thenReturn(
new URL("file://console/main.js"));
lenient().when(pluginClassLoader.getResource(eq("console/style.css"))).thenReturn(
new URL("file://console/style.css"));
}
@Test
void getCssBundlePath() {
String cssBundlePath =
BundleResourceUtils.getCssBundlePath(pluginManager, "nothing-plugin");
assertThat(cssBundlePath).isNull();
cssBundlePath = BundleResourceUtils.getCssBundlePath(pluginManager, "fake-plugin");
assertThat(cssBundlePath).isEqualTo("/plugins/fake-plugin/assets/console/style.css");
}
@Test
void getJsBundlePath() {
String jsBundlePath =
BundleResourceUtils.getJsBundlePath(pluginManager, "nothing-plugin");
assertThat(jsBundlePath).isNull();
jsBundlePath = BundleResourceUtils.getJsBundlePath(pluginManager, "fake-plugin");
assertThat(jsBundlePath).isEqualTo("/plugins/fake-plugin/assets/console/main.js");
}
@Test
void getJsBundleResource() {
Resource jsBundleResource =
BundleResourceUtils.getJsBundleResource(pluginManager, "fake-plugin", "main.js");
assertThat(jsBundleResource).isNotNull();
assertThat(jsBundleResource.exists()).isTrue();
jsBundleResource =
BundleResourceUtils.getJsBundleResource(pluginManager, "fake-plugin", "test.js");
assertThat(jsBundleResource).isNull();
jsBundleResource =
BundleResourceUtils.getJsBundleResource(pluginManager, "nothing-plugin", "main.js");
assertThat(jsBundleResource).isNull();
assertThatThrownBy(() -> {
BundleResourceUtils.getJsBundleResource(pluginManager, "fake-plugin",
"../test/main.js");
}).isInstanceOf(AccessDeniedException.class);
}
}