aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-02-09 21:11:53 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2012-02-09 22:48:17 +0400
commite476635c3816598feefd88517b6f12421b807a0c (patch)
treef6ab574ee7d62f7a97c27a2c19620af18782422e /sonar-batch
parentc3bc6982b7e165304e269d7b7ef309a05148be98 (diff)
downloadsonarqube-e476635c3816598feefd88517b6f12421b807a0c.tar.gz
sonarqube-e476635c3816598feefd88517b6f12421b807a0c.zip
SONAR-3209 Add new batch component - FileLinesContextFactory
* As was discussed with Simon: better to have new beta class than new beta method. * This also provides ability to use this factory from Decorator.
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java7
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java39
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java5
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java2
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java31
5 files changed, 77 insertions, 7 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java
index c4a8e9a7579..4c998af512c 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java
@@ -30,6 +30,7 @@ import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.PersistenceMode;
import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.api.utils.KeyValueFormat.Converter;
@@ -50,6 +51,8 @@ public class DefaultFileLinesContext implements FileLinesContext {
private final Map<String, Map<Integer, Object>> map = Maps.newHashMap();
public DefaultFileLinesContext(SonarIndex index, Resource resource) {
+ Preconditions.checkNotNull(index);
+ Preconditions.checkArgument(Scopes.isFile(resource));
this.index = index;
this.resource = resource;
}
@@ -118,6 +121,7 @@ public class DefaultFileLinesContext implements FileLinesContext {
.setPersistenceMode(PersistenceMode.DATABASE)
.setData(data);
index.addMeasure(resource, measure);
+ entry.setValue(ImmutableMap.copyOf(lines));
}
}
}
@@ -133,9 +137,10 @@ public class DefaultFileLinesContext implements FileLinesContext {
}
/**
- * Checks that measure was not loaded.
+ * Checks that measure was not saved.
*
* @see #loadData(String, Converter)
+ * @see #save()
*/
private boolean shouldSave(Map<Integer, Object> lines) {
return !(lines instanceof ImmutableMap);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java
new file mode 100644
index 00000000000..497cbe1c08e
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+import org.sonar.api.batch.SonarIndex;
+import org.sonar.api.measures.FileLinesContext;
+import org.sonar.api.measures.FileLinesContextFactory;
+import org.sonar.api.resources.Resource;
+
+public class DefaultFileLinesContextFactory implements FileLinesContextFactory {
+
+ private final SonarIndex index;
+
+ public DefaultFileLinesContextFactory(SonarIndex index) {
+ this.index = index;
+ }
+
+ public FileLinesContext createFor(Resource resource) {
+ return new DefaultFileLinesContext(index, resource);
+ }
+
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java
index 2a75b837b6d..fe42bfca0c0 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java
@@ -23,7 +23,6 @@ import org.sonar.api.batch.Event;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.SonarIndex;
import org.sonar.api.design.Dependency;
-import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
@@ -123,10 +122,6 @@ public class DefaultSensorContext implements SensorContext {
return index.addMeasure(resourceOrProject(resource), measure);
}
- public FileLinesContext createFileLinesContext(Resource resource) {
- return new DefaultFileLinesContext(index, resource);
- }
-
public void saveViolation(Violation violation, boolean force) {
if (violation.getResource() == null) {
violation.setResource(resourceOrProject(violation.getResource()));
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
index 1948b6a686b..bc7516ae550 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
@@ -24,6 +24,7 @@ import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.ServerHttpClient;
+import org.sonar.batch.DefaultFileLinesContextFactory;
import org.sonar.batch.DefaultResourceCreationLock;
import org.sonar.batch.ProjectConfigurator;
import org.sonar.batch.ProjectTree;
@@ -53,6 +54,7 @@ public class BatchModule extends Module {
addCoreSingleton(ProjectConfigurator.class);
addCoreSingleton(DefaultResourceCreationLock.class);
addCoreSingleton(DefaultIndex.class);
+ addCoreSingleton(DefaultFileLinesContextFactory.class);
if (dryRun) {
addCoreSingleton(ReadOnlyPersistenceManager.class);
diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java
index 2cf15e2bd68..d3f73831031 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java
@@ -27,7 +27,9 @@ import org.sonar.api.batch.SonarIndex;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.PersistenceMode;
+import org.sonar.api.resources.Directory;
import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.Scopes;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
@@ -44,9 +46,15 @@ public class DefaultFileLinesContextTest {
public void setUp() {
index = mock(SonarIndex.class);
resource = mock(Resource.class);
+ when(resource.getScope()).thenReturn(Scopes.FILE);
fileLineMeasures = new DefaultFileLinesContext(index, resource);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldNotAllowCreationForDirectory() {
+ new DefaultFileLinesContext(index, new Directory("key"));
+ }
+
@Test
public void shouldSave() {
fileLineMeasures.setIntValue("hits", 1, 2);
@@ -68,8 +76,20 @@ public class DefaultFileLinesContextTest {
fileLineMeasures.setStringValue("author", 1, "simon");
fileLineMeasures.setStringValue("author", 3, "evgeny");
fileLineMeasures.save();
+ fileLineMeasures.setIntValue("branches", 1, 2);
+ fileLineMeasures.setIntValue("branches", 3, 4);
+ fileLineMeasures.save();
- verify(index, times(2)).addMeasure(Mockito.eq(resource), Mockito.any(Measure.class));
+ verify(index, times(3)).addMeasure(Mockito.eq(resource), Mockito.any(Measure.class));
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void shouldNotModifyAfterSave() {
+ fileLineMeasures.setIntValue("hits", 1, 2);
+ fileLineMeasures.save();
+ fileLineMeasures.save();
+ verify(index).addMeasure(Mockito.eq(resource), Mockito.any(Measure.class));
+ fileLineMeasures.setIntValue("hits", 1, 2);
}
@Test
@@ -103,6 +123,15 @@ public class DefaultFileLinesContextTest {
verify(index, never()).addMeasure(Mockito.eq(resource), Mockito.any(Measure.class));
}
+ @Test(expected = UnsupportedOperationException.class)
+ public void shouldNotModifyAfterLoad() {
+ when(index.getMeasure(Mockito.any(Resource.class), Mockito.any(Metric.class)))
+ .thenReturn(new Measure("author").setData("1=simon;3=evgeny"));
+
+ fileLineMeasures.getStringValue("author", 1);
+ fileLineMeasures.setStringValue("author", 1, "evgeny");
+ }
+
@Test
public void shouldNotFailIfNoMeasureInIndex() {
assertThat(fileLineMeasures.getIntValue("hits", 1), nullValue());