]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5960 SONAR-5906 guess author and assignee of new issues
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 20 Jan 2015 22:25:47 +0000 (23:25 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 21 Jan 2015 11:10:18 +0000 (12:10 +0100)
29 files changed:
server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportService.java
server/sonar-server/src/main/java/org/sonar/server/computation/ComputationComponents.java
server/sonar-server/src/main/java/org/sonar/server/computation/ComputationService.java
server/sonar-server/src/main/java/org/sonar/server/computation/issue/FinalIssues.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/issue/IssueCache.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/issue/IssueComputation.java
server/sonar-server/src/main/java/org/sonar/server/computation/issue/RuleCache.java
server/sonar-server/src/main/java/org/sonar/server/computation/issue/RuleCacheLoader.java
server/sonar-server/src/main/java/org/sonar/server/computation/issue/ScmAccountCache.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/issue/ScmAccountCacheLoader.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/issue/SourceLinesCache.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistIssuesStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/SendIssueNotificationsStep.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineResultSetIterator.java
server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java
server/sonar-server/src/main/java/org/sonar/server/util/cache/MemoryCache.java
server/sonar-server/src/test/java/org/sonar/server/computation/issue/IssueComputationTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/issue/RuleCacheLoaderTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/issue/RuleCacheTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/issue/SourceLinesCacheTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/util/cache/MemoryCacheTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/issue/RuleCacheLoaderTest/shared.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest/user1.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/computation/issue/SourceLinesCacheTest/load_data.xml [new file with mode: 0644]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/component/ReportComponent.java
sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java
sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDao.java
sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDto.java

index eef338fca44cbec2d263dabae5ec0797520ba41f..9a48c923812529deef4af8d25b85d54ae92ce745 100644 (file)
@@ -35,6 +35,8 @@ import org.sonar.batch.protocol.output.component.ReportComponents;
 import org.sonar.batch.protocol.output.issue.ReportIssue;
 import org.sonar.server.computation.issue.IssueComputation;
 
+import javax.annotation.Nullable;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -76,14 +78,14 @@ public class AnalysisReportService {
 
   private void browseComponent(ComputationContext context, ReportHelper helper, ReportComponent component) {
     Iterable<ReportIssue> reportIssues = helper.getIssues(component.batchId());
-    browseComponentIssues(context, reportIssues);
+    browseComponentIssues(context, component, reportIssues);
     for (ReportComponent child : component.children()) {
       browseComponent(context, helper, child);
     }
   }
 
-  private void browseComponentIssues(final ComputationContext context, Iterable<ReportIssue> reportIssues) {
-    issueComputation.processComponentIssues(Iterables.transform(reportIssues, new Function<ReportIssue, DefaultIssue>() {
+  private void browseComponentIssues(final ComputationContext context, ReportComponent component, Iterable<ReportIssue> reportIssues) {
+    issueComputation.processComponentIssues(component.uuid(), Iterables.transform(reportIssues, new Function<ReportIssue, DefaultIssue>() {
       @Override
       public DefaultIssue apply(ReportIssue input) {
         return toIssue(context, input);
@@ -94,7 +96,8 @@ public class AnalysisReportService {
   private DefaultIssue toIssue(ComputationContext context, ReportIssue issue) {
     DefaultIssue defaultIssue = new DefaultIssue();
     defaultIssue.setKey(issue.key());
-    setComponentId(defaultIssue, context.getComponentByBatchId(issue.componentBatchId()));
+    ReportComponent component = context.getComponentByBatchId(issue.componentBatchId());
+    setComponent(defaultIssue, component);
     defaultIssue.setRuleKey(RuleKey.of(issue.ruleRepo(), issue.ruleKey()));
     defaultIssue.setSeverity(issue.severity());
     defaultIssue.setManualSeverity(issue.isManualSeverity());
@@ -130,14 +133,15 @@ public class AnalysisReportService {
     return issue;
   }
 
-  private DefaultIssue setComponentId(DefaultIssue issue, ReportComponent component) {
+  private DefaultIssue setComponent(DefaultIssue issue, @Nullable ReportComponent component) {
     if (component != null) {
-      issue.setComponentId((long) component.id());
+      issue.setComponentId((long)component.id());
+      issue.setComponentUuid(component.uuid());
     }
     return issue;
   }
 
-  private DefaultIssue setDebt(DefaultIssue issue, Long debt) {
+  private DefaultIssue setDebt(DefaultIssue issue, @Nullable Long debt) {
     if (debt != null) {
       issue.setDebt(Duration.create(debt));
     }
index 98397d982d6c2e9f959bbf7efdb17efc5dbf2357..46c1e3c82f3f7f1e97623242b5a9097a08400193 100644 (file)
 package org.sonar.server.computation;
 
 import org.sonar.core.issue.db.UpdateConflictResolver;
+import org.sonar.server.computation.issue.IssueCache;
 import org.sonar.server.computation.issue.IssueComputation;
-import org.sonar.server.computation.issue.FinalIssues;
 import org.sonar.server.computation.issue.RuleCache;
 import org.sonar.server.computation.issue.RuleCacheLoader;
+import org.sonar.server.computation.issue.ScmAccountCache;
+import org.sonar.server.computation.issue.ScmAccountCacheLoader;
+import org.sonar.server.computation.issue.SourceLinesCache;
 import org.sonar.server.computation.step.ComputationSteps;
 
 import java.util.Arrays;
@@ -42,10 +45,13 @@ public class ComputationComponents {
       AnalysisReportService.class,
 
       // issues
+      ScmAccountCacheLoader.class,
+      ScmAccountCache.class,
+      SourceLinesCache.class,
       IssueComputation.class,
       RuleCache.class,
       RuleCacheLoader.class,
-      FinalIssues.class,
+      IssueCache.class,
       UpdateConflictResolver.class);
   }
 }
index d76f3a130f9634b0783fae6086a7220f7a2c7cfb..804d69fa62845f1bda54836e3484cee3c5a26033 100644 (file)
@@ -98,6 +98,7 @@ public class ComputationService implements ServerComponent {
     try {
       report.setFinishedAt(System2.INSTANCE.now());
       activityService.write(session, Activity.Type.ANALYSIS_REPORT, new AnalysisReportLog(report, project));
+      session.commit();
     } finally {
       MyBatis.closeQuietly(session);
     }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/FinalIssues.java b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/FinalIssues.java
deleted file mode 100644 (file)
index 03cb9c1..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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.server.computation.issue;
-
-import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.utils.System2;
-import org.sonar.api.utils.TempFolder;
-import org.sonar.server.util.cache.DiskCache;
-
-import java.io.IOException;
-
-/**
- * Cache of all the issues involved in the analysis. Their state is as it will be
- * persisted in database (after issue tracking, auto-assignment, ...)
- *
- */
-public class FinalIssues extends DiskCache<DefaultIssue> {
-
-  public FinalIssues(TempFolder tempFolder, System2 system2) throws IOException {
-    super(tempFolder.newFile("issues", ".dat"), system2);
-  }
-
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/IssueCache.java b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/IssueCache.java
new file mode 100644 (file)
index 0000000..2a0303d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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.server.computation.issue;
+
+import org.sonar.api.issue.internal.DefaultIssue;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.TempFolder;
+import org.sonar.server.util.cache.DiskCache;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Cache of all the issues involved in the analysis. Their state is as it will be
+ * persisted in database (after issue tracking, auto-assignment, ...)
+ *
+ */
+public class IssueCache extends DiskCache<DefaultIssue> {
+
+  public IssueCache(TempFolder tempFolder, System2 system2) throws IOException {
+    super(tempFolder.newFile("issues", ".dat"), system2);
+  }
+
+  IssueCache(File file, System2 system2) {
+    super(file, system2);
+  }
+}
index f6f53dd7ac1d6cf667928062a281dc26430ae3c7..01b61913eea68cdbb1b0b2ee251c2a907520e537 100644 (file)
@@ -27,14 +27,20 @@ import org.sonar.server.util.cache.DiskCache;
 public class IssueComputation {
 
   private final RuleCache ruleCache;
+  private final ScmAccountCache scmAccountCache;
+  private final SourceLinesCache linesCache;
   private final DiskCache<DefaultIssue>.DiskAppender finalIssuesAppender;
 
-  public IssueComputation(RuleCache ruleCache, FinalIssues finalIssues) {
+  public IssueComputation(RuleCache ruleCache, SourceLinesCache linesCache, ScmAccountCache scmAccountCache,
+                          IssueCache issueCache) {
     this.ruleCache = ruleCache;
-    this.finalIssuesAppender = finalIssues.newAppender();
+    this.linesCache = linesCache;
+    this.scmAccountCache = scmAccountCache;
+    this.finalIssuesAppender = issueCache.newAppender();
   }
 
-  public void processComponentIssues(Iterable<DefaultIssue> issues) {
+  public void processComponentIssues(String componentUuid, Iterable<DefaultIssue> issues) {
+    linesCache.init(componentUuid);
     for (DefaultIssue issue : issues) {
       if (issue.isNew()) {
         guessAuthor(issue);
@@ -44,6 +50,7 @@ public class IssueComputation {
       }
       finalIssuesAppender.append(issue);
     }
+    linesCache.clear();
   }
 
   public void afterReportProcessing() {
@@ -51,11 +58,16 @@ public class IssueComputation {
   }
 
   private void guessAuthor(DefaultIssue issue) {
-    // TODO
+    if (issue.line() != null) {
+      issue.setAuthorLogin(linesCache.lineAuthor(issue.line()));
+    }
   }
 
   private void autoAssign(DefaultIssue issue) {
-    // TODO
+    String scmAccount = issue.authorLogin();
+    if (scmAccount != null) {
+      issue.setAssignee(scmAccountCache.getNullable(scmAccount));
+    }
   }
 
   private void copyRuleTags(DefaultIssue issue) {
index c739cbef4e710ca10ceeb85f6613e4af9148651b..6b6a07a681d1637701c3a02c2b96053d7c0beeb0 100644 (file)
@@ -36,7 +36,7 @@ public class RuleCache extends MemoryCache<RuleKey, RuleDto> {
 
   @CheckForNull
   public String ruleName(RuleKey key) {
-    RuleDto rule = get(key);
+    RuleDto rule = getNullable(key);
     return rule != null ? rule.getName() : null;
   }
 }
index ab467b60584d08ba08299177621b87648ea02282..c6936344a6c112aa84e6ed490f7b07b0f5e0f2a1 100644 (file)
@@ -22,12 +22,10 @@ package org.sonar.server.computation.issue;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.rule.RuleDto;
-import org.sonar.server.util.cache.CacheLoader;
 import org.sonar.server.db.DbClient;
+import org.sonar.server.util.cache.CacheLoader;
 
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 public class RuleCacheLoader implements CacheLoader<RuleKey, RuleDto> {
@@ -50,16 +48,6 @@ public class RuleCacheLoader implements CacheLoader<RuleKey, RuleDto> {
 
   @Override
   public Map<RuleKey, RuleDto> loadAll(Collection<? extends RuleKey> keys) {
-    Map<RuleKey, RuleDto> result = new HashMap<>();
-    DbSession session = dbClient.openSession(false);
-    try {
-      List<RuleDto> dtos = dbClient.ruleDao().getByKeys(session, (Collection<RuleKey>) keys);
-      for (RuleDto dto : dtos) {
-        result.put(dto.getKey(), dto);
-      }
-      return result;
-    } finally {
-      session.close();
-    }
+    throw new UnsupportedOperationException("See RuleDao");
   }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/ScmAccountCache.java b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/ScmAccountCache.java
new file mode 100644 (file)
index 0000000..1170c2d
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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.server.computation.issue;
+
+import org.sonar.server.util.cache.MemoryCache;
+
+/**
+ * Cache of dictionary {SCM account -> SQ user login}
+ */
+public class ScmAccountCache extends MemoryCache<String,String> {
+  public ScmAccountCache(ScmAccountCacheLoader loader) {
+    super(loader);
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/ScmAccountCacheLoader.java b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/ScmAccountCacheLoader.java
new file mode 100644 (file)
index 0000000..6a74a52
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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.server.computation.issue;
+
+import org.sonar.server.user.index.UserDoc;
+import org.sonar.server.user.index.UserIndex;
+import org.sonar.server.util.cache.CacheLoader;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Loads the association between a SCM account and a SQ user
+ */
+public class ScmAccountCacheLoader implements CacheLoader<String,String> {
+
+  private final UserIndex index;
+
+  public ScmAccountCacheLoader(UserIndex index) {
+    this.index = index;
+  }
+
+  @Override
+  public String load(String scmAccount) {
+    UserDoc user = index.getNullableByScmAccount(scmAccount);
+    return user != null ? user.login() : null;
+  }
+
+  @Override
+  public Map<String, String> loadAll(Collection<? extends String> scmAccounts) {
+    throw new UnsupportedOperationException("Loading by multiple scm accounts is not supported yet");
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/SourceLinesCache.java b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/SourceLinesCache.java
new file mode 100644 (file)
index 0000000..09bcbcb
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * 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.server.computation.issue;
+
+import com.google.common.base.Function;
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVParser;
+import org.apache.commons.csv.CSVRecord;
+import org.apache.commons.io.IOUtils;
+import org.sonar.core.source.db.FileSourceDto;
+import org.sonar.server.db.DbClient;
+
+import javax.annotation.CheckForNull;
+
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Cache of the lines of the currently processed file. Only a single file
+ * is kept in memory at a time. Moreover data is loaded on demand to avoid
+ * useless db trips.
+ * <p/>
+ * It assumes that db table FILE_SOURCES is up-to-date before using this
+ * cache.
+ */
+public class SourceLinesCache {
+
+  private final DbClient dbClient;
+  private final List<String> authors = new ArrayList<>();
+  private final FileDataParser parserFunction = new FileDataParser();
+  private boolean loaded = false;
+  private String currentFileUuid = null;
+
+  public SourceLinesCache(DbClient dbClient) {
+    this.dbClient = dbClient;
+  }
+
+  /**
+   * Marks the currently processed component
+   */
+  void init(String fileUuid) {
+    loaded = false;
+    currentFileUuid = fileUuid;
+  }
+
+  /**
+   * Last committer of the line, can be null.
+   * @param lineId starts at 1
+   */
+  @CheckForNull
+  public String lineAuthor(int lineId) {
+    loadIfNeeded();
+    if (lineId <= authors.size()) {
+      return authors.get(lineId - 1);
+    }
+    return null;
+  }
+
+  /**
+   * Load only on demand, to avoid useless db requests on files without any new issues
+   */
+  private void loadIfNeeded() {
+    if (!loaded) {
+      dbClient.fileSourceDao().readDataStream(currentFileUuid, parserFunction);
+      loaded = true;
+    }
+  }
+
+  /**
+   * Makes cache eligible to GC
+   */
+  public void clear() {
+    authors.clear();
+  }
+
+  /**
+   * Number of lines in cache of the current file
+   */
+  int countLines() {
+    return authors.size();
+  }
+
+  class FileDataParser implements Function<Reader, Void> {
+    @Override
+    public Void apply(Reader input) {
+      CSVParser csvParser = null;
+      try {
+        csvParser = new CSVParser(input, CSVFormat.DEFAULT);
+        authors.clear();
+        for (CSVRecord csvRecord : csvParser) {
+          // do not keep all fields in memory
+          String author = csvRecord.get(FileSourceDto.CSV_INDEX_SCM_AUTHOR);
+          authors.add(author);
+        }
+        return null;
+      } catch (Exception e) {
+        throw new IllegalStateException("Fail to parse CSV data", e);
+      } finally {
+        IOUtils.closeQuietly(csvParser);
+      }
+    }
+  }
+}
index 1fd281b67be251dc7dedcca6e91a9098cd0b3a91..e64ce2c590f91b5d2b97ed254e99b9093510a887 100644 (file)
@@ -34,7 +34,7 @@ import org.sonar.core.persistence.BatchSession;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.persistence.MyBatis;
 import org.sonar.server.computation.ComputationContext;
-import org.sonar.server.computation.issue.FinalIssues;
+import org.sonar.server.computation.issue.IssueCache;
 import org.sonar.server.computation.issue.RuleCache;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.util.CloseableIterator;
@@ -45,15 +45,15 @@ public class PersistIssuesStep implements ComputationStep {
   private final System2 system2;
   private final UpdateConflictResolver conflictResolver;
   private final RuleCache ruleCache;
-  private final FinalIssues finalIssues;
+  private final IssueCache issueCache;
 
   public PersistIssuesStep(DbClient dbClient, System2 system2, UpdateConflictResolver conflictResolver,
-    RuleCache ruleCache, FinalIssues finalIssues) {
+    RuleCache ruleCache, IssueCache issueCache) {
     this.dbClient = dbClient;
     this.system2 = system2;
     this.conflictResolver = conflictResolver;
     this.ruleCache = ruleCache;
-    this.finalIssues = finalIssues;
+    this.issueCache = issueCache;
   }
 
   @Override
@@ -63,7 +63,7 @@ public class PersistIssuesStep implements ComputationStep {
     IssueChangeMapper changeMapper = session.getMapper(IssueChangeMapper.class);
     int count = 0;
 
-    CloseableIterator<DefaultIssue> issues = finalIssues.traverse();
+    CloseableIterator<DefaultIssue> issues = issueCache.traverse();
     try {
       while (issues.hasNext()) {
         DefaultIssue issue = issues.next();
index 3b47050f96675d50168822a8693e25c561a68171..befe904d961eb92b51cf8ddfbb8c12f78ee33f93 100644 (file)
@@ -28,7 +28,7 @@ import org.sonar.api.rule.Severity;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.core.component.ComponentDto;
 import org.sonar.server.computation.ComputationContext;
-import org.sonar.server.computation.issue.FinalIssues;
+import org.sonar.server.computation.issue.IssueCache;
 import org.sonar.server.computation.issue.RuleCache;
 import org.sonar.server.issue.notification.IssueNotifications;
 import org.sonar.server.util.CloseableIterator;
@@ -40,13 +40,13 @@ import org.sonar.server.util.CloseableIterator;
  */
 public class SendIssueNotificationsStep implements ComputationStep {
 
-  private final FinalIssues finalIssues;
+  private final IssueCache issueCache;
   private final RuleCache rules;
   private final IssueNotifications service;
 
-  public SendIssueNotificationsStep(FinalIssues finalIssues, RuleCache rules,
+  public SendIssueNotificationsStep(IssueCache issueCache, RuleCache rules,
     IssueNotifications service) {
-    this.finalIssues = finalIssues;
+    this.issueCache = issueCache;
     this.rules = rules;
     this.service = service;
   }
@@ -54,7 +54,7 @@ public class SendIssueNotificationsStep implements ComputationStep {
   @Override
   public void execute(ComputationContext context) {
     NewIssuesStatistics newIssuesStatistics = new NewIssuesStatistics();
-    CloseableIterator<DefaultIssue> issues = finalIssues.traverse();
+    CloseableIterator<DefaultIssue> issues = issueCache.traverse();
     try {
       while (issues.hasNext()) {
         DefaultIssue issue = issues.next();
index aaa1650d24db1d7ce0f3c398327e7c329a8f3066..28c318f95c6fb3b62c96a5717eb494be83536097 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.math.NumberUtils;
 import org.sonar.api.utils.DateUtils;
+import org.sonar.core.source.db.FileSourceDto;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.db.ResultSetIterator;
 import org.sonar.server.db.migrations.SqlUtil;
@@ -136,26 +137,27 @@ public class SourceLineResultSetIterator extends ResultSetIterator<SourceLineRes
         doc.setFileUuid(fileUuid);
         doc.setLine(line);
         doc.setUpdateDate(updatedDate);
-        doc.setScmRevision(csvRecord.get(0));
-        doc.setScmAuthor(csvRecord.get(1));
-        doc.setScmDate(DateUtils.parseDateTimeQuietly(csvRecord.get(2)));
+        doc.setScmRevision(csvRecord.get(FileSourceDto.CSV_INDEX_SCM_REVISION));
+        doc.setScmAuthor(csvRecord.get(FileSourceDto.CSV_INDEX_SCM_AUTHOR));
+        doc.setScmDate(DateUtils.parseDateTimeQuietly(csvRecord.get(FileSourceDto.CSV_INDEX_SCM_DATE)));
         // UT
-        doc.setUtLineHits(parseIntegerFromRecord(csvRecord.get(3)));
-        doc.setUtConditions(parseIntegerFromRecord(csvRecord.get(4)));
-        doc.setUtCoveredConditions(parseIntegerFromRecord(csvRecord.get(5)));
+        doc.setUtLineHits(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_UT_LINE_HITS)));
+        doc.setUtConditions(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_UT_CONDITIONS)));
+        doc.setUtCoveredConditions(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_UT_COVERED_CONDITIONS)));
         // IT
-        doc.setItLineHits(parseIntegerFromRecord(csvRecord.get(6)));
-        doc.setItConditions(parseIntegerFromRecord(csvRecord.get(7)));
-        doc.setItCoveredConditions(parseIntegerFromRecord(csvRecord.get(8)));
+        doc.setItLineHits(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_IT_LINE_HITS)));
+        doc.setItConditions(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_IT_CONDITIONS)));
+        doc.setItCoveredConditions(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_IT_COVERED_CONDITIONS)));
         // OVERALL
-        doc.setOverallLineHits(parseIntegerFromRecord(csvRecord.get(9)));
-        doc.setOverallConditions(parseIntegerFromRecord(csvRecord.get(10)));
-        doc.setOverallCoveredConditions(parseIntegerFromRecord(csvRecord.get(11)));
-        doc.setHighlighting(csvRecord.get(12));
-        doc.setSymbols(csvRecord.get(13));
-
-        doc.setDuplications(parseDuplications(csvRecord.get(14)));
-        doc.setSource(csvRecord.get(csvRecord.size() - 1));
+        doc.setOverallLineHits(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_OVERALL_LINE_HITS)));
+        doc.setOverallConditions(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_OVERALL_CONDITIONS)));
+        doc.setOverallCoveredConditions(parseIntegerFromRecord(csvRecord.get(FileSourceDto.CSV_INDEX_OVERALL_COVERED_CONDITIONS)));
+
+        doc.setHighlighting(csvRecord.get(FileSourceDto.CSV_INDEX_HIGHLIGHTING));
+        doc.setSymbols(csvRecord.get(FileSourceDto.CSV_INDEX_SYMBOLS));
+
+        doc.setDuplications(parseDuplications(csvRecord.get(FileSourceDto.CSV_INDEX_DUPLICATIONS)));
+        doc.setSource(csvRecord.get(FileSourceDto.CSV_INDEX_SOURCE));
 
         result.addLine(doc);
 
index b988dd3143203b87bd3c239455a8e7f77c097d50..6e610ff74b6ac8d9818b47c80f0a1293f38d6211 100644 (file)
@@ -27,7 +27,7 @@ import org.sonar.server.es.IndexDefinition;
 import org.sonar.server.es.NewIndex;
 
 /**
- * Definition of ES index "issues", including settings and fields.
+ * Definition of ES index "users", including settings and fields.
  */
 public class UserIndexDefinition implements IndexDefinition {
 
@@ -61,7 +61,7 @@ public class UserIndexDefinition implements IndexDefinition {
       // else keep defaults (one shard)
     }
 
-    // type "users"
+    // type "user"
     NewIndex.NewIndexType mapping = index.createType(TYPE_USER);
     mapping.setAttribute("_id", ImmutableMap.of("path", FIELD_LOGIN));
     mapping.stringFieldBuilder(FIELD_LOGIN).build();
index d29c11c0b6a06d0a363155ef14d9d5901613d0b7..1d158d5fbecbb4f44c979d6353b9406fb159b1da 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.server.util.cache;
 
+import org.sonar.server.exceptions.NotFoundException;
+
 import javax.annotation.CheckForNull;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -54,11 +56,15 @@ public class MemoryCache<K, V> {
   public V get(K key) {
     V value = getNullable(key);
     if (value == null) {
-      throw new IllegalArgumentException("Not found: " + key);
+      throw new NotFoundException("Not found: " + key);
     }
     return value;
   }
 
+  /**
+   * Get values associated with keys. All the requested keys are included
+   * in the Map result. Value is null if the key is not found in cache.
+   */
   public Map<K, V> getAll(Iterable<K> keys) {
     List<K> missingKeys = new ArrayList<>();
     Map<K, V> result = new HashMap<>();
@@ -74,6 +80,12 @@ public class MemoryCache<K, V> {
       Map<K, V> missingValues = loader.loadAll(missingKeys);
       map.putAll(missingValues);
       result.putAll(missingValues);
+      for (K missingKey : missingKeys) {
+        if (!map.containsKey(missingKey)) {
+          map.put(missingKey, null);
+          result.put(missingKey, null);
+        }
+      }
     }
     return result;
   }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/IssueComputationTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/IssueComputationTest.java
new file mode 100644 (file)
index 0000000..fbade0f
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * 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.server.computation.issue;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterators;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.issue.internal.DefaultIssue;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.System2;
+import org.sonar.core.rule.RuleDto;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class IssueComputationTest {
+
+  public static final RuleKey RULE_KEY = RuleKey.of("squid", "R1");
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  // inputs
+  RuleCache ruleCache = mock(RuleCache.class);
+  SourceLinesCache lineCache = mock(SourceLinesCache.class);
+  ScmAccountCache scmAccountCache = mock(ScmAccountCache.class);
+  DefaultIssue issue = new DefaultIssue().setRuleKey(RULE_KEY).setKey("ISSUE_A");
+  RuleDto rule = new RuleDto().setRepositoryKey(RULE_KEY.repository()).setRuleKey(RULE_KEY.rule());
+
+  // output
+  IssueCache issueCache;
+
+  IssueComputation sut;
+
+  @Before
+  public void setUp() throws IOException {
+    when(ruleCache.get(RULE_KEY)).thenReturn(rule);
+    issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
+    sut = new IssueComputation(ruleCache, lineCache, scmAccountCache, issueCache);
+  }
+
+  @Test
+  public void store_issues_on_disk() throws Exception {
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).key()).isEqualTo("ISSUE_A");
+  }
+
+  @Test
+  public void copy_rule_tags_on_new_issues() throws Exception {
+    issue.setNew(true);
+    rule.setTags(ImmutableSet.of("bug", "performance"));
+    rule.setSystemTags(ImmutableSet.of("blocker"));
+
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).tags()).containsOnly("blocker", "bug", "performance");
+  }
+
+  @Test
+  public void do_not_copy_rule_tags_on_existing_issues() throws Exception {
+    issue.setNew(false);
+    rule.setTags(ImmutableSet.of("bug", "performance"));
+    rule.setSystemTags(ImmutableSet.of("blocker"));
+
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).tags()).isEmpty();
+  }
+
+  @Test
+  public void guess_author_of_new_issues() throws Exception {
+    issue.setNew(true);
+    issue.setLine(3);
+    when(lineCache.lineAuthor(3)).thenReturn("charlie");
+
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).authorLogin()).isEqualTo("charlie");
+  }
+
+  @Test
+  public void do_not_fail_if_missing_author_for_new_issues() throws Exception {
+    issue.setNew(true);
+    issue.setLine(3);
+    when(lineCache.lineAuthor(3)).thenReturn(null);
+
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).authorLogin()).isNull();
+  }
+
+  @Test
+  public void do_not_guess_author_of_existing_issues() throws Exception {
+    issue.setNew(false);
+    issue.setLine(3);
+    when(lineCache.lineAuthor(3)).thenReturn("charlie");
+
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).authorLogin()).isNull();
+  }
+
+  @Test
+  public void auto_assign_new_issues() throws Exception {
+    issue.setNew(true);
+    issue.setAuthorLogin("charlie");
+    when(scmAccountCache.getNullable("charlie")).thenReturn("char.lie");
+
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).assignee()).isEqualTo("char.lie");
+  }
+
+  @Test
+  public void do_not_auto_assign_existing_issues() throws Exception {
+    issue.setNew(false);
+    issue.setAuthorLogin("charlie");
+    when(scmAccountCache.getNullable("charlie")).thenReturn("char.lie");
+
+    process();
+
+    assertThat(Iterators.getOnlyElement(issueCache.traverse()).assignee()).isNull();
+  }
+
+  private void process() {
+    sut.processComponentIssues("FILE_A", Arrays.asList(issue));
+    sut.afterReportProcessing();
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/RuleCacheLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/RuleCacheLoaderTest.java
new file mode 100644 (file)
index 0000000..22c2d25
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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.server.computation.issue;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.rule.db.RuleDao;
+
+import java.util.Collections;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+
+public class RuleCacheLoaderTest {
+
+  @Rule
+  public DbTester dbTester = new DbTester();
+
+  @Test
+  public void load_by_key() throws Exception {
+    dbTester.prepareDbUnit(getClass(), "shared.xml");
+    DbClient dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), new RuleDao());
+    RuleCacheLoader loader = new RuleCacheLoader(dbClient);
+
+    assertThat(loader.load(RuleKey.of("squid", "R001")).getName()).isEqualTo("Rule One");
+    assertThat(loader.load(RuleKey.of("squid", "MISSING"))).isNull();
+  }
+
+  @Test
+  public void load_by_keys_is_not_supported() throws Exception {
+    DbClient dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), new RuleDao());
+    RuleCacheLoader loader = new RuleCacheLoader(dbClient);
+    try {
+      loader.loadAll(Collections.<RuleKey>emptyList());
+      fail();
+    } catch (UnsupportedOperationException e) {
+      // see RuleDao#getByKeys()
+    }
+  }
+
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/RuleCacheTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/RuleCacheTest.java
new file mode 100644 (file)
index 0000000..9c94c3e
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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.server.computation.issue;
+
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.core.rule.RuleDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class RuleCacheTest {
+
+  @Test
+  public void ruleName() throws Exception {
+    RuleCacheLoader loader = mock(RuleCacheLoader.class);
+    when(loader.load(RuleKey.of("squid", "R002"))).thenReturn(new RuleDto().setName("Rule Two"));
+    RuleCache cache = new RuleCache(loader);
+    assertThat(cache.ruleName(RuleKey.of("squid", "R001"))).isNull();
+    assertThat(cache.ruleName(RuleKey.of("squid", "R002"))).isEqualTo("Rule Two");
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest.java
new file mode 100644 (file)
index 0000000..924fa5b
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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.server.computation.issue;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.config.Settings;
+import org.sonar.server.es.EsTester;
+import org.sonar.server.user.index.UserIndex;
+import org.sonar.server.user.index.UserIndexDefinition;
+
+import java.util.Collections;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+
+public class ScmAccountCacheLoaderTest {
+
+  @Rule
+  public EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings()));
+
+  @Test
+  public void load_login_for_scm_account() throws Exception {
+    esTester.putDocuments("users", "user", getClass(), "user1.json");
+
+    UserIndex index = new UserIndex(esTester.client());
+    ScmAccountCacheLoader loader = new ScmAccountCacheLoader(index);
+
+    assertThat(loader.load("missing")).isNull();
+    assertThat(loader.load("jesuis@charlie.com")).isEqualTo("charlie");
+  }
+
+  @Test
+  public void load_by_multiple_scm_accounts_is_not_supported_yet() throws Exception {
+    UserIndex index = new UserIndex(esTester.client());
+    ScmAccountCacheLoader loader = new ScmAccountCacheLoader(index);
+    try {
+      loader.loadAll(Collections.<String>emptyList());
+      fail();
+    } catch (UnsupportedOperationException ignored) {
+    }
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/SourceLinesCacheTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/SourceLinesCacheTest.java
new file mode 100644 (file)
index 0000000..718c452
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.server.computation.issue;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.core.source.db.FileSourceDao;
+import org.sonar.server.db.DbClient;
+import org.sonar.test.DbTests;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Category(DbTests.class)
+public class SourceLinesCacheTest {
+
+  @Rule
+  public DbTester dbTester = new DbTester();
+
+  @Test
+  public void load_data() throws Exception {
+    dbTester.prepareDbUnit(getClass(), "load_data.xml");
+    DbClient dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), new FileSourceDao(dbTester.myBatis()));
+    SourceLinesCache cache = new SourceLinesCache(dbClient);
+    cache.init("FILE_A");
+
+    // load data on demand -> still nothing in cache
+    assertThat(cache.countLines()).isEqualTo(0);
+
+    assertThat(cache.lineAuthor(1)).isEqualTo("charlie");
+    assertThat(cache.lineAuthor(2)).isEqualTo("cabu");
+    assertThat(cache.lineAuthor(3)).isNull();
+    assertThat(cache.countLines()).isEqualTo(2);
+
+    cache.clear();
+    assertThat(cache.countLines()).isEqualTo(0);
+  }
+}
index c860f4c63c0abdcd6f5af52015fdbb5be832e964..56559ba59e9dd2584cb83b811c97299e052600b2 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.util.cache;
 
 import com.google.common.collect.ImmutableMap;
 import org.junit.Test;
+import org.sonar.server.exceptions.NotFoundException;
 
 import java.util.Arrays;
 import java.util.HashMap;
@@ -62,18 +63,18 @@ public class MemoryCacheTest {
     try {
       cache.get("not_exists");
       fail();
-    } catch (IllegalArgumentException e) {
+    } catch (NotFoundException e) {
       assertThat(e).hasMessage("Not found: not_exists");
     }
   }
 
   @Test
   public void getAllNullable() throws Exception {
+    // ask for 3 keys but only 2 are available in backed (third key is missing)
     List<String> keys = Arrays.asList("one", "two", "three");
     Map<String, String> values = new HashMap<>();
     values.put("one", "un");
     values.put("two", "deux");
-    values.put("three", null);
     when(loader.loadAll(keys)).thenReturn(values);
     assertThat(cache.getAll(keys))
       .hasSize(3)
@@ -81,11 +82,13 @@ public class MemoryCacheTest {
       .containsEntry("two", "deux")
       .containsEntry("three", null);
 
+    // ask for 4 keys. Only a single one was never loaded. The 3 others are kept from cache
     when(loader.loadAll(Arrays.asList("four"))).thenReturn(ImmutableMap.of("four", "quatre"));
-    assertThat(cache.getAll(Arrays.asList("one", "two", "four")))
-      .hasSize(3)
+    assertThat(cache.getAll(Arrays.asList("one", "two", "three", "four")))
+      .hasSize(4)
       .containsEntry("one", "un")
       .containsEntry("two", "deux")
+      .containsEntry("three", null)
       .containsEntry("four", "quatre");
     verify(loader, times(2)).loadAll(anyCollection());
   }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/issue/RuleCacheLoaderTest/shared.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/issue/RuleCacheLoaderTest/shared.xml
new file mode 100644 (file)
index 0000000..97ee815
--- /dev/null
@@ -0,0 +1,14 @@
+<dataset>
+  <rules id="1" name="Rule One" plugin_name="squid" plugin_rule_key="R001"
+         plugin_config_key="[null]" description="[null]" priority="4"
+         status="READY"
+         is_template="[false]" template_id="[null]"
+         tags="[null]" system_tags="[null]"/>
+
+  <rules id="2" name="Rule Two" plugin_name="squid" plugin_rule_key="R002"
+         plugin_config_key="[null]" description="[null]" priority="4"
+         status="READY"
+         is_template="[false]" template_id="[null]"
+         tags="[null]" system_tags="[null]"/>
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest/user1.json b/server/sonar-server/src/test/resources/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest/user1.json
new file mode 100644 (file)
index 0000000..f509e6b
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "login": "charlie",
+  "name": "Charlie",
+  "email": "charlie@hebdo.com",
+  "active": true,
+  "scmAccounts": ["charlie", "jesuis@charlie.com"],
+  "createdAt": 1500000000000,
+  "updatedAt": 1500000000000
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/issue/SourceLinesCacheTest/load_data.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/issue/SourceLinesCacheTest/load_data.xml
new file mode 100644 (file)
index 0000000..4079fb9
--- /dev/null
@@ -0,0 +1,8 @@
+<dataset>
+  <file_sources id="1" file_uuid="FILE_A" project_uuid="PROJECT_A"
+                data=",charlie,,,,,,,,,,,,,,first line&#13;&#10;,cabu,,,,,,,,,,,,,,second line&#13;&#10;"
+                line_hashes="8d7b3d6b83c0a517eac07e1aac94b773&#10;9a0364b9e99bb480dd25e1f0284c8555"
+                data_hash="0263047cd758c68c27683625f072f010"
+                src_hash="123456"
+                created_at="1412952242000" updated_at="1412952242000"/>
+</dataset>
index 37b64b87e474ba5faf66a22ea1fba64b2bee0d4a..37768d824012501f113c325b644d4f68326ecce6 100644 (file)
@@ -41,6 +41,7 @@ public class ReportComponent {
   private int snapshotId;
   private String path;
   private String name;
+  private String uuid;
   private Type type;
   // Only for files
   private Boolean isTest;
@@ -84,6 +85,15 @@ public class ReportComponent {
     return path;
   }
 
+  public ReportComponent setUuid(String s) {
+    this.uuid = s;
+    return this;
+  }
+
+  public String uuid() {
+    return uuid;
+  }
+
   public ReportComponent setName(@Nullable String name) {
     this.name = name;
     return this;
index 2117266659acb856718f3eac320585515ef05908..5917d9d0d43bc223b5604f5cacf228a89f8a2f6d 100644 (file)
@@ -62,6 +62,7 @@ public class ComponentsPublisher implements ReportPublisher {
       .setId(r.getId())
       .setName(getName(r))
       .setPath(r.getPath())
+      .setUuid(r.getUuid())
       .setType(getType(r))
       .setLanguageKey(getLanguageKey(r))
       .setTest(isTest(r));
index 3e67c040b32e46000f493d364eb0e0c62bb7513f..731a4ccd80f4c9d11990a12b056adb6ac6634a43 100644 (file)
@@ -20,6 +20,9 @@
 
 package org.sonar.core.source.db;
 
+import com.google.common.base.Function;
+import org.apache.commons.dbutils.DbUtils;
+import org.apache.commons.io.IOUtils;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.ServerComponent;
 import org.sonar.core.persistence.DaoComponent;
@@ -28,6 +31,12 @@ import org.sonar.core.persistence.MyBatis;
 
 import javax.annotation.CheckForNull;
 
+import java.io.Reader;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
 public class FileSourceDao implements BatchComponent, ServerComponent, DaoComponent {
 
   private final MyBatis mybatis;
@@ -47,6 +56,29 @@ public class FileSourceDao implements BatchComponent, ServerComponent, DaoCompon
     }
   }
 
+  public <T> void readDataStream(String fileUuid, Function<Reader, T> function) {
+    DbSession dbSession = mybatis.openSession(false);
+    Connection connection = dbSession.getConnection();
+    PreparedStatement pstmt = null;
+    ResultSet rs = null;
+    Reader reader = null;
+    try {
+      pstmt = connection.prepareStatement("select data from file_sources where file_uuid = ?");
+      pstmt.setString(1, fileUuid);
+      rs = pstmt.executeQuery();
+      if (rs.next()) {
+        reader = rs.getCharacterStream(1);
+        function.apply(reader);
+      }
+    } catch (SQLException e) {
+      throw new IllegalStateException("Fail to read FILE_SOURCES.DATA of file " + fileUuid, e);
+    } finally {
+      IOUtils.closeQuietly(reader);
+      DbUtils.closeQuietly(connection, pstmt, rs);
+      MyBatis.closeQuietly(dbSession);
+    }
+  }
+
   public void insert(FileSourceDto dto) {
     DbSession session = mybatis.openSession(false);
     try {
index ac965c3170b8a82a144a3d0da3d3ae5c4b0326c1..bc5d3eacab790630e8bb42825b2a963e2ea8aadf 100644 (file)
@@ -23,6 +23,24 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
 public class FileSourceDto {
+
+  public static final int CSV_INDEX_SCM_REVISION = 0;
+  public static final int CSV_INDEX_SCM_AUTHOR = 1;
+  public static final int CSV_INDEX_SCM_DATE = 2;
+  public static final int CSV_INDEX_UT_LINE_HITS = 3;
+  public static final int CSV_INDEX_UT_CONDITIONS = 4;
+  public static final int CSV_INDEX_UT_COVERED_CONDITIONS = 5;
+  public static final int CSV_INDEX_IT_LINE_HITS = 6;
+  public static final int CSV_INDEX_IT_CONDITIONS = 7;
+  public static final int CSV_INDEX_IT_COVERED_CONDITIONS = 8;
+  public static final int CSV_INDEX_OVERALL_LINE_HITS = 9;
+  public static final int CSV_INDEX_OVERALL_CONDITIONS = 10;
+  public static final int CSV_INDEX_OVERALL_COVERED_CONDITIONS = 11;
+  public static final int CSV_INDEX_HIGHLIGHTING = 12;
+  public static final int CSV_INDEX_SYMBOLS = 13;
+  public static final int CSV_INDEX_DUPLICATIONS = 14;
+  public static final int CSV_INDEX_SOURCE = 15;
+
   private Long id;
   private String projectUuid;
   private String fileUuid;