You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectPullRequestsTest.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan.branch;
  21. import java.util.Arrays;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.concurrent.atomic.AtomicInteger;
  25. import org.junit.*;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. public class ProjectPullRequestsTest {
  28. private static AtomicInteger counter = new AtomicInteger();
  29. @Test
  30. public void should_pick_the_latest_branch_by_analysis_date() {
  31. String branchName = "dummyBranch";
  32. long date = 1000;
  33. PullRequestInfo pr1 = newPullRequestInfo(branchName, date - 2);
  34. PullRequestInfo pr2 = newPullRequestInfo(branchName, date - 1);
  35. PullRequestInfo latest = newPullRequestInfo(branchName, date);
  36. PullRequestInfo pullRequestInfo = getPullRequestInfo(branchName, pr1, pr2, latest);
  37. assertThat(pullRequestInfo.getKey()).isEqualTo(latest.getKey());
  38. assertThat(pullRequestInfo.getAnalysisDate()).isEqualTo(date);
  39. }
  40. @Test
  41. public void should_not_crash_when_cannot_pick_a_unique_branch() {
  42. String branchName = "dummyBranch";
  43. long date = 1000;
  44. PullRequestInfo pr1 = newPullRequestInfo(branchName, date);
  45. PullRequestInfo pr2 = newPullRequestInfo(branchName, date);
  46. PullRequestInfo pullRequestInfo = getPullRequestInfo(branchName, pr1, pr2);
  47. assertThat(pullRequestInfo.getAnalysisDate()).isEqualTo(date);
  48. }
  49. @Test
  50. public void should_get_correct_branch() {
  51. long date = 1000;
  52. PullRequestInfo foo = newPullRequestInfo("foo", date);
  53. PullRequestInfo bar = newPullRequestInfo("bar", date);
  54. assertThat(getPullRequestInfo("foo", foo, bar).getBranch()).isEqualTo("foo");
  55. assertThat(getPullRequestInfo("bar", foo, bar).getBranch()).isEqualTo("bar");
  56. }
  57. private PullRequestInfo newPullRequestInfo(String branchName, long date) {
  58. return new PullRequestInfo("pr" + counter.incrementAndGet(), branchName, null, date);
  59. }
  60. private PullRequestInfo getPullRequestInfo(String branchName, PullRequestInfo ...prs) {
  61. return new ProjectPullRequests(Arrays.asList(prs)).get(branchName);
  62. }
  63. }