aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-07-12 10:06:55 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-07-12 10:09:36 +0200
commit238334f4b4a9cbf9b4c1384e185082937548c058 (patch)
treea5b05cddba8b390d8d02a0a0169cd5626e446245 /sonar-core
parent7e59c12b5c8b65e0c44e3f70b38f01eae5cade15 (diff)
downloadsonarqube-238334f4b4a9cbf9b4c1384e185082937548c058.tar.gz
sonarqube-238334f4b4a9cbf9b4c1384e185082937548c058.zip
Add ResourceDao#getResource(ResourceQuery query)
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java14
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java7
2 files changed, 18 insertions, 3 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
index 260b4abcb7e..58763fb621e 100644
--- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
@@ -41,6 +41,18 @@ public class ResourceDao {
}
}
+ /**
+ * Return a single result or null. If the request returns multiple rows, then
+ * the first row is returned.
+ */
+ public ResourceDto getResource(ResourceQuery query) {
+ List<ResourceDto> resources = getResources(query);
+ if (!resources.isEmpty()) {
+ return resources.get(0);
+ }
+ return null;
+ }
+
public List<Long> getResourceIds(ResourceQuery query) {
SqlSession session = mybatis.openSession();
try {
@@ -92,7 +104,7 @@ public class ResourceDao {
ResourceMapper mapper = session.getMapper(ResourceMapper.class);
try {
for (ResourceDto resource : resources) {
- if (resource.getId()==null) {
+ if (resource.getId() == null) {
mapper.insert(resource);
} else {
mapper.update(resource);
diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
index 9899b31cd2f..f33b4f30a9a 100644
--- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
@@ -57,7 +57,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
}
@Test
- public void getResource() {
+ public void get_resource_by_id() {
setupData("fixture");
ResourceDto resource = dao.getResource(1L);
@@ -104,9 +104,12 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
public void getResources_filter_by_key() {
setupData("fixture");
- List<ResourceDto> resources = dao.getResources(ResourceQuery.create().setKey("org.struts:struts-core"));
+ ResourceQuery query = ResourceQuery.create().setKey("org.struts:struts-core");
+ List<ResourceDto> resources = dao.getResources(query);
assertThat(resources).hasSize(1);
assertThat(resources.get(0).getKey()).isEqualTo("org.struts:struts-core");
+
+ assertThat(dao.getResource(query).getKey()).isEqualTo("org.struts:struts-core");
}
@Test