]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 25 Feb 2015 14:27:46 +0000 (15:27 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 25 Feb 2015 14:27:55 +0000 (15:27 +0100)
sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java
sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueValueCoder.java
sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java [new file with mode: 0644]

index 7d67696196eb0747ac06ce7b9c18646eb8c85b90..9b5b26f730ba8cb90217eb59850d7b17a4933233 100644 (file)
@@ -44,6 +44,8 @@ import org.sonar.core.component.ComponentKeys;
 import org.sonar.core.issue.IssueUpdater;
 import org.sonar.core.issue.workflow.IssueWorkflow;
 
+import javax.annotation.CheckForNull;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
@@ -111,21 +113,11 @@ public class LocalIssueTracking implements BatchComponent {
     }
 
     // all the issues that are not closed in db before starting this module scan, including manual issues
-    Collection<ServerIssue> previousIssues = new ArrayList<>();
-    for (org.sonar.batch.protocol.input.BatchInput.ServerIssue previousIssue : serverIssueRepository.byComponent(component)) {
-      previousIssues.add(new ServerIssueFromWs(previousIssue));
-    }
+    Collection<ServerIssue> serverIssues = loadServerIssues(component);
 
-    SourceHashHolder sourceHashHolder = null;
-    if (component.isFile()) {
-      DefaultInputFile file = (DefaultInputFile) inputPathCache.getInputPath(component);
-      if (file == null) {
-        throw new IllegalStateException("Resource " + component.resource() + " was not found in InputPath cache");
-      }
-      sourceHashHolder = new SourceHashHolder((DefaultInputFile) file, lastLineHashes);
-    }
+    SourceHashHolder sourceHashHolder = loadSourceHashes(component);
 
-    IssueTrackingResult trackingResult = tracking.track(sourceHashHolder, previousIssues, issues);
+    IssueTrackingResult trackingResult = tracking.track(sourceHashHolder, serverIssues, issues);
 
     // unmatched = issues that have been resolved + issues on disabled/removed rules + manual issues
     addUnmatched(trackingResult.unmatched(), sourceHashHolder, issues);
@@ -143,6 +135,27 @@ public class LocalIssueTracking implements BatchComponent {
     }
   }
 
+  @CheckForNull
+  private SourceHashHolder loadSourceHashes(BatchResource component) {
+    SourceHashHolder sourceHashHolder = null;
+    if (component.isFile()) {
+      DefaultInputFile file = (DefaultInputFile) inputPathCache.getInputPath(component);
+      if (file == null) {
+        throw new IllegalStateException("Resource " + component.resource() + " was not found in InputPath cache");
+      }
+      sourceHashHolder = new SourceHashHolder((DefaultInputFile) file, lastLineHashes);
+    }
+    return sourceHashHolder;
+  }
+
+  private Collection<ServerIssue> loadServerIssues(BatchResource component) {
+    Collection<ServerIssue> serverIssues = new ArrayList<>();
+    for (org.sonar.batch.protocol.input.BatchInput.ServerIssue previousIssue : serverIssueRepository.byComponent(component)) {
+      serverIssues.add(new ServerIssueFromWs(previousIssue));
+    }
+    return serverIssues;
+  }
+
   @VisibleForTesting
   protected void mergeMatched(IssueTrackingResult result) {
     for (DefaultIssue issue : result.matched()) {
index 321d09a852bfc420341ace1483e17a6b405cbd82..d3c2ee8bb932be8952b7570b6bee46a569611074 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.batch.issue.tracking;
 import com.persistit.Value;
 import com.persistit.encoding.CoderContext;
 import com.persistit.encoding.ValueCoder;
-import com.persistit.exception.ConversionException;
 import org.sonar.batch.protocol.input.BatchInput.ServerIssue;
 
 import java.io.IOException;
@@ -30,13 +29,13 @@ import java.io.IOException;
 public class ServerIssueValueCoder implements ValueCoder {
 
   @Override
-  public void put(Value value, Object object, CoderContext context) throws ConversionException {
+  public void put(Value value, Object object, CoderContext context) {
     ServerIssue issue = (ServerIssue) object;
     value.putByteArray(issue.toByteArray());
   }
 
   @Override
-  public Object get(Value value, Class<?> clazz, CoderContext context) throws ConversionException {
+  public Object get(Value value, Class<?> clazz, CoderContext context) {
     try {
       return ServerIssue.parseFrom(value.getByteArray());
     } catch (IOException e) {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java
new file mode 100644 (file)
index 0000000..4cc756f
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.batch.repository;
+
+import com.google.common.base.Function;
+import com.google.common.io.InputSupplier;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.batch.bootstrap.ServerClient;
+import org.sonar.batch.protocol.input.BatchInput;
+import org.sonar.batch.protocol.input.BatchInput.ServerIssue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class DefaultServerIssuesLoaderTest {
+  private DefaultServerIssuesLoader loader;
+  private ServerClient serverClient;
+
+  @Before
+  public void prepare() {
+    serverClient = mock(ServerClient.class);
+    loader = new DefaultServerIssuesLoader(serverClient);
+  }
+
+  @Test
+  public void loadFromWs() throws Exception {
+    InputSupplier<InputStream> is = mock(InputSupplier.class);
+    when(serverClient.doRequest("/batch/issues?key=foo", "GET", null)).thenReturn(is);
+
+    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+    ServerIssue.newBuilder().setKey("ab1").build()
+      .writeDelimitedTo(bos);
+    ServerIssue.newBuilder().setKey("ab2").build()
+      .writeDelimitedTo(bos);
+
+    when(is.getInput()).thenReturn(new ByteArrayInputStream(bos.toByteArray()));
+
+    final List<ServerIssue> result = new ArrayList<>();
+    loader.load("foo", new Function<BatchInput.ServerIssue, Void>() {
+
+      @Override
+      public Void apply(ServerIssue input) {
+        result.add(input);
+        return null;
+      }
+    }, false);
+
+    assertThat(result).extracting("key").containsExactly("ab1", "ab2");
+  }
+}