This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new df6d9c7f6 add load jar plugin unit test (#4523)
df6d9c7f6 is described below
commit df6d9c7f684f73d33521dfec906c5bc2659122f9
Author: 杨文杰 <[email protected]>
AuthorDate: Mon Apr 3 14:39:48 2023 +0800
add load jar plugin unit test (#4523)
Co-authored-by: moremind <[email protected]>
---
.../shenyu/web/loader/ShenyuPluginLoader.java | 10 ++++---
.../shenyu/web/loader/ShenyuPluginLoaderTest.java | 32 ++++++++++++++++++----
.../src/test/resources/plugin-jar-base64.txt | 1 +
3 files changed, 34 insertions(+), 9 deletions(-)
diff --git
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
index 0b254a809..86055205e 100644
---
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
+++
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
@@ -141,10 +141,12 @@ public final class ShenyuPluginLoader extends ClassLoader
implements Closeable {
names.forEach(className -> {
Object instance;
try {
- instance = getOrCreateSpringBean(className);
- if (Objects.nonNull(instance)) {
- results.add(buildResult(instance));
- LOG.info("The class successfully loaded into a ext-plugin
{} is registered as a spring bean", className);
+ if (!uploadedJarClassByteArrayCache.containsKey(className)) {
+ instance = getOrCreateSpringBean(className);
+ if (Objects.nonNull(instance)) {
+ results.add(buildResult(instance));
+ LOG.info("The class successfully loaded into a
ext-plugin {} is registered as a spring bean", className);
+ }
}
} catch (ClassNotFoundException | IllegalAccessException |
InstantiationException e) {
LOG.warn("Registering ext-plugins succeeds spring bean
fails:{}", className);
diff --git
a/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
b/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
index ca5e3e3d6..799857bf2 100644
---
a/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
+++
b/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.web.loader;
import com.google.common.collect.Lists;
+import com.google.common.io.CharStreams;
import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Test;
@@ -33,10 +34,15 @@ import org.springframework.stereotype.Service;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
+import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
@@ -58,11 +64,11 @@ public final class ShenyuPluginLoaderTest {
@TempDir
private static Path folder;
-
+
private ShenyuPluginLoader shenyuPluginLoader;
private Path path;
-
+
@BeforeEach
public void setUp() throws IOException, NoSuchFieldException,
IllegalAccessException {
shenyuPluginLoader = ShenyuPluginLoader.getInstance();
@@ -86,12 +92,13 @@ public final class ShenyuPluginLoaderTest {
ApplicationContext mockApplication =
mock(ApplicationContext.class);
when(mockApplication.getBean("dividePlugin")).thenReturn(new Object());
+ when(mockApplication.getBean("customPlugin")).thenReturn(new Object());
when(mockApplication.getAutowireCapableBeanFactory()).thenReturn(mock(DefaultListableBeanFactory.class));
when(mockApplication.containsBean("dividePlugin")).thenReturn(true);
SpringBeanUtils instance = SpringBeanUtils.getInstance();
instance.setApplicationContext(mockApplication);
}
-
+
@Test
public void testGetBean() {
boolean exist =
SpringBeanUtils.getInstance().existBean("dividePlugin");
@@ -99,12 +106,12 @@ public final class ShenyuPluginLoaderTest {
Object dividePlugin =
SpringBeanUtils.getInstance().getBean("dividePlugin");
assertNotNull(dividePlugin);
}
-
+
@Test
public void testGetInstance() {
assertThat(shenyuPluginLoader, is(ShenyuPluginLoader.getInstance()));
}
-
+
@Test
public void testGetPluginPathWithNoJar() throws IOException {
List<ShenyuLoaderResult> pluginList =
shenyuPluginLoader.loadExtendPlugins("test");
@@ -157,6 +164,21 @@ public final class ShenyuPluginLoaderTest {
loader.close();
}
+ @Test
+ public void testLoadUploadPluginJar() throws IOException,
NoSuchFieldException, IllegalAccessException {
+ testLoadSameJarTwice();
+ ShenyuPluginLoader loader = ShenyuPluginLoader.getInstance();
+ Field field = ShenyuPluginLoader.class.getDeclaredField("names");
+ field.setAccessible(true);
+ field.set(loader, new HashSet<>());
+ InputStream is =
this.getClass().getResourceAsStream("/plugin-jar-base64.txt");
+ assertNotNull(is);
+ String jarTxt = CharStreams.toString(new InputStreamReader(is,
StandardCharsets.UTF_8));
+ List<ShenyuLoaderResult> shenyuLoaderResults =
loader.loadUploadedJarPlugins(Collections.singletonList(jarTxt));
+ assertThat(shenyuLoaderResults.size(), is(1));
+ loader.close();
+ }
+
@Component
public static class TestComponent {
}
diff --git a/shenyu-web/src/test/resources/plugin-jar-base64.txt
b/shenyu-web/src/test/resources/plugin-jar-base64.txt
new file mode 100644
index 000000000..51b8a2f8a
--- /dev/null
+++ b/shenyu-web/src/test/resources/plugin-jar-base64.txt
@@ -0,0 +1 @@
+UEsDBAoAAAAAAOp0e1YAAAAAAAAAAAAAAAAJAAAATUVUQS1JTkYvUEsDBAoAAAAIAOl0e1YncjDccwAAAIkAAAAUAAAATUVUQS1JTkYvTUFOSUZFU1QuTUbzTczLTEstLtENSy0qzszPs1Iw1DPg5XIsSs7ILEstQggH5KRWlBYrwCR4uZxKM3NKdJ0qrRTKU/OyMlP1KhPz0nm5nItSE0tSU8ASjgWJyRmpCr6JZal5CsZ6FnpmEG0pul4p2SCbLPQM4o0MjHi5eLkAUEsDBAoAAAAAAOl0e1YAAAAAAAAAAAAAAAAEAAAAb3JnL1BLAwQKAAAAAADpdHtWAAAAAAAAAAAAAAAACwAAAG9yZy9hcGFjaGUvUEsDBAoAAAAAAOl0e1YAAAAAAAAAAAAAAAASAAAAb3JnL2FwYWNoZS9zaGVueXUvUEsDBAoAAAAAAOl0e1YAAAAAAAAAAAAAAAAZAAAAb3JnL2FwYWNoZS9z
[...]
\ No newline at end of file