]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5827 Use byte[] instead of String to manipulate file_sources data so that all...
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 21 Nov 2014 16:19:11 +0000 (17:19 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 21 Nov 2014 16:39:35 +0000 (17:39 +0100)
sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistNewFileNoScmNoHighlighting-result.xml [deleted file]
sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistNewFileWithScmAndHighlighting-result.xml [deleted file]
sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistUpdateChanged-result.xml [deleted file]
sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDto.java
sonar-core/src/test/java/org/sonar/core/source/db/FileSourceDaoTest.java
sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/insert-result.xml
sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/shared.xml
sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update-result.xml

index 0ce9d1f7737c8f39dbe7a266bd85c14bf9d26e45..3867095ad879d81a5e31d373b5b91a5330c300a3 100644 (file)
@@ -152,14 +152,15 @@ public class SourcePersister implements ScanPersister {
     String newDataHash = newData != null ? DigestUtils.md5Hex(newData) : "0";
     Date now = system2.newDate();
     if (previous == null) {
-      FileSourceDto newFileSource = new FileSourceDto().setProjectUuid(projectTree.getRootProject().getUuid()).setFileUuid(fileUuid).setData(newData).setDataHash(newDataHash)
+      FileSourceDto newFileSource = new FileSourceDto().setProjectUuid(projectTree.getRootProject().getUuid()).setFileUuid(fileUuid).setStringData(newData)
+        .setDataHash(newDataHash)
         .setCreatedAt(now)
         .setUpdatedAt(now);
       mapper.insert(newFileSource);
       session.commit();
     } else {
       if (!newDataHash.equals(previous.getDataHash())) {
-        previous.setData(newData).setDataHash(newDataHash).setUpdatedAt(now);
+        previous.setStringData(newData).setDataHash(newDataHash).setUpdatedAt(now);
         mapper.update(previous);
         session.commit();
       }
index 453363df1ef3d2dd3a9824e0f5c8b79fc4bdbed3..5efb6cf5ffc07b56cd83f962c9ca84133c036e2b 100644 (file)
@@ -43,11 +43,14 @@ import org.sonar.batch.scan.measure.MeasureCache;
 import org.sonar.batch.source.CodeColorizers;
 import org.sonar.core.persistence.AbstractDaoTestCase;
 import org.sonar.core.source.SnapshotDataTypes;
+import org.sonar.core.source.db.FileSourceDao;
+import org.sonar.core.source.db.FileSourceDto;
 import org.sonar.core.source.db.SnapshotSourceDao;
 
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Date;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.any;
@@ -121,7 +124,8 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
   @Test
   public void testPersistUpdateChanged() throws Exception {
     setupData("file_sources");
-    when(system2.newDate()).thenReturn(DateUtils.parseDateTime("2014-10-29T16:44:02+0100"));
+    Date now = DateUtils.parseDateTime("2014-10-29T16:44:02+0100");
+    when(system2.newDate()).thenReturn(now);
 
     String relativePathSame = "src/changed.java";
     java.io.File sameFile = new java.io.File(basedir, relativePathSame);
@@ -132,7 +136,13 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
     mockResourceCache(relativePathSame, PROJECT_KEY, "uuidsame");
 
     sourcePersister.persist();
-    checkTables("testPersistUpdateChanged", "file_sources");
+
+    FileSourceDto fileSourceDto = new FileSourceDao(getMyBatis()).select("uuidsame");
+    assertThat(fileSourceDto.getCreatedAt()).isEqualTo(DateUtils.parseDateTime("2014-10-10T16:44:02+0200"));
+    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now);
+    assertThat(fileSourceDto.getStringData()).isEqualTo(
+      ",,,,changed\r\n,,,,content\r\n");
+    assertThat(fileSourceDto.getDataHash()).isEqualTo("e41cca9c51ff853c748f708f39dfc035");
   }
 
   @Test
@@ -153,7 +163,8 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
   @Test
   public void testPersistNewFileNoScmNoHighlighting() throws Exception {
     setupData("file_sources");
-    when(system2.newDate()).thenReturn(DateUtils.parseDateTime("2014-10-29T16:44:02+0100"));
+    Date now = DateUtils.parseDateTime("2014-10-29T16:44:02+0100");
+    when(system2.newDate()).thenReturn(now);
 
     String relativePathNew = "src/new.java";
     java.io.File newFile = new java.io.File(basedir, relativePathNew);
@@ -164,13 +175,20 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
     mockResourceCache(relativePathNew, PROJECT_KEY, "uuidnew");
 
     sourcePersister.persist();
-    checkTables("testPersistNewFileNoScmNoHighlighting", "file_sources");
+    FileSourceDto fileSourceDto = new FileSourceDao(getMyBatis()).select("uuidnew");
+    assertThat(fileSourceDto.getCreatedAt()).isEqualTo(now);
+    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now);
+    assertThat(fileSourceDto.getStringData()).isEqualTo(
+      ",,,,foo\r\n,,,,bar\r\n,,,,biz\r\n");
+    assertThat(fileSourceDto.getDataHash()).isEqualTo("0c43ed6418d690ee0ffc3e43e6660967");
+
   }
 
   @Test
   public void testPersistNewFileWithScmAndHighlighting() throws Exception {
     setupData("file_sources");
-    when(system2.newDate()).thenReturn(DateUtils.parseDateTime("2014-10-29T16:44:02+0100"));
+    Date now = DateUtils.parseDateTime("2014-10-29T16:44:02+0100");
+    when(system2.newDate()).thenReturn(now);
 
     String relativePathNew = "src/new.java";
     java.io.File newFile = new java.io.File(basedir, relativePathNew);
@@ -199,7 +217,15 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
       .thenReturn(highlighting);
 
     sourcePersister.persist();
-    checkTables("testPersistNewFileWithScmAndHighlighting", "file_sources");
+
+    FileSourceDto fileSourceDto = new FileSourceDao(getMyBatis()).select("uuidnew");
+    assertThat(fileSourceDto.getCreatedAt()).isEqualTo(now);
+    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now);
+    assertThat(fileSourceDto.getStringData()).isEqualTo(
+      "123,julien,2014-10-11T16:44:02+0100,\"0,3,a\",foo\r\n"
+        + "234,simon,2014-10-12T16:44:02+0100,\"0,1,cd\",bar\r\n"
+        + "345,julien,2014-10-13T16:44:02+0100,\"0,9,c\",biz\r\n");
+    assertThat(fileSourceDto.getDataHash()).isEqualTo("a2aaee165e33957a67331fb9f869e0f1");
   }
 
   @Test
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistNewFileNoScmNoHighlighting-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistNewFileNoScmNoHighlighting-result.xml
deleted file mode 100644 (file)
index 782ada2..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<dataset>
-    <file_sources id="101" project_uuid="projectUuid" file_uuid="uuidsame" data=",,,,unchanged&#13;&#10;,,,,content&#13;&#10;" data_hash="ee716d4ed9faae16eb9167714442a3bc" created_at="2014-10-10 16:44:02.000" updated_at="2014-10-10 16:44:02.000" />
-    <file_sources id="102" project_uuid="projectUuid" file_uuid="uuidnew" data=",,,,foo&#13;&#10;,,,,bar&#13;&#10;,,,,biz&#13;&#10;" data_hash="0c43ed6418d690ee0ffc3e43e6660967" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-29 16:44:02.000" />
-</dataset>
-
-
-
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistNewFileWithScmAndHighlighting-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistNewFileWithScmAndHighlighting-result.xml
deleted file mode 100644 (file)
index 6f3788d..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<dataset>
-    <file_sources id="101" project_uuid="projectUuid" file_uuid="uuidsame" data=",,,,unchanged&#13;&#10;,,,,content&#13;&#10;" data_hash="ee716d4ed9faae16eb9167714442a3bc" created_at="2014-10-10 16:44:02.000" updated_at="2014-10-10 16:44:02.000" />
-    <file_sources id="102" project_uuid="projectUuid" file_uuid="uuidnew" data="123,julien,2014-10-11T16:44:02+0100,&#34;0,3,a&#34;,foo&#13;&#10;234,simon,2014-10-12T16:44:02+0100,&#34;0,1,cd&#34;,bar&#13;&#10;345,julien,2014-10-13T16:44:02+0100,&#34;0,9,c&#34;,biz&#13;&#10;" data_hash="a2aaee165e33957a67331fb9f869e0f1" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-29 16:44:02.000" />
-</dataset>
-
-
-
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistUpdateChanged-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistUpdateChanged-result.xml
deleted file mode 100644 (file)
index dfc771b..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-<dataset>
-
-  <file_sources id="101" project_uuid="projectUuid" file_uuid="uuidsame" data=",,,,changed&#13;&#10;,,,,content&#13;&#10;" data_hash="e41cca9c51ff853c748f708f39dfc035" created_at="2014-10-10 16:44:02.000" updated_at="2014-10-29 16:44:02.000" />
-
-</dataset>
index b089d7b117d6010445cb50ca976951a8af3d9c9f..87452635ede2a45a7bd8549ee04add22b096b4cb 100644 (file)
  */
 package org.sonar.core.source.db;
 
