From 32ff596c042f0d95ad1b2a575a3867fd6fc775c9 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Thu, 29 Dec 2022 11:36:56 +0100 Subject: [PATCH] [NO-JIRA] Unit test should generate jars in temporary folder --- server/sonar-webserver-api/p1.jar | 0 server/sonar-webserver-api/p2.jar | 0 .../plugins/ServerPluginManagerTest.java | 20 ++++++------------- 3 files changed, 6 insertions(+), 14 deletions(-) delete mode 100644 server/sonar-webserver-api/p1.jar delete mode 100644 server/sonar-webserver-api/p2.jar diff --git a/server/sonar-webserver-api/p1.jar b/server/sonar-webserver-api/p1.jar deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/server/sonar-webserver-api/p2.jar b/server/sonar-webserver-api/p2.jar deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginManagerTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginManagerTest.java index 4164eb7c373..bc2aaabb139 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginManagerTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginManagerTest.java @@ -29,6 +29,7 @@ import org.jetbrains.annotations.NotNull; import org.junit.After; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.api.Plugin; import org.sonar.api.utils.log.LogTester; import org.sonar.core.platform.ExplodedPlugin; @@ -48,6 +49,8 @@ public class ServerPluginManagerTest { @Rule public LogTester logTester = new LogTester(); + @Rule + public TemporaryFolder temp = new TemporaryFolder(); private PluginClassLoader pluginClassLoader = mock(PluginClassLoader.class); private PluginJarExploder jarExploder = mock(PluginJarExploder.class); @@ -61,7 +64,7 @@ public class ServerPluginManagerTest { } @Test - public void load_plugins() { + public void load_plugins() throws IOException { ServerPluginInfo p1 = newPluginInfo("p1"); ServerPluginInfo p2 = newPluginInfo("p2"); when(jarLoader.loadPlugins()).thenReturn(Arrays.asList(p1, p2)); @@ -89,11 +92,11 @@ public class ServerPluginManagerTest { .allMatch(p -> logTester.logs().contains(String.format("Deploy %s / %s / %s", p.getName(), p.getVersion(), p.getImplementationBuild()))); } - private static ServerPluginInfo newPluginInfo(String key) { + private ServerPluginInfo newPluginInfo(String key) throws IOException { ServerPluginInfo pluginInfo = mock(ServerPluginInfo.class); when(pluginInfo.getKey()).thenReturn(key); when(pluginInfo.getType()).thenReturn(EXTERNAL); - when(pluginInfo.getNonNullJarFile()).thenReturn(getJarFile(key)); + when(pluginInfo.getNonNullJarFile()).thenReturn(temp.newFile(key + ".jar")); when(pluginInfo.getName()).thenReturn(key + "_name"); Version version = mock(Version.class); when(version.getName()).thenReturn(key + "_version"); @@ -102,17 +105,6 @@ public class ServerPluginManagerTest { return pluginInfo; } - @NotNull - private static File getJarFile(String key) { - File file = new File(key + ".jar"); - try { - file.createNewFile(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return file; - } - private static FileAndMd5 newFileAndMd5(File file) { return new PluginFilesAndMd5.FileAndMd5(file); } -- 2.39.5