aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2013-08-21 17:15:40 +0200
committerJulien Lancelot <julien.lancelot@gmail.com>2013-08-21 17:15:40 +0200
commitac37e02d6d3d99e3e6acd3557e9a3fc202fe742f (patch)
tree58c10adcf0b8cafd13092eec4a03965506805c03 /sonar-batch
parent7fb8d5ab68f92fc0e9ca123c635ef687cab1bf16 (diff)
downloadsonarqube-ac37e02d6d3d99e3e6acd3557e9a3fc202fe742f.tar.gz
sonarqube-ac37e02d6d3d99e3e6acd3557e9a3fc202fe742f.zip
Add some unit tests
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java48
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java123
2 files changed, 117 insertions, 54 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java
index 97cdacea26c..2440064362c 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java
@@ -19,63 +19,87 @@
*/
package org.sonar.batch.components;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.startsWith;
-import static org.junit.Assert.assertThat;
-
import org.junit.Test;
import org.sonar.api.CoreProperties;
import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Qualifiers;
import java.util.Date;
+import static org.fest.assertions.Assertions.assertThat;
+
public class PastSnapshotTest {
@Test
+ public void test_some_setters_and_getters() {
+ Snapshot snapshot = new Snapshot().setQualifier(Qualifiers.FILE).setCreatedAt(new Date());
+ snapshot.setId(10);
+ PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION, new Date(),
+ snapshot)
+ .setModeParameter("2.3")
+ .setIndex(1);
+
+ assertThat(pastSnapshot.getModeParameter()).isEqualTo("2.3");
+ assertThat(pastSnapshot.getIndex()).isEqualTo(1);
+ assertThat(pastSnapshot.getQualifier()).isEqualTo(Qualifiers.FILE);
+ assertThat(pastSnapshot.getDate()).isNotNull();
+ assertThat(pastSnapshot.getProjectSnapshotId()).isEqualTo(10);
+ }
+
+ @Test
+ public void test_some_setters_and_getters2() {
+ PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION, new Date(), new Snapshot());
+
+ assertThat(pastSnapshot.getQualifier()).isNull();
+ assertThat(pastSnapshot.getDate()).isNull();
+ assertThat(pastSnapshot.getProjectSnapshotId()).isNull();
+ }
+
+ @Test
public void testToStringForVersion() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION, new Date()).setModeParameter("2.3");
- assertThat(pastSnapshot.toString(), startsWith("Compare to version 2.3"));
+ assertThat(pastSnapshot.toString()).startsWith("Compare to version 2.3");
}
@Test
public void testToStringForVersionWithoutDate() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION).setModeParameter("2.3");
- assertThat(pastSnapshot.toString(), equalTo("Compare to version 2.3"));
+ assertThat(pastSnapshot.toString()).isEqualTo("Compare to version 2.3");
}
@Test
public void testToStringForNumberOfDays() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DAYS, new Date()).setModeParameter("30");
- assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days ("));
+ assertThat(pastSnapshot.toString()).startsWith("Compare over 30 days (");
}
@Test
public void testToStringForNumberOfDaysWithSnapshot() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DAYS, new Date(), new Snapshot().setCreatedAt(new Date())).setModeParameter("30");
- assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days ("));
+ assertThat(pastSnapshot.toString()).startsWith("Compare over 30 days (");
}
@Test
public void testToStringForDate() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, new Date());
- assertThat(pastSnapshot.toString(), startsWith("Compare to date "));
+ assertThat(pastSnapshot.toString()).startsWith("Compare to date ");
}
@Test
public void testToStringForDateWithSnapshot() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, new Date(), new Snapshot().setCreatedAt(new Date()));
- assertThat(pastSnapshot.toString(), startsWith("Compare to date "));
+ assertThat(pastSnapshot.toString()).startsWith("Compare to date ");
}
@Test
public void testToStringForPreviousAnalysis() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, new Date(), new Snapshot().setCreatedAt(new Date()));
- assertThat(pastSnapshot.toString(), startsWith("Compare to previous analysis "));
+ assertThat(pastSnapshot.toString()).startsWith("Compare to previous analysis ");
}
@Test
public void testToStringForPreviousAnalysisWithoutDate() {
PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
- assertThat(pastSnapshot.toString(), equalTo("Compare to previous analysis"));
+ assertThat(pastSnapshot.toString()).isEqualTo("Compare to previous analysis");
}
}
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 cef290c6f00..be5cc3c4540 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
@@ -42,28 +42,29 @@ import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.ScanIssues;
import org.sonar.core.component.ScanGraph;
-import static org.hamcrest.Matchers.nullValue;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class DefaultIndexTest {
private DefaultIndex index = null;
+ private DeprecatedViolations deprecatedViolations;
private DefaultResourceCreationLock lock;
private Rule rule;
private RuleFinder ruleFinder;
@Before
public void createIndex() {
+ deprecatedViolations = mock(DeprecatedViolations.class);
lock = new DefaultResourceCreationLock(new Settings());
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
ruleFinder = mock(RuleFinder.class);
- index = new DefaultIndex(mock(PersistenceManager.class), lock, mock(ProjectTree.class), metricFinder, mock(ScanGraph.class),
- mock(DeprecatedViolations.class));
+ index = new DefaultIndex(mock(PersistenceManager.class), lock, mock(ProjectTree.class), metricFinder, mock(ScanGraph.class), deprecatedViolations);
Project project = new Project("project");
ResourceFilter filter = new ResourceFilter() {
@@ -83,26 +84,26 @@ public class DefaultIndexTest {
@Test
public void shouldCreateUID() {
Project project = new Project("my_project");
- assertThat(DefaultIndex.createUID(project, project), is("my_project"));
+ assertThat(DefaultIndex.createUID(project, project)).isEqualTo("my_project");
JavaPackage javaPackage = new JavaPackage("org.foo");
- assertThat(DefaultIndex.createUID(project, javaPackage), is("my_project:org.foo"));
+ assertThat(DefaultIndex.createUID(project, javaPackage)).isEqualTo("my_project:org.foo");
Library library = new Library("junit:junit", "4.7");
- assertThat(DefaultIndex.createUID(project, library), is("junit:junit"));
+ assertThat(DefaultIndex.createUID(project, library)).isEqualTo("junit:junit");
}
@Test
public void shouldIndexParentOfDeprecatedFiles() {
File file = new File("org/foo/Bar.java");
- assertThat(index.index(file), is(true));
+ assertThat(index.index(file)).isTrue();
Directory reference = new Directory("org/foo");
- assertThat(index.getResource(reference).getName(), is("org/foo"));
- assertThat(index.isIndexed(reference, true), is(true));
- assertThat(index.isExcluded(reference), is(false));
- assertThat(index.getChildren(reference).size(), is(1));
- assertThat(index.getParent(reference), is(Project.class));
+ assertThat(index.getResource(reference).getName()).isEqualTo("org/foo");
+ assertThat(index.isIndexed(reference, true)).isTrue();
+ assertThat(index.isExcluded(reference)).isFalse();
+ assertThat(index.getChildren(reference)).hasSize(1);
+ assertThat(index.getParent(reference)).isInstanceOf(Project.class);
}
@Test
@@ -111,27 +112,27 @@ public class DefaultIndexTest {
File file = new File("org/foo/Bar.java");
file.setLanguage(Java.INSTANCE);
- assertThat(index.index(directory), is(true));
- assertThat(index.index(file, directory), is(true));
+ assertThat(index.index(directory)).isTrue();
+ assertThat(index.index(file, directory)).isTrue();
File fileRef = new File("org/foo/Bar.java");
- assertThat(index.getResource(fileRef).getKey(), is("org/foo/Bar.java"));
- assertThat(index.getResource(fileRef).getLanguage(), is((Language) Java.INSTANCE));
- assertThat(index.isIndexed(fileRef, true), is(true));
- assertThat(index.isExcluded(fileRef), is(false));
- assertThat(index.getChildren(fileRef).size(), is(0));
- assertThat(index.getParent(fileRef), is(Directory.class));
+ assertThat(index.getResource(fileRef).getKey()).isEqualTo("org/foo/Bar.java");
+ assertThat(index.getResource(fileRef).getLanguage().getKey()).isEqualTo("java");
+ assertThat(index.isIndexed(fileRef, true)).isTrue();
+ assertThat(index.isExcluded(fileRef)).isFalse();
+ assertThat(index.getChildren(fileRef)).isEmpty();
+ assertThat(index.getParent(fileRef)).isInstanceOf(Directory.class);
}
@Test
public void shouldIndexLibraryOutsideProjectTree() {
Library lib = new Library("junit", "4.8");
- assertThat(index.index(lib), is(true));
+ assertThat(index.index(lib)).isTrue();
Library reference = new Library("junit", "4.8");
- assertThat(index.getResource(reference).getQualifier(), is(Qualifiers.LIBRARY));
- assertThat(index.isIndexed(reference, true), is(true));
- assertThat(index.isExcluded(reference), is(false));
+ assertThat(index.getResource(reference).getQualifier()).isEqualTo(Qualifiers.LIBRARY);
+ assertThat(index.isIndexed(reference, true)).isTrue();
+ assertThat(index.isExcluded(reference)).isFalse();
}
@Test
@@ -139,14 +140,14 @@ public class DefaultIndexTest {
Directory directory = new Directory("org/other");
File file = new File("org/foo/Bar.java");
- assertThat(index.index(file, directory), is(false));
+ assertThat(index.index(file, directory)).isFalse();
File fileRef = new File("org/foo/Bar.java");
- assertThat(index.isIndexed(directory, true), is(false));
- assertThat(index.isIndexed(fileRef, true), is(false));
- assertThat(index.isExcluded(fileRef), is(false));
- assertThat(index.getChildren(fileRef).size(), is(0));
- assertThat(index.getParent(fileRef), nullValue());
+ assertThat(index.isIndexed(directory, true)).isFalse();
+ assertThat(index.isIndexed(fileRef, true)).isFalse();
+ assertThat(index.isExcluded(fileRef)).isFalse();
+ assertThat(index.getChildren(fileRef)).isEmpty();
+ assertThat(index.getParent(fileRef)).isNull();
}
/**
@@ -157,8 +158,8 @@ public class DefaultIndexTest {
lock.lock();
Directory dir = new Directory("org/foo");
- assertThat(index.index(dir), is(true));
- assertThat(index.isIndexed(dir, true), is(true));
+ assertThat(index.index(dir)).isTrue();
+ assertThat(index.isIndexed(dir, true)).isTrue();
}
@Test(expected = SonarException.class)
@@ -173,10 +174,10 @@ public class DefaultIndexTest {
@Test
public void shouldBeExcluded() {
File file = new File("org/foo/ExcludedBar.java");
- assertThat(index.index(file), is(false));
- assertThat(index.isIndexed(file, true), is(true));
- assertThat(index.isIndexed(file, false), is(false));
- assertThat(index.isExcluded(file), is(true));
+ assertThat(index.index(file)).isFalse();
+ assertThat(index.isIndexed(file, true)).isTrue();
+ assertThat(index.isIndexed(file, false)).isFalse();
+ assertThat(index.isExcluded(file)).isTrue();
}
@Test
@@ -184,8 +185,8 @@ public class DefaultIndexTest {
Resource dir = new Directory("org/foo");
index.addMeasure(dir, new Measure("ncloc").setValue(50.0));
- assertThat(index.isIndexed(dir, true), is(true));
- assertThat(index.getMeasures(dir, MeasuresFilters.metric("ncloc")).getIntValue(), is(50));
+ assertThat(index.isIndexed(dir, true)).isTrue();
+ assertThat(index.getMeasures(dir, MeasuresFilters.metric("ncloc")).getIntValue()).isEqualTo(50);
}
/**
@@ -197,7 +198,7 @@ public class DefaultIndexTest {
Violation violation = Violation.create((Rule) null, file);
index.addViolation(violation);
- assertThat(index.getViolations(file).size(), is(0));
+ assertThat(index.getViolations(file)).isEmpty();
}
/**
@@ -211,7 +212,45 @@ public class DefaultIndexTest {
Violation violation = Violation.create(ruleWithoutID, file);
index.addViolation(violation);
- assertThat(index.getViolations(file).size(), is(0));
+ assertThat(index.getViolations(file)).isEmpty();
+ }
+
+ @Test
+ public void should_get_violation() {
+ Rule rule = Rule.create("repoKey", "ruleKey", "Rule");
+ File file = new File("org/foo/Bar.java");
+ Violation violation = Violation.create(rule, file);
+ when(deprecatedViolations.get(anyString())).thenReturn(newArrayList(violation));
+
+ index.addViolation(violation);
+
+ assertThat(index.getViolations(file)).hasSize(1);
+ }
+
+ @Test
+ public void should_get_filtered_violation_with_off_switch_mode() {
+ Rule rule = Rule.create("repoKey", "ruleKey", "Rule");
+ File file = new File("org/foo/Bar.java");
+ Violation violation = Violation.create(rule, file).setSwitchedOff(true);
+
+ when(deprecatedViolations.get(anyString())).thenReturn(newArrayList(violation));
+
+ index.addViolation(violation);
+
+ assertThat(index.getViolations(ViolationQuery.create().forResource(file).setSwitchMode(ViolationQuery.SwitchMode.OFF))).hasSize(1);
+ }
+
+ @Test
+ public void should_get_filtered_violation_with_on_switch_mode() {
+ Rule rule = Rule.create("repoKey", "ruleKey", "Rule");
+ File file = new File("org/foo/Bar.java");
+ Violation violation = Violation.create(rule, file).setSwitchedOff(false);
+
+ when(deprecatedViolations.get(anyString())).thenReturn(newArrayList(violation));
+
+ index.addViolation(violation);
+
+ assertThat(index.getViolations(ViolationQuery.create().forResource(file).setSwitchMode(ViolationQuery.SwitchMode.ON))).hasSize(1);
}
@Test(expected = IllegalArgumentException.class)