aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-03-11 18:55:54 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-03-12 11:29:00 +0100
commit48ac6cdb612920154aa4bca46fb4f626173adcbc (patch)
tree7766ac2186a94a47c1913473607c1e26bb37527f /sonar-core
parent440894cb0033f0492be159bdd21426c27675085a (diff)
downloadsonarqube-48ac6cdb612920154aa4bca46fb4f626173adcbc.tar.gz
sonarqube-48ac6cdb612920154aa4bca46fb4f626173adcbc.zip
Complete the MyBatis DAO for PROPERTIES
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java25
-rw-r--r--sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java73
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml12
-rw-r--r--sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java47
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectGlobalProperties.xml14
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml19
8 files changed, 185 insertions, 9 deletions
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 68a0b83adb1..a82ef62da35 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
@@ -33,6 +33,7 @@ import org.sonar.core.dashboard.*;
import org.sonar.core.duplication.DuplicationMapper;
import org.sonar.core.duplication.DuplicationUnitDto;
import org.sonar.core.properties.PropertiesMapper;
+import org.sonar.core.properties.PropertyDto;
import org.sonar.core.purge.PurgeMapper;
import org.sonar.core.purge.PurgeVendorMapper;
import org.sonar.core.purge.PurgeableSnapshotDto;
@@ -69,6 +70,7 @@ public class MyBatis implements BatchComponent, ServerComponent {
loadAlias(conf, "Dashboard", DashboardDto.class);
loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class);
loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class);
+ loadAlias(conf, "Property", PropertyDto.class);
loadAlias(conf, "PurgeableSnapshot", PurgeableSnapshotDto.class);
loadAlias(conf, "Review", ReviewDto.class);
loadAlias(conf, "Resource", ResourceDto.class);
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
index 3762d35e529..78392eeb473 100644
--- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
@@ -19,13 +19,13 @@
*/
package org.sonar.core.properties;
-import java.util.List;
-
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;
import org.sonar.core.persistence.MyBatis;
+import java.util.List;
+
public class PropertiesDao implements BatchComponent, ServerComponent {
private MyBatis mybatis;
@@ -36,7 +36,7 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
/**
* Returns the logins of users who have flagged as favourite the resource identified by the given id.
- *
+ *
* @param resourceId the resource id
* @return the list of logins (maybe be empty - obviously)
*/
@@ -50,4 +50,23 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
}
}
+ public List<PropertyDto> selectGlobalProperties() {
+ SqlSession session = mybatis.openSession();
+ PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+ try {
+ return mapper.selectGlobalProperties();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public List<PropertyDto> selectProjectProperties(String resourceKey) {
+ SqlSession session = mybatis.openSession();
+ PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+ try {
+ return mapper.selectProjectProperties(resourceKey);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
}
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
index 4862efb0382..be3e79db7b0 100644
--- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
@@ -26,5 +26,7 @@ import org.apache.ibatis.annotations.Param;
public interface PropertiesMapper {
List<String> findUserIdsForFavouriteResource(@Param("resource_id") Integer resourceId);
+ List<PropertyDto> selectGlobalProperties();
+ List<PropertyDto> selectProjectProperties(String resourceKey);
}
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java
new file mode 100644
index 00000000000..9623f5f686e
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java
@@ -0,0 +1,73 @@
+/*
+ * 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.properties;
+
+public final class PropertyDto {
+ private Integer id;
+ private String key;
+ private String value;
+ private Integer resourceId;
+ private Integer userId;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public PropertyDto setId(Integer id) {
+ this.id = id;
+ return this;
+ }
+
+ public String getKey() {
+ return key;
+ }
+
+ public PropertyDto setKey(String key) {
+ this.key = key;
+ return this;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public PropertyDto setValue(String value) {
+ this.value = value;
+ return this;
+ }
+
+ public Integer getResourceId() {
+ return resourceId;
+ }
+
+ public PropertyDto setResourceId(Integer resourceId) {
+ this.resourceId = resourceId;
+ return this;
+ }
+
+ public Integer getUserId() {
+ return userId;
+ }
+
+ public PropertyDto setUserId(Integer userId) {
+ this.userId = userId;
+ return this;
+ }
+}
diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
index 21dc926f564..1878b5f31af 100644
--- a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
@@ -9,4 +9,16 @@
WHERE P.prop_key = 'favourite' AND P.resource_id = #{resource_id} AND P.user_id = U.id
</select>
+ <select id="selectGlobalProperties" resultType="Property">
+ select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+ from properties p
+ where p.resource_id is null and p.user_id is null
+ </select>
+
+ <select id="selectProjectProperties" parameterType="String" resultType="Property">
+ select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+ from properties p, projects r
+ where p.resource_id=r.id and p.user_id is null and r.kee=#{id}
+ </select>
+
</mapper>
diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
index 590c24404d0..074e4f36a96 100644
--- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
@@ -19,16 +19,16 @@
*/
package org.sonar.core.properties;
-import static org.hamcrest.Matchers.hasItems;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-import java.util.List;
-
import org.junit.Before;
import org.junit.Test;
import org.sonar.core.persistence.DaoTestCase;
+import java.util.List;
+
+import static org.hamcrest.Matchers.hasItems;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
public class PropertiesDaoTest extends DaoTestCase {
private PropertiesDao dao;
@@ -45,4 +45,39 @@ public class PropertiesDaoTest extends DaoTestCase {
assertThat(userIds.size(), is(2));
assertThat(userIds, hasItems("user3", "user4"));
}
+
+ @Test
+ public void selectGlobalProperties() throws Exception {
+ setupData("selectGlobalProperties");
+ List<PropertyDto> properties = dao.selectGlobalProperties();
+ assertThat(properties.size(), is(2));
+
+ PropertyDto first = findById(properties, 1);
+ assertThat(first.getKey(), is("global.one"));
+ assertThat(first.getValue(), is("one"));
+
+ PropertyDto second = findById(properties, 2);
+ assertThat(second.getKey(), is("global.two"));
+ assertThat(second.getValue(), is("two"));
+ }
+
+ @Test
+ public void selectProjectProperties() throws Exception {
+ setupData("selectProjectProperties");
+ List<PropertyDto> properties = dao.selectProjectProperties("org.struts:struts");
+ assertThat(properties.size(), is(1));
+
+ PropertyDto first = properties.get(0);
+ assertThat(first.getKey(), is("struts.one"));
+ assertThat(first.getValue(), is("one"));
+ }
+
+ private PropertyDto findById(List<PropertyDto> properties, int id) {
+ for (PropertyDto property : properties) {
+ if (property.getId() == id) {
+ return property;
+ }
+ }
+ return null;
+ }
}
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectGlobalProperties.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectGlobalProperties.xml
new file mode 100644
index 00000000000..e5aa737e9a0
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectGlobalProperties.xml
@@ -0,0 +1,14 @@
+<dataset>
+
+ <!-- global -->
+ <properties id="1" prop_key="global.one" text_value="one" resource_id="[null]" user_id="[null]"/>
+ <properties id="2" prop_key="global.two" text_value="two" resource_id="[null]" user_id="[null]"/>
+
+ <!-- project -->
+ <properties id="3" prop_key="project.one" text_value="one" resource_id="10" user_id="[null]"/>
+
+ <!-- user -->
+ <properties id="4" prop_key="user.one" text_value="one" resource_id="[null]" user_id="100"/>
+ <properties id="5" prop_key="user.two" text_value="two" resource_id="10" user_id="100"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml
new file mode 100644
index 00000000000..a6ea934fdc3
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/selectProjectProperties.xml
@@ -0,0 +1,19 @@
+<dataset>
+
+ <!-- global -->
+ <properties id="1" prop_key="global.one" text_value="one" resource_id="[null]" user_id="[null]"/>
+ <properties id="2" prop_key="global.two" text_value="two" resource_id="[null]" user_id="[null]"/>
+
+ <!-- struts -->
+ <properties id="3" prop_key="struts.one" text_value="one" resource_id="10" user_id="[null]"/>
+
+ <!-- struts -->
+ <properties id="4" prop_key="commonslang.one" text_value="one" resource_id="11" user_id="[null]"/>
+
+ <!-- user -->
+ <properties id="5" prop_key="user.one" text_value="one" resource_id="[null]" user_id="100"/>
+ <properties id="6" prop_key="user.two" text_value="two" resource_id="10" user_id="100"/>
+
+ <projects id="10" kee="org.struts:struts"/>
+ <projects id="11" kee="org.apache:commons-lang"/>
+</dataset>