]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3893 Symbol perspective data persistence in snapshot_data
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Tue, 16 Apr 2013 09:16:08 +0000 (11:16 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Tue, 16 Apr 2013 09:16:08 +0000 (11:16 +0200)
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
sonar-batch/src/main/java/org/sonar/batch/scan/source/DefaultHighlightable.java
sonar-batch/src/main/java/org/sonar/batch/scan/source/DefaultSymbolPerspective.java
sonar-batch/src/main/java/org/sonar/batch/scan/source/SourceDataPersister.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/scan/source/SymbolDataCache.java
sonar-batch/src/main/java/org/sonar/batch/scan/source/SyntaxHighlightingCache.java
sonar-batch/src/main/java/org/sonar/batch/scan/source/SyntaxHighlightingPersister.java [deleted file]
sonar-batch/src/test/java/org/sonar/batch/scan/source/DefaultHighlightableTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/source/DefaultSymbolPerspectiveTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/source/SourceDataPersisterTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/scan/source/SyntaxHighlightingPersisterTest.java [deleted file]

index 50e19dc336e9098ae009bfffb38c003028a1fe4e..e6d7cdd500352521b76653e1ca34caf67453224f 100644 (file)
@@ -39,8 +39,9 @@ import org.sonar.batch.issue.ScanIssueActions;
 import org.sonar.batch.phases.GraphPersister;
 import org.sonar.batch.scan.maven.FakeMavenPluginExecutor;
 import org.sonar.batch.scan.maven.MavenPluginExecutor;
+import org.sonar.batch.scan.source.SourceDataPersister;
+import org.sonar.batch.scan.source.SymbolDataCache;
 import org.sonar.batch.scan.source.SyntaxHighlightingCache;
-import org.sonar.batch.scan.source.SyntaxHighlightingPersister;
 import org.sonar.core.component.ScanGraph;
 import org.sonar.core.notification.DefaultNotificationManager;
 import org.sonar.core.test.TestPlanBuilder;
@@ -93,7 +94,8 @@ public class ProjectScanContainer extends ComponentContainer {
       GraphPersister.class,
 
       SyntaxHighlightingCache.class,
-      SyntaxHighlightingPersister.class
+      SymbolDataCache.class,
+      SourceDataPersister.class
     );
   }
 
index f4a97278c3816c50cf67e9a8711c4ca8c442b5a5..3129bcec164df80f2ca1d20f0534fa509310839a 100644 (file)
@@ -62,7 +62,7 @@ public class DefaultHighlightable implements Highlightable {
     @Override
     public void done() {
       String serializedHighlightingRules = highlightingRulesBuilder.build().serializeAsString();
-      syntaxHighlightingCache.registerSourceHighlighting(component().key(), serializedHighlightingRules);
+      syntaxHighlightingCache.registerSourceData(component().key(), serializedHighlightingRules);
     }
   }
 }
