From: Godin Date: Wed, 3 Nov 2010 09:18:50 +0000 (+0000) Subject: Use the same rules everywhere to transform plugin key X-Git-Tag: 2.6~686 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cacf463535b7d680af91d21d3e687956901f94c1;p=sonarqube.git Use the same rules everywhere to transform plugin key --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java index 98d3e114f21..f4ef2689516 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/BatchPluginRepository.java @@ -19,6 +19,10 @@ */ package org.sonar.batch; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; @@ -40,10 +44,6 @@ import org.sonar.core.plugin.JpaPluginFile; import com.google.common.collect.Lists; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.List; - public class BatchPluginRepository extends AbstractPluginRepository { private static final Logger LOG = LoggerFactory.getLogger(BatchPluginRepository.class); @@ -103,16 +103,6 @@ public class BatchPluginRepository extends AbstractPluginRepository { invokeExtensionProviders(pico); } - private String getOldCoveragePluginKey(String pluginKey) { - if (StringUtils.equals("sonar-jacoco-plugin", pluginKey)) { - return "jacoco"; - } - if (StringUtils.equals("sonar-emma-plugin", pluginKey)) { - return "emma"; - } - return null; - } - @Override protected boolean shouldRegisterExtension(PicoContainer container, String pluginKey, Object extension) { boolean ok = isType(extension, BatchExtension.class); @@ -132,8 +122,7 @@ public class BatchPluginRepository extends AbstractPluginRepository { if (ArrayUtils.isEmpty(selectedPluginKeys)) { selectedPluginKeys = new String[] { AbstractCoverageExtension.DEFAULT_PLUGIN }; } - String oldCoveragePluginKey = getOldCoveragePluginKey(pluginKey); - ok = ArrayUtils.contains(selectedPluginKeys, pluginKey) || ArrayUtils.contains(selectedPluginKeys, oldCoveragePluginKey); + ok = ArrayUtils.contains(selectedPluginKeys, pluginKey); } return ok; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/BatchPluginRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/BatchPluginRepositoryTest.java index 02bee6344fe..dacd396b820 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/BatchPluginRepositoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/BatchPluginRepositoryTest.java @@ -19,6 +19,9 @@ */ package org.sonar.batch; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Test; @@ -30,9 +33,6 @@ import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; import org.sonar.api.utils.IocContainer; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - public class BatchPluginRepositoryTest { @Test @@ -57,7 +57,7 @@ public class BatchPluginRepositoryTest { BatchPluginRepository repository = new BatchPluginRepository(); PropertiesConfiguration conf = new PropertiesConfiguration(); assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(), conf), is(true)); - assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(),conf), is(false)); + assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(), conf), is(false)); } @Test @@ -65,36 +65,10 @@ public class BatchPluginRepositoryTest { Configuration conf = new PropertiesConfiguration(); conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "clover,phpunit"); BatchPluginRepository repository = new BatchPluginRepository(); - assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(),conf), is(false)); + assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(), conf), is(false)); assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(), conf), is(true)); - assertThat(repository.shouldRegisterCoverageExtension("phpunit", newJavaProject(),conf), is(true)); - assertThat(repository.shouldRegisterCoverageExtension("other", newJavaProject(),conf), is(false)); - } - - @Test - public void shouldActivateOldVersionOfEmma() { - Configuration conf = new PropertiesConfiguration(); - conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "emma"); - BatchPluginRepository repository = new BatchPluginRepository(); - - assertThat(repository.shouldRegisterCoverageExtension("sonar-emma-plugin", newJavaProject(),conf), is(true)); - assertThat(repository.shouldRegisterCoverageExtension("emma", newJavaProject(),conf), is(true)); - - assertThat(repository.shouldRegisterCoverageExtension("sonar-jacoco-plugin", newJavaProject(),conf), is(false)); - assertThat(repository.shouldRegisterCoverageExtension("jacoco", newJavaProject(),conf), is(false)); - assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(),conf), is(false)); - assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(),conf), is(false)); - } - - @Test - public void shouldActivateOldVersionOfJacoco() { - Configuration conf = new PropertiesConfiguration(); - conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "cobertura,jacoco"); - BatchPluginRepository repository = new BatchPluginRepository(); - - assertThat(repository.shouldRegisterCoverageExtension("sonar-jacoco-plugin", newJavaProject(),conf), is(true)); - assertThat(repository.shouldRegisterCoverageExtension("jacoco", newJavaProject(),conf), is(true)); - assertThat(repository.shouldRegisterCoverageExtension("emma", newJavaProject(),conf), is(false)); + assertThat(repository.shouldRegisterCoverageExtension("phpunit", newJavaProject(), conf), is(true)); + assertThat(repository.shouldRegisterCoverageExtension("other", newJavaProject(), conf), is(false)); } @Test @@ -103,8 +77,8 @@ public class BatchPluginRepositoryTest { conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "cobertura"); BatchPluginRepository repository = new BatchPluginRepository(); - assertThat(repository.shouldRegisterCoverageExtension("groovy", newGroovyProject(),conf), is(true)); - assertThat(repository.shouldRegisterCoverageExtension("groovy", newJavaProject(),conf), is(false)); + assertThat(repository.shouldRegisterCoverageExtension("groovy", newGroovyProject(), conf), is(true)); + assertThat(repository.shouldRegisterCoverageExtension("groovy", newJavaProject(), conf), is(false)); } private static Project newJavaProject() { diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java index b9bde56556c..e07c1519f56 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDeployer.java @@ -19,6 +19,15 @@ */ package org.sonar.server.plugins; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; @@ -34,19 +43,11 @@ import org.sonar.core.plugin.JpaPlugin; import org.sonar.core.plugin.JpaPluginDao; import org.sonar.server.platform.DefaultServerFileSystem; import org.sonar.server.platform.ServerStartException; +import org.sonar.updatecenter.common.PluginKeyUtils; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; - public final class PluginDeployer implements ServerComponent { private static final Logger LOG = LoggerFactory.getLogger(PluginDeployer.class); @@ -229,9 +230,16 @@ public final class PluginDeployer implements ServerComponent { } private File moveDownloadedFile(File jar) { + File destDir = fileSystem.getUserPluginsDir(); + File destFile = new File(destDir, jar.getName()); + if (destFile.exists()) { + // plugin with same filename already installed + FileUtils.deleteQuietly(jar); + return destFile; + } try { - FileUtils.moveFileToDirectory(jar, fileSystem.getUserPluginsDir(), true); - return new File(fileSystem.getUserPluginsDir(), jar.getName()); + FileUtils.moveFileToDirectory(jar, destDir, true); + return new File(destDir, jar.getName()); } catch (IOException e) { LOG.error("Fail to move the downloaded file: " + jar.getAbsolutePath(), e); @@ -259,8 +267,7 @@ public final class PluginDeployer implements ServerComponent { map.remove(pluginKey); Logs.INFO.info("Old plugin " + existing.getFilename() + " replaced by new " + metadata.getFilename()); } else { - throw new ServerStartException("Found two plugins with the same key '" + pluginKey + "': " - + metadata.getFilename() + " and " + throw new ServerStartException("Found two plugins with the same key '" + pluginKey + "': " + metadata.getFilename() + " and " + existing.getFilename()); } } @@ -277,7 +284,7 @@ public final class PluginDeployer implements ServerComponent { try { URLClassLoader pluginClassLoader = URLClassLoader.newInstance(new URL[] { tempFile.toURI().toURL() }, getClass().getClassLoader()); Plugin pluginInstance = (Plugin) pluginClassLoader.loadClass(mainClass).newInstance(); - plugin.setKey(pluginInstance.getKey()); + plugin.setKey(PluginKeyUtils.getPluginKey(pluginInstance.getKey())); plugin.setDescription(pluginInstance.getDescription()); plugin.setName(pluginInstance.getName()); diff --git a/subprojects/sonar-update-center/sonar-packaging-maven-plugin/src/main/java/org/sonar/updatecenter/mavenplugin/AbstractSonarPluginMojo.java b/subprojects/sonar-update-center/sonar-packaging-maven-plugin/src/main/java/org/sonar/updatecenter/mavenplugin/AbstractSonarPluginMojo.java index fd3d4252ab0..eda6b943405 100644 --- a/subprojects/sonar-update-center/sonar-packaging-maven-plugin/src/main/java/org/sonar/updatecenter/mavenplugin/AbstractSonarPluginMojo.java +++ b/subprojects/sonar-update-center/sonar-packaging-maven-plugin/src/main/java/org/sonar/updatecenter/mavenplugin/AbstractSonarPluginMojo.java @@ -19,17 +19,17 @@ */ package org.sonar.updatecenter.mavenplugin; -import org.apache.commons.lang.StringUtils; +import java.io.File; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; - -import java.io.File; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import org.sonar.updatecenter.common.PluginKeyUtils; /** * Base class for Sonar-plugin-packaging related tasks. @@ -37,6 +37,7 @@ import java.util.Set; * @author Evgeny Mandrikov */ public abstract class AbstractSonarPluginMojo extends AbstractMojo { + public static final String SONAR_GROUPID = "org.codehaus.sonar"; public static final String SONAR_PLUGIN_API_ARTIFACTID = "sonar-plugin-api"; public static final String SONAR_PLUGIN_API_TYPE = "jar"; @@ -173,13 +174,7 @@ public abstract class AbstractSonarPluginMojo extends AbstractMojo { } public String getPluginKey() { - if (StringUtils.startsWith(pluginKey, "sonar-") && StringUtils.endsWith(pluginKey, "-plugin")) { - return StringUtils.removeEnd(StringUtils.removeStart(pluginKey, "sonar-"), "-plugin"); - } - if (StringUtils.endsWith(pluginKey, "-sonar-plugin")) { - return StringUtils.removeEnd(pluginKey, "-sonar-plugin"); - } - return StringUtils.remove(pluginKey, "-"); + return PluginKeyUtils.getPluginKey(pluginKey); } protected final String getPluginClass() { @@ -214,7 +209,7 @@ public abstract class AbstractSonarPluginMojo extends AbstractMojo { return skipDependenciesPackaging; } - @SuppressWarnings( { "unchecked" }) + @SuppressWarnings({ "unchecked" }) protected Set getDependencyArtifacts() { return getProject().getDependencyArtifacts(); } @@ -229,7 +224,7 @@ public abstract class AbstractSonarPluginMojo extends AbstractMojo { return result; } - @SuppressWarnings( { "unchecked" }) + @SuppressWarnings({ "unchecked" }) protected Set getIncludedArtifacts() { Set result = new HashSet(); Set artifacts = getProject().getArtifacts(); @@ -246,8 +241,7 @@ public abstract class AbstractSonarPluginMojo extends AbstractMojo { Set dependencies = getDependencyArtifacts(); if (dependencies != null) { for (Artifact dep : dependencies) { - if (SONAR_GROUPID.equals(dep.getGroupId()) - && SONAR_PLUGIN_API_ARTIFACTID.equals(dep.getArtifactId()) + if (SONAR_GROUPID.equals(dep.getGroupId()) && SONAR_PLUGIN_API_ARTIFACTID.equals(dep.getArtifactId()) && SONAR_PLUGIN_API_TYPE.equals(dep.getType())) { return dep; } diff --git a/subprojects/sonar-update-center/sonar-packaging-maven-plugin/src/test/java/org/sonar/updatecenter/mavenplugin/AbstractSonarPluginMojoTest.java b/subprojects/sonar-update-center/sonar-packaging-maven-plugin/src/test/java/org/sonar/updatecenter/mavenplugin/AbstractSonarPluginMojoTest.java deleted file mode 100644 index 27f8acfb396..00000000000 --- a/subprojects/sonar-update-center/sonar-packaging-maven-plugin/src/test/java/org/sonar/updatecenter/mavenplugin/AbstractSonarPluginMojoTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.updatecenter.mavenplugin; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.junit.Test; - -public class AbstractSonarPluginMojoTest { - - @Test - public void shouldExtractPluginKeyFromArtifactId() { - AbstractSonarPluginMojo mojo = new AbstractSonarPluginMojo() { - public void execute() throws MojoExecutionException, MojoFailureException { - } - }; - mojo.pluginKey = "sonar-test-plugin"; - assertThat(mojo.getPluginKey(), is("test")); - mojo.pluginKey = "test-sonar-plugin"; - assertThat(mojo.getPluginKey(), is("test")); - mojo.pluginKey = "test"; - assertThat(mojo.getPluginKey(), is("test")); - mojo.pluginKey = "test-foo"; - assertThat(mojo.getPluginKey(), is("testfoo")); - - } - -} diff --git a/subprojects/sonar-update-center/sonar-update-center-common/src/main/java/org/sonar/updatecenter/common/PluginKeyUtils.java b/subprojects/sonar-update-center/sonar-update-center-common/src/main/java/org/sonar/updatecenter/common/PluginKeyUtils.java new file mode 100644 index 00000000000..3978cac2831 --- /dev/null +++ b/subprojects/sonar-update-center/sonar-update-center-common/src/main/java/org/sonar/updatecenter/common/PluginKeyUtils.java @@ -0,0 +1,20 @@ +package org.sonar.updatecenter.common; + +import org.apache.commons.lang.StringUtils; + +public final class PluginKeyUtils { + + public static String getPluginKey(String pluginKey) { + String key = pluginKey; + if (StringUtils.startsWith(pluginKey, "sonar-") && StringUtils.endsWith(pluginKey, "-plugin")) { + key = StringUtils.removeEnd(StringUtils.removeStart(pluginKey, "sonar-"), "-plugin"); + } else if (StringUtils.endsWith(pluginKey, "-sonar-plugin")) { + key = StringUtils.removeEnd(pluginKey, "-sonar-plugin"); + } + return StringUtils.remove(key, "-"); + } + + private PluginKeyUtils() { + } + +} diff --git a/subprojects/sonar-update-center/sonar-update-center-common/src/main/java/org/sonar/updatecenter/common/PluginManifest.java b/subprojects/sonar-update-center/sonar-update-center-common/src/main/java/org/sonar/updatecenter/common/PluginManifest.java index 4934978abba..ed86cf52a96 100644 --- a/subprojects/sonar-update-center/sonar-update-center-common/src/main/java/org/sonar/updatecenter/common/PluginManifest.java +++ b/subprojects/sonar-update-center/sonar-update-center-common/src/main/java/org/sonar/updatecenter/common/PluginManifest.java @@ -19,8 +19,7 @@ */ package org.sonar.updatecenter.common; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import static org.sonar.updatecenter.common.FormatUtils.toDate; import java.io.File; import java.io.IOException; @@ -29,7 +28,8 @@ import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; -import static org.sonar.updatecenter.common.FormatUtils.toDate; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; /** * This class loads Sonar plugin metadata from JAR manifest. @@ -94,7 +94,8 @@ public final class PluginManifest { } /** - * @param manifest, can not be null + * @param manifest + * , can not be null */ public PluginManifest(Manifest manifest) { loadManifest(manifest); @@ -105,7 +106,7 @@ public final class PluginManifest { private void loadManifest(Manifest manifest) { Attributes attributes = manifest.getMainAttributes(); - this.key = attributes.getValue(KEY); + this.key = PluginKeyUtils.getPluginKey(attributes.getValue(KEY)); this.mainClass = attributes.getValue(MAIN_CLASS); this.name = attributes.getValue(NAME); this.description = attributes.getValue(DESCRIPTION); diff --git a/subprojects/sonar-update-center/sonar-update-center-common/src/test/java/org/sonar/updatecenter/common/PluginKeyUtilsTest.java b/subprojects/sonar-update-center/sonar-update-center-common/src/test/java/org/sonar/updatecenter/common/PluginKeyUtilsTest.java new file mode 100644 index 00000000000..1e9e646344a --- /dev/null +++ b/subprojects/sonar-update-center/sonar-update-center-common/src/test/java/org/sonar/updatecenter/common/PluginKeyUtilsTest.java @@ -0,0 +1,20 @@ +package org.sonar.updatecenter.common; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import org.junit.Test; + +public class PluginKeyUtilsTest { + + @Test + public void shouldExtractCorrectPluginKey() { + assertThat(PluginKeyUtils.getPluginKey("sonar-test-plugin"), is("test")); + assertThat(PluginKeyUtils.getPluginKey("test-sonar-plugin"), is("test")); + assertThat(PluginKeyUtils.getPluginKey("test"), is("test")); + + assertThat(PluginKeyUtils.getPluginKey("sonar-test-foo-plugin"), is("testfoo")); + assertThat(PluginKeyUtils.getPluginKey("test-foo-sonar-plugin"), is("testfoo")); + assertThat(PluginKeyUtils.getPluginKey("test-foo"), is("testfoo")); + } +}