From: Teryk Bellahsene Date: Tue, 13 Jan 2015 14:04:26 +0000 (+0100) Subject: fix quality flaws X-Git-Tag: latest-silver-master-#65~232 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=463501a6f1ccdf792334f0673924fcfcfe596985;p=sonarqube.git fix quality flaws --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportService.java b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportService.java index 6a2af749f04..08b6d03b023 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportService.java @@ -24,8 +24,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.collect.Iterables; import org.apache.commons.io.IOUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.issue.internal.FieldDiffs; @@ -46,7 +44,6 @@ import java.util.Date; public class AnalysisReportService implements ServerComponent { - private static final Logger LOG = LoggerFactory.getLogger(AnalysisReportService.class); private final ComputeEngineIssueStorageFactory issueStorageFactory; public AnalysisReportService(ComputeEngineIssueStorageFactory issueStorageFactory) { @@ -69,7 +66,6 @@ public class AnalysisReportService implements ServerComponent { } catch (IOException e) { throw new IllegalStateException("Failed to read issues", e); } - } @VisibleForTesting diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedAnalysisReportsLongDates.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedAnalysisReportsLongDates.java index 7f2fc39173d..42c5a6564a1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedAnalysisReportsLongDates.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedAnalysisReportsLongDates.java @@ -56,21 +56,13 @@ public class FeedAnalysisReportsLongDates extends BaseDataChange { Date finishedAt = row.getDate(4); Long id = row.getLong(5); - updateColumn(update, 1, createdAt); - updateColumn(update, 2, updatedAt); + update.setLong(1, createdAt == null ? now : Math.min(now, createdAt.getTime())); + update.setLong(2, updatedAt == null ? now : Math.min(now, updatedAt.getTime())); update.setLong(3, startedAt == null ? null : startedAt.getTime()); update.setLong(4, finishedAt == null ? null : finishedAt.getTime()); update.setLong(5, id); return true; } - - private void updateColumn(SqlStatement update, int position, Date time) throws SQLException { - if (time == null) { - update.setLong(position, now); - } else { - update.setLong(position, Math.min(now, time.getTime())); - } - } }); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedIssueChangesLongDates.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedIssueChangesLongDates.java index da74eaf1307..259e82b0060 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedIssueChangesLongDates.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedIssueChangesLongDates.java @@ -55,16 +55,12 @@ public class FeedIssueChangesLongDates extends BaseDataChange { Date functionalCreatedAt = row.getDate(3); Long id = row.getLong(4); - updateColumn(update, 1, createdAt); - updateColumn(update, 2, updatedAt); + update.setLong(1, createdAt == null ? now : Math.min(now, createdAt.getTime())); + update.setLong(2, updatedAt == null ? now : Math.min(now, updatedAt.getTime())); update.setLong(3, functionalCreatedAt == null ? null : functionalCreatedAt.getTime()); update.setLong(4, id); return true; } - - private void updateColumn(SqlStatement update, int position, Date time) throws SQLException { - update.setLong(position, time == null ? now : Math.min(now, time.getTime())); - } }); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java index 556db486621..1ff7bc4ac4a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java @@ -20,6 +20,7 @@ package org.sonar.api.utils; import javax.annotation.Nullable; + import java.io.NotSerializableException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -56,11 +57,7 @@ public final class DateUtils { } public static String formatDateTimeNullSafe(@Nullable Date date) { - if (date == null) { - return ""; - } - - return THREAD_SAFE_DATETIME_FORMAT.format(date); + return date == null ? "" : THREAD_SAFE_DATETIME_FORMAT.format(date); } public static Date timeToDate(@Nullable Long time) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java index 63b785960bb..888ceeb4c69 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java @@ -20,6 +20,7 @@ package org.sonar.api.utils; import com.google.common.collect.Lists; +import org.assertj.core.api.Assertions; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -27,10 +28,8 @@ import org.junit.rules.ExpectedException; import java.util.Date; import java.util.List; +import static org.hamcrest.Matchers.*; import static org.hamcrest.core.Is.is; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.*; public class DateUtilsTest { @@ -100,6 +99,20 @@ public class DateUtilsTest { assertThat(DateUtils.formatDateTime(new Date()).length(), greaterThan(20)); } + @Test + public void format_date_time_null_safe() throws Exception { + Assertions.assertThat(DateUtils.formatDateTimeNullSafe(new Date())).startsWith("20"); + Assertions.assertThat(DateUtils.formatDateTimeNullSafe(new Date()).length()).isGreaterThan(20); + Assertions.assertThat(DateUtils.formatDateTimeNullSafe(null)).isEmpty(); + } + + @Test + public void time_to_date() throws Exception { + Date date = new Date(); + Assertions.assertThat(DateUtils.timeToDate(date.getTime())).isEqualTo(date); + Assertions.assertThat(DateUtils.timeToDate(null)).isNull(); + } + /** * Cordially copied from XStream unit test * See http://koders.com/java/fid8A231D75F2C6E6909FB26BCA11C12D08AD05FB50.aspx?s=ThreadSafeDateFormatTest