From 99fa2be3429f1612edd016d1260c08d97ebc8404 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 8 Oct 2014 15:13:54 +0200 Subject: Fix quality flaws --- .../sonar/core/source/db/SnapshotSourceDto.java | 9 +++-- .../src/main/java/org/sonar/core/user/UserDao.java | 12 +++---- .../java/org/sonar/core/issue/db/IssueDtoTest.java | 15 ++++++++ .../org/sonar/core/resource/ResourceDaoTest.java | 2 ++ .../core/source/db/SnapshotSourceDaoTest.java | 8 +++++ .../core/source/db/SnapshotSourceDtoTest.java | 40 ++++++++++++++++++++++ .../db/SnapshotSourceDaoTest/insert-result.xml | 12 +++++++ 7 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDtoTest.java create mode 100644 sonar-core/src/test/resources/org/sonar/core/source/db/SnapshotSourceDaoTest/insert-result.xml (limited to 'sonar-core') diff --git a/sonar-core/src/main/java/org/sonar/core/source/db/SnapshotSourceDto.java b/sonar-core/src/main/java/org/sonar/core/source/db/SnapshotSourceDto.java index 34df236684d..012b0803843 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/db/SnapshotSourceDto.java +++ b/sonar-core/src/main/java/org/sonar/core/source/db/SnapshotSourceDto.java @@ -28,23 +28,26 @@ public class SnapshotSourceDto { return id; } - public void setId(Long id) { + public SnapshotSourceDto setId(Long id) { this.id = id; + return this; } public Long getSnapshotId() { return snapshotId; } - public void setSnapshotId(Long snapshotId) { + public SnapshotSourceDto setSnapshotId(Long snapshotId) { this.snapshotId = snapshotId; + return this; } public String getData() { return data; } - public void setData(String data) { + public SnapshotSourceDto setData(String data) { this.data = data; + return this; } } diff --git a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java index d4060795d3f..ef50b7cdd0a 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java +++ b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java @@ -78,13 +78,11 @@ public class UserDao implements BatchComponent, ServerComponent, DaoComponent { public List selectUsersByLogins(List logins) { List users = Lists.newArrayList(); - if (!logins.isEmpty()) { - SqlSession session = mybatis.openSession(false); - try { - users.addAll(selectUsersByLogins(session, logins)); - } finally { - MyBatis.closeQuietly(session); - } + SqlSession session = mybatis.openSession(false); + try { + users.addAll(selectUsersByLogins(session, logins)); + } finally { + MyBatis.closeQuietly(session); } return users; } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java index 791c7125556..5b9abf13033 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDtoTest.java @@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException; import org.sonar.api.issue.Issue; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.utils.Duration; +import org.sonar.core.rule.RuleDto; import java.util.Calendar; import java.util.Date; @@ -105,4 +106,18 @@ public class IssueDtoTest { assertThat(issue.isNew()).isFalse(); } + @Test + public void set_rule() { + IssueDto dto = new IssueDto() + .setKee("100") + .setRule(new RuleDto().setId(1).setRuleKey("AvoidCycle").setRepositoryKey("squid")) + .setLanguage("xoo"); + + assertThat(dto.getRuleId()).isEqualTo(1); + assertThat(dto.getRuleRepo()).isEqualTo("squid"); + assertThat(dto.getRule()).isEqualTo("AvoidCycle"); + assertThat(dto.getRuleKey().toString()).isEqualTo("squid:AvoidCycle"); + assertThat(dto.getLanguage()).isEqualTo("xoo"); + } + } 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 c287b65acf5..08bd833909b 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 @@ -251,6 +251,8 @@ public class ResourceDaoTest extends AbstractDaoTestCase { assertThat(dao.getRootProjectByComponentKey("org.struts:struts-core:src/org/struts").getKey()).isEqualTo("org.struts:struts"); assertThat(dao.getRootProjectByComponentKey("org.struts:struts-core").getKey()).isEqualTo("org.struts:struts"); assertThat(dao.getRootProjectByComponentKey("org.struts:struts").getKey()).isEqualTo("org.struts:struts"); + + assertThat(dao.getRootProjectByComponentKey("unknown")).isNull(); } @Test diff --git a/sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDaoTest.java index 473c5fe9439..743edcb679e 100644 --- a/sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDaoTest.java @@ -49,4 +49,12 @@ public class SnapshotSourceDaoTest extends AbstractDaoTestCase { assertThat(snapshotSource).isEqualTo("public class Foo {public Foo(){}}"); } + + @Test + public void insert() throws Exception { + dao.insert(new SnapshotSourceDto().setId(102L).setData("bar").setSnapshotId(11L)); + + checkTable("insert", "snapshot_sources"); + } + } diff --git a/sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDtoTest.java b/sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDtoTest.java new file mode 100644 index 00000000000..6f11516b0b7 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/source/db/SnapshotSourceDtoTest.java @@ -0,0 +1,40 @@ +/* + * 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.core.source.db; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class SnapshotSourceDtoTest { + + @Test + public void test_setters_and_getters() throws Exception { + SnapshotSourceDto dto = new SnapshotSourceDto() + .setId(1L) + .setSnapshotId(10L) + .setData("data"); + + assertThat(dto.getId()).isEqualTo(1L); + assertThat(dto.getSnapshotId()).isEqualTo(10L); + assertThat(dto.getData()).isEqualTo("data"); + } +} diff --git a/sonar-core/src/test/resources/org/sonar/core/source/db/SnapshotSourceDaoTest/insert-result.xml b/sonar-core/src/test/resources/org/sonar/core/source/db/SnapshotSourceDaoTest/insert-result.xml new file mode 100644 index 00000000000..c7004b60f81 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/source/db/SnapshotSourceDaoTest/insert-result.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + -- cgit v1.2.3