From fac42bff8871befc994f1efd5cb8e6c803f44283 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 8 Oct 2014 17:26:35 +0200 Subject: [PATCH] Fix some quality flaws --- .../plugins/scm/git/JGitBlameCommand.java | 39 ++++++++------- .../plugins/ServerPluginJarsInstaller.java | 2 +- .../org/sonar/batch/scan2/MeasureCache.java | 2 +- .../sonar/batch/scm/DefaultBlameResult.java | 4 +- .../org/sonar/api/batch/scm/BlameLine.java | 13 ++--- .../sonar/api/batch/scm/BlameLineTest.java | 49 +++++++++++++++++++ 6 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java diff --git a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java index 6c15a8c871f..3e3d6642e2b 100644 --- a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java +++ b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java @@ -57,24 +57,7 @@ public class JGitBlameCommand implements BlameCommand, BatchComponent { git = Git.wrap(repo); File gitBaseDir = repo.getWorkTree(); for (InputFile inputFile : files) { - - String filename = pathResolver.relativePath(gitBaseDir, inputFile.file()); - org.eclipse.jgit.blame.BlameResult blameResult = git.blame().setFilePath(filename).call(); - List lines = new ArrayList(); - for (int i = 0; i < blameResult.getResultContents().size(); i++) { - if (blameResult.getSourceAuthor(i) == null || blameResult.getSourceCommit(i) == null || blameResult.getSourceCommitter(i) == null) { - throw new IllegalStateException("Unable to blame file " + inputFile.relativePath() + ". No blame info at line " + (i + 1) + ". Is file commited?"); - } - lines.add(new org.sonar.api.batch.scm.BlameLine(blameResult.getSourceAuthor(i).getWhen(), - blameResult.getSourceCommit(i).getName(), - blameResult.getSourceAuthor(i).getEmailAddress(), - blameResult.getSourceCommitter(i).getEmailAddress())); - } - if (lines.size() == inputFile.lines() - 1) { - // SONARPLUGINS-3097 Git do not report blame on last empty line - lines.add(lines.get(lines.size() - 1)); - } - result.add(inputFile, lines); + blame(result, git, gitBaseDir, inputFile); } } catch (IOException e) { throw new IllegalStateException("Unable to open Git repository", e); @@ -87,4 +70,24 @@ public class JGitBlameCommand implements BlameCommand, BatchComponent { } } + private void blame(BlameResult result, Git git, File gitBaseDir, InputFile inputFile) throws GitAPIException { + String filename = pathResolver.relativePath(gitBaseDir, inputFile.file()); + org.eclipse.jgit.blame.BlameResult blameResult = git.blame().setFilePath(filename).call(); + List lines = new ArrayList(); + for (int i = 0; i < blameResult.getResultContents().size(); i++) { + if (blameResult.getSourceAuthor(i) == null || blameResult.getSourceCommit(i) == null || blameResult.getSourceCommitter(i) == null) { + throw new IllegalStateException("Unable to blame file " + inputFile.relativePath() + ". No blame info at line " + (i + 1) + ". Is file commited?"); + } + lines.add(new org.sonar.api.batch.scm.BlameLine(blameResult.getSourceAuthor(i).getWhen(), + blameResult.getSourceCommit(i).getName(), + blameResult.getSourceAuthor(i).getEmailAddress(), + blameResult.getSourceCommitter(i).getEmailAddress())); + } + if (lines.size() == inputFile.lines() - 1) { + // SONARPLUGINS-3097 Git do not report blame on last empty line + lines.add(lines.get(lines.size() - 1)); + } + result.add(inputFile, lines); + } + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarsInstaller.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarsInstaller.java index 5f02a203649..2b15b1c8f26 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarsInstaller.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarsInstaller.java @@ -53,7 +53,7 @@ public class ServerPluginJarsInstaller { private final ServerPluginJarInstaller installer; private final Map pluginByKeys = Maps.newHashMap(); private final ServerUpgradeStatus serverUpgradeStatus; - private final static Set BLACKLISTED_PLUGINS = new HashSet(Arrays.asList("scmactivity")); + private static final Set BLACKLISTED_PLUGINS = new HashSet(Arrays.asList("scmactivity")); public ServerPluginJarsInstaller(Server server, ServerUpgradeStatus serverUpgradeStatus, DefaultServerFileSystem fs, ServerPluginJarInstaller installer) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/MeasureCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/MeasureCache.java index 57fe7dddbc0..e0c9d5a6db5 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/MeasureCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/MeasureCache.java @@ -49,7 +49,7 @@ public class MeasureCache implements BatchComponent { return cache.values(projectKey); } - public DefaultMeasure byMetric(String projectKey, String resourceKey, String metricKey) { + public DefaultMeasure byMetric(String projectKey, String resourceKey, String metricKey) { return cache.get(projectKey, resourceKey, metricKey); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameResult.java b/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameResult.java index 9d82a9372ff..905f7c01f45 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameResult.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameResult.java @@ -32,6 +32,7 @@ import org.sonar.api.utils.DateUtils; import javax.annotation.Nullable; import java.text.Normalizer; +import java.util.Date; import java.util.List; import java.util.regex.Pattern; @@ -60,7 +61,8 @@ class DefaultBlameResult implements BlameResult { int lineNumber = 1; for (BlameLine line : lines) { authors.add(lineNumber, normalizeString(line.getAuthor())); - dates.add(lineNumber, DateUtils.formatDateTime(line.getDate())); + Date date = line.getDate(); + dates.add(lineNumber, date != null ? DateUtils.formatDateTime(date) : ""); revisions.add(lineNumber, line.getRevision()); lineNumber++; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/BlameLine.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/BlameLine.java index 02d16f29ab4..221282b04e6 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/BlameLine.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/BlameLine.java @@ -91,21 +91,18 @@ public class BlameLine { /** * @return the commit date */ + @CheckForNull public Date getDate() { - if (date != null) - { + if (date != null) { return (Date) date.clone(); } return null; } - public void setDate(Date date) { - if (date != null) - { + public void setDate(@Nullable Date date) { + if (date != null) { this.date = new Date(date.getTime()); - } - else - { + } else { this.date = null; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java new file mode 100644 index 00000000000..6d084ead610 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java @@ -0,0 +1,49 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.batch.scm; + +import org.junit.Test; + +import java.util.Date; + +import static org.fest.assertions.Assertions.assertThat; + +public class BlameLineTest { + + @Test + public void testBlameLine() { + Date date = new Date(); + BlameLine line1 = new BlameLine(date, "1", "foo"); + BlameLine line1b = new BlameLine(date, "1", "foo"); + BlameLine line2 = new BlameLine(date, "2", "foo2"); + + assertThat(line1.getAuthor()).isEqualTo("foo"); + assertThat(line1.getCommitter()).isEqualTo("foo"); + + assertThat(line1).isEqualTo(line1); + assertThat(line1).isEqualTo(line1b); + assertThat(line1.hashCode()).isEqualTo(line1b.hashCode()); + assertThat(line1).isNotEqualTo(line2); + assertThat(line1).isNotEqualTo("foo"); + + assertThat(line1.toString()).contains("revision=1,author=foo,committer=foo"); + } + +} -- 2.39.5