]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8178 ignore rows with corrupted data 1283/head
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 30 Sep 2016 08:35:08 +0000 (10:35 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 30 Sep 2016 09:14:08 +0000 (11:14 +0200)
server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java
server/sonar-server/src/test/java/org/sonar/server/test/index/TestResultSetIteratorTest.java

index 525a6b0a2f7d942f5ca5cf59129d3bc5a50ad460..86e81e7021d725e9fdadf0e33df0c2c654ce2945 100644 (file)
@@ -31,6 +31,7 @@ import java.util.Date;
 import java.util.List;
 import javax.annotation.Nullable;
 import org.elasticsearch.action.update.UpdateRequest;
+import org.sonar.api.utils.log.Loggers;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -78,14 +79,20 @@ public class TestResultSetIterator extends ResultSetIterator<Row> {
     String projectUuid = rs.getString(1);
     String fileUuid = rs.getString(2);
     Date updatedAt = new Date(rs.getLong(3));
-    InputStream dataInput = rs.getBinaryStream(4);
-    List<DbFileSources.Test> data;
+    List<DbFileSources.Test> tests = parseData(fileUuid, rs.getBinaryStream(4));
+    return toRow(projectUuid, fileUuid, updatedAt, tests);
+  }
+
+  private static List<DbFileSources.Test> parseData(String fileUuid, @Nullable InputStream dataInput) {
+    List<DbFileSources.Test> tests = Collections.emptyList();
     if (dataInput != null) {
-      data = FileSourceDto.decodeTestData(dataInput);
-    } else {
-      data = Collections.emptyList();
+      try {
+        tests = FileSourceDto.decodeTestData(dataInput);
+      } catch (Exception e) {
+        Loggers.get(TestResultSetIterator.class).warn(String.format("Invalid file_sources.binary_data on row with file_uuid='%s', test file will be ignored", fileUuid), e);
+      }
     }
-    return toRow(projectUuid, fileUuid, updatedAt, data);
+    return tests;
   }
 
   /**
index 46d484986b48f5339cc2e66dd696a209522aeffd..ac4b0806ee3ca8dfcbd22a6c5f0657d2b30cad83 100644 (file)
@@ -28,13 +28,14 @@ import org.junit.After;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.db.DbTester;
 import org.sonar.db.protobuf.DbFileSources;
 import org.sonar.server.source.index.FileSourcesUpdaterHelper;
 import org.sonar.server.test.db.TestTesting;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.fail;
 import static org.assertj.core.data.MapEntry.entry;
 import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILES;
 import static org.sonar.server.test.index.TestIndexDefinition.FIELD_DURATION_IN_MS;
@@ -53,6 +54,9 @@ public class TestResultSetIteratorTest {
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
+  @Rule
+  public LogTester logTester = new LogTester();
+
   TestResultSetIterator underTest;
 
   private static List<DbFileSources.Test> newFakeTests(int numberOfTests) {
@@ -186,19 +190,18 @@ public class TestResultSetIteratorTest {
   }
 
   @Test
-  public void fail_on_bad_data_format() throws Exception {
+  public void read_does_not_fail_if_corrupted_data() throws Exception {
     dbTester.prepareDbUnit(getClass(), "shared.xml");
 
     TestTesting.updateDataColumn(dbTester.getSession(), "F1", "THIS_IS_NOT_PROTOBUF".getBytes());
 
     underTest = TestResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 0L, null);
-    try {
-      assertThat(underTest.hasNext()).isTrue();
-      underTest.next();
-      fail("it should not be possible to go through not compliant data");
-    } catch (IllegalStateException e) {
-      // ok
-    }
+    FileSourcesUpdaterHelper.Row row = underTest.next();
+    assertThat(row.getFileUuid()).isEqualTo("F1");
+    assertThat(row.getUpdateRequests()).isEmpty();
+    assertThat(underTest.hasNext()).isFalse();
+
+    assertThat(logTester.logs(LoggerLevel.WARN)).contains("Invalid file_sources.binary_data on row with file_uuid='F1', test file will be ignored");
   }
 
   @Test