From: Simon Brandhof Date: Tue, 24 Jul 2012 08:17:52 +0000 (+0200) Subject: SONAR-3688 improve message X-Git-Tag: 3.2~9 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c5e1f897110e1defa323508c9476a272721c16a0;p=sonarqube.git SONAR-3688 improve message --- diff --git a/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java b/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java index 2b2c27600be..f4c6c7e87f9 100644 --- a/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java +++ b/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java @@ -19,9 +19,11 @@ */ package org.sonar.core.plugins; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.SystemUtils; import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; @@ -60,12 +62,18 @@ public class PluginClassloaders { private static final String[] PREFIXES_TO_EXPORT = {"org.sonar.plugins.", "com.sonar.plugins.", "com.sonarsource.plugins."}; private static final Logger LOG = LoggerFactory.getLogger(PluginClassloaders.class); - private ClassWorld world = new ClassWorld(); + private ClassWorld world; private ClassLoader baseClassloader; private boolean done = false; public PluginClassloaders(ClassLoader baseClassloader) { + this(baseClassloader, new ClassWorld()); + } + + @VisibleForTesting + PluginClassloaders(ClassLoader baseClassloader, ClassWorld world) { this.baseClassloader = baseClassloader; + this.world = world; } public Map init(Collection plugins) { @@ -122,6 +130,9 @@ public class PluginClassloaders { realm.addURL(url); } return realm; + } catch (UnsupportedClassVersionError e) { + throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e); + } catch (Throwable e) { throw new SonarException("Fail to build the classloader of " + plugin.getKey(), e); } @@ -143,8 +154,11 @@ public class PluginClassloaders { base.addURL(file.toURI().toURL()); } return true; + } catch (UnsupportedClassVersionError e) { + throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e); + } catch (Throwable e) { - throw new SonarException("Fail to extend the plugin " + plugin.getBasePlugin() + " for " + plugin.getKey(), e); + throw new SonarException("Fail to extend the plugin " + plugin.getBasePlugin() + " with " + plugin.getKey(), e); } } @@ -202,15 +216,16 @@ public class PluginClassloaders { } } - public Plugin instantiatePlugin(PluginMetadata metadata) { + public Plugin instantiatePlugin(PluginMetadata plugin) { try { - Class claz = get(metadata.getKey()).loadClass(metadata.getMainClass()); - return (Plugin) claz.newInstance(); + Class clazz = get(plugin.getKey()).loadClass(plugin.getMainClass()); + return (Plugin) clazz.newInstance(); + + } catch (UnsupportedClassVersionError e) { + throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e); } catch (Throwable e) { - // Do not catch only Exception in order to detect the plugins compiled for Java > 5 - // (it raises a java.lang.UnsupportedClassVersionError) - throw new SonarException("Fail to load plugin " + metadata.getKey(), e); + throw new SonarException("Fail to load the plugin " + plugin.getKey(), e); } } @@ -225,7 +240,7 @@ public class PluginClassloaders { } catch (Exception e) { // Ignore } - world=null; + world = null; } } } diff --git a/sonar-core/src/test/java/org/sonar/core/plugins/PluginClassloadersTest.java b/sonar-core/src/test/java/org/sonar/core/plugins/PluginClassloadersTest.java index e26e6e47428..451b0466592 100644 --- a/sonar-core/src/test/java/org/sonar/core/plugins/PluginClassloadersTest.java +++ b/sonar-core/src/test/java/org/sonar/core/plugins/PluginClassloadersTest.java @@ -20,26 +20,34 @@ package org.sonar.core.plugins; import org.apache.commons.io.FileUtils; +import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.Plugin; import org.sonar.api.platform.PluginMetadata; +import org.sonar.api.utils.SonarException; import java.io.File; import java.util.Arrays; import java.util.Map; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class PluginClassloadersTest { private PluginClassloaders classloaders; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void before() { classloaders = new PluginClassloaders(getClass().getClassLoader()); @@ -47,7 +55,9 @@ public class PluginClassloadersTest { @After public void clean() { - classloaders.clean(); + if (classloaders != null) { + classloaders.clean(); + } } @Test @@ -57,23 +67,23 @@ public class PluginClassloadersTest { classloaders.done(); String resourceName = "org/sonar/plugins/bar/api/resource.txt"; - assertThat(classloaders.get("bar").getResourceAsStream(resourceName), notNullValue()); - assertThat(classloaders.get("foo").getResourceAsStream(resourceName), notNullValue()); + assertThat(classloaders.get("bar").getResourceAsStream(resourceName)).isNotNull(); + assertThat(classloaders.get("foo").getResourceAsStream(resourceName)).isNotNull(); } @Test public void shouldCreateBaseClassloader() { classloaders = new PluginClassloaders(getClass().getClassLoader()); DefaultPluginMetadata checkstyle = DefaultPluginMetadata.create(null) - .setKey("checkstyle") - .setMainClass("org.sonar.plugins.checkstyle.CheckstylePlugin") - .addDeployedFile(getFile("sonar-checkstyle-plugin-2.8.jar")); + .setKey("checkstyle") + .setMainClass("org.sonar.plugins.checkstyle.CheckstylePlugin") + .addDeployedFile(getFile("sonar-checkstyle-plugin-2.8.jar")); Map map = classloaders.init(Arrays.asList(checkstyle)); Plugin checkstyleEntryPoint = map.get("checkstyle"); ClassRealm checkstyleRealm = (ClassRealm) checkstyleEntryPoint.getClass().getClassLoader(); - assertThat(checkstyleRealm.getId(), is("checkstyle")); + assertThat(checkstyleRealm.getId()).isEqualTo("checkstyle"); } @Test @@ -81,22 +91,40 @@ public class PluginClassloadersTest { classloaders = new PluginClassloaders(getClass().getClassLoader()); DefaultPluginMetadata checkstyle = DefaultPluginMetadata.create(null) - .setKey("checkstyle") - .setMainClass("org.sonar.plugins.checkstyle.CheckstylePlugin") - .addDeployedFile(getFile("sonar-checkstyle-plugin-2.8.jar")); + .setKey("checkstyle") + .setMainClass("org.sonar.plugins.checkstyle.CheckstylePlugin") + .addDeployedFile(getFile("sonar-checkstyle-plugin-2.8.jar")); DefaultPluginMetadata checkstyleExt = DefaultPluginMetadata.create(null) - .setKey("checkstyle-ext") - .setBasePlugin("checkstyle") - .setMainClass("com.mycompany.sonar.checkstyle.CheckstyleExtensionsPlugin") - .addDeployedFile(getFile("sonar-checkstyle-extensions-plugin-0.1-SNAPSHOT.jar")); + .setKey("checkstyle-ext") + .setBasePlugin("checkstyle") + .setMainClass("com.mycompany.sonar.checkstyle.CheckstyleExtensionsPlugin") + .addDeployedFile(getFile("sonar-checkstyle-extensions-plugin-0.1-SNAPSHOT.jar")); Map map = classloaders.init(Arrays.asList(checkstyle, checkstyleExt)); Plugin checkstyleEntryPoint = map.get("checkstyle"); Plugin checkstyleExtEntryPoint = map.get("checkstyle-ext"); - assertEquals(checkstyleEntryPoint.getClass().getClassLoader(), checkstyleExtEntryPoint.getClass().getClassLoader()); + assertThat(checkstyleEntryPoint.getClass().getClassLoader().equals(checkstyleExtEntryPoint.getClass().getClassLoader())).isTrue(); + } + + @Test + public void detect_plugins_compiled_for_bad_java_version() throws Exception { + thrown.expect(SonarException.class); + thrown.expectMessage("The plugin checkstyle is not supported with Java 1."); + + ClassWorld world = mock(ClassWorld.class); + when(world.newRealm(anyString(), any(ClassLoader.class))).thenThrow(new UnsupportedClassVersionError()); + + classloaders = new PluginClassloaders(getClass().getClassLoader(), world); + + DefaultPluginMetadata checkstyle = DefaultPluginMetadata.create(null) + .setKey("checkstyle") + .setMainClass("org.sonar.plugins.checkstyle.CheckstylePlugin") + .addDeployedFile(getFile("sonar-checkstyle-plugin-2.8.jar")); + + classloaders.init(Arrays.asList(checkstyle)); } private File getFile(String filename) {