+import com.google.common.base.Charsets;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
 import java.util.Date;
 
 public class FileSourceDto {
@@ -27,7 +32,7 @@ public class FileSourceDto {
   private String fileUuid;
   private Date createdAt;
   private Date updatedAt;
-  private String data;
+  private byte[] data;
   private String dataHash;
 
   public Long getId() {
@@ -57,15 +62,25 @@ public class FileSourceDto {
     return this;
   }
 
-  public String getData() {
+  @CheckForNull
+  public byte[] getData() {
     return data;
   }
 
-  public FileSourceDto setData(String data) {
+  @CheckForNull
+  public String getStringData() {
+    return data != null ? new String(data, Charsets.UTF_8) : null;
+  }
+
+  public FileSourceDto setData(@Nullable byte[] data) {
     this.data = data;
     return this;
   }
 
+  public FileSourceDto setStringData(@Nullable String data) {
+    return setData(data != null ? data.getBytes(Charsets.UTF_8) : null);
+  }
+
   public String getDataHash() {
     return dataHash;
   }
index a2adccdc7d1ca4f6beb5f5ecab5d997e9d3686f4..0bae030da9fcfa1c38cde5e71008b0cda88f009a 100644 (file)
@@ -41,7 +41,7 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
   public void select() throws Exception {
     FileSourceDto fileSourceDto = dao.select("ab12");
 
-    assertThat(fileSourceDto.getData()).isEqualTo("public class Foo {public Foo(){}}");
+    assertThat(fileSourceDto.getStringData()).isEqualTo("public class Foo {public Foo(){}}");
     assertThat(fileSourceDto.getDataHash()).isEqualTo("hash");
     assertThat(fileSourceDto.getProjectUuid()).isEqualTo("abcd");
     assertThat(fileSourceDto.getFileUuid()).isEqualTo("ab12");
@@ -51,7 +51,7 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
 
   @Test
   public void insert() throws Exception {
-    dao.insert(new FileSourceDto().setProjectUuid("prj").setFileUuid("file").setData("bla bla").setDataHash("hash2")
+    dao.insert(new FileSourceDto().setProjectUuid("prj").setFileUuid("file").setStringData("bla bla").setDataHash("hash2")
       .setCreatedAt(DateUtils.parseDateTime("2014-10-31T16:44:02+0100"))
       .setUpdatedAt(DateUtils.parseDateTime("2014-10-31T16:44:02+0100")));
 
@@ -60,7 +60,7 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
 
   @Test
   public void update() throws Exception {
-    dao.update(new FileSourceDto().setId(101L).setProjectUuid("prj").setFileUuid("file").setData("bla bla").setDataHash("hash2")
+    dao.update(new FileSourceDto().setId(101L).setProjectUuid("prj").setFileUuid("file").setStringData("bla bla").setDataHash("hash2")
       .setUpdatedAt(DateUtils.parseDateTime("2014-10-31T16:44:02+0100")));
 
     checkTable("update", "file_sources");
index ebe11950e5020c076b857c14718318f3330a45d7..0f06cc6efea5904d4d867dca4361eafa77829ba9 100644 (file)
@@ -1,7 +1,7 @@
 <dataset>
 
-    <file_sources id="101" project_uuid="abcd" file_uuid="ab12" data="public class Foo {public Foo(){}}" data_hash="hash" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-30 16:44:02.000" />
+    <file_sources id="101" project_uuid="abcd" file_uuid="ab12" data="7075626c696320636c61737320466f6f207b7075626c696320466f6f28297b7d7d" data_hash="hash" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-30 16:44:02.000" />
 
-    <file_sources id="102" project_uuid="prj" file_uuid="file" data="bla bla" data_hash="hash2" created_at="2014-10-31 16:44:02.000" updated_at="2014-10-31 16:44:02.000" />
+    <file_sources id="102" project_uuid="prj" file_uuid="file" data="626c6120626c61" data_hash="hash2" created_at="2014-10-31 16:44:02.000" updated_at="2014-10-31 16:44:02.000" />
 
 </dataset>
index 8b6fc5cd77939a165dbadadc1010407cdee01f0c..19b2dec689645e1290c4b5f36b58783cde676621 100644 (file)
@@ -1,5 +1,5 @@
 <dataset>
 
-    <file_sources id="101" project_uuid="abcd" file_uuid="ab12" data="public class Foo {public Foo(){}}" data_hash="hash" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-30 16:44:02.000" />
+    <file_sources id="101" project_uuid="abcd" file_uuid="ab12" data="7075626c696320636c61737320466f6f207b7075626c696320466f6f28297b7d7d" data_hash="hash" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-30 16:44:02.000" />
 
 </dataset>
index dd3b16263634b57ef4faa58da0607ac2a4cbbd27..15e6bf6c0dc0eefa8941cb61f3de4ec02181288c 100644 (file)
@@ -1,5 +1,5 @@
 <dataset>
 
-    <file_sources id="101" project_uuid="abcd" file_uuid="ab12" data="bla bla" data_hash="hash2" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-31 16:44:02.000" />
+    <file_sources id="101" project_uuid="abcd" file_uuid="ab12" data="626c6120626c61" data_hash="hash2" created_at="2014-10-29 16:44:02.000" updated_at="2014-10-31 16:44:02.000" />
 
 </dataset>