index 197611578a06cc21511ba60ad2a7bce9f1c14866..879e1dc9bae4eb9944344c65f773438cecf03e09 100644 (file)
@@ -59,7 +59,7 @@ public class DefaultSymbolPerspective implements SymbolPerspective {
 
   @Override
   public void end() {
-    symbolDataCache.registerSymbolData(component().key(), symbolDataRepository.serializeAsString());
+    symbolDataCache.registerSourceData(component().key(), symbolDataRepository.serializeAsString());
   }
 
   @Override
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/source/SourceDataPersister.java b/sonar-batch/src/main/java/org/sonar/batch/scan/source/SourceDataPersister.java
new file mode 100644 (file)
index 0000000..f301afb
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.batch.scan.source;
+
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.batch.index.ScanPersister;
+import org.sonar.batch.index.SnapshotCache;
+import org.sonar.core.source.jdbc.SnapshotDataDao;
+import org.sonar.core.source.jdbc.SnapshotDataDto;
+
+import java.util.Map;
+
+public class SourceDataPersister implements ScanPersister {
+
+  private final SnapshotDataDao snapshotDataDao;
+  private final SourceDataCache[] sourceDataCache;
+  private final SnapshotCache snapshots;
+
+  public SourceDataPersister(SnapshotDataDao snapshotDataDao, SourceDataCache[] sourceDataCache, SnapshotCache snapshots) {
+    this.snapshotDataDao = snapshotDataDao;
+    this.sourceDataCache = sourceDataCache;
+    this.snapshots = snapshots;
+  }
+
+  @Override
+  public void persist() {
+
+    for (SourceDataCache dataCache : sourceDataCache) {
+      persistDataCache(dataCache.getDataType(), dataCache.getSourceDataByComponent());
+    }
+  }
+
+  private void persistDataCache(String dataType, Map<String, String> sourceDataByComponent) {
+
+    for (Map.Entry<String, String> componentRules : sourceDataByComponent.entrySet()) {
+
+      Snapshot snapshotForComponent = snapshots.get(componentRules.getKey());
+
+      SnapshotDataDto snapshotDataDto = new SnapshotDataDto();
+      if(snapshotForComponent != null) {
+        snapshotDataDto.setSnapshotId(snapshotForComponent.getId());
+        snapshotDataDto.setResourceId(snapshotForComponent.getResourceId());
+        snapshotDataDto.setDataType(dataType);
+        snapshotDataDto.setData(sourceDataByComponent.get(componentRules.getValue()));
+        snapshotDataDao.insert(snapshotDataDto);
+      }
+    }
+  }
+}
index 47ba1404f7510683bcb31f7370d962aa5b7dddc0..7d8607a5dfe33f5e37333bff57ec3074be3b6141 100644 (file)
 
 package org.sonar.batch.scan.source;
 
-import com.google.common.collect.Maps;
+public class SymbolDataCache extends SourceDataCache {
 
-import java.util.Map;
+  public static final String DATA_TYPE = "symbol";
 
-public class SymbolDataCache {
-
-  private final Map<String, String> symbolDataCache;
-
-  public SymbolDataCache() {
-    symbolDataCache = Maps.newHashMap();
-  }
-
-  public void registerSymbolData(String componentKey, String serializedSymbolData) {
-    symbolDataCache.put(componentKey, serializedSymbolData);
-  }
-
-  public Map<String, String> getSymbolDataByComponent() {
-    return symbolDataCache;
+  @Override
+  public String getDataType() {
+    return DATA_TYPE;
   }
 }
index d2e90f20b913b440bd2feffcb4f5dc575f0d4f54..6783c816cc484aaab9d063de93cbfad3e97e6b74 100644 (file)
 
 package org.sonar.batch.scan.source;
 
-import com.google.common.collect.Maps;
-import org.sonar.api.BatchComponent;
+public class SyntaxHighlightingCache extends SourceDataCache {
 
-import java.util.Map;
+  public static final String DATA_TYPE = "highlight_syntax";
 
-public class SyntaxHighlightingCache implements BatchComponent {
-
-  private final Map<String, String> highlightingCache;
-
-  public SyntaxHighlightingCache() {
-    highlightingCache = Maps.newHashMap();
-  }
-
-  public void registerSourceHighlighting(String componentKey, String serializedHighlightingRules) {
-    highlightingCache.put(componentKey, serializedHighlightingRules);
-  }
-
-  public Map<String, String> getHighlightingRulesByComponent() {
-    return highlightingCache;
+  @Override
+  public String getDataType() {
+    return DATA_TYPE;
   }
 }
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/source/SyntaxHighlightingPersister.java b/sonar-batch/src/main/java/org/sonar/batch/scan/source/SyntaxHighlightingPersister.java
deleted file mode 100644 (file)
index e852454..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-
-package org.sonar.batch.scan.source;
-
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.batch.index.ScanPersister;
-import org.sonar.batch.index.SnapshotCache;
-import org.sonar.core.source.jdbc.SnapshotDataDao;
-import org.sonar.core.source.jdbc.SnapshotDataDto;
-
-import java.util.Map;
-
-public class SyntaxHighlightingPersister implements ScanPersister {
-
-  public static final String DATA_TYPE = "highlight_syntax";
-
-  private final SnapshotDataDao snapshotDataDao;
-  private final SyntaxHighlightingCache highlightingCache;
-  private final SnapshotCache snapshots;
-
-  public SyntaxHighlightingPersister(SnapshotDataDao snapshotDataDao, SyntaxHighlightingCache highlightingCache, SnapshotCache snapshots) {
-    this.snapshotDataDao = snapshotDataDao;
-    this.highlightingCache = highlightingCache;
-    this.snapshots = snapshots;
-  }
-
-  @Override
-  public void persist() {
-
-    Map<String, String> highlightingRules = highlightingCache.getHighlightingRulesByComponent();
-
-    for (Map.Entry<String, String> componentRules : highlightingRules.entrySet()) {
-
-      Snapshot snapshotForComponent = snapshots.get(componentRules.getKey());
-
-      SnapshotDataDto snapshotDataDto = new SnapshotDataDto();
-      if(snapshotForComponent != null) {
-        snapshotDataDto.setSnapshotId(snapshotForComponent.getId());
-        snapshotDataDto.setResourceId(snapshotForComponent.getResourceId());
-        snapshotDataDto.setDataType(DATA_TYPE);
-        snapshotDataDto.setData(highlightingRules.get(componentRules.getValue()));
-        snapshotDataDao.insert(snapshotDataDto);
-      }
-    }
-  }
-}
index d6fdaa44bb5421b75c49217a465c18f918c85ed0..3fb17eab3cb3a4f419744fd436cd2c5834345295 100644 (file)
@@ -57,6 +57,6 @@ public class DefaultHighlightableTest {
             .highlight(20, 30, "cppd")
             .done();
 
-    verify(highlightingCache).registerSourceHighlighting("myComponent", "0,10,k;20,30,cppd;");
+    verify(highlightingCache).registerSourceData("myComponent", "0,10,k;20,30,cppd;");
   }
 }
index 61f8c53a35eb4de73450c2f451298e12a923268d..e1ce5d3540c2125abdee4d91ed69b9b454b15166 100644 (file)
@@ -61,6 +61,6 @@ public class DefaultSymbolPerspectiveTest {
 
     symbolPerspective.end();
 
-    verify(cache).registerSymbolData("myComponent", "4,8,4,12,70;25,33,25,44,60,108;");
+    verify(cache).registerSourceData("myComponent", "4,8,4,12,70;25,33,25,44,60,108;");
   }
 }
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/source/SourceDataPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/source/SourceDataPersisterTest.java
new file mode 100644 (file)
index 0000000..f3c17ad
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.batch.scan.source;
+
+import com.google.common.collect.Maps;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentMatcher;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.batch.index.SnapshotCache;
+import org.sonar.core.source.jdbc.SnapshotDataDao;
+import org.sonar.core.source.jdbc.SnapshotDataDto;
+
+import java.util.Map;
+
+import static org.mockito.Mockito.*;
+
+public class SourceDataPersisterTest {
+
+  private SnapshotCache snapshots;
+  private SourceDataCache sourceDataCache;
+  private SnapshotDataDao snapshotDataDao;
+
+  @Before
+  public void setUpInjectedObjects() {
+    snapshots = mock(SnapshotCache.class);
+    sourceDataCache = mock(SyntaxHighlightingCache.class);
+    snapshotDataDao = mock(SnapshotDataDao.class);
+  }
+
+  @Test
+  public void should_persist_source_cache_data() throws Exception {
+
+    Snapshot snapshotComponent1 = mock(Snapshot.class);
+    when(snapshotComponent1.getId()).thenReturn(1);
+    when(snapshotComponent1.getResourceId()).thenReturn(1);
+
+    Map<String, String> sourceData = Maps.newHashMap();
+    sourceData.put("component1", "source data for component 1");
+
+    when(sourceDataCache.getSourceDataByComponent()).thenReturn(sourceData);
+    when(sourceDataCache.getDataType()).thenReturn("myDataType");
+    when(snapshots.get("component1")).thenReturn(snapshotComponent1);
+
+    SourceDataPersister persister = new SourceDataPersister(snapshotDataDao, new SourceDataCache[]{sourceDataCache}, snapshots);
+    persister.persist();
+
+    verify(snapshotDataDao).insert(argThat(new ArgumentMatcher<SnapshotDataDto>() {
+      @Override
+      public boolean matches(Object o) {
+        SnapshotDataDto insertedData = (SnapshotDataDto) o;
+        return insertedData.getSnapshotId() == 1 && insertedData.getDataType() == "myDataType";
+      }
+    }));
+  }
+
+  @Test
+  public void should_ignore_components_without_snapshot() throws Exception {
+
+    Map<String, String> sourceData = Maps.newHashMap();
+    sourceData.put("component1", "source data for component 1");
+
+    when(sourceDataCache.getSourceDataByComponent()).thenReturn(sourceData);
+    when(snapshots.get("component1")).thenReturn(null);
+
+    SourceDataPersister persister = new SourceDataPersister(snapshotDataDao, new SourceDataCache[]{sourceDataCache}, snapshots);
+    persister.persist();
+
+    verify(snapshotDataDao, times(0)).insert(any(SnapshotDataDto.class));
+  }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/source/SyntaxHighlightingPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/source/SyntaxHighlightingPersisterTest.java
deleted file mode 100644 (file)
index fb8816a..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-
-package org.sonar.batch.scan.source;
-
-import com.google.common.collect.Maps;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.ArgumentMatcher;
-import org.mockito.InOrder;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.batch.index.SnapshotCache;
-import org.sonar.core.source.jdbc.SnapshotDataDao;
-import org.sonar.core.source.jdbc.SnapshotDataDto;
-
-import java.util.Map;
-
-import static org.mockito.Mockito.*;
-
-public class SyntaxHighlightingPersisterTest {
-
-  private SnapshotCache snapshots;
-  private SyntaxHighlightingCache highlightingCache;
-  private SnapshotDataDao snapshotDataDao;
-
-  @Before
-  public void setUpInjectedObjects() {
-    snapshots = mock(SnapshotCache.class);
-    highlightingCache = mock(SyntaxHighlightingCache.class);
-    snapshotDataDao = mock(SnapshotDataDao.class);
-  }
-
-  @Test
-  public void should_persist_components_highlighting() throws Exception {
-
-    Snapshot snapshotComponent1 = mock(Snapshot.class);
-    when(snapshotComponent1.getId()).thenReturn(1);
-    when(snapshotComponent1.getResourceId()).thenReturn(1);
-
-    Snapshot snapshotComponent2 = mock(Snapshot.class);
-    when(snapshotComponent2.getId()).thenReturn(2);
-    when(snapshotComponent2.getResourceId()).thenReturn(2);
-
-    Map<String, String> highlightingRules = Maps.newHashMap();
-    highlightingRules.put("component1", "0,10,k;2,4,k;15,25,cppd;");
-    highlightingRules.put("component2", "0,5,cppd;15,25,k;");
-
-    when(highlightingCache.getHighlightingRulesByComponent()).thenReturn(highlightingRules);
-
-    when(snapshots.get("component1")).thenReturn(snapshotComponent1);
-    when(snapshots.get("component2")).thenReturn(snapshotComponent2);
-
-    SyntaxHighlightingPersister persister = new SyntaxHighlightingPersister(snapshotDataDao, highlightingCache, snapshots);
-    persister.persist();
-
-    InOrder orderedMock = inOrder(snapshotDataDao);
-
-    orderedMock.verify(snapshotDataDao).insert(argThat(new ArgumentMatcher<SnapshotDataDto>() {
-      @Override
-      public boolean matches(Object o) {
-        return ((SnapshotDataDto)o).getSnapshotId() == 1;
-      }
-    }));
-
-    orderedMock.verify(snapshotDataDao).insert(argThat(new ArgumentMatcher<SnapshotDataDto>() {
-      @Override
-      public boolean matches(Object o) {
-        return ((SnapshotDataDto)o).getSnapshotId() == 2;
-      }
-    }));
-  }
-}