]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5801 fix compatibility with all dbs
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 23 Nov 2014 22:05:25 +0000 (23:05 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 23 Nov 2014 22:05:25 +0000 (23:05 +0100)
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineResultSetIterator.java
server/sonar-server/src/test/java/org/sonar/server/computation/db/AnalysisReportDaoTest.java
server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineResultSetIteratorTest.java
sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
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 e315e83b0b4cb542973136bbb96142fe7bdecc68..ae0884e3a959d260fa7d7fc8636357245166b87c 100644 (file)
@@ -31,11 +31,13 @@ import org.sonar.server.db.DbClient;
 import org.sonar.server.db.ResultSetIterator;
 import org.sonar.server.db.migrations.SqlUtil;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.io.Reader;
-import java.sql.*;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Timestamp;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -82,7 +84,7 @@ class SourceLineResultSetIterator extends ResultSetIterator<Collection<SourceLin
     String projectUuid = rs.getString(1);
     String fileUuid = rs.getString(2);
     Date updatedAt = SqlUtil.getDate(rs, 4);
-    Reader dataStream = new InputStreamReader(new ByteArrayInputStream(rs.getBytes(5)));
+    Reader dataStream = rs.getClob(5).getCharacterStream();
 
     int line = 1;
     List<SourceLineDoc> lines = Lists.newArrayList();
index 0c78f41524c463ca7b669e7202676e45f2fd49cd..d10772367a9ad8d7c498457729bac23e153aae6a 100644 (file)
@@ -43,7 +43,6 @@ import static org.sonar.core.computation.db.AnalysisReportDto.Status.WORKING;
 
 public class AnalysisReportDaoTest {
   private static final String DEFAULT_PROJECT_KEY = "123456789-987654321";
-  private static final String DEFAULT_PROJECT_NAME = "default project name";
   private static final long DEFAULT_SNAPSHOT_ID = 123L;
 
   @Rule
@@ -288,14 +287,12 @@ public class AnalysisReportDaoTest {
   }
 
   private AnalysisReportDto newDefaultReport() {
-    AnalysisReportDto report = AnalysisReportDto.newForTests(1L)
+    return AnalysisReportDto.newForTests(1L)
       .setStatus(PENDING)
       .setProjectKey(DEFAULT_PROJECT_KEY)
       .setCreatedAt(DateUtils.parseDate("2014-09-30"))
       .setFinishedAt(DateUtils.parseDate("2014-09-30"))
       .setStartedAt(DateUtils.parseDate("2014-09-30"))
       .setUpdatedAt(DateUtils.parseDate("2014-09-30"));
-
-    return report;
   }
 }
index 0c0caad7658eec9f66438c52f714fbb3a8f351e4..ae3f3ad201b2f0e7a34951751955e1840cb870c7 100644 (file)
@@ -61,10 +61,10 @@ public class SourceLineResultSetIteratorTest {
     db.prepareDbUnit(getClass(), "source-with-scm.xml");
     Connection connection = db.openConnection();
     PreparedStatement stmt = connection.prepareStatement("UPDATE file_sources SET data = ? WHERE id=1");
-    stmt.setBytes(1, ("aef12a,alice,2014-04-25T12:34:56+0100,,class Foo {\r\n" +
+    stmt.setString(1, "aef12a,alice,2014-04-25T12:34:56+0100,,class Foo {\r\n" +
       "abe465,bob,2014-07-25T12:34:56+0100,,  // Empty\r\n" +
       "afb789,carol,2014-03-23T12:34:56+0100,,}\r\n" +
-      "afb789,carol,2014-03-23T12:34:56+0100,,\r\n").getBytes(Charsets.UTF_8));
+      "afb789,carol,2014-03-23T12:34:56+0100,,\r\n");
     stmt.executeUpdate();
 
     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
index 3867095ad879d81a5e31d373b5b91a5330c300a3..b0f1199117eb08d703fa961310e3bc1d9f777052 100644 (file)
@@ -152,7 +152,7 @@ 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).setStringData(newData)
+      FileSourceDto newFileSource = new FileSourceDto().setProjectUuid(projectTree.getRootProject().getUuid()).setFileUuid(fileUuid).setData(newData)
         .setDataHash(newDataHash)
         .setCreatedAt(now)
         .setUpdatedAt(now);
@@ -160,7 +160,7 @@ public class SourcePersister implements ScanPersister {
       session.commit();
     } else {
       if (!newDataHash.equals(previous.getDataHash())) {
-        previous.setStringData(newData).setDataHash(newDataHash).setUpdatedAt(now);
+        previous.setData(newData).setDataHash(newDataHash).setUpdatedAt(now);
         mapper.update(previous);
         session.commit();
       }
index 5efb6cf5ffc07b56cd83f962c9ca84133c036e2b..17226a69a4c384462e7c08e3d3f0fdb37418c507 100644 (file)
@@ -140,7 +140,7 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
     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(
+    assertThat(fileSourceDto.getData()).isEqualTo(
       ",,,,changed\r\n,,,,content\r\n");
     assertThat(fileSourceDto.getDataHash()).isEqualTo("e41cca9c51ff853c748f708f39dfc035");
   }
@@ -178,7 +178,7 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
     FileSourceDto fileSourceDto = new FileSourceDao(getMyBatis()).select("uuidnew");
     assertThat(fileSourceDto.getCreatedAt()).isEqualTo(now);
     assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now);
-    assertThat(fileSourceDto.getStringData()).isEqualTo(
+    assertThat(fileSourceDto.getData()).isEqualTo(
       ",,,,foo\r\n,,,,bar\r\n,,,,biz\r\n");
     assertThat(fileSourceDto.getDataHash()).isEqualTo("0c43ed6418d690ee0ffc3e43e6660967");
 
@@ -221,7 +221,7 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
     FileSourceDto fileSourceDto = new FileSourceDao(getMyBatis()).select("uuidnew");
     assertThat(fileSourceDto.getCreatedAt()).isEqualTo(now);
     assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now);
-    assertThat(fileSourceDto.getStringData()).isEqualTo(
+    assertThat(fileSourceDto.getData()).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");
index 87452635ede2a45a7bd8549ee04add22b096b4cb..3659ff2056946a5f9ec402f484d57c28003212ef 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 {
@@ -32,7 +29,7 @@ public class FileSourceDto {
   private String fileUuid;
   private Date createdAt;
   private Date updatedAt;
-  private byte[] data;
+  private String data;
   private String dataHash;
 
   public Long getId() {
@@ -63,24 +60,15 @@ public class FileSourceDto {
   }
 
   @CheckForNull
-  public byte[] getData() {
+  public String getData() {
     return data;
   }
 
-  @CheckForNull
-  public String getStringData() {
-    return data != null ? new String(data, Charsets.UTF_8) : null;
-  }
-
-  public FileSourceDto setData(@Nullable byte[] data) {
+  public FileSourceDto setData(@Nullable String 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 0bae030da9fcfa1c38cde5e71008b0cda88f009a..04c3ec923b500a455f3cd71d51e15d8e8d3e8cf0 100644 (file)
@@ -41,7 +41,7 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
   public void select() throws Exception {
     FileSourceDto fileSourceDto = dao.select("ab12");
 
-    assertThat(fileSourceDto.getStringData()).isEqualTo("public class Foo {public Foo(){}}");
+    assertThat(fileSourceDto.getData()).isEqualTo("aef12a,alice,2014-04-25T12:34:56+0100,,class 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").setStringData("bla bla").setDataHash("hash2")
+    dao.insert(new FileSourceDto().setProjectUuid("prj").setFileUuid("file").setData("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").setStringData("bla bla").setDataHash("hash2")
+    dao.update(new FileSourceDto().setId(101L).setProjectUuid("prj").setFileUuid("file").setData("updated data").setDataHash("hash2")
       .setUpdatedAt(DateUtils.parseDateTime("2014-10-31T16:44:02+0100")));
 
     checkTable("update", "file_sources");
index 0f06cc6efea5904d4d867dca4361eafa77829ba9..caa220868f4fc48a4560370c5d119e150871bdee 100644 (file)
@@ -1,7 +1,12 @@
 <dataset>
 
-    <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="101" project_uuid="abcd" file_uuid="ab12"
+                  data="aef12a,alice,2014-04-25T12:34:56+0100,,class 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="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" />
+
+    <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" />
 
 </dataset>
index 19b2dec689645e1290c4b5f36b58783cde676621..080676c67f2a38c575f357c59850faf3f1f3a948 100644 (file)
@@ -1,5 +1,7 @@
 <dataset>
 
-    <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="101" project_uuid="abcd" file_uuid="ab12"
+                  data="aef12a,alice,2014-04-25T12:34:56+0100,,class Foo" data_hash="hash"
+                  created_at="2014-10-29 16:44:02.000" updated_at="2014-10-30 16:44:02.000" />
 
 </dataset>
index 15e6bf6c0dc0eefa8941cb61f3de4ec02181288c..aee5ffcf405cfb37c3e991ee58316efec6e26c2b 100644 (file)
@@ -1,5 +1,8 @@
 <dataset>
 
-    <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" />
+    <file_sources id="101" project_uuid="abcd" file_uuid="ab12"
+                  data="updated data" data_hash="hash2"
+                  created_at="2014-10-29 16:44:02.000" updated_at="2014-10-31 16:44:02.000" />
+
 
 </dataset>