diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-03-19 16:37:10 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-03-19 16:41:35 +0100 |
commit | a7afaede85e8d53089125a8dfeea7a0d628ec7a3 (patch) | |
tree | 15fed33a0b8b36df5969d2dd7c757a4ade7c30d1 /sonar-batch/src | |
parent | 81bf694d6a0223c58a45100be3a93c3e3155a734 (diff) | |
download | sonarqube-a7afaede85e8d53089125a8dfeea7a0d628ec7a3.tar.gz sonarqube-a7afaede85e8d53089125a8dfeea7a0d628ec7a3.zip |
SONAR-4069 Allow to persist a project with a copy_resource_id
Diffstat (limited to 'sonar-batch/src')
4 files changed, 95 insertions, 11 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index 93b48113c92..2391510155c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -76,6 +76,11 @@ public final class DefaultResourcePersister implements ResourcePersister { ResourceModel model = findOrCreateModel(project); model.setLanguageKey(project.getLanguageKey());// ugly, only for projects + // For views + if (project instanceof ResourceCopy) { + model.setCopyResourceId(((ResourceCopy) project).getCopyResourceId()); + } + Snapshot parentSnapshot = null; if (parent != null) { // assume that the parent project has already been saved @@ -120,7 +125,6 @@ public final class DefaultResourcePersister implements ResourcePersister { return snapshotsByResource; } - public Snapshot saveResource(Project project, Resource resource) { return saveResource(project, resource, null); } @@ -134,7 +138,6 @@ public final class DefaultResourcePersister implements ResourcePersister { return snapshot; } - private Snapshot persist(Project project, Resource resource, Resource parent) { Snapshot snapshot; if (resource instanceof Project) { @@ -151,7 +154,6 @@ public final class DefaultResourcePersister implements ResourcePersister { return snapshot; } - private Snapshot persistLibrary(Project project, Library library) { ResourceModel model = findOrCreateModel(library); model = session.save(model); @@ -224,7 +226,7 @@ public final class DefaultResourcePersister implements ResourcePersister { public void clear() { // we keep cache of projects - for (Iterator<Map.Entry<Resource, Snapshot>> it = snapshotsByResource.entrySet().iterator(); it.hasNext(); ) { + for (Iterator<Map.Entry<Resource, Snapshot>> it = snapshotsByResource.entrySet().iterator(); it.hasNext();) { Map.Entry<Resource, Snapshot> entry = it.next(); if (!ResourceUtils.isSet(entry.getKey())) { it.remove(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceCopy.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceCopy.java new file mode 100644 index 00000000000..2129ac098cc --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceCopy.java @@ -0,0 +1,26 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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; + +public interface ResourceCopy { + + int getCopyResourceId(); + +} 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 a9ea3b30b51..534a1496629 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 @@ -44,7 +44,7 @@ import static org.mockito.Mockito.when; public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { - private Project singleProject, multiModuleProject, moduleA, moduleB, moduleB1; + private Project singleProject, singleCopyProject, multiModuleProject, moduleA, moduleB, moduleB1; @Before public void before() throws ParseException { @@ -52,6 +52,9 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { singleProject = newProject("foo", "java"); singleProject.setName("Foo").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")); @@ -75,7 +78,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); persister.saveProject(singleProject, null); - checkTables("shouldSaveNewProject", new String[]{"build_date", "created_at"}, "projects", "snapshots"); + checkTables("shouldSaveNewProject", new String[] {"build_date", "created_at"}, "projects", "snapshots"); // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table ResourceModel model = getSession().getSingleResult(ResourceModel.class, "key", singleProject.getKey()); @@ -83,6 +86,16 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { } @Test + public void shouldSaveCopyProject() { + setupData("shared"); + + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + persister.saveProject(singleCopyProject, null); + + checkTables("shouldSaveCopyProject", new String[] {"build_date", "created_at"}, "projects", "snapshots"); + } + + @Test public void shouldSaveNewMultiModulesProject() { setupData("shared"); @@ -92,7 +105,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveProject(moduleB, multiModuleProject); persister.saveProject(moduleB1, moduleB); - checkTables("shouldSaveNewMultiModulesProject", new String[]{"build_date", "created_at"}, "projects", "snapshots"); + checkTables("shouldSaveNewMultiModulesProject", new String[] {"build_date", "created_at"}, "projects", "snapshots"); } @Test @@ -104,7 +117,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveResource(singleProject, new JavaPackage("org.foo").setEffectiveKey("foo:org.foo")); // check that the directory is attached to the project - checkTables("shouldSaveNewDirectory", new String[]{"build_date", "created_at"}, "projects", "snapshots"); + checkTables("shouldSaveNewDirectory", new String[] {"build_date", "created_at"}, "projects", "snapshots"); } @Test @@ -117,7 +130,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { 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"}, "projects", "snapshots"); + checkTables("shouldSaveNewLibrary", new String[] {"build_date", "created_at"}, "projects", "snapshots"); } @Test @@ -145,7 +158,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { singleProject.setDescription("new description"); persister.saveProject(singleProject, null); - checkTables("shouldUpdateExistingResource", new String[]{"build_date", "created_at"}, "projects", "snapshots"); + checkTables("shouldUpdateExistingResource", new String[] {"build_date", "created_at"}, "projects", "snapshots"); } // SONAR-1700 @@ -156,7 +169,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); persister.saveProject(singleProject, null); - checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[]{"build_date", "created_at"}, "projects", "snapshots"); + checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date", "created_at"}, "projects", "snapshots"); } @Test @@ -191,4 +204,25 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { return new Project(key).setConfiguration(configuration).setAnalysisType(Project.AnalysisType.DYNAMIC); } + private static Project newCopyProject(String key, String language, int copyResourceId) { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.setProperty("sonar.language", language); + return new CopyProject(key, copyResourceId).setConfiguration(configuration).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/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveCopyProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveCopyProject-result.xml new file mode 100644 index 00000000000..e5169508831 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveCopyProject-result.xml @@ -0,0 +1,22 @@ +<dataset> + + <!-- other project --> + <projects id="1000" scope="PRJ" qualifier="TRK" kee="my:key" root_id="[null]" + name="Other project" long_name="Other" description="[null]" + enabled="true" language="java" copy_resource_id="[null]" person_id="[null]" /> + + <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="3000" project_id="1000" parent_snapshot_id="[null]" root_project_id="1000" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-11-01 13:58:00.00" build_date="2008-11-01 13:58:00.00" version="[null]" path="" + status="P" islast="false" depth="0" /> + + + <!-- new project --> + <projects id="1001" scope="PRJ" qualifier="TRK" kee="foo" root_id="[null]" + name="Foo" long_name="Foo" description="some description" + enabled="true" language="java" copy_resource_id="10" person_id="[null]" /> + + <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="3001" project_id="1001" parent_snapshot_id="[null]" root_project_id="1001" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2010-12-25 00:00:00.00" build_date="2010-12-25 00:00:00.00" version="[null]" path="" + status="U" islast="false" depth="0" /> + +</dataset> |