]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18545 Report processing fails when a warning message is too long
authorZipeng WU <zipeng.wu@sonarsource.com>
Wed, 22 Feb 2023 13:57:06 +0000 (14:57 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 23 Feb 2023 20:03:01 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDto.java
server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDtoTest.java

index 204043161b645badb403a6d714f8a2327b7c4487..1b3b710fa81e010611a57cfb95a61a69c2e49cc7 100644 (file)
  */
 package org.sonar.db.ce;
 
+import com.google.common.annotations.VisibleForTesting;
+
 import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.commons.lang.StringUtils.abbreviate;
 
 public class CeTaskMessageDto {
+  @VisibleForTesting
+  static final int MAX_MESSAGE_SIZE = 4000;
   /**
    * Unique identifier of each message. Not null
    */
@@ -67,8 +72,7 @@ public class CeTaskMessageDto {
 
   public CeTaskMessageDto setMessage(String message) {
     checkArgument(message != null && !message.isEmpty(), "message can't be null nor empty");
-    checkArgument(message.length() <= 4000, "message is too long: %s", message.length());
-    this.message = message;
+    this.message = abbreviate(message, MAX_MESSAGE_SIZE);
     return this;
   }
 
index 9370f5c2de863386f87bb9dddbe3144e9f87f7ec..597b2d55b22da55aa70e1e7237a578044d6ca6b5 100644 (file)
  */
 package org.sonar.db.ce;
 
-import java.util.Random;
 import org.junit.Test;
 
 import static org.apache.commons.lang.StringUtils.repeat;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.sonar.db.ce.CeTaskMessageDto.MAX_MESSAGE_SIZE;
 
 public class CeTaskMessageDtoTest {
 
@@ -46,19 +46,18 @@ public class CeTaskMessageDtoTest {
 
   @Test
   public void setMessage_accept_argument_of_size_4000() {
-    String str = repeat("a", 4000);
+    String str = repeat("a", MAX_MESSAGE_SIZE);
     underTest.setMessage(str);
 
     assertThat(underTest.getMessage()).isEqualTo(str);
   }
 
   @Test
-  public void setMessage_fails_with_IAE_if_argument_has_size_bigger_then_4000() {
-    int size = 4000 + 1 + new Random().nextInt(100);
-    String str = repeat("a", size);
+  public void setMessage_truncates_the_message_if_argument_has_size_bigger_then_4000() {
+    String str = repeat("a", MAX_MESSAGE_SIZE) + "extra long tail!!!";
 
-    assertThatThrownBy(() -> underTest.setMessage(str))
-      .isInstanceOf(IllegalArgumentException.class)
-      .hasMessage("message is too long: " + size);
+    underTest.setMessage(str);
+
+    assertThat(underTest.getMessage()).isEqualTo(repeat("a", MAX_MESSAGE_SIZE - 3) + "...");
   }
 }