]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6014 Fix issue when saving dependencies
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 7 Jan 2015 20:33:46 +0000 (21:33 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 7 Jan 2015 20:59:56 +0000 (21:59 +0100)
sonar-batch/src/main/java/org/sonar/batch/design/MavenDependenciesSensor.java
sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java
sonar-batch/src/test/java/org/sonar/batch/design/MavenDependenciesSensorTest.java

index 9b618b7b4e8f208cb11b1159b9194f76d219e90b..3462478091677667e50db5b133abfe36e66d9035 100644 (file)
@@ -54,6 +54,7 @@ import org.sonar.api.resources.Library;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.utils.SonarException;
+import org.sonar.batch.index.ResourcePersister;
 
 import java.lang.reflect.Type;
 import java.util.ArrayList;
@@ -67,16 +68,17 @@ public class MavenDependenciesSensor implements Sensor {
 
   private static final Logger LOG = LoggerFactory.getLogger(MavenDependenciesSensor.class);
 
-  private ArtifactRepository localRepository;
-  private ArtifactFactory artifactFactory;
-  private ArtifactMetadataSource artifactMetadataSource;
-  private ArtifactCollector artifactCollector;
-  private DependencyTreeBuilder treeBuilder;
-  private SonarIndex index;
-  private Settings settings;
+  private final ArtifactRepository localRepository;
+  private final ArtifactFactory artifactFactory;
+  private final ArtifactMetadataSource artifactMetadataSource;
+  private final ArtifactCollector artifactCollector;
+  private final DependencyTreeBuilder treeBuilder;
+  private final SonarIndex index;
+  private final Settings settings;
+  private final ResourcePersister resourcePersister;
 
   public MavenDependenciesSensor(Settings settings, ArtifactRepository localRepository, ArtifactFactory artifactFactory, ArtifactMetadataSource artifactMetadataSource,
-    ArtifactCollector artifactCollector, DependencyTreeBuilder treeBuilder, SonarIndex index) {
+    ArtifactCollector artifactCollector, DependencyTreeBuilder treeBuilder, SonarIndex index, ResourcePersister resourcePersister) {
     this.settings = settings;
     this.localRepository = localRepository;
     this.artifactFactory = artifactFactory;
@@ -84,14 +86,14 @@ public class MavenDependenciesSensor implements Sensor {
     this.artifactCollector = artifactCollector;
     this.index = index;
     this.treeBuilder = treeBuilder;
+    this.resourcePersister = resourcePersister;
   }
 
   /**
    * Used with SQ Maven plugin 2.5+
    */
-  public MavenDependenciesSensor(Settings settings, SonarIndex index) {
-    this.settings = settings;
-    this.index = index;
+  public MavenDependenciesSensor(Settings settings, SonarIndex index, ResourcePersister resourcePersister) {
+    this(settings, null, null, null, null, null, index, resourcePersister);
   }
 
   @Override
index df0979454f52f4984963140971b5b2e1e44b2357..ffefb3568582c59b3c0a5eec5557e1f87269999b 100644 (file)
@@ -74,11 +74,19 @@ public final class ResourcePersister implements ScanPersister {
     }
 
     for (BatchResource lib : resourceCache.allLibraries()) {
-      Snapshot s = persistLibrary(projectTree.getRootProject().getAnalysisDate(), (Library) lib.resource());
-      lib.setSnapshot(s);
+      if (lib.snapshot() != null) {
+        // already persisted
+        continue;
+      }
+      persistLibrary(lib);
     }
   }
 
+  private void persistLibrary(BatchResource lib) {
+    Snapshot s = persistLibrary(projectTree.getRootProject().getAnalysisDate(), (Library) lib.resource());
+    lib.setSnapshot(s);
+  }
+
   private void persist(BatchResource batchResource) {
     if (batchResource.snapshot() != null) {
       // already persisted
index 504b82981a08dc68bdf3fb6b829915ca51b484aa..b05067227d99f8eeed364b55461d47aee716dae5 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.design.Dependency;
 import org.sonar.api.resources.Library;
 import org.sonar.api.resources.Project;
+import org.sonar.batch.index.ResourcePersister;
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
@@ -47,7 +48,7 @@ public class MavenDependenciesSensorTest {
   public void prepare() {
     settings = new Settings();
     sonarIndex = mock(SonarIndex.class);
-    sensor = new MavenDependenciesSensor(settings, sonarIndex);
+    sensor = new MavenDependenciesSensor(settings, sonarIndex, mock(ResourcePersister.class));
     sensorContext = mock(SensorContext.class);
     when(sensorContext.getResource(any(Library.class))).thenAnswer(new Answer<Library>() {
       public Library answer(InvocationOnMock invocation) throws Throwable {