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.

PaginationTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.db;
  21. import java.util.Random;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.sonar.db.Pagination.forPage;
  27. public class PaginationTest {
  28. @Rule
  29. public ExpectedException expectedException = ExpectedException.none();
  30. @Test
  31. public void all_is_page_1_with_MAX_INTEGER_page_size() {
  32. Pagination pagination = Pagination.all();
  33. assertThat(pagination.getPage()).isEqualTo(1);
  34. assertThat(pagination.getPageSize()).isEqualTo(Integer.MAX_VALUE);
  35. }
  36. @Test
  37. public void all_returns_a_constant() {
  38. assertThat(Pagination.all()).isSameAs(Pagination.all());
  39. }
  40. @Test
  41. public void forPage_fails_with_IAE_if_page_is_0() {
  42. expectedException.expect(IllegalArgumentException.class);
  43. expectedException.expectMessage("page index must be >= 1");
  44. forPage(0);
  45. }
  46. @Test
  47. public void forPage_fails_with_IAE_if_page_is_less_than_0() {
  48. expectedException.expect(IllegalArgumentException.class);
  49. expectedException.expectMessage("page index must be >= 1");
  50. forPage(-Math.abs(new Random().nextInt()) - 1);
  51. }
  52. @Test
  53. public void andSize_fails_with_IAE_if_size_is_0() {
  54. Pagination.Builder builder = forPage(1);
  55. expectedException.expect(IllegalArgumentException.class);
  56. expectedException.expectMessage("page size must be >= 1");
  57. builder.andSize(0);
  58. }
  59. @Test
  60. public void andSize_fails_with_IAE_if_size_is_less_than_0() {
  61. Pagination.Builder builder = forPage(1);
  62. expectedException.expect(IllegalArgumentException.class);
  63. expectedException.expectMessage("page size must be >= 1");
  64. builder.andSize(-Math.abs(new Random().nextInt()) - 1);
  65. }
  66. @Test
  67. public void offset_is_computed_from_page_and_size() {
  68. assertThat(forPage(2).andSize(3).getOffset()).isEqualTo(3);
  69. assertThat(forPage(5).andSize(3).getOffset()).isEqualTo(12);
  70. assertThat(forPage(5).andSize(1).getOffset()).isEqualTo(4);
  71. }
  72. @Test
  73. public void startRowNumber_is_computed_from_page_and_size() {
  74. assertThat(forPage(2).andSize(3).getStartRowNumber()).isEqualTo(4);
  75. assertThat(forPage(5).andSize(3).getStartRowNumber()).isEqualTo(13);
  76. assertThat(forPage(5).andSize(1).getStartRowNumber()).isEqualTo(5);
  77. }
  78. @Test
  79. public void endRowNumber_is_computed_from_page_and_size() {
  80. assertThat(forPage(2).andSize(3).getEndRowNumber()).isEqualTo(6);
  81. assertThat(forPage(5).andSize(3).getEndRowNumber()).isEqualTo(15);
  82. assertThat(forPage(5).andSize(1).getEndRowNumber()).isEqualTo(5);
  83. }
  84. }