]> source.dussan.org Git - sonarqube.git/commitdiff
fix quality flaws
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 13 Jan 2015 14:04:26 +0000 (15:04 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 13 Jan 2015 14:17:23 +0000 (15:17 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportService.java
server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedAnalysisReportsLongDates.java
server/sonar-server/src/main/java/org/sonar/server/db/migrations/v51/FeedIssueChangesLongDates.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java

index 6a2af749f04b3701241532d8af4bf5e7ea0f4c10..08b6d03b02367bd48f433f8947e22c3d378bedda 100644 (file)
@@ -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
index 7f2fc39173d637426ab2df331fddff5002ebaceb..42c5a6564a1be84a9feba2874531f636b0a02d65 100644 (file)
@@ -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()));
-        }
-      }
     });
   }
 
index da74eaf13079f7e1bb4b106d93e828858fb38bb9..259e82b00602e8f8063527f1b78d21bf9d0dcd2a 100644 (file)
@@ -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()));
-      }
     });
   }
 
index 556db4866213b6f5cc1c2188e40a507c715d7ec4..1ff7bc4ac4af431275345c16134651fdf3c64f94 100644 (file)
@@ -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) {
index 63b785960bbda1f7c79f26b2b0a1bd93b86bae22..888ceeb4c69128dd4157e9d970c0545db9d64d7d 100644 (file)
@@ -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