diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-06-10 14:06:37 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-06-10 14:06:37 +0200 |
commit | 23d30654116e9088b92cbbe8ed70fc71e5598854 (patch) | |
tree | 0cc65d0a54f11a98510b87ca57c3303b440f4f06 /sonar-batch | |
parent | e9957c5d4049a2f8f39c83a6ff6a09e5fa333f7f (diff) | |
download | sonarqube-23d30654116e9088b92cbbe8ed70fc71e5598854.tar.gz sonarqube-23d30654116e9088b92cbbe8ed70fc71e5598854.zip |
SONAR-2507 support deprecated directory /extensions/rules/
Diffstat (limited to 'sonar-batch')
4 files changed, 74 insertions, 89 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ArtifactDownloader.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ArtifactDownloader.java index b8ae7330848..946df51833e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ArtifactDownloader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ArtifactDownloader.java @@ -29,6 +29,7 @@ import org.sonar.api.BatchComponent; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.SonarException; import org.sonar.batch.ServerMetadata; +import org.sonar.core.plugins.RemotePlugin; import java.io.File; import java.net.URI; @@ -62,28 +63,36 @@ public class ArtifactDownloader implements BatchComponent { } } - public File downloadPlugin(RemotePluginLocation remote) { - File targetFile = new File(workingDirectories.getDir("plugins/" + remote.getPluginKey()), remote.getFilename()); - String url = baseUrl + "/deploy/plugins/" + remote.getRemotePath(); + public List<File> downloadPlugin(RemotePlugin remote) { try { - FileUtils.forceMkdir(targetFile.getParentFile()); - LOG.info("Download plugin to " + targetFile); - httpDownloader.download(new URI(url), targetFile); - return targetFile; + File targetDir = workingDirectories.getDir("plugins/" + remote.getKey()); + FileUtils.forceMkdir(targetDir); + LOG.info("Downloading plugin " + remote.getKey() + " into " + targetDir); + + List<File> files = Lists.newArrayList(); + for (String filename : remote.getFilenames()) { + String url = baseUrl + "/deploy/plugins/" + remote.getKey() + "/" + filename; + File toFile = new File(targetDir, filename); + httpDownloader.download(new URI(url), toFile); + files.add(toFile); + } + + + return files; } catch (Exception e) { - throw new SonarException("Fail to download extension: " + url, e); + throw new SonarException("Fail to download plugin: " + remote.getKey(), e); } } - public List<RemotePluginLocation> downloadPluginIndex() { + public List<RemotePlugin> downloadPluginIndex() { String url = baseUrl + "/deploy/plugins/index.txt"; try { String indexContent = httpDownloader.downloadPlainText(new URI(url), "UTF-8"); String[] rows = StringUtils.split(indexContent, CharUtils.LF); - List<RemotePluginLocation> remoteLocations = Lists.newArrayList(); + List<RemotePlugin> remoteLocations = Lists.newArrayList(); for (String row : rows) { - remoteLocations.add(RemotePluginLocation.createFromRow(row)); + remoteLocations.add(RemotePlugin.unmarshal(row)); } return remoteLocations; @@ -92,57 +101,4 @@ public class ArtifactDownloader implements BatchComponent { } } - public static final class RemotePluginLocation { - private String pluginKey; - private String remotePath; - private boolean core; - - private RemotePluginLocation(String pluginKey, String remotePath, boolean core) { - this.pluginKey = pluginKey; - this.remotePath = remotePath; - this.core = core; - } - - static RemotePluginLocation create(String key) { - return new RemotePluginLocation(key, null, false); - } - - static RemotePluginLocation createFromRow(String row) { - String[] fields = StringUtils.split(row, ","); - return new RemotePluginLocation(fields[0], fields[1], Boolean.parseBoolean(fields[2])); - } - - public String getPluginKey() { - return pluginKey; - } - - public String getRemotePath() { - return remotePath; - } - - public String getFilename() { - return StringUtils.substringAfterLast(remotePath, "/"); - } - - public boolean isCore() { - return core; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - RemotePluginLocation that = (RemotePluginLocation) o; - return pluginKey.equals(that.pluginKey); - } - - @Override - public int hashCode() { - return pluginKey.hashCode(); - } - } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java index 2452983bd11..e8d698d2959 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java @@ -34,6 +34,7 @@ import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; import org.sonar.core.plugins.PluginClassloaders; import org.sonar.core.plugins.PluginFileExtractor; +import org.sonar.core.plugins.RemotePlugin; import java.io.File; import java.util.*; @@ -66,13 +67,14 @@ public class BatchPluginRepository implements PluginRepository { doStart(artifactDownloader.downloadPluginIndex()); } - void doStart(List<ArtifactDownloader.RemotePluginLocation> remoteLocations) { + void doStart(List<RemotePlugin> remotePlugins) { PluginFileExtractor extractor = new PluginFileExtractor(); metadataByKey = Maps.newHashMap(); - for (ArtifactDownloader.RemotePluginLocation remoteLocation : remoteLocations) { - if (isAccepted(remoteLocation.getPluginKey())) { - File pluginFile = artifactDownloader.downloadPlugin(remoteLocation); - PluginMetadata metadata = extractor.installInSameLocation(pluginFile, remoteLocation.isCore()); + for (RemotePlugin remote : remotePlugins) { + if (isAccepted(remote.getKey())) { + List<File> pluginFiles = artifactDownloader.downloadPlugin(remote); + List<File> extensionFiles = pluginFiles.subList(1, pluginFiles.size()); + PluginMetadata metadata = extractor.installInSameLocation(pluginFiles.get(0), remote.isCore(), extensionFiles); if (StringUtils.isBlank(metadata.getBasePlugin()) || isAccepted(metadata.getBasePlugin())) { // TODO log when excluding plugin metadataByKey.put(metadata.getKey(), metadata); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java index 9fc772f0dd5..c65cfb61cd5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java @@ -19,21 +19,23 @@ */ package org.sonar.batch.bootstrap; +import com.google.common.collect.Lists; import org.apache.commons.configuration.PropertiesConfiguration; import org.codehaus.plexus.util.FileUtils; import org.hamcrest.Matchers; import org.junit.After; import org.junit.Test; import org.sonar.api.CoreProperties; +import org.sonar.core.plugins.RemotePlugin; import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.List; import static org.hamcrest.Matchers.not; import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -50,33 +52,34 @@ public class BatchPluginRepositoryTest { @Test public void shouldLoadPlugin() throws IOException { - ArtifactDownloader.RemotePluginLocation checkstyleLocation = ArtifactDownloader.RemotePluginLocation.create("checkstyle"); + RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); ArtifactDownloader downloader = mock(ArtifactDownloader.class); - when(downloader.downloadPlugin(eq(checkstyleLocation))).thenReturn(copyFile("sonar-checkstyle-plugin-2.8.jar")); + when(downloader.downloadPlugin(checkstyle)).thenReturn(copyFiles("sonar-checkstyle-plugin-2.8.jar")); repository = new BatchPluginRepository(downloader, new PropertiesConfiguration()); - repository.doStart(Arrays.asList(checkstyleLocation)); + repository.doStart(Arrays.asList(checkstyle)); assertThat(repository.getPlugins().size(), Matchers.is(1)); assertThat(repository.getPlugin("checkstyle"), not(nullValue())); assertThat(repository.getMetadata().size(), Matchers.is(1)); assertThat(repository.getMetadata("checkstyle").getName(), Matchers.is("Checkstyle")); + assertThat(repository.getMetadata("checkstyle").getDeployedFiles().size(), Matchers.is(4)); // plugin + 3 dependencies } @Test public void shouldLoadPluginExtension() throws IOException { - ArtifactDownloader.RemotePluginLocation checkstyleLocation = ArtifactDownloader.RemotePluginLocation.create("checkstyle"); - ArtifactDownloader.RemotePluginLocation checkstyleExtLocation = ArtifactDownloader.RemotePluginLocation.create("checkstyleextensions"); + RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); + RemotePlugin checkstyleExt = new RemotePlugin("checkstyleextensions", false); ArtifactDownloader downloader = mock(ArtifactDownloader.class); - when(downloader.downloadPlugin(eq(checkstyleLocation))).thenReturn(copyFile("sonar-checkstyle-plugin-2.8.jar")); - when(downloader.downloadPlugin(eq(checkstyleExtLocation))).thenReturn(copyFile("sonar-checkstyle-extensions-plugin-0.1-SNAPSHOT.jar")); + when(downloader.downloadPlugin(checkstyle)).thenReturn(copyFiles("sonar-checkstyle-plugin-2.8.jar")); + when(downloader.downloadPlugin(checkstyleExt)).thenReturn(copyFiles("sonar-checkstyle-extensions-plugin-0.1-SNAPSHOT.jar")); repository = new BatchPluginRepository(downloader, new PropertiesConfiguration()); - repository.doStart(Arrays.asList(checkstyleLocation, checkstyleExtLocation)); + repository.doStart(Arrays.asList(checkstyle, checkstyleExt)); assertThat(repository.getPlugins().size(), Matchers.is(2)); assertThat(repository.getPlugin("checkstyle"), not(nullValue())); @@ -87,30 +90,53 @@ public class BatchPluginRepositoryTest { } @Test + public void shouldLoadPluginDeprecatedExtensions() throws IOException { + RemotePlugin checkstyle = new RemotePlugin("checkstyle", true) + .addFilename("checkstyle-ext.xml"); + + ArtifactDownloader downloader = mock(ArtifactDownloader.class); + when(downloader.downloadPlugin(checkstyle)).thenReturn(copyFiles("sonar-checkstyle-plugin-2.8.jar", "checkstyle-ext.xml")); + + repository = new BatchPluginRepository(downloader, new PropertiesConfiguration()); + + repository.doStart(Arrays.asList(checkstyle)); + + assertThat(repository.getPlugins().size(), Matchers.is(1)); + assertThat(repository.getPlugin("checkstyle"), not(nullValue())); + assertThat(repository.getMetadata().size(), Matchers.is(1)); + assertThat(repository.getMetadata("checkstyle").getName(), Matchers.is("Checkstyle")); + assertThat(repository.getMetadata("checkstyle").getDeployedFiles().size(), Matchers.is(5)); // plugin + 3 dependencies + 1 deprecated extension + } + + @Test public void shouldExcludePluginAndItsExtensions() throws IOException { - ArtifactDownloader.RemotePluginLocation checkstyleLocation = ArtifactDownloader.RemotePluginLocation.create("checkstyle"); - ArtifactDownloader.RemotePluginLocation checkstyleExtLocation = ArtifactDownloader.RemotePluginLocation.create("checkstyleextensions"); + RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); + RemotePlugin checkstyleExt = new RemotePlugin("checkstyleextensions", false); ArtifactDownloader downloader = mock(ArtifactDownloader.class); - when(downloader.downloadPlugin(eq(checkstyleLocation))).thenReturn(copyFile("sonar-checkstyle-plugin-2.8.jar")); - when(downloader.downloadPlugin(eq(checkstyleExtLocation))).thenReturn(copyFile("sonar-checkstyle-extensions-plugin-0.1-SNAPSHOT.jar")); + when(downloader.downloadPlugin(checkstyle)).thenReturn(copyFiles("sonar-checkstyle-plugin-2.8.jar")); + when(downloader.downloadPlugin(checkstyleExt)).thenReturn(copyFiles("sonar-checkstyle-extensions-plugin-0.1-SNAPSHOT.jar")); PropertiesConfiguration conf = new PropertiesConfiguration(); conf.setProperty(CoreProperties.EXCLUDE_PLUGINS, "checkstyle"); repository = new BatchPluginRepository(downloader, conf); - repository.doStart(Arrays.asList(checkstyleLocation, checkstyleExtLocation)); + repository.doStart(Arrays.asList(checkstyle, checkstyleExt)); assertThat(repository.getPlugins().size(), Matchers.is(0)); assertThat(repository.getMetadata().size(), Matchers.is(0)); } - private File copyFile(String filename) throws IOException { - File file = FileUtils.toFile(getClass().getResource("/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/" + filename)); - File tempDir = new File("target/test-tmp/BatchPluginRepositoryTest"); - FileUtils.forceMkdir(tempDir); - FileUtils.copyFileToDirectory(file, tempDir); - return new File(tempDir, filename); + private List<File> copyFiles(String... filenames) throws IOException { + List files = Lists.newArrayList(); + for (String filename : filenames) { + File file = FileUtils.toFile(getClass().getResource("/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/" + filename)); + File tempDir = new File("target/test-tmp/BatchPluginRepositoryTest"); + FileUtils.forceMkdir(tempDir); + FileUtils.copyFileToDirectory(file, tempDir); + files.add(new File(tempDir, filename)); + } + return files; } diff --git a/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/checkstyle-ext.xml b/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/checkstyle-ext.xml new file mode 100644 index 00000000000..75a263db3c3 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/checkstyle-ext.xml @@ -0,0 +1 @@ +<fake/>
\ No newline at end of file |