aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/test
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-01-21 14:32:24 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-01-21 14:32:24 +0100
commit08cb4444e0341652a4bf34133485ac405fb1ec50 (patch)
treebfefc7453a2ad73be491cdeaa10c3b1bd45bd060 /sonar-batch/src/test
parent1ab2475003835b71ff44b3652691c538059db699 (diff)
downloadsonarqube-08cb4444e0341652a4bf34133485ac405fb1ec50.tar.gz
sonarqube-08cb4444e0341652a4bf34133485ac405fb1ec50.zip
SONAR-2127 API: do not automatically create hierarchy of resource tree
Diffstat (limited to 'sonar-batch/src/test')
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java113
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/DefaultPersistenceManagerTest.java46
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java22
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java12
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java12
5 files changed, 175 insertions, 30 deletions
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 67a0e4e36d6..25f6bcb6cc1 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
@@ -19,25 +19,124 @@
*/
package org.sonar.batch.index;
+import org.apache.commons.lang.StringUtils;
+import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
-import org.sonar.api.resources.JavaPackage;
-import org.sonar.api.resources.Library;
-import org.sonar.api.resources.Project;
+import org.sonar.api.batch.ResourceFilter;
+import org.sonar.api.measures.MetricFinder;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.*;
+import org.sonar.batch.DefaultResourceCreationLock;
+import org.sonar.batch.ProjectTree;
+import org.sonar.batch.ResourceFilters;
+import org.sonar.batch.ViolationFilters;
+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;
public class DefaultIndexTest {
+ private DefaultIndex index = null;
+
+ @Before
+ public void createIndex() {
+ index = new DefaultIndex(mock(PersistenceManager.class), new DefaultResourceCreationLock(), mock(ProjectTree.class), mock(MetricFinder.class));
+ Project project = new Project("project");
+
+ ResourceFilter filter = new ResourceFilter() {
+ public boolean isIgnored(Resource resource) {
+ return StringUtils.containsIgnoreCase(resource.getKey(), "excluded");
+ }
+ };
+ index.setCurrentProject(project, new ResourceFilters(new ResourceFilter[]{filter}), new ViolationFilters(), RulesProfile.create());
+ index.doStart(project);
+ }
+
+
@Test
- public void shouldCalculateResourceEffectiveKey() {
+ public void shouldCreateUID() {
Project project = new Project("my_project");
- assertThat(DefaultIndex.calculateResourceEffectiveKey(project, project), is("my_project"));
+ assertThat(DefaultIndex.createUID(project, project), is("my_project"));
JavaPackage javaPackage = new JavaPackage("org.foo");
- assertThat(DefaultIndex.calculateResourceEffectiveKey(project, javaPackage), is("my_project:org.foo"));
+ assertThat(DefaultIndex.createUID(project, javaPackage), is("my_project:org.foo"));
Library library = new Library("junit:junit", "4.7");
- assertThat(DefaultIndex.calculateResourceEffectiveKey(project, library), is("junit:junit"));
+ assertThat(DefaultIndex.createUID(project, library), is("junit:junit"));
+ }
+
+ @Test
+ public void shouldIndexParentOfDeprecatedFiles() {
+ File file = new File("org/foo/Bar.java");
+ assertThat(index.index(file), is(true));
+
+ Directory reference = new Directory("org/foo");
+ assertThat(index.getResource(reference).getName(), is("org/foo"));
+ assertThat(index.isIndexed(reference), is(true));
+ assertThat(index.isExcluded(reference), is(false));
+ assertThat(index.getChildren(reference).size(), is(1));
+ assertThat(index.getParent(reference), is(Project.class));
+ }
+
+ @Test
+ public void shouldIndexTreeOfResources() {
+ Directory directory = new Directory("org/foo");
+ 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));
+
+ 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), is(true));
+ assertThat(index.isExcluded(fileRef), is(false));
+ assertThat(index.getChildren(fileRef).size(), is(0));
+ assertThat(index.getParent(fileRef), is(Directory.class));
+ }
+
+ @Test
+ public void shouldIndexLibraryOutsideProjectTree() {
+ Library lib = new Library("junit", "4.8");
+ assertThat(index.index(lib), is(true));
+
+ Library reference = new Library("junit", "4.8");
+ assertThat(index.getResource(reference).getQualifier(), is(Qualifiers.LIBRARY));
+ assertThat(index.isIndexed(reference), is(true));
+ assertThat(index.isExcluded(reference), is(false));
+ }
+
+ @Test
+ public void shouldNotIndexResourceIfParentNotIndexed() {
+ Directory directory = new Directory("org/other");
+ File file = new File("org/foo/Bar.java");
+
+ assertThat(index.index(file, directory), is(false));
+
+ File fileRef = new File("org/foo/Bar.java");
+ assertThat(index.isIndexed(directory), is(false));
+ assertThat(index.isIndexed(fileRef), is(false));
+ assertThat(index.isExcluded(fileRef), is(false));
+ assertThat(index.getChildren(fileRef).size(), is(0));
+ assertThat(index.getParent(fileRef), nullValue());
+ }
+
+
+ @Test
+ @Ignore("TODO: should it be really possible")
+ public void shouldIndexDirectChildOfProject() {
+
+ }
+
+ @Test
+ public void shouldBeExcluded() {
+ File file = new File("org/foo/ExcludedBar.java");
+ assertThat(index.index(file), is(false));
+ assertThat(index.isIndexed(file), is(false));
+ assertThat(index.isExcluded(file), is(true));
}
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultPersistenceManagerTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultPersistenceManagerTest.java
new file mode 100644
index 00000000000..48458f63a1c
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultPersistenceManagerTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.index;
+
+import org.junit.Test;
+import org.sonar.api.resources.Directory;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.Library;
+import org.sonar.api.resources.Project;
+import org.sonar.java.api.JavaClass;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class DefaultPersistenceManagerTest {
+
+ @Test
+ public void shouldPersistResoucesWithScopeHigherThanFile() {
+ assertThat(DefaultPersistenceManager.isPersistable(new File("Foo.java")), is(true));
+ assertThat(DefaultPersistenceManager.isPersistable(new Directory("bar/Foo.java")), is(true));
+ assertThat(DefaultPersistenceManager.isPersistable(new Project("foo")), is(true));
+ assertThat(DefaultPersistenceManager.isPersistable(new Library("foo", "1.2")), is(true));
+ }
+
+ @Test
+ public void shouldNotPersistResoucesWithScopeLowerThanFile() {
+ assertThat(DefaultPersistenceManager.isPersistable(JavaClass.createRef("com.foo.Bar")), is(false));
+ }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java
index bfd8a913dc6..ec61785d170 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java
@@ -66,7 +66,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
setupData("shared");
ResourcePersister persister = new DefaultResourcePersister(getSession());
- persister.saveProject(singleProject);
+ persister.saveProject(singleProject, null);
checkTables("shouldSaveNewProject", "projects", "snapshots");
}
@@ -76,10 +76,10 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
setupData("shared");
ResourcePersister persister = new DefaultResourcePersister(getSession());
- persister.saveProject(multiModuleProject);
- persister.saveProject(moduleA);
- persister.saveProject(moduleB);
- persister.saveProject(moduleB1);
+ persister.saveProject(multiModuleProject, null);
+ persister.saveProject(moduleA, multiModuleProject);
+ persister.saveProject(moduleB, multiModuleProject);
+ persister.saveProject(moduleB1, moduleB);
checkTables("shouldSaveNewMultiModulesProject", "projects", "snapshots");
}
@@ -89,7 +89,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
setupData("shared");
ResourcePersister persister = new DefaultResourcePersister(getSession());
- persister.saveProject(singleProject);
+ persister.saveProject(singleProject, null);
persister.saveResource(singleProject, new JavaPackage("org.foo").setEffectiveKey("foo:org.foo"));
// check that the directory is attached to the project
@@ -101,7 +101,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
setupData("shared");
ResourcePersister persister = new DefaultResourcePersister(getSession());
- persister.saveProject(singleProject);
+ persister.saveProject(singleProject, null);
persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));
persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));// do nothing, already saved
persister.saveResource(singleProject, new Library("junit:junit", "3.2").setEffectiveKey("junit:junit"));
@@ -114,8 +114,8 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
setupData("shared");
DefaultResourcePersister persister = new DefaultResourcePersister(getSession());
- persister.saveProject(multiModuleProject);
- persister.saveProject(moduleA);
+ persister.saveProject(multiModuleProject, null);
+ persister.saveProject(moduleA, multiModuleProject);
persister.saveResource(moduleA, new JavaPackage("org.foo").setEffectiveKey("a:org.foo"));
persister.saveResource(moduleA, new JavaFile("org.foo.MyClass").setEffectiveKey("a:org.foo.MyClass"));
persister.clear();
@@ -132,9 +132,9 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
ResourcePersister persister = new DefaultResourcePersister(getSession());
singleProject.setName("new name");
singleProject.setDescription("new description");
- persister.saveProject(singleProject);
+ persister.saveProject(singleProject, null);
checkTables("shouldUpdateExistingResource", "projects", "snapshots");
}
-
+
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
index 90d597908c5..13a299b38b3 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
@@ -67,9 +67,9 @@ public class MeasurePersisterTest extends AbstractDbUnitTestCase {
fileSnapshot = getSession().getSingleResult(Snapshot.class, "id", FILE_SNAPSHOT_ID);
ncloc = getSession().getSingleResult(Metric.class, "key", "ncloc");
coverage = getSession().getSingleResult(Metric.class, "key", "coverage");
- when(resourcePersister.saveResource((Project) anyObject(), eq(project))).thenReturn(projectSnapshot);
- when(resourcePersister.saveResource((Project) anyObject(), eq(aPackage))).thenReturn(packageSnapshot);
- when(resourcePersister.saveResource((Project) anyObject(), eq(aFile))).thenReturn(fileSnapshot);
+ when(resourcePersister.getSnapshotOrFail(eq(project))).thenReturn(projectSnapshot);
+ when(resourcePersister.getSnapshotOrFail(eq(aPackage))).thenReturn(packageSnapshot);
+ when(resourcePersister.getSnapshotOrFail(eq(aFile))).thenReturn(fileSnapshot);
when(resourcePersister.getSnapshot(project)).thenReturn(projectSnapshot);
when(resourcePersister.getSnapshot(aPackage)).thenReturn(packageSnapshot);
when(resourcePersister.getSnapshot(aFile)).thenReturn(fileSnapshot);
@@ -122,7 +122,7 @@ public class MeasurePersisterTest extends AbstractDbUnitTestCase {
measurePersister.setDelayedMode(true);
measurePersister.saveMeasure(project, new Measure(ncloc).setValue(1234.0));
- measurePersister.saveMeasure(project, aPackage, new Measure(ncloc).setValue(50.0));
+ measurePersister.saveMeasure(aPackage, new Measure(ncloc).setValue(50.0));
assertThat(getSession().getResults(MeasureModel.class, "metricId", 1).size(), is(0));
@@ -135,7 +135,7 @@ public class MeasurePersisterTest extends AbstractDbUnitTestCase {
measurePersister.setDelayedMode(true);
measurePersister.saveMeasure(project, new Measure(ncloc).setValue(1234.0).setPersistenceMode(PersistenceMode.DATABASE)); // database only
- measurePersister.saveMeasure(project, aPackage, new Measure(ncloc).setValue(50.0)); // database + memory
+ measurePersister.saveMeasure(aPackage, new Measure(ncloc).setValue(50.0)); // database + memory
// no dump => the db-only measure is saved
@@ -160,7 +160,7 @@ public class MeasurePersisterTest extends AbstractDbUnitTestCase {
public void shouldNotSaveBestValueMeasuresInDelayedMode() {
measurePersister.setDelayedMode(true);
- measurePersister.saveMeasure(project, aFile, new Measure(coverage).setValue(100.0));
+ measurePersister.saveMeasure(aFile, new Measure(coverage).setValue(100.0));
assertThat(getSession().getResults(MeasureModel.class, "metricId", COVERAGE_METRIC_ID, "snapshotId", FILE_SNAPSHOT_ID).size(), is(0));
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
index d92fdb8adf7..cc92ff24a4c 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
@@ -22,6 +22,7 @@ package org.sonar.batch.index;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.DuplicatedSourceException;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
@@ -41,21 +42,20 @@ public class SourcePersisterTest extends AbstractDbUnitTestCase {
setupData("shared");
Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000);
ResourcePersister resourcePersister = mock(ResourcePersister.class);
- when(resourcePersister.saveResource((Project) anyObject(), (Resource) anyObject())).thenReturn(snapshot);
+ when(resourcePersister.getSnapshotOrFail((Resource) anyObject())).thenReturn(snapshot);
sourcePersister = new SourcePersister(getSession(), resourcePersister);
}
@Test
public void shouldSaveSource() {
- sourcePersister.saveSource(new Project(""), new JavaFile("org.foo.Bar"), "this is the file content");
+ sourcePersister.saveSource(new JavaFile("org.foo.Bar"), "this is the file content");
checkTables("shouldSaveSource", "snapshot_sources");
}
- @Test(expected = SonarException.class)
+ @Test(expected = DuplicatedSourceException.class)
public void shouldFailIfSourceSavedSeveralTimes() {
- Project project = new Project("project");
JavaFile file = new JavaFile("org.foo.Bar");
- sourcePersister.saveSource(project, file, "this is the file content");
- sourcePersister.saveSource(project, file, "new content"); // fail
+ sourcePersister.saveSource(file, "this is the file content");
+ sourcePersister.saveSource(file, "new content"); // fail
}
}