From cbf9a517c208b45b1b6b719436fa08ef217e5196 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 28 Jun 2012 15:15:44 +0200 Subject: [PATCH] Resource Snapshot Mapper --- .../sonar/core/dependency/DependencyDto.java | 50 ++++------------ .../core/dependency/DependencyMapper.java | 4 -- .../core/dependency/ResourceSnapshotDto.java | 53 +++++++++++++++++ .../dependency/ResourceSnapshotMapper.java | 26 ++++++++ .../org/sonar/core/persistence/MyBatis.java | 4 ++ .../core/dependency/DependencyMapper.xml | 5 +- .../dependency/ResourceSnapshotMapper.xml | 10 ++++ .../core/dependency/DependencyMapperTest.java | 33 ++--------- .../ResourceSnapshotMapperTest.java | 59 +++++++++++++++++++ .../DependencyMapperTest/fixture.xml | 4 -- .../ResourceSnapshotMapperTest/fixture.xml | 4 ++ 11 files changed, 171 insertions(+), 81 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotDto.java create mode 100644 sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotMapper.java create mode 100644 sonar-core/src/main/resources/org/sonar/core/dependency/ResourceSnapshotMapper.xml create mode 100644 sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java create mode 100644 sonar-core/src/test/resources/org/sonar/core/dependency/ResourceSnapshotMapperTest/fixture.xml diff --git a/sonar-core/src/main/java/org/sonar/core/dependency/DependencyDto.java b/sonar-core/src/main/java/org/sonar/core/dependency/DependencyDto.java index 00599da2344..40d7e12cc02 100644 --- a/sonar-core/src/main/java/org/sonar/core/dependency/DependencyDto.java +++ b/sonar-core/src/main/java/org/sonar/core/dependency/DependencyDto.java @@ -21,12 +21,9 @@ package org.sonar.core.dependency; public final class DependencyDto { private Long id; - private Long fromResourceId; - private String fromVersion; - private Long toResourceId; - private String toVersion; + private Long fromSnapshotId; + private Long toSnapshotId; private String usage; - private Integer weight; public Long getId() { return id; @@ -37,39 +34,21 @@ public final class DependencyDto { return this; } - public Long getFromResourceId() { - return fromResourceId; + public Long getFromSnapshotId() { + return fromSnapshotId; } - public DependencyDto setFromResourceId(Long fromResourceId) { - this.fromResourceId = fromResourceId; + public DependencyDto setFromSnapshotId(Long fromSnapshotId) { + this.fromSnapshotId = fromSnapshotId; return this; } - public String getFromVersion() { - return fromVersion; + public Long getToSnapshotId() { + return toSnapshotId; } - public DependencyDto setFromVersion(String fromVersion) { - this.fromVersion = fromVersion; - return this; - } - - public Long getToResourceId() { - return toResourceId; - } - - public DependencyDto setToResourceId(Long toResourceId) { - this.toResourceId = toResourceId; - return this; - } - - public String getToVersion() { - return toVersion; - } - - public DependencyDto setToVersion(String toVersion) { - this.toVersion = toVersion; + public DependencyDto setToSnapshotId(Long toSnapshotId) { + this.toSnapshotId = toSnapshotId; return this; } @@ -81,13 +60,4 @@ public final class DependencyDto { this.usage = usage; return this; } - - public Integer getWeight() { - return weight; - } - - public DependencyDto setWeight(Integer weight) { - this.weight = weight; - return this; - } } diff --git a/sonar-core/src/main/java/org/sonar/core/dependency/DependencyMapper.java b/sonar-core/src/main/java/org/sonar/core/dependency/DependencyMapper.java index e2f2d450ad9..e42257b8ee1 100644 --- a/sonar-core/src/main/java/org/sonar/core/dependency/DependencyMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/dependency/DependencyMapper.java @@ -21,10 +21,6 @@ package org.sonar.core.dependency; import org.apache.ibatis.session.ResultHandler; -import java.util.List; - public interface DependencyMapper { - List selectAll(); - void selectAll(ResultHandler handler); } diff --git a/sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotDto.java b/sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotDto.java new file mode 100644 index 00000000000..10d07da572e --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotDto.java @@ -0,0 +1,53 @@ +/* + * 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.core.dependency; + +public final class ResourceSnapshotDto { + private Long id; + private Long projectId; + private String version; + + public Long getId() { + return id; + } + + public ResourceSnapshotDto setId(Long id) { + this.id = id; + return this; + } + + public Long getProjectId() { + return projectId; + } + + public ResourceSnapshotDto setProjectId(Long projectId) { + this.projectId = projectId; + return this; + } + + public String getVersion() { + return version; + } + + public ResourceSnapshotDto setVersion(String version) { + this.version = version; + return this; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotMapper.java b/sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotMapper.java new file mode 100644 index 00000000000..343fb771d3c --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/dependency/ResourceSnapshotMapper.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.core.dependency; + +import org.apache.ibatis.session.ResultHandler; + +public interface ResourceSnapshotMapper { + void selectAll(ResultHandler handler); +} diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java index 501efd8ed36..6f1d450e016 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java @@ -43,6 +43,8 @@ import org.sonar.core.dashboard.WidgetPropertyDto; import org.sonar.core.dashboard.WidgetPropertyMapper; import org.sonar.core.dependency.DependencyDto; import org.sonar.core.dependency.DependencyMapper; +import org.sonar.core.dependency.ResourceSnapshotDto; +import org.sonar.core.dependency.ResourceSnapshotMapper; import org.sonar.core.duplication.DuplicationMapper; import org.sonar.core.duplication.DuplicationUnitDto; import org.sonar.core.filter.CriterionDto; @@ -99,6 +101,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadAlias(conf, "FilterColumn", FilterColumnDto.class); loadAlias(conf, "Dashboard", DashboardDto.class); loadAlias(conf, "Dependency", DependencyDto.class); + loadAlias(conf, "ResourceSnapshot", ResourceSnapshotDto.class); loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class); loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class); loadAlias(conf, "Property", PropertyDto.class); @@ -120,6 +123,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadMapper(conf, FilterColumnMapper.class); loadMapper(conf, DashboardMapper.class); loadMapper(conf, DependencyMapper.class); + loadMapper(conf, ResourceSnapshotMapper.class); loadMapper(conf, DuplicationMapper.class); loadMapper(conf, LoadedTemplateMapper.class); loadMapper(conf, PropertiesMapper.class); diff --git a/sonar-core/src/main/resources/org/sonar/core/dependency/DependencyMapper.xml b/sonar-core/src/main/resources/org/sonar/core/dependency/DependencyMapper.xml index b4bad32087a..b12dbb37535 100644 --- a/sonar-core/src/main/resources/org/sonar/core/dependency/DependencyMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/dependency/DependencyMapper.xml @@ -4,10 +4,7 @@ diff --git a/sonar-core/src/main/resources/org/sonar/core/dependency/ResourceSnapshotMapper.xml b/sonar-core/src/main/resources/org/sonar/core/dependency/ResourceSnapshotMapper.xml new file mode 100644 index 00000000000..7fdb3e7f36c --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/dependency/ResourceSnapshotMapper.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java b/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java index 45f408e7824..d236ff29ce1 100644 --- a/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java +++ b/sonar-core/src/test/java/org/sonar/core/dependency/DependencyMapperTest.java @@ -32,31 +32,8 @@ import java.util.List; import static org.fest.assertions.Assertions.assertThat; public class DependencyMapperTest extends DaoTestCase { - @Test - public void testDescendantProjects_do_not_include_self() { - setupData("fixture"); - - SqlSession session = getMyBatis().openSession(); - try { - List dependencies = session.getMapper(DependencyMapper.class).selectAll(); - - assertThat(dependencies).hasSize(2); - - DependencyDto dep = dependencies.get(0); - assertThat(dep.getUsage()).isEqualTo("compile"); - assertThat(dep.getFromResourceId()).isEqualTo(100L); - assertThat(dep.getFromVersion()).isEqualTo("1.0"); - assertThat(dep.getToResourceId()).isEqualTo(101L); - assertThat(dep.getToVersion()).isEqualTo("3.0"); - assertThat(dep.getId()).isEqualTo(1L); - } finally { - MyBatis.closeQuietly(session); - } - } - - @Test - public void should_use_result_handler() { + public void should_find_all() { setupData("fixture"); final List dependencies = Lists.newArrayList(); @@ -75,11 +52,9 @@ public class DependencyMapperTest extends DaoTestCase { assertThat(dependencies).hasSize(2); DependencyDto dep = dependencies.get(0); - assertThat(dep.getUsage()).isEqualTo("compile"); - assertThat(dep.getFromResourceId()).isEqualTo(100L); - assertThat(dep.getFromVersion()).isEqualTo("1.0"); - assertThat(dep.getToResourceId()).isEqualTo(101L); - assertThat(dep.getToVersion()).isEqualTo("3.0"); assertThat(dep.getId()).isEqualTo(1L); + assertThat(dep.getFromSnapshotId()).isEqualTo(1000L); + assertThat(dep.getToSnapshotId()).isEqualTo(1001L); + assertThat(dep.getUsage()).isEqualTo("compile"); } } diff --git a/sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java b/sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java new file mode 100644 index 00000000000..76ddf641b04 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/dependency/ResourceSnapshotMapperTest.java @@ -0,0 +1,59 @@ +/* + * 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.core.dependency; + +import com.google.common.collect.Lists; +import org.apache.ibatis.session.ResultContext; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.SqlSession; +import org.junit.Test; +import org.sonar.core.persistence.DaoTestCase; +import org.sonar.core.persistence.MyBatis; + +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; + +public class ResourceSnapshotMapperTest extends DaoTestCase { + @Test + public void should_find_all() { + setupData("fixture"); + + final List snapshots = Lists.newArrayList(); + + SqlSession session = getMyBatis().openSession(); + try { + session.getMapper(ResourceSnapshotMapper.class).selectAll(new ResultHandler() { + public void handleResult(ResultContext context) { + snapshots.add((ResourceSnapshotDto) context.getResultObject()); + } + }); + } finally { + MyBatis.closeQuietly(session); + } + + assertThat(snapshots).hasSize(2); + + ResourceSnapshotDto dep = snapshots.get(0); + assertThat(dep.getId()).isEqualTo(1L); + assertThat(dep.getProjectId()).isEqualTo(1000L); + assertThat(dep.getVersion()).isEqualTo("1.0"); + } +} diff --git a/sonar-core/src/test/resources/org/sonar/core/dependency/DependencyMapperTest/fixture.xml b/sonar-core/src/test/resources/org/sonar/core/dependency/DependencyMapperTest/fixture.xml index b6cc818679b..7e8a8e9f677 100644 --- a/sonar-core/src/test/resources/org/sonar/core/dependency/DependencyMapperTest/fixture.xml +++ b/sonar-core/src/test/resources/org/sonar/core/dependency/DependencyMapperTest/fixture.xml @@ -1,8 +1,4 @@ - - - - \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/dependency/ResourceSnapshotMapperTest/fixture.xml b/sonar-core/src/test/resources/org/sonar/core/dependency/ResourceSnapshotMapperTest/fixture.xml new file mode 100644 index 00000000000..9b9fa7d4942 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/dependency/ResourceSnapshotMapperTest/fixture.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file -- 2.39.5