diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-24 10:32:34 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-24 11:04:32 +0200 |
commit | 40f40f2432bcf0874f264d605ef210d05c89bc74 (patch) | |
tree | 2d5dcae2e13a91c4ed02a4d249f818b719bdd4f7 /sonar-db | |
parent | 85272d930b2bfdf1fc8e1be42bc27378be880b17 (diff) | |
download | sonarqube-40f40f2432bcf0874f264d605ef210d05c89bc74.tar.gz sonarqube-40f40f2432bcf0874f264d605ef210d05c89bc74.zip |
Improve some DAOs with conventions and DatabaseUtils.executeLargeInputs()
Diffstat (limited to 'sonar-db')
20 files changed, 256 insertions, 59 deletions
diff --git a/sonar-db/src/main/java/org/sonar/core/user/DefaultUserFinder.java b/sonar-db/src/main/java/org/sonar/core/user/DefaultUserFinder.java index 16d2867c6c0..428d7b27918 100644 --- a/sonar-db/src/main/java/org/sonar/core/user/DefaultUserFinder.java +++ b/sonar-db/src/main/java/org/sonar/core/user/DefaultUserFinder.java @@ -49,7 +49,7 @@ public class DefaultUserFinder implements UserFinder { @Override public List<User> findByLogins(List<String> logins) { - List<UserDto> dtos = userDao.selectUsersByLogins(logins); + List<UserDto> dtos = userDao.selectByLogins(logins); return toUsers(dtos); } diff --git a/sonar-db/src/main/java/org/sonar/db/DaoModule.java b/sonar-db/src/main/java/org/sonar/db/DaoModule.java index 6300c253adf..dde96831544 100644 --- a/sonar-db/src/main/java/org/sonar/db/DaoModule.java +++ b/sonar-db/src/main/java/org/sonar/db/DaoModule.java @@ -58,6 +58,7 @@ import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualityprofile.QualityProfileDao; +import org.sonar.db.rule.RuleDao; import org.sonar.db.source.FileSourceDao; import org.sonar.db.user.AuthorDao; import org.sonar.db.user.AuthorizationDao; @@ -102,6 +103,7 @@ public class DaoModule extends Module { ProjectQgateAssociationDao.class, QualityProfileDao.class, PurgeDao.class, + RuleDao.class, CharacteristicDao.class, ResourceIndexDao.class, ResourceDao.class, diff --git a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java index a42de7dcafd..df9fbc98739 100644 --- a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java +++ b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java @@ -26,6 +26,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -84,7 +85,7 @@ public class DatabaseUtils { if (input.isEmpty()) { return Collections.emptyList(); } - List<OUTPUT> results = newArrayList(); + List<OUTPUT> results = new ArrayList<>(); List<List<INPUT>> partitionList = Lists.partition(newArrayList(input), PARTITION_SIZE_FOR_ORACLE); for (List<INPUT> partition : partitionList) { List<OUTPUT> subResults = function.apply(partition); diff --git a/sonar-db/src/main/java/org/sonar/db/DbClient.java b/sonar-db/src/main/java/org/sonar/db/DbClient.java index 6ff2e99fdd6..f6471e7388e 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -57,6 +57,7 @@ import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualityprofile.QualityProfileDao; +import org.sonar.db.rule.RuleDao; import org.sonar.db.source.FileSourceDao; import org.sonar.db.user.AuthorDao; import org.sonar.db.user.AuthorizationDao; @@ -113,6 +114,7 @@ public class DbClient { private final CustomMeasureDao customMeasureDao; private final MetricDao metricDao; private final GroupDao groupDao; + private final RuleDao ruleDao; public DbClient(Database database, MyBatis myBatis, Dao... daos) { this.database = database; @@ -165,6 +167,7 @@ public class DbClient { customMeasureDao = getDao(map, CustomMeasureDao.class); metricDao = getDao(map, MetricDao.class); groupDao = getDao(map, GroupDao.class); + ruleDao = getDao(map, RuleDao.class); doOnLoad(map); } @@ -357,6 +360,10 @@ public class DbClient { return groupDao; } + public RuleDao ruleDao() { + return ruleDao; + } + protected <K extends Dao> K getDao(Map<Class, Dao> map, Class<K> clazz) { return (K) map.get(clazz); } diff --git a/sonar-db/src/main/java/org/sonar/db/issue/ActionPlanDao.java b/sonar-db/src/main/java/org/sonar/db/issue/ActionPlanDao.java index b925c34b6ae..fed8d421950 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/ActionPlanDao.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/ActionPlanDao.java @@ -20,16 +20,16 @@ package org.sonar.db.issue; -import com.google.common.collect.Lists; +import com.google.common.base.Function; import java.util.Collection; -import java.util.Collections; import java.util.List; +import javax.annotation.Nonnull; import org.apache.ibatis.session.SqlSession; import org.sonar.db.Dao; +import org.sonar.db.DatabaseUtils; +import org.sonar.db.DbSession; import org.sonar.db.MyBatis; -import static com.google.common.collect.Lists.newArrayList; - public class ActionPlanDao implements Dao { private final MyBatis mybatis; @@ -78,23 +78,31 @@ public class ActionPlanDao implements Dao { } public List<ActionPlanDto> selectByKeys(Collection<String> keys) { - if (keys.isEmpty()) { - return Collections.emptyList(); - } - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { - List<ActionPlanDto> dtosList = newArrayList(); - List<List<String>> keysPartition = Lists.partition(newArrayList(keys), 1000); - for (List<String> partition : keysPartition) { - List<ActionPlanDto> dtos = session.getMapper(ActionPlanMapper.class).findByKeys(partition); - dtosList.addAll(dtos); - } - return dtosList; + return selectByKeys(session, keys); } finally { MyBatis.closeQuietly(session); } } + public List<ActionPlanDto> selectByKeys(DbSession dbSession, Collection<String> keys) { + return DatabaseUtils.executeLargeInputs(keys, new SelectByKeys(dbSession.getMapper(ActionPlanMapper.class))); + } + + private static class SelectByKeys implements Function<List<String>, List<ActionPlanDto>> { + private final ActionPlanMapper mapper; + + private SelectByKeys(ActionPlanMapper mapper) { + this.mapper = mapper; + } + + @Override + public List<ActionPlanDto> apply(@Nonnull List<String> partitionOfKeys) { + return mapper.findByKeys(partitionOfKeys); + } + } + public List<ActionPlanDto> selectOpenByProjectId(Long projectId) { SqlSession session = mybatis.openSession(false); try { diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java index 076681365eb..7a3f7bc686d 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java @@ -20,19 +20,20 @@ package org.sonar.db.issue; +import com.google.common.base.Function; import com.google.common.collect.Lists; import java.util.Collection; -import java.util.Collections; import java.util.List; import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import org.sonar.api.server.ServerSide; import org.sonar.core.issue.DefaultIssueComment; import org.sonar.core.issue.FieldDiffs; import org.sonar.db.Dao; +import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; -import static com.google.common.collect.Lists.newArrayList; import static java.util.Arrays.asList; @ServerSide @@ -46,7 +47,7 @@ public class IssueChangeDao implements Dao { public List<DefaultIssueComment> selectCommentsByIssues(DbSession session, Collection<String> issueKeys) { List<DefaultIssueComment> comments = Lists.newArrayList(); - for (IssueChangeDto dto : selectByIssuesAndType(session, issueKeys, IssueChangeDto.TYPE_COMMENT)) { + for (IssueChangeDto dto : selectByTypeAndIssueKeys(session, issueKeys, IssueChangeDto.TYPE_COMMENT)) { comments.add(dto.toComment()); } return comments; @@ -56,7 +57,7 @@ public class IssueChangeDao implements Dao { DbSession session = mybatis.openSession(false); try { List<FieldDiffs> result = Lists.newArrayList(); - for (IssueChangeDto dto : selectByIssuesAndType(session, asList(issueKey), IssueChangeDto.TYPE_FIELD_CHANGE)) { + for (IssueChangeDto dto : selectByTypeAndIssueKeys(session, asList(issueKey), IssueChangeDto.TYPE_FIELD_CHANGE)) { result.add(dto.toFieldDiffs()); } return result; @@ -68,7 +69,7 @@ public class IssueChangeDao implements Dao { public List<IssueChangeDto> selectChangelogOfNonClosedIssuesByComponent(String componentUuid) { DbSession session = mybatis.openSession(false); try { - IssueChangeMapper mapper = session.getMapper(IssueChangeMapper.class); + IssueChangeMapper mapper = mapper(session); return mapper.selectChangelogOfNonClosedIssuesByComponent(componentUuid, IssueChangeDto.TYPE_FIELD_CHANGE); } finally { @@ -80,7 +81,7 @@ public class IssueChangeDao implements Dao { public DefaultIssueComment selectCommentByKey(String commentKey) { DbSession session = mybatis.openSession(false); try { - IssueChangeMapper mapper = session.getMapper(IssueChangeMapper.class); + IssueChangeMapper mapper = mapper(session); IssueChangeDto dto = mapper.selectByKeyAndType(commentKey, IssueChangeDto.TYPE_COMMENT); return dto != null ? dto.toComment() : null; @@ -89,28 +90,35 @@ public class IssueChangeDao implements Dao { } } - List<IssueChangeDto> selectByIssuesAndType(DbSession session, Collection<String> issueKeys, String changeType) { - if (issueKeys.isEmpty()) { - return Collections.emptyList(); + public List<IssueChangeDto> selectByTypeAndIssueKeys(DbSession session, Collection<String> issueKeys, String changeType) { + return DatabaseUtils.executeLargeInputs(issueKeys, new SelectByIssueKeys(mapper(session), changeType)); + } + + private static class SelectByIssueKeys implements Function<List<String>, List<IssueChangeDto>> { + + private final IssueChangeMapper mapper; + private final String changeType; + + private SelectByIssueKeys(IssueChangeMapper mapper, String changeType) { + this.mapper = mapper; + this.changeType = changeType; } - IssueChangeMapper mapper = session.getMapper(IssueChangeMapper.class); - List<IssueChangeDto> dtosList = newArrayList(); - List<List<String>> keysPartition = Lists.partition(newArrayList(issueKeys), 1000); - for (List<String> partition : keysPartition) { - List<IssueChangeDto> dtos = mapper.selectByIssuesAndType(partition, changeType); - dtosList.addAll(dtos); + + @Override + public List<IssueChangeDto> apply(@Nonnull List<String> issueKeys) { + return mapper.selectByIssuesAndType(issueKeys, changeType); } - return dtosList; + } public void insert(DbSession session, IssueChangeDto change) { - session.getMapper(IssueChangeMapper.class).insert(change); + mapper(session).insert(change); } public boolean delete(String key) { DbSession session = mybatis.openSession(false); try { - IssueChangeMapper mapper = session.getMapper(IssueChangeMapper.class); + IssueChangeMapper mapper = mapper(session); int count = mapper.delete(key); session.commit(); return count == 1; @@ -123,7 +131,7 @@ public class IssueChangeDao implements Dao { public boolean update(IssueChangeDto change) { DbSession session = mybatis.openSession(false); try { - IssueChangeMapper mapper = session.getMapper(IssueChangeMapper.class); + IssueChangeMapper mapper = mapper(session); int count = mapper.update(change); session.commit(); return count == 1; @@ -132,4 +140,8 @@ public class IssueChangeDao implements Dao { MyBatis.closeQuietly(session); } } + + private IssueChangeMapper mapper(DbSession session) { + return session.getMapper(IssueChangeMapper.class); + } } diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java index 42583413a09..36dbf668650 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java @@ -62,7 +62,7 @@ public class IssueDao implements Dao { return Optional.fromNullable(mapper(session).selectByKey(key)); } - public IssueDto selectByKeyOrFail(DbSession session, String key) { + public IssueDto selectOrFailByKey(DbSession session, String key) { Optional<IssueDto> issue = selectByKey(session, key); if (!issue.isPresent()) { throw new RowNotFoundException(String.format("Issue with key '%s' does not exist", key)); @@ -103,9 +103,9 @@ public class IssueDao implements Dao { * if input keys contain multiple occurrences of a key. * <p>Contrary to {@link #selectByKeys(DbSession, List)}, results are in the same order as input keys.</p> */ - public Iterable<IssueDto> selectByOrderedKeys(DbSession session, List<String> keys) { + public List<IssueDto> selectByOrderedKeys(DbSession session, List<String> keys) { List<IssueDto> unordered = selectByKeys(session, keys); - return from(keys).transform(new KeyToIssue(unordered)).filter(Predicates.notNull()); + return from(keys).transform(new KeyToIssue(unordered)).filter(Predicates.notNull()).toList(); } private static class KeyToIssue implements Function<String, IssueDto> { diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java index ede94b1f5e2..51518fb1dc9 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java @@ -341,6 +341,7 @@ public final class IssueDto implements Serializable { return this; } + @CheckForNull public String getAssignee() { return assignee; } @@ -350,6 +351,7 @@ public final class IssueDto implements Serializable { return this; } + @CheckForNull public String getAuthorLogin() { return authorLogin; } @@ -359,6 +361,7 @@ public final class IssueDto implements Serializable { return this; } + @CheckForNull public String getIssueAttributes() { return issueAttributes; } diff --git a/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java b/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java new file mode 100644 index 00000000000..b20552a3b62 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java @@ -0,0 +1,58 @@ +/* + * 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.db.rule; + +import com.google.common.base.Function; +import java.util.List; +import javax.annotation.Nonnull; +import org.sonar.api.rule.RuleKey; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + +public class RuleDao implements Dao { + + /** + * Select rules by keys, whatever their status. Returns an empty list + * if the list of {@code keys} is empty, without any db round trip. + */ + public List<RuleDto> selectByKeys(final DbSession session, List<RuleKey> keys) { + return executeLargeInputs(keys, new KeyToDto(mapper(session))); + } + + private RuleMapper mapper(DbSession session) { + return session.getMapper(RuleMapper.class); + } + + private static class KeyToDto implements Function<List<RuleKey>, List<RuleDto>> { + private final RuleMapper mapper; + + private KeyToDto(RuleMapper mapper) { + this.mapper = mapper; + } + + @Override + public List<RuleDto> apply(@Nonnull List<RuleKey> partitionOfKeys) { + return mapper.selectByKeys(partitionOfKeys); + } + } + +} diff --git a/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java b/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java index 5074160a11c..79a1eebfe42 100644 --- a/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/rule/RuleMapper.java @@ -39,6 +39,8 @@ public interface RuleMapper { RuleDto selectByKey(RuleKey ruleKey); + List<RuleDto> selectByKeys(@Param("ruleKeys") List<RuleKey> keys); + RuleDto selectByName(String name); void update(RuleDto rule); diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java index 108fa563892..5c1e52d5f8c 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java @@ -19,13 +19,16 @@ */ package org.sonar.db.user; -import com.google.common.collect.Lists; +import com.google.common.base.Function; +import java.util.Collection; import java.util.List; import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import org.apache.ibatis.session.SqlSession; import org.sonar.api.user.UserQuery; import org.sonar.api.utils.System2; import org.sonar.db.Dao; +import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.RowNotFoundException; @@ -74,27 +77,34 @@ public class UserDao implements Dao { return mapper.selectUserByLogin(login); } - public List<UserDto> selectUsersByLogins(List<String> logins) { - List<UserDto> users = Lists.newArrayList(); + /** + * Select users by logins, including disabled users. An empty list is returned + * if list of logins is empty, without any db round trips. + */ + public List<UserDto> selectByLogins(DbSession session, Collection<String> logins) { + return DatabaseUtils.executeLargeInputs(logins, new SelectByLogins(mapper(session))); + } + + public List<UserDto> selectByLogins(Collection<String> logins) { DbSession session = mybatis.openSession(false); try { - users.addAll(selectUsersByLogins(session, logins)); + return selectByLogins(session, logins); } finally { MyBatis.closeQuietly(session); } - return users; } - public List<UserDto> selectUsersByLogins(DbSession session, List<String> logins) { - List<UserDto> users = Lists.newArrayList(); - if (!logins.isEmpty()) { - UserMapper mapper = session.getMapper(UserMapper.class); - List<List<String>> partitions = Lists.partition(logins, 1000); - for (List<String> partition : partitions) { - users.addAll(mapper.selectUsersByLogins(partition)); - } + private static class SelectByLogins implements Function<List<String>, List<UserDto>> { + private final UserMapper mapper; + + private SelectByLogins(UserMapper mapper) { + this.mapper = mapper; + } + + @Override + public List<UserDto> apply(@Nonnull List<String> partitionOfLogins) { + return mapper.selectByLogins(partitionOfLogins); } - return users; } public List<UserDto> selectUsers(UserQuery query) { diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java b/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java index e387210942a..bef663e87d1 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java @@ -49,6 +49,8 @@ public interface UserMapper { List<UserDto> selectUsers(UserQuery query); + List<UserDto> selectByLogins(List<String> logins); + @CheckForNull GroupDto selectGroupByName(String name); @@ -76,4 +78,5 @@ public interface UserMapper { void deactivateUser(@Param("id") long userId, @Param("now") long now); + } diff --git a/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml b/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml index fd93ba7b0a5..4651984855a 100644 --- a/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml @@ -71,6 +71,16 @@ FROM rules r WHERE r.plugin_name=#{repository} AND r.plugin_rule_key=#{rule} </select> + <select id="selectByKeys" parameterType="map" resultType="Rule"> + SELECT + <include refid="selectColumns"/> + FROM rules r + WHERE + <foreach collection="ruleKeys" index="index" item="ruleKey" open="" separator=" or " close=""> + (r.plugin_name=#{ruleKey.repository} and r.plugin_rule_key=#{ruleKey.rule}) + </foreach> + </select> + <select id="selectByName" parameterType="String" resultType="Rule"> select <include refid="selectColumns"/> diff --git a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml index d46d44f2eaa..14e57dcd5df 100644 --- a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml @@ -47,6 +47,16 @@ WHERE u.login=#{id} AND u.active=${_true} </select> + <select id="selectByLogins" parameterType="string" resultType="User"> + SELECT + <include refid="userColumns"/> + FROM users u + WHERE u.login in + <foreach collection="list" open="(" close=")" item="login" separator=","> + #{login} + </foreach> + </select> + <select id="selectUsersByLogins" parameterType="map" resultType="User"> SELECT <include refid="userColumns"/> diff --git a/sonar-db/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java b/sonar-db/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java index 2a97bef0a21..cc67e416c6d 100644 --- a/sonar-db/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java +++ b/sonar-db/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java @@ -48,7 +48,7 @@ public class DefaultUserFinderTest { public void findByLogins() { UserDto david = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com"); UserDto john = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com"); - when(dao.selectUsersByLogins(Arrays.asList("david", "john"))).thenReturn(Arrays.asList(david, john)); + when(dao.selectByLogins(Arrays.asList("david", "john"))).thenReturn(Arrays.asList(david, john)); Collection<User> users = finder.findByLogins(Arrays.asList("david", "john")); assertThat(users).hasSize(2); diff --git a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java index a99a08aa856..eb4d016d162 100644 --- a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java @@ -30,6 +30,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(45); + assertThat(container.size()).isEqualTo(46); } } diff --git a/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java b/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java index c13d873d510..f1fc842061a 100644 --- a/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java @@ -98,7 +98,7 @@ public class IssueDaoTest { public void selectByKeyOrFail() { dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml"); - IssueDto issue = dao.selectByKeyOrFail(dbTester.getSession(), "I1"); + IssueDto issue = dao.selectOrFailByKey(dbTester.getSession(), "I1"); assertThat(issue.getKee()).isEqualTo("I1"); assertThat(issue.getId()).isEqualTo(1L); assertThat(issue.getComponentUuid()).isEqualTo("CDEF"); @@ -135,7 +135,7 @@ public class IssueDaoTest { dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml"); - dao.selectByKeyOrFail(dbTester.getSession(), "DOES_NOT_EXIST"); + dao.selectOrFailByKey(dbTester.getSession(), "DOES_NOT_EXIST"); } @Test diff --git a/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java new file mode 100644 index 00000000000..80de551b89f --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java @@ -0,0 +1,58 @@ +/* + * 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.db.rule; + +import java.util.Collections; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.ExpectedException; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.test.DbTests; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +@Category(DbTests.class) +public class RuleDaoTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + + RuleDao underTest = dbTester.getDbClient().ruleDao(); + + @Test + public void selectByKeys() { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + assertThat(underTest.selectByKeys(dbTester.getSession(), Collections.<RuleKey>emptyList())).isEmpty(); + assertThat(underTest.selectByKeys(dbTester.getSession(), asList(RuleKey.of("NOT", "FOUND"))).isEmpty()); + + List<RuleDto> rules = underTest.selectByKeys(dbTester.getSession(), asList(RuleKey.of("java", "S001"), RuleKey.of("java", "OTHER"))); + assertThat(rules).hasSize(1); + assertThat(rules.get(0).getId()).isEqualTo(1); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java index 2602ba47394..07fea9e3c47 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java @@ -36,6 +36,7 @@ import org.sonar.db.DbTester; import org.sonar.db.RowNotFoundException; import org.sonar.test.DbTests; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -87,7 +88,7 @@ public class UserDaoTest { public void selectUsersByLogins() { db.prepareDbUnit(getClass(), "selectUsersByLogins.xml"); - Collection<UserDto> users = underTest.selectUsersByLogins(Arrays.asList("marius", "inactive_user", "other")); + Collection<UserDto> users = underTest.selectByLogins(asList("marius", "inactive_user", "other")); assertThat(users).hasSize(2); assertThat(users).extracting("login").containsOnly("marius", "inactive_user"); } @@ -97,7 +98,7 @@ public class UserDaoTest { db.truncateTables(); // no need to access db - Collection<UserDto> users = underTest.selectUsersByLogins(Collections.<String>emptyList()); + Collection<UserDto> users = underTest.selectByLogins(Collections.<String>emptyList()); assertThat(users).isEmpty(); } diff --git a/sonar-db/src/test/resources/org/sonar/db/rule/RuleDaoTest/shared.xml b/sonar-db/src/test/resources/org/sonar/db/rule/RuleDaoTest/shared.xml new file mode 100644 index 00000000000..d7d86c81a35 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/rule/RuleDaoTest/shared.xml @@ -0,0 +1,12 @@ +<dataset> + + <rules id="1" name="Null Pointer" plugin_rule_key="S001" + plugin_config_key="S1" plugin_name="java" description="[null]" priority="4" status="READY" + is_template="[false]" template_id="[null]" + tags="bug,performance" system_tags="cwe" /> + + <rules id="2" name="Slow" plugin_rule_key="S002" + plugin_config_key="S2" plugin_name="java" description="[null]" priority="4" status="BETA" + is_template="[false]" template_id="[null]" + tags="[null]" system_tags="[null]" /> +</dataset> |