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;
GraphPersister.class,
SyntaxHighlightingCache.class,
- SyntaxHighlightingPersister.class
+ SymbolDataCache.class,
+ SourceDataPersister.class
);
}
@Override
public void done() {
String serializedHighlightingRules = highlightingRulesBuilder.build().serializeAsString();
- syntaxHighlightingCache.registerSourceHighlighting(component().key(), serializedHighlightingRules);
+ syntaxHighlightingCache.registerSourceData(component().key(), serializedHighlightingRules);
}
}
}
@Override
public void end() {
- symbolDataCache.registerSymbolData(component().key(), symbolDataRepository.serializeAsString());
+ symbolDataCache.registerSourceData(component().key(), symbolDataRepository.serializeAsString());
}
@Override
--- /dev/null
+/*
+ * 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);
+ }
+ }
+ }
+}
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;
}
}
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;
}
}
+++ /dev/null
-/*
- * 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);
- }
- }
- }
-}
.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;");
}
}
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;");
}
}
--- /dev/null
+/*
+ * 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));
+ }
+}
+++ /dev/null
-/*
- * 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;
- }
- }));
- }
-}