aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/test
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-24 16:02:35 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-24 16:02:35 +0000
commit51c728457cb0ea1763625d3600bddc2c3d7f049b (patch)
treeb5b29779f065bb57d4755a541db08879cb61bfc2 /sonar-batch/src/test
parenta5ba257ffd83b0418f12c0dc1ba9542873f0d6e8 (diff)
downloadsonarqube-51c728457cb0ea1763625d3600bddc2c3d7f049b.tar.gz
sonarqube-51c728457cb0ea1763625d3600bddc2c3d7f049b.zip
SONAR-1711 add a lock on SensorContext/DecoratorContext in order to avoid creation of resources in methods saveViolation() and saveMeasure().
Diffstat (limited to 'sonar-batch/src/test')
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/DefaultResourceCreationLockTest.java43
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/indexer/DefaultSonarIndexTest.java16
2 files changed, 58 insertions, 1 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultResourceCreationLockTest.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultResourceCreationLockTest.java
new file mode 100644
index 00000000000..24918b4326e
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultResourceCreationLockTest.java
@@ -0,0 +1,43 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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.junit.Test;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class DefaultResourceCreationLockTest {
+
+ @Test
+ public void shouldNotBeLockedAtStartup() {
+ assertThat(new DefaultResourceCreationLock().isLocked(), is(false));
+ }
+
+ @Test
+ public void shouldLock() {
+ DefaultResourceCreationLock lock = new DefaultResourceCreationLock();
+ lock.lock();
+ assertThat(lock.isLocked(), is(true));
+
+ lock.unlock();
+ assertThat(lock.isLocked(), is(false));
+ }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/indexer/DefaultSonarIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/indexer/DefaultSonarIndexTest.java
index fdbc09cc47c..c818de86ad5 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/indexer/DefaultSonarIndexTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/indexer/DefaultSonarIndexTest.java
@@ -20,6 +20,11 @@
package org.sonar.batch.indexer;
import org.junit.Test;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.PersistenceMode;
+import org.sonar.api.utils.SonarException;
+import org.sonar.batch.DefaultResourceCreationLock;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import org.sonar.api.design.Dependency;
import org.sonar.api.resources.JavaFile;
@@ -33,7 +38,7 @@ public class DefaultSonarIndexTest extends AbstractDbUnitTestCase {
@Test
public void indexDependencies() {
- DefaultSonarIndex index = new DefaultSonarIndex(getSession(), null);
+ DefaultSonarIndex index = new DefaultSonarIndex(getSession(), null, new DefaultResourceCreationLock());
Resource from = new JavaFile("org.foo.Foo");
Resource to = new JavaFile("org.bar.Bar");
@@ -53,4 +58,13 @@ public class DefaultSonarIndexTest extends AbstractDbUnitTestCase {
assertTrue(index.getOutgoingEdges(from).contains(dependency));
assertThat(index.getOutgoingEdges(to).isEmpty(), is(true));
}
+
+ @Test(expected = SonarException.class)
+ public void failIfLockedAndAddingMeasureOnUnknownResource() {
+ DefaultResourceCreationLock lock = new DefaultResourceCreationLock();
+ lock.lock();
+
+ DefaultSonarIndex index = new DefaultSonarIndex(getSession(), null, lock);
+ index.saveMeasure(new JavaFile("org.foo.Bar"), new Measure(CoreMetrics.LINES, 200.0));
+ }
}