From bde531603464da7c5dacf2e7f4c5ea2e4555a986 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 7 Jan 2015 17:09:55 +0100 Subject: SONAR-6014 Isolate persistence of resources to ease its removal in preview mode --- .../org/sonar/batch/index/DefaultIndexTest.java | 10 +- .../batch/index/DefaultResourcePersisterTest.java | 419 --------------------- .../sonar/batch/index/FileHashesPersisterTest.java | 2 +- .../sonar/batch/index/MeasurePersisterTest.java | 2 +- .../org/sonar/batch/index/ResourceCacheTest.java | 5 +- .../sonar/batch/index/ResourcePersisterTest.java | 417 ++++++++++++++++++++ .../org/sonar/batch/index/SourcePersisterTest.java | 2 +- .../batch/issue/DeprecatedViolationsTest.java | 5 +- .../batch/qualitygate/QualityGateVerifierTest.java | 2 +- .../batch/report/ComponentsPublisherTest.java | 23 +- .../filesystem/PreviousFileHashLoaderTest.java | 2 +- .../index/DefaultResourcePersisterTest/shared.xml | 12 - ...ldRemoveRootIndexIfResourceIsProject-result.xml | 17 - .../shouldRemoveRootIndexIfResourceIsProject.xml | 12 - .../shouldSaveCopyProject-result.xml | 22 -- .../shouldSaveNewDirectory-result.xml | 30 -- .../shouldSaveNewLibrary-result.xml | 34 -- .../shouldSaveNewMultiModulesProject-result.xml | 61 --- ...SaveNewMultiModulesProjectAndLibrary-result.xml | 65 ---- .../shouldSaveNewProject-result.xml | 22 -- .../shouldUpdateExistingResource-result.xml | 17 - .../shouldUpdateExistingResource.xml | 11 - .../batch/index/ResourcePersisterTest/shared.xml | 12 + ...ldRemoveRootIndexIfResourceIsProject-result.xml | 17 + .../shouldRemoveRootIndexIfResourceIsProject.xml | 12 + .../shouldSaveCopyProject-result.xml | 22 ++ .../shouldSaveNewDirectory-result.xml | 30 ++ .../shouldSaveNewLibrary-result.xml | 34 ++ .../shouldSaveNewMultiModulesProject-result.xml | 61 +++ ...SaveNewMultiModulesProjectAndLibrary-result.xml | 65 ++++ .../shouldSaveNewProject-result.xml | 22 ++ .../shouldUpdateExistingResource-result.xml | 17 + .../shouldUpdateExistingResource.xml | 11 + 33 files changed, 745 insertions(+), 750 deletions(-) delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveCopyProject-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewProject-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource-result.xml delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shared.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveCopyProject-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewDirectory-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewLibrary-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewProject-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource.xml (limited to 'sonar-batch/src/test') 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 e943cf779ca..42ca92d27b2 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 @@ -25,7 +25,6 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.database.model.Snapshot; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.MeasuresFilters; @@ -45,13 +44,11 @@ import org.sonar.batch.ProjectTree; import org.sonar.batch.issue.DeprecatedViolations; import org.sonar.batch.issue.ModuleIssues; import org.sonar.batch.scan.measure.MeasureCache; -import org.sonar.core.component.ScanGraph; import java.io.IOException; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -80,8 +77,8 @@ public class DefaultIndexTest { ruleFinder = mock(RuleFinder.class); ProjectTree projectTree = mock(ProjectTree.class); - ResourcePersister resourcePersister = mock(ResourcePersister.class); - index = new DefaultIndex(resourcePersister, null, null, null, projectTree, metricFinder, mock(ScanGraph.class), deprecatedViolations, + ResourceCache resourceCache = new ResourceCache(); + index = new DefaultIndex(resourceCache, null, null, null, projectTree, metricFinder, deprecatedViolations, mock(ResourceKeyMigration.class), mock(MeasureCache.class)); @@ -95,9 +92,6 @@ public class DefaultIndexTest { moduleB1 = new Project("moduleB1").setParent(moduleB); when(projectTree.getProjectDefinition(moduleB1)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleB/moduleB1"))); - when(resourcePersister.saveResource(any(Project.class), any(Resource.class), any(Resource.class))).thenReturn( - new BatchResource(1, mock(Resource.class), new Snapshot().setId(1), null)); - RulesProfile rulesProfile = RulesProfile.create(); rule = Rule.create("repoKey", "ruleKey", "Rule"); rule.setId(1); 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 deleted file mode 100644 index d08e6b9ded3..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java +++ /dev/null @@ -1,419 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import org.apache.ibatis.session.SqlSession; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.config.Settings; -import org.sonar.api.database.model.Snapshot; -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.api.resources.Resource; -import org.sonar.api.security.ResourcePermissions; -import org.sonar.batch.ProjectTree; -import org.sonar.batch.issue.DeprecatedViolations; -import org.sonar.batch.scan.measure.MeasureCache; -import org.sonar.core.component.ComponentDto; -import org.sonar.core.component.ScanGraph; -import org.sonar.core.component.db.ComponentMapper; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import javax.persistence.Query; - -import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Arrays; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - private Project singleProject, singleCopyProject, multiModuleProject, moduleA, moduleB, moduleB1, existingProject; - private ResourceCache resourceCache; - - @Before - public void before() throws ParseException { - resourceCache = new ResourceCache(); - - SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); - singleProject = newProject("foo", "java"); - singleProject.setName("Foo").setDescription("some description").setAnalysisDate(format.parse("25/12/2010")); - - existingProject = newProject("my:key", "java"); - existingProject.setName("Other project").setDescription("some description").setAnalysisDate(format.parse("25/12/2010")); - - singleCopyProject = newCopyProject("foo", "java", 10); - singleCopyProject.setName("Foo").setDescription("some description").setAnalysisDate(format.parse("25/12/2010")); - - multiModuleProject = newProject("root", "java"); - multiModuleProject.setName("Root").setAnalysisDate(format.parse("25/12/2010")); - - moduleA = newProject("a", "java"); - moduleA.setName("A").setAnalysisDate(format.parse("25/12/2010")); - moduleA.setParent(multiModuleProject); - moduleA.setPath("/moduleA"); - - moduleB = newProject("b", "java"); - moduleB.setName("B").setAnalysisDate(format.parse("25/12/2010")); - moduleB.setParent(multiModuleProject); - moduleB.setPath("/moduleB"); - - moduleB1 = newProject("b1", "java"); - moduleB1.setName("B1").setAnalysisDate(format.parse("25/12/2010")); - moduleB1.setParent(moduleB); - moduleB1.setPath("/moduleB1"); - } - - @Test - public void shouldSaveNewProject() { - setupData("shared"); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - persister.saveProject(singleProject, null); - - checkTables("shouldSaveNewProject", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, - "projects", "snapshots"); - - // Need to enable snapshot to make resource visible using ComponentMapper - enableSnapshot(1001); - try (SqlSession session = getMyBatis().openSession(false)) { - ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); - assertThat(newProject.uuid()).isNotNull(); - assertThat(newProject.projectUuid()).isEqualTo(newProject.uuid()); - assertThat(newProject.moduleUuid()).isNull(); - assertThat(newProject.moduleUuidPath()).isEmpty(); - // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table - assertThat(newProject.getCreatedAt()).isNotNull(); - } - } - - @Test - public void shouldSaveCopyProject() { - setupData("shared"); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - persister.saveProject(singleCopyProject, null); - - checkTables("shouldSaveCopyProject", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, - "projects", "snapshots"); - // Need to enable snapshot to make resource visible using ComponentMapper - enableSnapshot(1001); - try (SqlSession session = getMyBatis().openSession(false)) { - ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); - assertThat(newProject.uuid()).isNotNull(); - assertThat(newProject.projectUuid()).isEqualTo(newProject.uuid()); - assertThat(newProject.moduleUuid()).isNull(); - assertThat(newProject.moduleUuidPath()).isEmpty(); - } - } - - @Test - public void shouldSaveNewMultiModulesProject() { - setupData("shared"); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - persister.saveProject(multiModuleProject, null); - persister.saveProject(moduleA, multiModuleProject); - persister.saveProject(moduleB, multiModuleProject); - persister.saveProject(moduleB1, moduleB); - Resource file = File.create("src/main/java/org/Foo.java").setEffectiveKey("b1:src/main/java/org/Foo.java"); - file.getParent().setEffectiveKey("b1:src/main/java/org"); - persister.saveResource(moduleB1, file.getParent()); - persister.saveResource(moduleB1, file, file.getParent()); - - checkTables("shouldSaveNewMultiModulesProject", - new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, "projects", "snapshots"); - - // Need to enable snapshot to make resource visible using ComponentMapper - enableSnapshot(1001); - enableSnapshot(1002); - enableSnapshot(1003); - enableSnapshot(1004); - enableSnapshot(1005); - enableSnapshot(1006); - try (SqlSession session = getMyBatis().openSession(false)) { - ComponentDto root = session.getMapper(ComponentMapper.class).selectByKey("root"); - assertThat(root.uuid()).isNotNull(); - assertThat(root.projectUuid()).isEqualTo(root.uuid()); - assertThat(root.moduleUuid()).isNull(); - assertThat(root.moduleUuidPath()).isEmpty(); - ComponentDto a = session.getMapper(ComponentMapper.class).selectByKey("a"); - assertThat(a.uuid()).isNotNull(); - assertThat(a.projectUuid()).isEqualTo(root.uuid()); - assertThat(a.moduleUuid()).isEqualTo(root.uuid()); - assertThat(a.moduleUuidPath()).isEqualTo(root.uuid()); - ComponentDto b = session.getMapper(ComponentMapper.class).selectByKey("b"); - assertThat(b.uuid()).isNotNull(); - assertThat(b.projectUuid()).isEqualTo(root.uuid()); - assertThat(b.moduleUuid()).isEqualTo(root.uuid()); - assertThat(b.moduleUuidPath()).isEqualTo(root.uuid()); - ComponentDto b1 = session.getMapper(ComponentMapper.class).selectByKey("b1"); - assertThat(b1.uuid()).isNotNull(); - assertThat(b1.projectUuid()).isEqualTo(root.uuid()); - assertThat(b1.moduleUuid()).isEqualTo(b.uuid()); - assertThat(b1.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid()); - ComponentDto dir = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org"); - assertThat(dir.uuid()).isNotNull(); - assertThat(dir.projectUuid()).isEqualTo(root.uuid()); - assertThat(dir.moduleUuid()).isEqualTo(b1.uuid()); - assertThat(dir.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); - ComponentDto fileComp = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org/Foo.java"); - assertThat(fileComp.uuid()).isNotNull(); - assertThat(fileComp.projectUuid()).isEqualTo(root.uuid()); - assertThat(fileComp.moduleUuid()).isEqualTo(b1.uuid()); - assertThat(fileComp.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); - } - } - - // FIXME this is a kind of medium test - @Test - public void shouldSaveNewMultiModulesProjectUsingIndex() throws IOException { - setupData("shared"); - - java.io.File baseDir = temp.newFolder(); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - - ProjectTree projectTree = mock(ProjectTree.class); - when(projectTree.getRootProject()).thenReturn(multiModuleProject); - when(projectTree.getProjectDefinition(multiModuleProject)).thenReturn(ProjectDefinition.create().setBaseDir(baseDir)); - when(projectTree.getProjectDefinition(moduleA)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleA"))); - when(projectTree.getProjectDefinition(moduleB)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleB"))); - when(projectTree.getProjectDefinition(moduleB1)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleB/moduleB1"))); - - DefaultIndex index = new DefaultIndex(persister, null, null, null, projectTree, mock(MetricFinder.class), mock(ScanGraph.class), - mock(DeprecatedViolations.class), - mock(ResourceKeyMigration.class), - mock(MeasureCache.class)); - - index.start(); - - Resource file = File.create("src/main/java/org/Foo.java"); - - index.setCurrentProject(moduleB1, null); - index.index(file); - - // Emulate another project having library dependency on moduleA - index.addResource(new Library(moduleA.getKey(), "1.0")); - - checkTables("shouldSaveNewMultiModulesProjectAndLibrary", - new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, "projects", "snapshots"); - - // Need to enable snapshot to make resource visible using ComponentMapper - enableSnapshot(1001); - enableSnapshot(1002); - enableSnapshot(1003); - enableSnapshot(1004); - enableSnapshot(1005); - enableSnapshot(1006); - try (SqlSession session = getMyBatis().openSession(false)) { - ComponentDto root = session.getMapper(ComponentMapper.class).selectByKey("root"); - System.out.println("Root: " + root.uuid()); - assertThat(root.uuid()).isNotNull(); - assertThat(root.projectUuid()).isEqualTo(root.uuid()); - assertThat(root.moduleUuid()).isNull(); - assertThat(root.moduleUuidPath()).isEmpty(); - ComponentDto a = session.getMapper(ComponentMapper.class).selectByKey("a"); - System.out.println("A: " + a.uuid()); - assertThat(a.uuid()).isNotNull(); - assertThat(a.projectUuid()).isEqualTo(root.uuid()); - assertThat(a.moduleUuid()).isEqualTo(root.uuid()); - assertThat(a.moduleUuidPath()).isEqualTo(root.uuid()); - ComponentDto b = session.getMapper(ComponentMapper.class).selectByKey("b"); - System.out.println("B: " + b.uuid()); - assertThat(b.uuid()).isNotNull(); - assertThat(b.projectUuid()).isEqualTo(root.uuid()); - assertThat(b.moduleUuid()).isEqualTo(root.uuid()); - assertThat(b.moduleUuidPath()).isEqualTo(root.uuid()); - ComponentDto b1 = session.getMapper(ComponentMapper.class).selectByKey("b1"); - System.out.println("B1: " + b1.uuid()); - assertThat(b1.uuid()).isNotNull(); - assertThat(b1.projectUuid()).isEqualTo(root.uuid()); - assertThat(b1.moduleUuid()).isEqualTo(b.uuid()); - assertThat(b1.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid()); - ComponentDto dir = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org"); - assertThat(dir.uuid()).isNotNull(); - assertThat(dir.projectUuid()).isEqualTo(root.uuid()); - assertThat(dir.moduleUuid()).isEqualTo(b1.uuid()); - assertThat(dir.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); - ComponentDto fileComp = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org/Foo.java"); - assertThat(fileComp.uuid()).isNotNull(); - assertThat(fileComp.projectUuid()).isEqualTo(root.uuid()); - assertThat(fileComp.moduleUuid()).isEqualTo(b1.uuid()); - assertThat(fileComp.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); - } - } - - @Test - public void shouldSaveNewDirectory() { - setupData("shared"); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - persister.saveProject(singleProject, null); - persister.saveResource(singleProject, - Directory.create("src/main/java/org/foo", "org.foo").setEffectiveKey("foo:src/main/java/org/foo")); - // check that the directory is attached to the project - checkTables("shouldSaveNewDirectory", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, - "projects", "snapshots"); - - // Need to enable snapshot to make resource visible using ComponentMapper - enableSnapshot(1001); - enableSnapshot(1002); - try (SqlSession session = getMyBatis().openSession(false)) { - ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); - ComponentDto newDir = session.getMapper(ComponentMapper.class).selectByKey("foo:src/main/java/org/foo"); - assertThat(newDir.uuid()).isNotNull(); - assertThat(newDir.projectUuid()).isEqualTo(newProject.uuid()); - assertThat(newDir.moduleUuid()).isEqualTo(newProject.uuid()); - assertThat(newDir.moduleUuidPath()).isEqualTo(newProject.uuid()); - } - } - - private void enableSnapshot(int resourceId) { - String hql = "UPDATE " + Snapshot.class.getSimpleName() + " SET last=true"; - hql += " WHERE project_id=:resourceId"; - Query query = getSession().createQuery(hql); - query.setParameter("resourceId", resourceId); - query.executeUpdate(); - getSession().commit(); - } - - @Test - public void shouldSaveNewLibrary() { - setupData("shared"); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - 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")); - - checkTables("shouldSaveNewLibrary", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, - "projects", "snapshots"); - - // Need to enable snapshot to make resource visible using ComponentMapper - enableSnapshot(1002); - try (SqlSession session = getMyBatis().openSession(false)) { - // FIXME selectByKey returns duplicates for libraries because of the join on snapshots table - ComponentDto newLib = session.getMapper(ComponentMapper.class).findByKeys(Arrays.asList("junit:junit")).get(0); - assertThat(newLib.uuid()).isNotNull(); - assertThat(newLib.projectUuid()).isEqualTo(newLib.uuid()); - assertThat(newLib.moduleUuid()).isNull(); - assertThat(newLib.moduleUuidPath()).isEmpty(); - } - } - - @Test - public void shouldUpdateExistingResource() { - setupData("shouldUpdateExistingResource"); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - singleProject.setName("new name"); - singleProject.setDescription("new description"); - persister.saveProject(singleProject, null); - - checkTables("shouldUpdateExistingResource", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); - } - - // SONAR-1700 - @Test - public void shouldRemoveRootIndexIfResourceIsProject() { - setupData("shouldRemoveRootIndexIfResourceIsProject"); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), resourceCache); - persister.saveProject(singleProject, null); - - checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); - } - - @Test - public void shouldGrantDefaultPermissionsIfNewProject() { - setupData("shared"); - - ResourcePermissions permissions = mock(ResourcePermissions.class); - when(permissions.hasRoles(singleProject)).thenReturn(false); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), permissions, resourceCache); - persister.saveProject(singleProject, null); - - verify(permissions).grantDefaultRoles(singleProject); - } - - @Test - public void shouldNotGrantDefaultPermissionsIfExistingProject() { - setupData("shared"); - - ResourcePermissions permissions = mock(ResourcePermissions.class); - when(permissions.hasRoles(singleProject)).thenReturn(true); - - ResourcePersister persister = new DefaultResourcePersister(getSession(), permissions, resourceCache); - persister.saveProject(singleProject, null); - - verify(permissions, never()).grantDefaultRoles(singleProject); - } - - private static Project newProject(String key, String language) { - Settings settings = new Settings(); - settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, language); - return new Project(key).setSettings(settings).setAnalysisType(Project.AnalysisType.DYNAMIC); - } - - private static Project newCopyProject(String key, String language, int copyResourceId) { - Settings settings = new Settings(); - settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, language); - return new CopyProject(key, copyResourceId).setSettings(settings).setAnalysisType(Project.AnalysisType.DYNAMIC); - } - - private static class CopyProject extends Project implements ResourceCopy { - - private int copyResourceId; - - public CopyProject(String key, int copyResourceId) { - super(key); - this.copyResourceId = copyResourceId; - } - - public int getCopyResourceId() { - return copyResourceId; - } - - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java index c66809ec396..5d01dde9045 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java @@ -57,7 +57,7 @@ public class FileHashesPersisterTest extends AbstractDaoTestCase { Snapshot snapshot = new Snapshot(); snapshot.setId(100); snapshot.setResourceId(200); - resourceCache.add(new Project("myProject").setId(200), null, snapshot); + resourceCache.add(new Project("myProject").setId(200), null).setSnapshot(snapshot); data = new ComponentDataCache(caches); data.setStringData("myProject", SnapshotDataTypes.FILE_HASHES, "org/struts/Action.java=123ABC"); 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 3ebb40264a2..8beab279cbf 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 @@ -201,7 +201,7 @@ public class MeasurePersisterTest extends AbstractDaoTestCase { private static BatchResource batchResource(Resource resource, int id) { Snapshot snapshot = mock(Snapshot.class); when(snapshot.getId()).thenReturn(id); - return new BatchResource(1, resource, snapshot, null); + return new BatchResource(1, resource, null).setSnapshot(snapshot); } private static Metric ncloc() { diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java index bc4330f8734..466947602c0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java @@ -20,7 +20,6 @@ package org.sonar.batch.index; import org.junit.Test; -import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.File; import org.sonar.api.resources.Resource; @@ -33,7 +32,7 @@ public class ResourceCacheTest { ResourceCache cache = new ResourceCache(); String componentKey = "struts:src/org/struts/Action.java"; Resource resource = new File("org/struts/Action.java").setEffectiveKey(componentKey); - cache.add(resource, null, new Snapshot()); + cache.add(resource, null); assertThat(cache.get(componentKey).resource()).isSameAs(resource); assertThat(cache.get("other")).isNull(); @@ -44,7 +43,7 @@ public class ResourceCacheTest { ResourceCache cache = new ResourceCache(); Resource resource = new File("org/struts/Action.java").setEffectiveKey(null); try { - cache.add(resource, null, new Snapshot()); + cache.add(resource, null); fail(); } catch (IllegalStateException e) { // success diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java new file mode 100644 index 00000000000..33da7d65bed --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java @@ -0,0 +1,417 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.index; + +import org.apache.ibatis.session.SqlSession; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.measure.MetricFinder; +import org.sonar.api.config.Settings; +import org.sonar.api.database.model.Snapshot; +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.api.resources.Resource; +import org.sonar.api.security.ResourcePermissions; +import org.sonar.batch.ProjectTree; +import org.sonar.batch.issue.DeprecatedViolations; +import org.sonar.batch.scan.measure.MeasureCache; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.component.ScanGraph; +import org.sonar.core.component.db.ComponentMapper; +import org.sonar.jpa.test.AbstractDbUnitTestCase; + +import javax.persistence.Query; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class ResourcePersisterTest extends AbstractDbUnitTestCase { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private Project singleProject, singleCopyProject, multiModuleProject, moduleA, moduleB, moduleB1, existingProject; + private ResourceCache resourceCache; + + private ResourcePersister persister; + + private ProjectTree projectTree; + + private ResourcePermissions permissions; + + @Before + public void before() throws ParseException { + resourceCache = new ResourceCache(); + + SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); + singleProject = newProject("foo", "java"); + singleProject.setName("Foo").setDescription("some description").setAnalysisDate(format.parse("25/12/2010")); + + existingProject = newProject("my:key", "java"); + existingProject.setName("Other project").setDescription("some description").setAnalysisDate(format.parse("25/12/2010")); + + singleCopyProject = newCopyProject("foo", "java", 10); + singleCopyProject.setName("Foo").setDescription("some description").setAnalysisDate(format.parse("25/12/2010")); + + multiModuleProject = newProject("root", "java"); + multiModuleProject.setName("Root").setAnalysisDate(format.parse("25/12/2010")); + + moduleA = newProject("a", "java"); + moduleA.setName("A").setAnalysisDate(format.parse("25/12/2010")); + moduleA.setParent(multiModuleProject); + moduleA.setPath("/moduleA"); + + moduleB = newProject("b", "java"); + moduleB.setName("B").setAnalysisDate(format.parse("25/12/2010")); + moduleB.setParent(multiModuleProject); + moduleB.setPath("/moduleB"); + + moduleB1 = newProject("b1", "java"); + moduleB1.setName("B1").setAnalysisDate(format.parse("25/12/2010")); + moduleB1.setParent(moduleB); + moduleB1.setPath("/moduleB1"); + + projectTree = mock(ProjectTree.class); + permissions = mock(ResourcePermissions.class); + persister = new ResourcePersister(projectTree, getSession(), permissions, resourceCache, mock(ScanGraph.class)); + } + + @Test + public void shouldSaveNewProject() { + setupData("shared"); + + persister.persist(null, singleProject, null); + + checkTables("shouldSaveNewProject", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, + "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + try (SqlSession session = getMyBatis().openSession(false)) { + ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); + assertThat(newProject.uuid()).isNotNull(); + assertThat(newProject.projectUuid()).isEqualTo(newProject.uuid()); + assertThat(newProject.moduleUuid()).isNull(); + assertThat(newProject.moduleUuidPath()).isEmpty(); + // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table + assertThat(newProject.getCreatedAt()).isNotNull(); + } + } + + @Test + public void shouldSaveCopyProject() { + setupData("shared"); + + persister.persist(null, singleCopyProject, null); + + checkTables("shouldSaveCopyProject", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, + "projects", "snapshots"); + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + try (SqlSession session = getMyBatis().openSession(false)) { + ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); + assertThat(newProject.uuid()).isNotNull(); + assertThat(newProject.projectUuid()).isEqualTo(newProject.uuid()); + assertThat(newProject.moduleUuid()).isNull(); + assertThat(newProject.moduleUuidPath()).isEmpty(); + } + } + + @Test + public void shouldSaveNewMultiModulesProject() { + setupData("shared"); + + resourceCache.add(multiModuleProject, null).setSnapshot(persister.persist(null, multiModuleProject, null)); + resourceCache.add(moduleA, multiModuleProject).setSnapshot(persister.persist(null, moduleA, multiModuleProject)); + resourceCache.add(moduleB, multiModuleProject).setSnapshot(persister.persist(null, moduleB, multiModuleProject)); + resourceCache.add(moduleB1, moduleB).setSnapshot(persister.persist(null, moduleB1, moduleB)); + Resource file = File.create("src/main/java/org/Foo.java").setEffectiveKey("b1:src/main/java/org/Foo.java"); + file.getParent().setEffectiveKey("b1:src/main/java/org"); + resourceCache.add(file.getParent(), moduleB1).setSnapshot(persister.persist(moduleB1, file.getParent(), null)); + persister.persist(moduleB1, file, file.getParent()); + + checkTables("shouldSaveNewMultiModulesProject", + new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + enableSnapshot(1002); + enableSnapshot(1003); + enableSnapshot(1004); + enableSnapshot(1005); + enableSnapshot(1006); + try (SqlSession session = getMyBatis().openSession(false)) { + ComponentDto root = session.getMapper(ComponentMapper.class).selectByKey("root"); + assertThat(root.uuid()).isNotNull(); + assertThat(root.projectUuid()).isEqualTo(root.uuid()); + assertThat(root.moduleUuid()).isNull(); + assertThat(root.moduleUuidPath()).isEmpty(); + ComponentDto a = session.getMapper(ComponentMapper.class).selectByKey("a"); + assertThat(a.uuid()).isNotNull(); + assertThat(a.projectUuid()).isEqualTo(root.uuid()); + assertThat(a.moduleUuid()).isEqualTo(root.uuid()); + assertThat(a.moduleUuidPath()).isEqualTo(root.uuid()); + ComponentDto b = session.getMapper(ComponentMapper.class).selectByKey("b"); + assertThat(b.uuid()).isNotNull(); + assertThat(b.projectUuid()).isEqualTo(root.uuid()); + assertThat(b.moduleUuid()).isEqualTo(root.uuid()); + assertThat(b.moduleUuidPath()).isEqualTo(root.uuid()); + ComponentDto b1 = session.getMapper(ComponentMapper.class).selectByKey("b1"); + assertThat(b1.uuid()).isNotNull(); + assertThat(b1.projectUuid()).isEqualTo(root.uuid()); + assertThat(b1.moduleUuid()).isEqualTo(b.uuid()); + assertThat(b1.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid()); + ComponentDto dir = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org"); + assertThat(dir.uuid()).isNotNull(); + assertThat(dir.projectUuid()).isEqualTo(root.uuid()); + assertThat(dir.moduleUuid()).isEqualTo(b1.uuid()); + assertThat(dir.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); + ComponentDto fileComp = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org/Foo.java"); + assertThat(fileComp.uuid()).isNotNull(); + assertThat(fileComp.projectUuid()).isEqualTo(root.uuid()); + assertThat(fileComp.moduleUuid()).isEqualTo(b1.uuid()); + assertThat(fileComp.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); + } + } + + // FIXME this is a kind of medium test + @Test + public void shouldSaveNewMultiModulesProjectUsingIndex() throws IOException { + setupData("shared"); + + java.io.File baseDir = temp.newFolder(); + + when(projectTree.getRootProject()).thenReturn(multiModuleProject); + when(projectTree.getProjectDefinition(multiModuleProject)).thenReturn(ProjectDefinition.create().setBaseDir(baseDir)); + when(projectTree.getProjectDefinition(moduleA)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleA"))); + when(projectTree.getProjectDefinition(moduleB)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleB"))); + when(projectTree.getProjectDefinition(moduleB1)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleB/moduleB1"))); + + DefaultIndex index = new DefaultIndex(resourceCache, null, null, null, projectTree, mock(MetricFinder.class), + mock(DeprecatedViolations.class), + mock(ResourceKeyMigration.class), + mock(MeasureCache.class)); + + index.start(); + + Resource file = File.create("src/main/java/org/Foo.java"); + + index.setCurrentProject(moduleB1, null); + index.index(file); + + // Emulate another project having library dependency on moduleA + index.addResource(new Library(moduleA.getKey(), "1.0")); + + persister.persist(); + + checkTables("shouldSaveNewMultiModulesProjectAndLibrary", + new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + enableSnapshot(1002); + enableSnapshot(1003); + enableSnapshot(1004); + enableSnapshot(1005); + enableSnapshot(1006); + try (SqlSession session = getMyBatis().openSession(false)) { + ComponentDto root = session.getMapper(ComponentMapper.class).selectByKey("root"); + System.out.println("Root: " + root.uuid()); + assertThat(root.uuid()).isNotNull(); + assertThat(root.projectUuid()).isEqualTo(root.uuid()); + assertThat(root.moduleUuid()).isNull(); + assertThat(root.moduleUuidPath()).isEmpty(); + ComponentDto a = session.getMapper(ComponentMapper.class).selectByKey("a"); + System.out.println("A: " + a.uuid()); + assertThat(a.uuid()).isNotNull(); + assertThat(a.projectUuid()).isEqualTo(root.uuid()); + assertThat(a.moduleUuid()).isEqualTo(root.uuid()); + assertThat(a.moduleUuidPath()).isEqualTo(root.uuid()); + ComponentDto b = session.getMapper(ComponentMapper.class).selectByKey("b"); + System.out.println("B: " + b.uuid()); + assertThat(b.uuid()).isNotNull(); + assertThat(b.projectUuid()).isEqualTo(root.uuid()); + assertThat(b.moduleUuid()).isEqualTo(root.uuid()); + assertThat(b.moduleUuidPath()).isEqualTo(root.uuid()); + ComponentDto b1 = session.getMapper(ComponentMapper.class).selectByKey("b1"); + System.out.println("B1: " + b1.uuid()); + assertThat(b1.uuid()).isNotNull(); + assertThat(b1.projectUuid()).isEqualTo(root.uuid()); + assertThat(b1.moduleUuid()).isEqualTo(b.uuid()); + assertThat(b1.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid()); + ComponentDto dir = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org"); + assertThat(dir.uuid()).isNotNull(); + assertThat(dir.projectUuid()).isEqualTo(root.uuid()); + assertThat(dir.moduleUuid()).isEqualTo(b1.uuid()); + assertThat(dir.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); + ComponentDto fileComp = session.getMapper(ComponentMapper.class).selectByKey("b1:src/main/java/org/Foo.java"); + assertThat(fileComp.uuid()).isNotNull(); + assertThat(fileComp.projectUuid()).isEqualTo(root.uuid()); + assertThat(fileComp.moduleUuid()).isEqualTo(b1.uuid()); + assertThat(fileComp.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid()); + } + } + + @Test + public void shouldSaveNewDirectory() { + setupData("shared"); + + resourceCache.add(singleProject, null).setSnapshot(persister.persist(null, singleProject, null)); + persister.persist(singleProject, Directory.create("src/main/java/org/foo", "org.foo").setEffectiveKey("foo:src/main/java/org/foo"), null); + // check that the directory is attached to the project + checkTables("shouldSaveNewDirectory", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, + "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + enableSnapshot(1002); + try (SqlSession session = getMyBatis().openSession(false)) { + ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); + ComponentDto newDir = session.getMapper(ComponentMapper.class).selectByKey("foo:src/main/java/org/foo"); + assertThat(newDir.uuid()).isNotNull(); + assertThat(newDir.projectUuid()).isEqualTo(newProject.uuid()); + assertThat(newDir.moduleUuid()).isEqualTo(newProject.uuid()); + assertThat(newDir.moduleUuidPath()).isEqualTo(newProject.uuid()); + } + } + + private void enableSnapshot(int resourceId) { + String hql = "UPDATE " + Snapshot.class.getSimpleName() + " SET last=true"; + hql += " WHERE project_id=:resourceId"; + Query query = getSession().createQuery(hql); + query.setParameter("resourceId", resourceId); + query.executeUpdate(); + getSession().commit(); + } + + @Test + public void shouldSaveNewLibrary() { + setupData("shared"); + + persister.persist(null, singleProject, null); + persister.persist(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"), null); + persister.persist(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"), null);// do nothing, already + // saved + persister.persist(singleProject, new Library("junit:junit", "3.2").setEffectiveKey("junit:junit"), null); + + checkTables("shouldSaveNewLibrary", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, + "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1002); + try (SqlSession session = getMyBatis().openSession(false)) { + // FIXME selectByKey returns duplicates for libraries because of the join on snapshots table + ComponentDto newLib = session.getMapper(ComponentMapper.class).findByKeys(Arrays.asList("junit:junit")).get(0); + assertThat(newLib.uuid()).isNotNull(); + assertThat(newLib.projectUuid()).isEqualTo(newLib.uuid()); + assertThat(newLib.moduleUuid()).isNull(); + assertThat(newLib.moduleUuidPath()).isEmpty(); + } + } + + @Test + public void shouldUpdateExistingResource() { + setupData("shouldUpdateExistingResource"); + + singleProject.setName("new name"); + singleProject.setDescription("new description"); + persister.persist(null, singleProject, null); + + checkTables("shouldUpdateExistingResource", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); + } + + // SONAR-1700 + @Test + public void shouldRemoveRootIndexIfResourceIsProject() { + setupData("shouldRemoveRootIndexIfResourceIsProject"); + + persister.persist(null, singleProject, null); + + checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); + } + + @Test + public void shouldGrantDefaultPermissionsIfNewProject() { + setupData("shared"); + + when(permissions.hasRoles(singleProject)).thenReturn(false); + + persister.persist(null, singleProject, null); + + verify(permissions).grantDefaultRoles(singleProject); + } + + @Test + public void shouldNotGrantDefaultPermissionsIfExistingProject() { + setupData("shared"); + + when(permissions.hasRoles(singleProject)).thenReturn(true); + + persister.persist(null, singleProject, null); + + verify(permissions, never()).grantDefaultRoles(singleProject); + } + + private static Project newProject(String key, String language) { + Settings settings = new Settings(); + settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, language); + return new Project(key).setSettings(settings).setAnalysisType(Project.AnalysisType.DYNAMIC); + } + + private static Project newCopyProject(String key, String language, int copyResourceId) { + Settings settings = new Settings(); + settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, language); + return new CopyProject(key, copyResourceId).setSettings(settings).setAnalysisType(Project.AnalysisType.DYNAMIC); + } + + private static class CopyProject extends Project implements ResourceCopy { + + private int copyResourceId; + + public CopyProject(String key, int copyResourceId) { + super(key); + this.copyResourceId = copyResourceId; + } + + public int getCopyResourceId() { + return copyResourceId; + } + + } + +} 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 40d6d14ce34..8f7289f2466 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 @@ -384,7 +384,7 @@ public class SourcePersisterTest extends AbstractDaoTestCase { private void mockResourceCache(String relativePathEmpty, String projectKey, String uuid) { File sonarFile = File.create(relativePathEmpty); sonarFile.setUuid(uuid); - when(resourceCache.get(projectKey + ":" + relativePathEmpty)).thenReturn(new BatchResource(1, sonarFile, new Snapshot(), null)); + when(resourceCache.get(projectKey + ":" + relativePathEmpty)).thenReturn(new BatchResource(1, sonarFile, null)); } private byte[] md5(String string) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedViolationsTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedViolationsTest.java index a94578a5769..b5c7862c740 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedViolationsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedViolationsTest.java @@ -20,7 +20,6 @@ package org.sonar.batch.issue; import org.junit.Test; -import org.sonar.api.database.model.Snapshot; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.resources.Project; import org.sonar.api.rule.RuleKey; @@ -50,7 +49,7 @@ public class DeprecatedViolationsTest { public void test_toViolation() throws Exception { RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles"); when(ruleFinder.findByKey(ruleKey)).thenReturn(new Rule("squid", "AvoidCycles")); - when(resourceCache.get("org.apache:struts")).thenReturn(new BatchResource(1, new Project("org.apache:struts"), new Snapshot(), null)); + when(resourceCache.get("org.apache:struts")).thenReturn(new BatchResource(1, new Project("org.apache:struts"), null)); DefaultIssue issue = newIssue(ruleKey); @@ -80,7 +79,7 @@ public class DeprecatedViolationsTest { public void test_get() throws Exception { RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles"); when(ruleFinder.findByKey(ruleKey)).thenReturn(new Rule("squid", "AvoidCycles")); - when(resourceCache.get("org.apache:struts")).thenReturn(new BatchResource(1, new Project("org.apache:struts"), new Snapshot(), null)); + when(resourceCache.get("org.apache:struts")).thenReturn(new BatchResource(1, new Project("org.apache:struts"), null)); when(issueCache.byComponent("org.apache:struts")).thenReturn(Arrays.asList(newIssue(ruleKey))); List violations = deprecatedViolations.get("org.apache:struts"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java index f323737ca37..f33205097d1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java @@ -95,7 +95,7 @@ public class QualityGateVerifierTest { project = new Project("foo"); resourceCache = new ResourceCache(); - resourceCache.add(project, null, snapshot); + resourceCache.add(project, null).setSnapshot(snapshot); verifier = new QualityGateVerifier(qualityGate, resourceCache, periods, i18n, durations); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java index 28932c22148..8c2722d11bc 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java @@ -28,7 +28,12 @@ import org.skyscreamer.jsonassert.JSONAssert; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.database.model.Snapshot; -import org.sonar.api.resources.*; +import org.sonar.api.resources.Directory; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Language; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.Scopes; import org.sonar.batch.index.ResourceCache; import java.io.File; @@ -47,27 +52,27 @@ public class ComponentsPublisherTest { Project root = new Project("foo").setName("Root project").setAnalysisDate(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/2012")); root.setId(1); - resourceCache.add(root, null, new Snapshot().setId(11)); + resourceCache.add(root, null).setSnapshot(new Snapshot().setId(11)); Project module1 = new Project("module1").setName("Module1"); module1.setParent(root); module1.setId(2); - resourceCache.add(module1, root, new Snapshot().setId(12)); + resourceCache.add(module1, root).setSnapshot(new Snapshot().setId(12)); Directory dir1 = Directory.create("src"); dir1.setEffectiveKey("foo:src"); dir1.setId(3); - resourceCache.add(dir1, module1, new Snapshot().setId(13)); + resourceCache.add(dir1, module1).setSnapshot(new Snapshot().setId(13)); org.sonar.api.resources.File mainFile = org.sonar.api.resources.File.create("src/Foo.java", "Foo.java", Java.INSTANCE, false); mainFile.setEffectiveKey("foo:src/Foo.java"); mainFile.setId(4); - resourceCache.add(mainFile, dir1, new Snapshot().setId(14)); + resourceCache.add(mainFile, dir1).setSnapshot(new Snapshot().setId(14)); Directory dir2 = Directory.create("test"); dir2.setEffectiveKey("foo:test"); dir2.setId(5); - resourceCache.add(dir2, module1, new Snapshot().setId(15)); + resourceCache.add(dir2, module1).setSnapshot(new Snapshot().setId(15)); org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", "FooTest.java", Java.INSTANCE, true); testFile.setEffectiveKey("foo:test/FooTest.java"); testFile.setId(6); - resourceCache.add(testFile, dir2, new Snapshot().setId(16)); + resourceCache.add(testFile, dir2).setSnapshot(new Snapshot().setId(16)); File exportDir = temp.newFolder(); publisher.export(exportDir); @@ -87,12 +92,12 @@ public class ComponentsPublisherTest { View view = new View("ALL_PROJECT"); view.setId(1); view.setAnalysisDate(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/2012")); - resourceCache.add(view, null, new Snapshot().setId(11)); + resourceCache.add(view, null).setSnapshot(new Snapshot().setId(11)); org.sonar.api.resources.File mainFile = org.sonar.api.resources.File.create("ALL_PROJECTsample", "ALL_PROJECTsample", null, false); mainFile.setEffectiveKey("ALL_PROJECTsample"); mainFile.setId(2); - resourceCache.add(mainFile, view, new Snapshot().setId(12)); + resourceCache.add(mainFile, view).setSnapshot(new Snapshot().setId(12)); File exportDir = temp.newFolder(); publisher.export(exportDir); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java index 86fa1d726b5..6ae7fef716a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java @@ -59,7 +59,7 @@ public class PreviousFileHashLoaderTest { @Before public void prepare() { resourceCache = new ResourceCache(); - resourceCache.add(project, null, snapshot); + resourceCache.add(project, null).setSnapshot(snapshot); loader = new PreviousFileHashLoader(project, resourceCache, snapshotDataDao, pastSnapshotFinder); } diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml deleted file mode 100644 index e031ae4ef66..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml deleted file mode 100644 index 283baaf3dde..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml deleted file mode 100644 index 81d3d953079..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveCopyProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveCopyProject-result.xml deleted file mode 100644 index 833ebcd5438..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveCopyProject-result.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml deleted file mode 100644 index 4ac03aed376..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml deleted file mode 100644 index 1f2478a3fc3..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml deleted file mode 100644 index f25ce3cbece..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml deleted file mode 100644 index c16c139570f..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewProject-result.xml deleted file mode 100644 index 7b0e006507a..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewProject-result.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource-result.xml deleted file mode 100644 index d04674f42b8..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource-result.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource.xml deleted file mode 100644 index 674397123dd..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shared.xml new file mode 100644 index 00000000000..e031ae4ef66 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shared.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml new file mode 100644 index 00000000000..283baaf3dde --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject-result.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml new file mode 100644 index 00000000000..81d3d953079 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldRemoveRootIndexIfResourceIsProject.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveCopyProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveCopyProject-result.xml new file mode 100644 index 00000000000..833ebcd5438 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveCopyProject-result.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewDirectory-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewDirectory-result.xml new file mode 100644 index 00000000000..4ac03aed376 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewDirectory-result.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewLibrary-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewLibrary-result.xml new file mode 100644 index 00000000000..1f2478a3fc3 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewLibrary-result.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml new file mode 100644 index 00000000000..f25ce3cbece --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml new file mode 100644 index 00000000000..c16c139570f --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewProject-result.xml new file mode 100644 index 00000000000..7b0e006507a --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewProject-result.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource-result.xml new file mode 100644 index 00000000000..d04674f42b8 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource-result.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource.xml new file mode 100644 index 00000000000..674397123dd --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource.xml @@ -0,0 +1,11 @@ + + + + + + + -- cgit v1.2.3