diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-08-21 14:56:16 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-09-24 20:21:15 +0200 |
commit | d53977760baa53c56b53c369a2e027c61a6ec2c2 (patch) | |
tree | df63bf62317f82f49fbc376cfbff7fd8a6861e90 /server/sonar-db-dao | |
parent | 89ab883abdf49d899598608c69359b0139ffb68a (diff) | |
download | sonarqube-d53977760baa53c56b53c369a2e027c61a6ec2c2.tar.gz sonarqube-d53977760baa53c56b53c369a2e027c61a6ec2c2.zip |
Improve code quality and test coverage
Diffstat (limited to 'server/sonar-db-dao')
4 files changed, 67 insertions, 8 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java index a1d9ee6c8d6..265848f541a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java @@ -31,7 +31,7 @@ import static java.util.Objects.requireNonNull; import static org.sonar.api.utils.Preconditions.checkArgument; public class NewCodePeriodDao implements Dao { - + private static final String MSG_PROJECT_UUID_NOT_SPECIFIED = "Project uuid must be specified."; private final System2 system2; private final UuidFactory uuidFactory; @@ -73,23 +73,23 @@ public class NewCodePeriodDao implements Dao { } public Optional<NewCodePeriodDto> selectByProject(DbSession dbSession, String projectUuid) { - requireNonNull(projectUuid, "Project uuid must be specified."); + requireNonNull(projectUuid, MSG_PROJECT_UUID_NOT_SPECIFIED); return Optional.ofNullable(mapper(dbSession).selectByProject(projectUuid)); } public List<NewCodePeriodDto> selectAllByProject(DbSession dbSession, String projectUuid) { - requireNonNull(projectUuid, "Project uuid must be specified."); + requireNonNull(projectUuid, MSG_PROJECT_UUID_NOT_SPECIFIED); return mapper(dbSession).selectAllByProject(projectUuid); } public Optional<NewCodePeriodDto> selectByBranch(DbSession dbSession, String projectUuid, String branchUuid) { - requireNonNull(projectUuid, "Project uuid must be specified."); + requireNonNull(projectUuid, MSG_PROJECT_UUID_NOT_SPECIFIED); requireNonNull(branchUuid, "Branch uuid must be specified."); return Optional.ofNullable(mapper(dbSession).selectByBranch(projectUuid, branchUuid)); } public boolean existsByProjectAnalysisUuid(DbSession dbSession, String projectAnalysisUuid) { - requireNonNull(projectAnalysisUuid, "Project analysis uuid must be specified."); + requireNonNull(projectAnalysisUuid, MSG_PROJECT_UUID_NOT_SPECIFIED); return mapper(dbSession).countByProjectAnalysis(projectAnalysisUuid) > 0; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodParser.java b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodParser.java index f0c2304b4ff..34168e68e5b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodParser.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodParser.java @@ -22,6 +22,10 @@ package org.sonar.db.newcodeperiod; import java.time.LocalDate; public class NewCodePeriodParser { + private NewCodePeriodParser() { + // static only + } + public static LocalDate parseDate(String value) { return LocalDate.parse(value); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/package-info.java b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/package-info.java new file mode 100644 index 00000000000..4d471334ab6 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.db.newcodeperiod; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java index 4e86529f544..1b32e961ed4 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java @@ -21,6 +21,7 @@ package org.sonar.db.component; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import java.util.Collection; import java.util.Map; import javax.annotation.Nullable; import org.junit.Rule; @@ -38,6 +39,7 @@ import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; +import static org.assertj.core.api.Assertions.tuple; @RunWith(DataProviderRunner.class) public class BranchDaoTest { @@ -101,7 +103,7 @@ public class BranchDaoTest { @DataProvider public static Object[][] nullOrEmpty() { - return new Object[][]{ + return new Object[][] { {null}, {""} }; @@ -111,7 +113,7 @@ public class BranchDaoTest { public static Object[][] oldAndNewValuesCombinations() { String value1 = randomAlphabetic(10); String value2 = randomAlphabetic(20); - return new Object[][]{ + return new Object[][] { {null, value1}, {"", value1}, {value1, null}, @@ -125,7 +127,7 @@ public class BranchDaoTest { @DataProvider public static Object[][] nonLongBranchType() { - return new Object[][]{ + return new Object[][] { {BranchType.SHORT}, {BranchType.PULL_REQUEST} }; @@ -367,6 +369,35 @@ public class BranchDaoTest { } @Test + public void selectByComponent() { + BranchDto mainBranch = new BranchDto(); + mainBranch.setProjectUuid("U1"); + mainBranch.setUuid("U1"); + mainBranch.setBranchType(BranchType.LONG); + mainBranch.setKey("master"); + underTest.insert(dbSession, mainBranch); + + BranchDto featureBranch = new BranchDto(); + featureBranch.setProjectUuid("U1"); + featureBranch.setUuid("U2"); + featureBranch.setBranchType(BranchType.SHORT); + featureBranch.setKey("feature/foo"); + featureBranch.setMergeBranchUuid("U3"); + underTest.insert(dbSession, featureBranch); + + ComponentDto component = new ComponentDto().setProjectUuid(mainBranch.getUuid()); + + // select the component + Collection<BranchDto> branches = underTest.selectByComponent(dbSession, component); + + assertThat(branches).hasSize(2); + + assertThat(branches).extracting(BranchDto::getUuid, BranchDto::getKey, BranchDto::getProjectUuid, BranchDto::getBranchType, BranchDto::getMergeBranchUuid) + .containsOnly(tuple(mainBranch.getUuid(), mainBranch.getKey(), mainBranch.getProjectUuid(), mainBranch.getBranchType(), mainBranch.getMergeBranchUuid()), + tuple(featureBranch.getUuid(), featureBranch.getKey(), featureBranch.getProjectUuid(), featureBranch.getBranchType(), featureBranch.getMergeBranchUuid())); + } + + @Test public void selectByPullRequestKey() { BranchDto mainBranch = new BranchDto(); mainBranch.setProjectUuid("U1"); |