diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-24 19:06:38 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-24 19:25:01 +0100 |
commit | 878cec92f8e39fd46628a83bba970293129de710 (patch) | |
tree | 8234845025634f6e8d589fc1ef5df79f4bb4fe12 /sonar-batch | |
parent | eb9462d6002daf7f01f8fe627b4517c065bdb5af (diff) | |
download | sonarqube-878cec92f8e39fd46628a83bba970293129de710.tar.gz sonarqube-878cec92f8e39fd46628a83bba970293129de710.zip |
SONAR-791 When the source directory is not exactly the java package root, Sonar should stop the analysis
Diffstat (limited to 'sonar-batch')
3 files changed, 62 insertions, 6 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultResourceCreationLock.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultResourceCreationLock.java index 93b33f6b53a..49e1244c191 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultResourceCreationLock.java +++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultResourceCreationLock.java @@ -19,6 +19,7 @@ */ package org.sonar.batch; +import org.apache.commons.configuration.Configuration; import org.sonar.api.batch.ResourceCreationLock; /** @@ -30,6 +31,14 @@ import org.sonar.api.batch.ResourceCreationLock; public final class DefaultResourceCreationLock implements ResourceCreationLock { private boolean locked = false; + private boolean failWhenLocked=false; + + public DefaultResourceCreationLock() { + } + + public DefaultResourceCreationLock(Configuration configuration) { + this.failWhenLocked = configuration.getBoolean("sonar.hardIndexLock", false); + } public boolean isLocked() { return locked; @@ -46,4 +55,11 @@ public final class DefaultResourceCreationLock implements ResourceCreationLock { locked = false; } + public boolean isFailWhenLocked() { + return failWhenLocked; + } + + public void setFailWhenLocked(boolean b) { + this.failWhenLocked = b; + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index b63bef696fd..5660bf44ade 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -460,9 +460,7 @@ public final class DefaultIndex extends SonarIndex { return bucket; } - if (lock.isLocked() && !ResourceUtils.isLibrary(resource)) { - LOG.warn("Resource will be ignored in next Sonar versions, index is locked: " + resource); - } + checkLock(resource); Resource parent = null; if (!ResourceUtils.isLibrary(resource)) { @@ -487,6 +485,14 @@ public final class DefaultIndex extends SonarIndex { return bucket; } + private void checkLock(Resource resource) { + if (lock.isLocked() && !ResourceUtils.isLibrary(resource)) { + if (lock.isFailWhenLocked()) { + throw new SonarException("Index is locked, resource can not be indexed: " + resource); + } + LOG.warn("Resource will be ignored in next Sonar versions, index is locked: " + resource); + } + } public boolean isExcluded(Resource reference) { Bucket bucket = getBucket(reference, true); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java index 25f6bcb6cc1..e90cd8a9b61 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java @@ -24,9 +24,13 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.sonar.api.batch.ResourceFilter; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.measures.MeasuresFilters; import org.sonar.api.measures.MetricFinder; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.*; +import org.sonar.api.utils.SonarException; import org.sonar.batch.DefaultResourceCreationLock; import org.sonar.batch.ProjectTree; import org.sonar.batch.ResourceFilters; @@ -36,14 +40,20 @@ import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class DefaultIndexTest { private DefaultIndex index = null; + private DefaultResourceCreationLock lock; @Before public void createIndex() { - index = new DefaultIndex(mock(PersistenceManager.class), new DefaultResourceCreationLock(), mock(ProjectTree.class), mock(MetricFinder.class)); + lock = new DefaultResourceCreationLock(); + MetricFinder metricFinder = mock(MetricFinder.class); + when(metricFinder.findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC); + + index = new DefaultIndex(mock(PersistenceManager.class), lock, mock(ProjectTree.class), metricFinder); Project project = new Project("project"); ResourceFilter filter = new ResourceFilter() { @@ -126,10 +136,25 @@ public class DefaultIndexTest { } + /** + * Only a warning is logged when index is locked. + */ @Test - @Ignore("TODO: should it be really possible") - public void shouldIndexDirectChildOfProject() { + public void shouldIndexEvenIfLocked() { + lock.lock(); + + Directory dir = new Directory("org/foo"); + assertThat(index.index(dir), is(true)); + assertThat(index.isIndexed(dir), is(true)); + } + + @Test(expected = SonarException.class) + public void shouldFailIfIndexingAndLocked() { + lock.setFailWhenLocked(true); + lock.lock(); + Directory dir = new Directory("org/foo"); + index.index(dir); } @Test @@ -139,4 +164,13 @@ public class DefaultIndexTest { assertThat(index.isIndexed(file), is(false)); assertThat(index.isExcluded(file), is(true)); } + + @Test + public void shouldIndexResourceWhenAddingMeasure() { + Resource dir = new Directory("org/foo"); + index.addMeasure(dir, new Measure("ncloc").setValue(50.0)); + + assertThat(index.isIndexed(dir), is(true)); + assertThat(index.getMeasures(dir, MeasuresFilters.metric("ncloc")).getIntValue(), is(50)); + } } |