From 3afe7a9001453a7106c488a1429e3bff91c8545f Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 13 Oct 2015 22:49:56 +0200 Subject: Improve message when trying to install a non-plugin JAR --- .../main/java/org/sonar/core/platform/PluginInfo.java | 9 +++++++-- .../java/org/sonar/core/platform/PluginInfoTest.java | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'sonar-core') diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java b/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java index 5d2759332eb..3ae4dc4543c 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java @@ -27,6 +27,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ComparisonChain; import com.google.common.collect.Ordering; import java.io.File; +import java.io.IOException; import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; @@ -34,6 +35,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; +import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Loggers; import org.sonar.updatecenter.common.PluginManifest; import org.sonar.updatecenter.common.Version; @@ -137,7 +139,7 @@ public class PluginInfo implements Comparable { private final Set requiredPlugins = new HashSet<>(); public PluginInfo(String key) { - Preconditions.checkNotNull(key); + Preconditions.checkNotNull(key, "Plugin key is missing from manifest"); this.key = key; this.name = key; } @@ -364,13 +366,16 @@ public class PluginInfo implements Comparable { PluginManifest manifest = new PluginManifest(jarFile); return create(jarFile, manifest); - } catch (Exception e) { + } catch (IOException e) { throw new IllegalStateException("Fail to extract plugin metadata from file: " + jarFile, e); } } @VisibleForTesting static PluginInfo create(File jarFile, PluginManifest manifest) { + if (StringUtils.isBlank(manifest.getKey())) { + throw MessageException.of(String.format("File is not a plugin. Please delete it and restart: %s", jarFile.getAbsolutePath())); + } PluginInfo info = new PluginInfo(manifest.getKey()); info.setJarFile(jarFile); diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java index 35893ede34e..1472639b0c6 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java @@ -28,7 +28,10 @@ import org.apache.commons.io.FileUtils; import org.assertj.core.api.Assertions; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.sonar.api.utils.MessageException; +import org.sonar.api.utils.ZipUtils; import org.sonar.updatecenter.common.PluginManifest; import org.sonar.updatecenter.common.Version; @@ -41,6 +44,9 @@ public class PluginInfoTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Test public void test_RequiredPlugin() throws Exception { PluginInfo.RequiredPlugin plugin = PluginInfo.RequiredPlugin.parse("java:1.1"); @@ -212,7 +218,20 @@ public class PluginInfoTest { public void l10n_plugins_should_not_extend_english_plugin() { PluginInfo pluginInfo = new PluginInfo("l10nfr").setBasePlugin("l10nen"); assertThat(pluginInfo.getBasePlugin()).isNull(); + } + + @Test + public void fail_when_jar_is_not_a_plugin() throws IOException { + // this JAR has a manifest but is not a plugin + File jarRootDir = temp.newFolder(); + FileUtils.write(new File(jarRootDir, "META-INF/MANIFEST.MF"), "Build-Jdk: 1.6.0_15"); + File jar = temp.newFile(); + ZipUtils.zipDir(jarRootDir, jar); + + expectedException.expect(MessageException.class); + expectedException.expectMessage("File is not a plugin. Please delete it and restart: " + jar.getAbsolutePath()); + PluginInfo.create(jar); } PluginInfo withMinSqVersion(@Nullable String version) { -- cgit v1.2.3