]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7842 truncate errorMessage to avoid max length error in DB
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 18 Aug 2016 08:49:54 +0000 (10:49 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 18 Aug 2016 10:37:32 +0000 (12:37 +0200)
sonar-db/src/main/java/org/sonar/db/ce/CeActivityDto.java
sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java

index 34e4a4237df84483f22dadcf7a56e2625e1a82b2..a8646769cf817a5fd96ee170bab2358ffaae7016 100644 (file)
@@ -30,6 +30,8 @@ import static java.lang.String.format;
 
 public class CeActivityDto {
 
+  private static final int MAX_SIZE_ERROR_MESSAGE = 1000;
+
   public enum Status {
     SUCCESS, FAILED, CANCELED
   }
@@ -212,10 +214,21 @@ public class CeActivityDto {
   }
 
   public CeActivityDto setErrorMessage(@Nullable String errorMessage) {
-    this.errorMessage = errorMessage;
+    this.errorMessage = ensureNotTooBig(errorMessage, MAX_SIZE_ERROR_MESSAGE);
     return this;
   }
 
+  @CheckForNull
+  private static String ensureNotTooBig(@Nullable String str, int maxSize) {
+    if (str == null) {
+      return null;
+    }
+    if (str.length() <= maxSize) {
+      return str;
+    }
+    return str.substring(0, maxSize);
+  }
+
   @CheckForNull
   public String getErrorStacktrace() {
     return errorStacktrace;
index edd626386a2176176453abf24f5304a47dbafca9..885d9b11ed2c9979ca672eef987cfc8962df4583 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db.ce;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
+import com.google.common.base.Strings;
 import com.google.common.collect.FluentIterable;
 import java.util.Collections;
 import java.util.List;
@@ -73,6 +74,27 @@ public class CeActivityDaoTest {
     assertThat(dto.getErrorStacktrace()).isNull();
   }
 
+  @Test
+  public void test_insert_of_errorMessage_of_1_000_chars() {
+    CeActivityDto dto = createActivityDto("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED)
+        .setErrorMessage(Strings.repeat("x", 1_000));
+    underTest.insert(db.getSession(), dto);
+
+    Optional<CeActivityDto> saved = underTest.selectByUuid(db.getSession(), "TASK_1");
+    assertThat(saved.get().getErrorMessage()).isEqualTo(dto.getErrorMessage());
+  }
+
+  @Test
+  public void test_insert_of_errorMessage_of_1_001_chars_is_truncated_to_1000() {
+    String expected = Strings.repeat("x", 1_000);
+    CeActivityDto dto = createActivityDto("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED)
+        .setErrorMessage(expected + "y");
+    underTest.insert(db.getSession(), dto);
+
+    Optional<CeActivityDto> saved = underTest.selectByUuid(db.getSession(), "TASK_1");
+    assertThat(saved.get().getErrorMessage()).isEqualTo(expected);
+  }
+
   @Test
   public void test_insert_error_message_and_stacktrace() {
     CeActivityDto dto = createActivityDto("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED)