refactor(接口测试): 优化插件加载单元测试

This commit is contained in:
AgAngle 2023-12-25 17:01:02 +08:00 committed by Craftsman
parent b85aa15910
commit 828f89b130
2 changed files with 23 additions and 12 deletions

View File

@ -70,6 +70,7 @@ public class PluginLoadService {
/** /**
* 从默认的对象存储下载插件到本地再加载 * 从默认的对象存储下载插件到本地再加载
*
* @param fileName * @param fileName
* @return * @return
* @throws Exception * @throws Exception
@ -78,17 +79,15 @@ public class PluginLoadService {
MsFileUtils.validateFileName(fileName); MsFileUtils.validateFileName(fileName);
String filePath = LocalRepositoryDir.getPluginDir() + "/" + fileName; String filePath = LocalRepositoryDir.getPluginDir() + "/" + fileName;
File file = new File(filePath); File file = new File(filePath);
try {
if (!file.exists()) { if (!file.exists()) {
InputStream fileAsStream = FileCenter.getDefaultRepository() try (InputStream fileAsStream = FileCenter.getDefaultRepository().getFileAsStream(getDefaultRepositoryFileRequest(fileName))) {
.getFileAsStream(getDefaultRepositoryFileRequest(fileName));
FileUtils.copyInputStreamToFile(fileAsStream, file); FileUtils.copyInputStreamToFile(fileAsStream, file);
}
msPluginManager.loadPlugin(Paths.get(filePath)); msPluginManager.loadPlugin(Paths.get(filePath));
} catch (Exception e) { } catch (Exception e) {
LogUtils.error("从对象存储加载插件异常", e); LogUtils.error("从对象存储加载插件异常", e);
} }
} }
}
/** /**
* 将插件上传到本地文件系统中 * 将插件上传到本地文件系统中
@ -227,6 +226,7 @@ public class PluginLoadService {
/** /**
* 删除本地插件 * 删除本地插件
*
* @param fileName * @param fileName
*/ */
public void deleteLocalPluginFile(String fileName) { public void deleteLocalPluginFile(String fileName) {
@ -258,9 +258,10 @@ public class PluginLoadService {
/** /**
* 获取插件中的是实现类列表 * 获取插件中的是实现类列表
*
* @param clazz * @param clazz
* @return
* @param <T> * @param <T>
* @return
*/ */
public <T> List<T> getExtensions(Class<T> clazz) { public <T> List<T> getExtensions(Class<T> clazz) {
return msPluginManager.getExtensions(clazz); return msPluginManager.getExtensions(clazz);
@ -268,10 +269,11 @@ public class PluginLoadService {
/** /**
* 获取插件中的是实现类 * 获取插件中的是实现类
*
* @param clazz * @param clazz
* @param pluginId * @param pluginId
* @return
* @param <T> * @param <T>
* @return
*/ */
public <T> Class<? extends T> getExtensionsClass(Class<T> clazz, String pluginId) { public <T> Class<? extends T> getExtensionsClass(Class<T> clazz, String pluginId) {
List<Class<? extends T>> classes = msPluginManager.getExtensionClasses(clazz, pluginId); List<Class<? extends T>> classes = msPluginManager.getExtensionClasses(clazz, pluginId);

View File

@ -5,6 +5,7 @@ import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.PluginScenarioType; import io.metersphere.sdk.constants.PluginScenarioType;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.base.BasePluginTestService; import io.metersphere.system.base.BasePluginTestService;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.param.PluginUpdateRequestDefinition; import io.metersphere.system.controller.param.PluginUpdateRequestDefinition;
@ -136,14 +137,14 @@ public class PluginControllerTests extends BaseTest {
addPlugin = plugin; addPlugin = plugin;
// 模拟其他节点加载插件 // 模拟其他节点加载插件
pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName()); unloadAndDeletePlugin(jarFile, plugin);
pluginLoadService.handlePluginAddNotified(plugin.getId(), jarFile.getName()); pluginLoadService.handlePluginAddNotified(plugin.getId(), jarFile.getName());
// 增加覆盖率 // 增加覆盖率
pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName()); unloadAndDeletePlugin(jarFile, plugin);
pluginLoadService.loadPlugin(jarFile.getName()); pluginLoadService.loadPlugin(jarFile.getName());
pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName()); unloadAndDeletePlugin(jarFile, plugin);
pluginLoadService.loadPlugins(); pluginLoadService.loadPlugins();
pluginLoadService.getExtensions(Platform.class); pluginLoadService.getExtensions(Platform.class);
@ -235,6 +236,14 @@ public class PluginControllerTests extends BaseTest {
requestMultipartPermissionTest(PermissionConstants.SYSTEM_PLUGIN_ADD, DEFAULT_ADD, multiValueMap); requestMultipartPermissionTest(PermissionConstants.SYSTEM_PLUGIN_ADD, DEFAULT_ADD, multiValueMap);
} }
private void unloadAndDeletePlugin(File jarFile, Plugin plugin) {
try {
pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName());
} catch (Exception e) {
LogUtils.error(e);
}
}
@Test @Test
@Order(2) @Order(2)
public void update() throws Exception { public void update() throws Exception {