diff --git a/build.gradle b/build.gradle index eaa235005..47f1f119b 100644 --- a/build.gradle +++ b/build.gradle @@ -57,7 +57,7 @@ dependencies { compileOnly fileTree(dir: sonarLibraries, include: '**/*.jar') testCompile fileTree(dir: sonarLibraries, include: '**/*.jar') testCompile group: 'junit', name: 'junit', version: '4.12' - testCompile group: 'org.mockito', name: 'mockito-core', version: '2.24.0' + testCompile group: 'org.mockito', name: 'mockito-core', version: '3.1.0' zip "sonarqube:sonarqube:${sonarqubeVersion}@zip" } diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/classloader/ClassReferenceElevatedClassLoaderFactoryTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/classloader/ClassReferenceElevatedClassLoaderFactoryTest.java index 11926450c..9292ec85b 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/classloader/ClassReferenceElevatedClassLoaderFactoryTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/classloader/ClassReferenceElevatedClassLoaderFactoryTest.java @@ -30,7 +30,11 @@ import java.io.File; import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -43,6 +47,7 @@ */ public class ClassReferenceElevatedClassLoaderFactoryTest { + private static final String SVN_PLUGIN_CLASS = "org.sonar.plugins.scm.svn.SvnPlugin"; private final ExpectedException expectedException = ExpectedException.none(); @Rule @@ -62,17 +67,15 @@ public void testExceptionOnNoSuchClass() { } @Test - public void testClassloaderReturnedOnHappyPath() { - ClassReferenceElevatedClassLoaderFactory testCase = - spy(new ClassReferenceElevatedClassLoaderFactory(getClass().getName())); - testCase.createClassLoader(((Plugin) context -> { - }).getClass()); + public void testClassloaderReturnedOnHappyPath() throws ReflectiveOperationException, MalformedURLException { + URLClassLoader mockClassLoader = new URLClassLoader(findSonarqubePluginJars()); + ElevatedClassLoaderFactory testCase = spy(new ClassReferenceElevatedClassLoaderFactory(getClass().getName())); + testCase.createClassLoader((Class) mockClassLoader.loadClass(SVN_PLUGIN_CLASS)); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ClassLoader.class); verify(testCase).createClassLoader(argumentCaptor.capture(), argumentCaptor.capture()); - assertEquals(Arrays.asList(getClass().getClassLoader(), getClass().getClassLoader()), - argumentCaptor.getAllValues()); + assertEquals(Arrays.asList(mockClassLoader, getClass().getClassLoader()), argumentCaptor.getAllValues()); } @Test @@ -85,17 +88,14 @@ public void testLoadClass() throws ClassNotFoundException, MalformedURLException builder.setParent("_customPlugin", "_api_", new Mask()); builder.setLoadingOrder("_customPlugin", ClassloaderBuilder.LoadingOrder.SELF_FIRST); - File[] sonarQubeDistributions = new File("sonarqube-lib/").listFiles(); - - for (File pluginJar : new File(sonarQubeDistributions[0], "extensions/plugins/").listFiles()) { - builder.addURL("_customPlugin", pluginJar.toURI().toURL()); + for (URL pluginUrl : findSonarqubePluginJars()) { + builder.addURL("_customPlugin", pluginUrl); } Map loaders = builder.build(); ClassLoader classLoader = loaders.get("_customPlugin"); - Class loadedClass = - (Class) classLoader.loadClass("org.sonar.plugins.scm.svn.SvnPlugin"); + Class loadedClass = (Class) classLoader.loadClass(SVN_PLUGIN_CLASS); ClassReferenceElevatedClassLoaderFactory testCase = new ClassReferenceElevatedClassLoaderFactory(ActiveRule.class.getName()); @@ -105,4 +105,14 @@ public void testLoadClass() throws ClassNotFoundException, MalformedURLException assertNotSame(elevatedLoader, elevatedClass.getClassLoader()); } + private static URL[] findSonarqubePluginJars() throws MalformedURLException { + List pluginUrls = new ArrayList<>(); + File[] sonarQubeDistributions = new File("sonarqube-lib/").listFiles(); + + for (File pluginJar : new File(sonarQubeDistributions[0], "extensions/plugins/").listFiles()) { + pluginUrls.add(pluginJar.toURI().toURL()); + } + return pluginUrls.toArray(new URL[0]); + } + }