]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7493 support long issue messages on Oracle
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 22 Aug 2017 15:34:28 +0000 (17:34 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 24 Aug 2017 09:54:24 +0000 (11:54 +0200)
sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/Issue.java
tests/src/test/java/org/sonarqube/tests/issue/IssueCreationTest.java

index f51d9bb30be4c6b5744b0ce358c0913e7a251f5e..f9c36b3bc2a5707463b68959375706bc885ceaa9 100644 (file)
@@ -36,7 +36,7 @@ import static org.mockito.Mockito.mock;
 
 public class DefaultIssueTest {
 
-  DefaultIssue issue = new DefaultIssue();
+  private DefaultIssue issue = new DefaultIssue();
 
   @Test
   public void test_setters_and_getters() throws Exception {
@@ -152,8 +152,8 @@ public class DefaultIssueTest {
 
   @Test
   public void message_should_be_abbreviated_if_too_long() {
-    issue.setMessage(StringUtils.repeat("a", 5000));
-    assertThat(issue.message()).hasSize(4000);
+    issue.setMessage(StringUtils.repeat("a", 5_000));
+    assertThat(issue.message()).hasSize(1_333);
   }
 
   @Test
index 314d704cba2ff04d8a5c2f73917da6bcdb2702a6..4ed5b6750c9c5fe2d95694ec421d05abab24b53f 100644 (file)
@@ -38,8 +38,14 @@ public interface Issue extends Serializable {
 
   /**
    * Maximum number of characters in the message.
-   */
-  int MESSAGE_MAX_SIZE = 4000;
+   * In theory it should be 4_000 UTF-8 characters but unfortunately
+   * Oracle DB does not support more than 4_000 bytes, even if column
+   * issues.message is created with type VARCHAR2(4000 CHAR).
+   * In order to have the same behavior on all databases, message
+   * is truncated to 4_000 / 3 (maximum bytes per UTF-8 character)
+   * = 1_333 characters.
+   */
+  int MESSAGE_MAX_SIZE = 1_333;
 
   /**
    * Default status when creating an issue.
index b6afaf1730492f2eafeeea02974371bda7950941..6c7d72ed9d5630b3b568f8de324710621153df78 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonarqube.tests.issue;
 
+import org.apache.commons.lang.StringUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.wsclient.issue.Issue;
@@ -32,6 +33,7 @@ import static util.ItUtils.runProjectAnalysis;
 public class IssueCreationTest extends AbstractIssueTest {
 
   private static final String SAMPLE_PROJECT_KEY = "sample";
+  private static final int ISSUE_MESSAGE_MAX_LENGTH = 1_333;
 
   @Before
   public void resetData() {
@@ -75,4 +77,40 @@ public class IssueCreationTest extends AbstractIssueTest {
       assertThat(issue.severity()).isEqualTo("BLOCKER");
     }
   }
+
+  /**
+   * SONAR-7493
+   */
+  @Test
+  public void issue_message_should_support_1_333_utf8_characters_encoded_on_3_bytes() {
+    String longMessage = StringUtils.repeat("を", ISSUE_MESSAGE_MAX_LENGTH);
+
+    assertThat(generateIssueWithMessage(longMessage)).isEqualTo(longMessage);
+  }
+
+  /**
+   * Test the maximum size of DB column issues.message, when all characters
+   * are encoded on 3-bytes
+   *
+   * SONAR-7493
+   */
+  @Test
+  public void issue_message_should_be_abbreviated_if_longer_than_1333_utf8_characters_encoded_on_3_bytes() {
+    String character = "を";
+    String message = generateIssueWithMessage(StringUtils.repeat(character, 10_000));
+
+    String abbreviatedMessage = StringUtils.repeat(character, ISSUE_MESSAGE_MAX_LENGTH - 3) + "...";
+    assertThat(message).isEqualTo(abbreviatedMessage);
+  }
+
+  private String generateIssueWithMessage(String longMessage) {
+    ORCHESTRATOR.getServer().provisionProject(SAMPLE_PROJECT_KEY, SAMPLE_PROJECT_KEY);
+    ItUtils.restoreProfile(ORCHESTRATOR, getClass().getResource("/issue/IssueCreationTest/with-custom-message.xml"));
+    ORCHESTRATOR.getServer().associateProjectToQualityProfile(SAMPLE_PROJECT_KEY, "xoo", "with-custom-message");
+
+    runProjectAnalysis(ORCHESTRATOR, "shared/xoo-sample", "sonar.customMessage.message", longMessage);
+
+    Issue issue = issueClient().find(IssueQuery.create()).list().get(0);
+    return issue.message();
+  }
 }